1 #ifdef __APPLE__
2 #include <RNReanimated/Scheduler.h>
3 #else
4 #include "Scheduler.h"
5 #endif
6 #include "ReanimatedRuntime.h"
7 #include "RuntimeManager.h"
8 
9 namespace reanimated {
10 
11 void Scheduler::scheduleOnUI(std::function<void()> job) {
12   uiJobs.push(std::move(job));
13 }
14 
15 void Scheduler::scheduleOnJS(std::function<void()> job) {
16   jsCallInvoker_->invokeAsync(std::move(job));
17 }
18 
19 void Scheduler::triggerUI() {
20   scheduledOnUI = false;
21 #if JS_RUNTIME_HERMES
22   // JSI's scope defined here allows for JSI-objects to be cleared up after
23   // each runtime loop. Within these loops we typically create some temporary
24   // JSI objects and hence it allows for such objects to be garbage collected
25   // much sooner.
26   // Apparently the scope API is only supported on Hermes at the moment.
27   auto scope = jsi::Scope(*runtimeManager.lock()->runtime);
28 #endif
29   while (uiJobs.getSize()) {
30     auto job = uiJobs.pop();
31     job();
32   }
33 }
34 
35 void Scheduler::setJSCallInvoker(
36     std::shared_ptr<facebook::react::CallInvoker> jsCallInvoker) {
37   jsCallInvoker_ = jsCallInvoker;
38 }
39 
40 void Scheduler::setRuntimeManager(
41     std::shared_ptr<RuntimeManager> runtimeManager) {
42   this->runtimeManager = runtimeManager;
43 }
44 
45 Scheduler::~Scheduler() {}
46 
47 Scheduler::Scheduler() {
48   this->scheduledOnUI = false;
49 }
50 
51 } // namespace reanimated
52