Skip to content

Commit

Permalink
verifed chain_defer_test
Browse files Browse the repository at this point in the history
  • Loading branch information
xhawk18 committed Sep 25, 2021
1 parent 95f4bcb commit 59f4364
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 9 deletions.
7 changes: 6 additions & 1 deletion Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion include/promise.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
34 changes: 27 additions & 7 deletions src/promise.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<Defer &>();
Promise &Promise::then(const any &deferOrPromiseOrOnResolved) {
if (deferOrPromiseOrOnResolved.type() == typeid(Defer)) {
Defer &defer = deferOrPromiseOrOnResolved.cast<Defer &>();
return then([defer](const any &arg) -> any {
defer.resolve(arg);
return nullptr;
Expand All @@ -279,8 +279,8 @@ Promise &Promise::then(const any &deferOrOnResolved) {
return nullptr;
});
}
else if (deferOrOnResolved.type() == typeid(DeferLoop)) {
DeferLoop &loop = deferOrOnResolved.cast<DeferLoop &>();
else if (deferOrPromiseOrOnResolved.type() == typeid(DeferLoop)) {
DeferLoop &loop = deferOrPromiseOrOnResolved.cast<DeferLoop &>();
return then([loop](const any &arg) -> any {
(void)arg;
loop.doContinue();
Expand All @@ -290,8 +290,19 @@ Promise &Promise::then(const any &deferOrOnResolved) {
return nullptr;
});
}
else if (deferOrPromiseOrOnResolved.type() == typeid(Promise)) {
Promise &promise = deferOrPromiseOrOnResolved.cast<Promise &>();
if (promise.sharedPromise_ && promise.sharedPromise_->promiseHolder_) {
join(this->sharedPromise_, promise.sharedPromise_->promiseHolder_);
if (this->sharedPromise_->promiseHolder_->pendingTasks_.size() > 0) {
std::shared_ptr<Task> task = this->sharedPromise_->promiseHolder_->pendingTasks_.front();
call(task);
}
}
return *this;
}
else {
return then(deferOrOnResolved, any());
return then(deferOrPromiseOrOnResolved, any());
}
}

Expand Down Expand Up @@ -386,7 +397,16 @@ Promise newPromise(const std::function<void(Defer &defer)> &run) {
}

Promise newPromise() {
return newPromise([](Defer &){});
Promise promise;
promise.sharedPromise_ = std::make_shared<SharedPromise>();
promise.sharedPromise_->promiseHolder_ = std::make_shared<PromiseHolder>();
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<void(DeferLoop &loop)> &run) {
Expand Down

0 comments on commit 59f4364

Please sign in to comment.