Skip to content

Commit 6943b28

Browse files
rubennortefacebook-github-bot
authored andcommitted
[skip ci] Add queueMicrotask method to JSI
Summary: Changelog: [internal] ## Context Microtasks are an important aspect of JavaScript and they will become increasingly important in the hosts where we're currently using JSI. For example, React Native is going to adopt an event loop processing model similar to the one on the Web, which means it would need the ability to schedule and execute microtasks in every iteration of the loop. See react-native-community/discussions-and-proposals#744 for details. JSI already has a method to execute all pending microtasks (`drainMicrotasks`) but without a method to schedule microtasks this is incomplete. We're currently testing microtasks with Hermes using an internal method to schedule microtasks (`HermesInternal.enqueueJob`) but we need a method in JSI so this also works in other runtimes like JSC and V8. ## Changes This adds the `queueMicrotask` to the Runtime API in JSI so we have symmetric API for microtasks and we can implement the necessary functionality. The expectation for JSI implementations is to queue microtasks from this method and from built-ins like Promises and async functions in the same queue, and not drain that queue until explicitly done via `drainMicrotasks` in JSI. This also modifies Hermes and JSC to provide stubs for those methods, and the actual implementation will be done in following diffs. Differential Revision: D54302536
1 parent 04bd850 commit 6943b28

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

packages/react-native/ReactCommon/jsc/JSCRuntime.cpp

+4-1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ class JSCRuntime : public jsi::Runtime {
5151
const std::shared_ptr<const jsi::Buffer>& buffer,
5252
const std::string& sourceURL) override;
5353

54+
void queueMicrotask(const jsi::Function& callback) override;
5455
bool drainMicrotasks(int maxMicrotasksHint = -1) override;
5556

5657
jsi::Object global() override;
@@ -434,7 +435,9 @@ jsi::Value JSCRuntime::evaluateJavaScript(
434435
return createValue(res);
435436
}
436437

437-
bool JSCRuntime::drainMicrotasks(int maxMicrotasksHint) {
438+
void JSCRuntime::queueMicrotask(const jsi::Function& /*callback*/) {}
439+
440+
bool JSCRuntime::drainMicrotasks(int /*maxMicrotasksHint*/) {
438441
return true;
439442
}
440443

packages/react-native/ReactCommon/jsi/jsi/decorator.h

+7
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,9 @@ class RuntimeDecorator : public Base, private jsi::Instrumentation {
126126
const std::shared_ptr<const PreparedJavaScript>& js) override {
127127
return plain().evaluatePreparedJavaScript(js);
128128
}
129+
void queueMicrotask(const jsi::Function& callback) override {
130+
return plain().queueMicrotask(callback);
131+
}
129132
bool drainMicrotasks(int maxMicrotasksHint) override {
130133
return plain().drainMicrotasks(maxMicrotasksHint);
131134
}
@@ -544,6 +547,10 @@ class WithRuntimeDecorator : public RuntimeDecorator<Plain, Base> {
544547
Around around{with_};
545548
return RD::evaluatePreparedJavaScript(js);
546549
}
550+
void queueMicrotask(const Function& callback) override {
551+
Around around{with_};
552+
RD::queueMicrotask(callback);
553+
}
547554
bool drainMicrotasks(int maxMicrotasksHint) override {
548555
Around around{with_};
549556
return RD::drainMicrotasks(maxMicrotasksHint);

packages/react-native/ReactCommon/jsi/jsi/jsi.h

+5
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,11 @@ class JSI_EXPORT Runtime {
209209
virtual Value evaluatePreparedJavaScript(
210210
const std::shared_ptr<const PreparedJavaScript>& js) = 0;
211211

212+
// Queues a microtask in the JavaScript VM internal Microtask (a.k.a. Job in
213+
// ECMA262) queue, to be executed when the host drains microtasks in
214+
// its event loop implementation.
215+
virtual void queueMicrotask(const jsi::Function& callback) = 0;
216+
212217
/// Drain the JavaScript VM internal Microtask (a.k.a. Job in ECMA262) queue.
213218
///
214219
/// \param maxMicrotasksHint a hint to tell an implementation that it should

0 commit comments

Comments
 (0)