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