Skip to content

Commit

Permalink
feat(core): add willexit callback
Browse files Browse the repository at this point in the history
  • Loading branch information
churchill-zhang authored and zoomchan-cxj committed Mar 16, 2023
1 parent 2ea7581 commit bd64345
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
5 changes: 5 additions & 0 deletions core/include/core/scope.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ class Scope {
turbo_host_object_map_[name] = host_object;
}

inline void AddWillExitCallback(std::function<void()> cb) { // cb will run in the js thread
will_exit_cbs_.push_back(cb);
}

private:
friend class Engine;
void Init();
Expand All @@ -140,4 +144,5 @@ class Scope {
std::vector<std::unique_ptr<hippy::napi::FuncWrapper>> func_wrapper_holder_;
std::unordered_map<std::string, std::shared_ptr<CtxValue>> turbo_instance_map_;
std::unordered_map<std::string, std::any> turbo_host_object_map_;
std::vector<std::function<void()>> will_exit_cbs_;
};
9 changes: 6 additions & 3 deletions core/src/scope.cc
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,10 @@ Scope::~Scope() {
void Scope::WillExit() {
TDF_BASE_DLOG(INFO) << "WillExit begin";
std::promise<std::shared_ptr<CtxValue>> promise;
std::future<std::shared_ptr<CtxValue>> future = promise.get_future();
auto future = promise.get_future();
std::weak_ptr<Ctx> weak_context = context_;
JavaScriptTask::Function cb = hippy::base::MakeCopyable(
[weak_context, p = std::move(promise)]() mutable {
auto cb = hippy::base::MakeCopyable(
[weak_context, will_exit_cbs = will_exit_cbs_, p = std::move(promise)]() mutable {
TDF_BASE_LOG(INFO) << "run js WillExit begin";
std::shared_ptr<CtxValue> rst = nullptr;
auto context = weak_context.lock();
Expand All @@ -102,6 +102,9 @@ void Scope::WillExit() {
context->CallFunction(fn, 0, nullptr);
}
}
for (const auto& will_exit_cb: will_exit_cbs) {
will_exit_cb();
}
p.set_value(rst);
});
auto runner = GetTaskRunner();
Expand Down

0 comments on commit bd64345

Please sign in to comment.