File tree Expand file tree Collapse file tree 4 files changed +190
-0
lines changed
ReactCommon/fabric/scheduler Expand file tree Collapse file tree 4 files changed +190
-0
lines changed Original file line number Diff line number Diff line change 1+ /*
2+ * Copyright (c) Facebook, Inc. and its affiliates.
3+ *
4+ * This source code is licensed under the MIT license found in the
5+ * LICENSE file in the root directory of this source tree.
6+ */
7+
8+ #include " AsynchronousEventBeat.h"
9+
10+ namespace facebook {
11+ namespace react {
12+
13+ AsynchronousEventBeat::AsynchronousEventBeat (
14+ RunLoopObserver::Unique uiRunLoopObserver,
15+ RuntimeExecutor runtimeExecutor)
16+ : EventBeat({}),
17+ uiRunLoopObserver_ (std::move(uiRunLoopObserver)),
18+ runtimeExecutor_ (std::move(runtimeExecutor)) {
19+ uiRunLoopObserver_->setDelegate (this );
20+ uiRunLoopObserver_->enable ();
21+ }
22+
23+ void AsynchronousEventBeat::activityDidChange (
24+ RunLoopObserver::Delegate const *delegate,
25+ RunLoopObserver::Activity activity) const noexcept {
26+ assert (delegate == this );
27+ induce ();
28+ }
29+
30+ void AsynchronousEventBeat::induce () const {
31+ if (!isRequested_) {
32+ return ;
33+ }
34+
35+ // Here we know that `this` object exists because the caller has a strong
36+ // pointer to `owner`. To ensure the object will exist inside
37+ // `runtimeExecutor_` callback, we need to copy the pointer there.
38+ auto weakOwner = uiRunLoopObserver_->getOwner ();
39+
40+ runtimeExecutor_ ([this , weakOwner](jsi::Runtime &runtime) mutable {
41+ auto owner = weakOwner.lock ();
42+ if (!owner) {
43+ return ;
44+ }
45+
46+ if (!isRequested_) {
47+ return ;
48+ }
49+
50+ this ->beat (runtime);
51+ });
52+ }
53+
54+ } // namespace react
55+ } // namespace facebook
Original file line number Diff line number Diff line change 1+ /*
2+ * Copyright (c) Facebook, Inc. and its affiliates.
3+ *
4+ * This source code is licensed under the MIT license found in the
5+ * LICENSE file in the root directory of this source tree.
6+ */
7+
8+ #include < ReactCommon/RuntimeExecutor.h>
9+ #include < react/core/EventBeat.h>
10+ #include < react/utils/RunLoopObserver.h>
11+
12+ namespace facebook {
13+ namespace react {
14+
15+ /*
16+ * Event beat associated with JavaScript runtime.
17+ * The beat is called on `RuntimeExecutor`'s thread induced by the UI thread
18+ * event loop.
19+ */
20+ class AsynchronousEventBeat : public EventBeat ,
21+ public RunLoopObserver::Delegate {
22+ public:
23+ AsynchronousEventBeat (
24+ RunLoopObserver::Unique uiRunLoopObserver,
25+ RuntimeExecutor runtimeExecutor);
26+
27+ void induce () const override ;
28+
29+ #pragma mark - RunLoopObserver::Delegate
30+
31+ void activityDidChange (
32+ RunLoopObserver::Delegate const *delegate,
33+ RunLoopObserver::Activity activity) const noexcept override ;
34+
35+ private:
36+ RunLoopObserver::Unique uiRunLoopObserver_;
37+ RuntimeExecutor runtimeExecutor_;
38+ };
39+
40+ } // namespace react
41+ } // namespace facebook
Original file line number Diff line number Diff line change 1+ /*
2+ * Copyright (c) Facebook, Inc. and its affiliates.
3+ *
4+ * This source code is licensed under the MIT license found in the
5+ * LICENSE file in the root directory of this source tree.
6+ */
7+
8+ #import " SynchronousEventBeat.h"
9+
10+ namespace facebook {
11+ namespace react {
12+
13+ SynchronousEventBeat::SynchronousEventBeat (
14+ RunLoopObserver::Unique uiRunLoopObserver,
15+ RuntimeExecutor runtimeExecutor)
16+ : EventBeat({}),
17+ uiRunLoopObserver_ (std::move(uiRunLoopObserver)),
18+ runtimeExecutor_ (std::move(runtimeExecutor)) {
19+ uiRunLoopObserver_->setDelegate (this );
20+ uiRunLoopObserver_->enable ();
21+ }
22+
23+ void SynchronousEventBeat::activityDidChange (
24+ RunLoopObserver::Delegate const *delegate,
25+ RunLoopObserver::Activity activity) const noexcept {
26+ assert (delegate == this );
27+ lockExecutorAndBeat ();
28+ }
29+
30+ void SynchronousEventBeat::induce () const {
31+ if (!this ->isRequested_ ) {
32+ return ;
33+ }
34+
35+ if (uiRunLoopObserver_->isOnRunLoopThread ()) {
36+ this ->lockExecutorAndBeat ();
37+ }
38+ }
39+
40+ void SynchronousEventBeat::lockExecutorAndBeat () const {
41+ if (!this ->isRequested_ ) {
42+ return ;
43+ }
44+
45+ executeSynchronouslyOnSameThread_CAN_DEADLOCK (
46+ runtimeExecutor_, [this ](jsi::Runtime &runtime) { beat (runtime); });
47+ }
48+
49+ } // namespace react
50+ } // namespace facebook
Original file line number Diff line number Diff line change 1+ /*
2+ * Copyright (c) Facebook, Inc. and its affiliates.
3+ *
4+ * This source code is licensed under the MIT license found in the
5+ * LICENSE file in the root directory of this source tree.
6+ */
7+
8+ #pragma once
9+
10+ #include < ReactCommon/RuntimeExecutor.h>
11+ #include < react/core/EventBeat.h>
12+ #include < react/utils/RunLoopObserver.h>
13+
14+ namespace facebook {
15+ namespace react {
16+
17+ /*
18+ * Event beat associated with main run loop.
19+ * The callback is always called on the main thread.
20+ */
21+ class SynchronousEventBeat final : public EventBeat,
22+ public RunLoopObserver::Delegate {
23+ public:
24+ SynchronousEventBeat (
25+ RunLoopObserver::Unique uiRunLoopObserver,
26+ RuntimeExecutor runtimeExecutor);
27+
28+ void induce () const override ;
29+
30+ #pragma mark - RunLoopObserver::Delegate
31+
32+ void activityDidChange (
33+ RunLoopObserver::Delegate const *delegate,
34+ RunLoopObserver::Activity activity) const noexcept override ;
35+
36+ private:
37+ void lockExecutorAndBeat () const ;
38+
39+ RunLoopObserver::Unique uiRunLoopObserver_;
40+ RuntimeExecutor runtimeExecutor_;
41+ };
42+
43+ } // namespace react
44+ } // namespace facebook
You can’t perform that action at this time.
0 commit comments