/src/serenity/Userland/Libraries/LibWeb/HTML/Timer.cpp
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2020, Andreas Kling <kling@serenityos.org> |
3 | | * |
4 | | * SPDX-License-Identifier: BSD-2-Clause |
5 | | */ |
6 | | |
7 | | #include <LibCore/Timer.h> |
8 | | #include <LibJS/Runtime/Object.h> |
9 | | #include <LibWeb/HTML/Timer.h> |
10 | | #include <LibWeb/HTML/Window.h> |
11 | | |
12 | | namespace Web::HTML { |
13 | | |
14 | | JS_DEFINE_ALLOCATOR(Timer); |
15 | | |
16 | | JS::NonnullGCPtr<Timer> Timer::create(JS::Object& window_or_worker_global_scope, i32 milliseconds, Function<void()> callback, i32 id) |
17 | 0 | { |
18 | 0 | auto heap_function_callback = JS::create_heap_function(window_or_worker_global_scope.heap(), move(callback)); |
19 | 0 | return window_or_worker_global_scope.heap().allocate_without_realm<Timer>(window_or_worker_global_scope, milliseconds, heap_function_callback, id); |
20 | 0 | } |
21 | | |
22 | | Timer::Timer(JS::Object& window_or_worker_global_scope, i32 milliseconds, JS::NonnullGCPtr<JS::HeapFunction<void()>> callback, i32 id) |
23 | 0 | : m_window_or_worker_global_scope(window_or_worker_global_scope) |
24 | 0 | , m_callback(move(callback)) |
25 | 0 | , m_id(id) |
26 | 0 | { |
27 | 0 | m_timer = Core::Timer::create_single_shot(milliseconds, [this] { |
28 | 0 | m_callback->function()(); |
29 | 0 | }); |
30 | 0 | } |
31 | | |
32 | | void Timer::visit_edges(Cell::Visitor& visitor) |
33 | 0 | { |
34 | 0 | Base::visit_edges(visitor); |
35 | 0 | visitor.visit(m_window_or_worker_global_scope); |
36 | 0 | visitor.visit(m_callback); |
37 | 0 | } |
38 | | |
39 | | Timer::~Timer() |
40 | 0 | { |
41 | 0 | VERIFY(!m_timer->is_active()); |
42 | 0 | } |
43 | | |
44 | | void Timer::start() |
45 | 0 | { |
46 | 0 | m_timer->start(); |
47 | 0 | } |
48 | | |
49 | | void Timer::stop() |
50 | 0 | { |
51 | 0 | m_timer->stop(); |
52 | 0 | } |
53 | | |
54 | | } |