Skip to content

Commit

Permalink
Register globals for idleCallback
Browse files Browse the repository at this point in the history
Summary:
Register Globals to call `requestIdleCallback` and `cancelIdleCallback`

## Changelog
[General][Added] - Register globals for IdleCallbacks

Differential Revision: D56472923
  • Loading branch information
cipolleschi authored and facebook-github-bot committed Apr 23, 2024
1 parent d1672af commit efd1181
Showing 1 changed file with 73 additions and 1 deletion.
74 changes: 73 additions & 1 deletion packages/react-native/ReactCommon/react/runtime/TimerManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,8 @@ void TimerManager::callTimer(uint32_t timerID) {
});
}

std::shared_ptr<TimerHandle> TimerManager::createIdleCallback(jsi::Function&& callback) {
std::shared_ptr<TimerHandle> TimerManager::createIdleCallback(
jsi::Function&& callback) {
return nullptr;
}

Expand Down Expand Up @@ -431,6 +432,77 @@ void TimerManager::attachGlobals(jsi::Runtime& runtime) {
deleteTimer(rt, host);
return jsi::Value::undefined();
}));

runtime.global().setProperty(
runtime,
"requestIdleCallback",
jsi::Function::createFromHostFunction(
runtime,
jsi::PropNameID::forAscii(runtime, "requestIdleCallback"),
2, // callback, options
[this](
jsi::Runtime& rt,
const jsi::Value& thisVal,
const jsi::Value* args,
size_t count) {
if (count < 0) {
throw jsi::JSError(
rt,
"requestIdleCallback must be called with at least a callback)");
}

if (!args[0].isObject() || !args[0].asObject(rt).isFunction(rt)) {
throw jsi::JSError(
rt,
"The first argument to requestIdleCallback must be a function.");
}

auto callback = args[0].getObject(rt).getFunction(rt);

if (count == 2) {
if (!args[1].isNull() && !args[1].isObject()) {
throw jsi::JSError(
rt,
"The second argument of requestIdleCallback, if provided, must be an object");
}
auto options = args[1].asObject(rt);
if (!options.hasProperty(rt, "timeout")) {
throw jsi::JSError(
rt,
"The second argument of requestIdleCallback must have a timeout property");
}
auto timeout = options.getProperty(rt, "timeout").asNumber();
auto handle =
createIdleCallbackWithTimeout(std::move(callback), timeout);
return jsi::Object::createFromHostObject(rt, handle);
}

auto handle = createIdleCallback(std::move(callback));
return jsi::Object::createFromHostObject(rt, handle);
}));

runtime.global().setProperty(
runtime,
"cancelIdleCallback",
jsi::Function::createFromHostFunction(
runtime,
jsi::PropNameID::forAscii(runtime, "cancelIdleCallback"),
1, // idleCallbackID
[this](
jsi::Runtime& rt,
const jsi::Value& thisVal,
const jsi::Value* args,
size_t count) {
if (count == 0 || !args[0].isObject() ||
!args[0].asObject(rt).isHostObject<TimerHandle>(rt)) {
return jsi::Value::undefined();
}

std::shared_ptr<TimerHandle> host =
args[0].asObject(rt).asHostObject<TimerHandle>(rt);
clearIdleCallback(rt, host);
return jsi::Value::undefined();
}));
}

} // namespace facebook::react

0 comments on commit efd1181

Please sign in to comment.