-
Notifications
You must be signed in to change notification settings - Fork 573
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
RJS-2852: Adding a CallInvoker
-based scheduler for Core on React Native
#6791
Changes from 8 commits
b2b449d
49bc2da
aa65852
43492ab
fca3679
edc2c1b
7acc8b5
4ee3d51
1fa05ed
5fac334
d24db8d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,7 +31,7 @@ | |
#import <objc/runtime.h> | ||
|
||
#import "jsi/jsi_init.h" | ||
#import "flush_ui_queue_workaround.h" | ||
#import "jsi/react_scheduler.h" | ||
|
||
// the part of the RCTCxxBridge private class we care about | ||
@interface RCTBridge (Realm_RCTCxxBridge) | ||
|
@@ -54,7 +54,7 @@ - (dispatch_queue_t)methodQueue { | |
} | ||
|
||
- (void)invalidate { | ||
realm::js::flush_ui_workaround::reset_js_call_invoker(); | ||
realm::js::react_scheduler::reset_scheduler(); | ||
#if DEBUG | ||
// Immediately close any open sync sessions to prevent race condition with new | ||
// JS thread when hot reloading | ||
|
@@ -79,7 +79,7 @@ - (void)dealloc { | |
// See https://github.com/facebook/react-native/pull/43396#issuecomment-2178586017 for context | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we remove the comments? |
||
// If it was, we could use the enablement of "microtasks" to avoid the overhead of calling the invokeAsync on every call from C++ into JS. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we remove the comments? |
||
// if (!facebook::react::ReactNativeFeatureFlags::enableMicrotasks()) { | ||
realm::js::flush_ui_workaround::inject_js_call_invoker([bridge jsCallInvoker]); | ||
realm::js::react_scheduler::create_scheduler([bridge jsCallInvoker]); | ||
|
||
auto exports = jsi::Object(rt); | ||
realm_jsi_init(rt, exports); | ||
|
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
//////////////////////////////////////////////////////////////////////////// | ||
// | ||
// Copyright 2024 Realm Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
//////////////////////////////////////////////////////////////////////////// | ||
|
||
#include "react_scheduler.h" | ||
|
||
#include <realm/object-store/util/scheduler.hpp> | ||
|
||
#include <ReactCommon/CallInvoker.h> | ||
#include <ReactCommon/SchedulerPriority.h> | ||
|
||
#include <thread> | ||
|
||
using Scheduler = realm::util::Scheduler; | ||
|
||
namespace { | ||
|
||
std::shared_ptr<Scheduler> scheduler_{}; | ||
|
||
class ReactScheduler : public realm::util::Scheduler { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is very minor, but this type and the global variable should be in an anonymous namespace so that the symbols are not exported. |
||
public: | ||
ReactScheduler(std::shared_ptr<facebook::react::CallInvoker> js_call_invoker) | ||
: m_js_call_invoker(js_call_invoker) | ||
{ | ||
} | ||
|
||
bool is_on_thread() const noexcept override | ||
{ | ||
return m_id == std::this_thread::get_id(); | ||
} | ||
|
||
bool is_same_as(const Scheduler* other) const noexcept override | ||
{ | ||
auto o = dynamic_cast<const ReactScheduler*>(other); | ||
return (o && (o->m_js_call_invoker == m_js_call_invoker)); | ||
} | ||
|
||
bool can_invoke() const noexcept override | ||
{ | ||
return true; | ||
} | ||
|
||
void invoke(realm::util::UniqueFunction<void()>&& func) override | ||
{ | ||
m_js_call_invoker->invokeAsync( | ||
// Using normal priority to avoid blocking higher priority tasks | ||
facebook::react::SchedulerPriority::NormalPriority, | ||
// Wrapping the func in a shared_ptr to ensure it outlives the invocation | ||
// and gets destructed even if the function is never called. | ||
[func = std::make_shared<realm::util::UniqueFunction<void()>>(func.release())] { | ||
(*func)(); | ||
}); | ||
} | ||
|
||
private: | ||
std::shared_ptr<facebook::react::CallInvoker> m_js_call_invoker; | ||
std::thread::id m_id = std::this_thread::get_id(); | ||
}; | ||
|
||
std::shared_ptr<Scheduler> get_scheduler() | ||
{ | ||
if (scheduler_) { | ||
REALM_ASSERT(scheduler_->is_on_thread()); | ||
return scheduler_; | ||
} | ||
else { | ||
return Scheduler::make_platform_default(); | ||
} | ||
} | ||
|
||
} // namespace | ||
|
||
namespace realm::js::react_scheduler { | ||
|
||
|
||
void create_scheduler(std::shared_ptr<facebook::react::CallInvoker> js_call_invoker) | ||
{ | ||
scheduler_ = std::make_shared<ReactScheduler>(js_call_invoker); | ||
Scheduler::set_default_factory(get_scheduler); | ||
} | ||
|
||
void reset_scheduler() | ||
{ | ||
scheduler_.reset(); | ||
} | ||
|
||
} // namespace realm::js::react_scheduler |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I got annoyed by these being indexed by watchman.