From 59f4364f327d2150546216e8f9cc8470e25c7212 Mon Sep 17 00:00:00 2001 From: X-Hawk Date: Sun, 26 Sep 2021 00:47:48 +0800 Subject: [PATCH] verifed chain_defer_test --- Readme.md | 7 ++++++- include/promise.hpp | 2 +- src/promise.cpp | 34 +++++++++++++++++++++++++++------- 3 files changed, 34 insertions(+), 9 deletions(-) diff --git a/Readme.md b/Readme.md index 602932e..a92bbba 100644 --- a/Readme.md +++ b/Readme.md @@ -23,6 +23,7 @@ - [Promise::then(FUNC_ON_RESOLVED on_resolved, FUNC_ON_REJECTED on_rejected)](#promisethenfunc_on_resolved-on_resolved-func_on_rejected-on_rejected) - [Promise::then(FUNC_ON_RESOLVED on_resolved)](#promisethenfunc_on_resolved-on_resolved) - [Promise::then(Defer d)](#promisethendefer-d) + - [Promise::then(Promise promise)](#promisethenpromise-promise) - [Promise::fail(FUNC_ON_REJECTED on_rejected)](#promisefailfunc_on_rejected-on_rejected) - [Promise::finally(FUNC_ON_FINALLY on_finally)](#promisefinallyfunc_on_finally-on_finally) - [Promise::always(FUNC_ON_ALWAYS on_always)](#promisealwaysfunc_on_always-on_always) @@ -372,7 +373,11 @@ return newPromise([](Defer d){ ``` ### Promise::then(Defer d) -Return the chaining promise object, where d is the promise object be called when +Return the chaining promise object, where d is the callback function be called when +previous promise object calls function resolve or reject. + +### Promise::then(Promise promise) +Return the chaining promise object, where "promise" is the promise object be called when previous promise object calls function resolve or reject. ### Promise::fail(FUNC_ON_REJECTED on_rejected) diff --git a/include/promise.hpp b/include/promise.hpp index f2aad7f..4184507 100644 --- a/include/promise.hpp +++ b/include/promise.hpp @@ -113,7 +113,7 @@ struct DeferLoop { }; struct Promise { - PROMISE_API Promise &then(const any &deferOrOnResolved); + PROMISE_API Promise &then(const any &deferOrPromiseOrOnResolved); PROMISE_API Promise &then(const any &onResolved, const any &onRejected); PROMISE_API Promise &fail(const any &onRejected); PROMISE_API Promise &always(const any &onAlways); diff --git a/src/promise.cpp b/src/promise.cpp index d3e44d7..44b4862 100644 --- a/src/promise.cpp +++ b/src/promise.cpp @@ -268,9 +268,9 @@ void PromiseHolder::handleUncaughtException(const any &onUncaughtException) { } -Promise &Promise::then(const any &deferOrOnResolved) { - if (deferOrOnResolved.type() == typeid(Defer)) { - Defer &defer = deferOrOnResolved.cast(); +Promise &Promise::then(const any &deferOrPromiseOrOnResolved) { + if (deferOrPromiseOrOnResolved.type() == typeid(Defer)) { + Defer &defer = deferOrPromiseOrOnResolved.cast(); return then([defer](const any &arg) -> any { defer.resolve(arg); return nullptr; @@ -279,8 +279,8 @@ Promise &Promise::then(const any &deferOrOnResolved) { return nullptr; }); } - else if (deferOrOnResolved.type() == typeid(DeferLoop)) { - DeferLoop &loop = deferOrOnResolved.cast(); + else if (deferOrPromiseOrOnResolved.type() == typeid(DeferLoop)) { + DeferLoop &loop = deferOrPromiseOrOnResolved.cast(); return then([loop](const any &arg) -> any { (void)arg; loop.doContinue(); @@ -290,8 +290,19 @@ Promise &Promise::then(const any &deferOrOnResolved) { return nullptr; }); } + else if (deferOrPromiseOrOnResolved.type() == typeid(Promise)) { + Promise &promise = deferOrPromiseOrOnResolved.cast(); + if (promise.sharedPromise_ && promise.sharedPromise_->promiseHolder_) { + join(this->sharedPromise_, promise.sharedPromise_->promiseHolder_); + if (this->sharedPromise_->promiseHolder_->pendingTasks_.size() > 0) { + std::shared_ptr task = this->sharedPromise_->promiseHolder_->pendingTasks_.front(); + call(task); + } + } + return *this; + } else { - return then(deferOrOnResolved, any()); + return then(deferOrPromiseOrOnResolved, any()); } } @@ -386,7 +397,16 @@ Promise newPromise(const std::function &run) { } Promise newPromise() { - return newPromise([](Defer &){}); + Promise promise; + promise.sharedPromise_ = std::make_shared(); + promise.sharedPromise_->promiseHolder_ = std::make_shared(); + promise.sharedPromise_->promiseHolder_->owners_.push_back(promise.sharedPromise_); + + auto returnAsIs = [](const any &arg) -> any { + return arg; + }; + promise.then(returnAsIs); + return promise; } Promise doWhile(const std::function &run) {