aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDeterminant <[email protected]>2018-01-29 17:52:05 -0500
committerDeterminant <[email protected]>2018-01-29 17:52:05 -0500
commitf97660608a0df24cb1200ac7d9c6b19ccb4620e3 (patch)
treef6f9de67ade6e958c75fd74f12a38a5722911e23
parent11e0a40e1c81a6fbf5978c3ce8118f0632fc3379 (diff)
use callback vector to avoid stack overflow
-rw-r--r--promise.hpp25
1 files changed, 21 insertions, 4 deletions
diff --git a/promise.hpp b/promise.hpp
index 58b644f..397ef70 100644
--- a/promise.hpp
+++ b/promise.hpp
@@ -52,8 +52,10 @@ namespace promise {
#define PROMISE_ERR_MISMATCH_TYPE do {throw std::runtime_error("mismatching promise value types");} while (0)
class Promise {
- function<void()> fulfilled_callback;
- function<void()> rejected_callback;
+ //function<void()> fulfilled_callback;
+ //function<void()> rejected_callback;
+ std::vector<function<void()>> fulfilled_callbacks;
+ std::vector<function<void()>> rejected_callbacks;
enum class State {
Pending,
Fulfilled,
@@ -62,6 +64,8 @@ namespace promise {
pm_any_t result;
pm_any_t reason;
+ /* this implementation causes stack overflow because of the nested lambdas */
+ /*
void add_on_fulfilled(function<void()> cb) {
auto old_cbs = fulfilled_callback;
fulfilled_callback = function<void()>(
@@ -79,6 +83,15 @@ namespace promise {
cb();
});
}
+ */
+
+ void add_on_fulfilled(function<void()> cb) {
+ fulfilled_callbacks.push_back(cb);
+ }
+
+ void add_on_rejected(function<void()> cb) {
+ rejected_callbacks.push_back(cb);
+ }
function<void()> gen_on_fulfilled(
function<promise_t(pm_any_t)> on_fulfilled,
@@ -131,8 +144,10 @@ namespace promise {
public:
Promise():
+ /*
fulfilled_callback(do_nothing),
rejected_callback(do_nothing),
+ */
state(State::Pending) {
//printf("%lx constructed\n", (uintptr_t)this);
}
@@ -207,7 +222,8 @@ namespace promise {
case State::Pending:
result = _result;
state = State::Fulfilled;
- fulfilled_callback();
+ //fulfilled_callback();
+ for (const auto &cb: fulfilled_callbacks) cb();
break;
default:
break;
@@ -220,7 +236,8 @@ namespace promise {
case State::Pending:
reason = _reason;
state = State::Rejected;
- rejected_callback();
+ //rejected_callback();
+ for (const auto &cb: rejected_callbacks) cb();
break;
default:
break;