1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 16:47:36 +00:00

LibWeb: Add the requestIdleCallback/cancelIdleCallback API

This commit is contained in:
Simon Wanner 2022-04-01 19:46:29 +02:00 committed by Linus Groh
parent 836d2ff259
commit ea9857a423
4 changed files with 80 additions and 1 deletions

View file

@ -91,6 +91,9 @@ void WindowObject::initialize_global_object()
define_native_function("queueMicrotask", queue_microtask, 1, attr);
define_native_function("requestIdleCallback", request_idle_callback, 1, attr);
define_native_function("cancelIdleCallback", cancel_idle_callback, 1, attr);
define_native_function("getComputedStyle", get_computed_style, 1, attr);
define_native_function("matchMedia", match_media, 1, attr);
define_native_function("getSelection", get_selection, 0, attr);
@ -333,6 +336,31 @@ JS_DEFINE_NATIVE_FUNCTION(WindowObject::queue_microtask)
return JS::js_undefined();
}
JS_DEFINE_NATIVE_FUNCTION(WindowObject::request_idle_callback)
{
auto* impl = TRY(impl_from(vm, global_object));
if (!vm.argument_count())
return vm.throw_completion<JS::TypeError>(global_object, JS::ErrorType::BadArgCountAtLeastOne, "requestIdleCallback");
auto* callback_object = TRY(vm.argument(0).to_object(global_object));
if (!callback_object->is_function())
return vm.throw_completion<JS::TypeError>(global_object, JS::ErrorType::NotAFunctionNoParam);
// FIXME: accept options object
auto callback = adopt_own(*new Bindings::CallbackType(JS::make_handle(callback_object), HTML::incumbent_settings_object()));
return JS::Value(impl->request_idle_callback(move(callback)));
}
JS_DEFINE_NATIVE_FUNCTION(WindowObject::cancel_idle_callback)
{
auto* impl = TRY(impl_from(vm, global_object));
if (!vm.argument_count())
return vm.throw_completion<JS::TypeError>(global_object, JS::ErrorType::BadArgCountOne, "cancelIdleCallback");
auto id = TRY(vm.argument(0).to_u32(global_object));
impl->cancel_idle_callback(id);
return JS::js_undefined();
}
JS_DEFINE_NATIVE_FUNCTION(WindowObject::atob)
{
if (!vm.argument_count())