SessionHistoryTraversalQueue.h 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. /*
  2. * Copyright (c) 2023, Aliaksandr Kalenik <kalenik.aliaksandr@gmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <LibCore/Timer.h>
  8. namespace Web::HTML {
  9. // https://html.spec.whatwg.org/multipage/document-sequences.html#tn-session-history-traversal-queue
  10. class SessionHistoryTraversalQueue {
  11. public:
  12. SessionHistoryTraversalQueue()
  13. {
  14. m_timer = Core::Timer::create_single_shot(0, [this] {
  15. while (m_queue.size() > 0) {
  16. auto steps = m_queue.take_first();
  17. steps();
  18. }
  19. }).release_value_but_fixme_should_propagate_errors();
  20. }
  21. void append(JS::SafeFunction<void()> steps)
  22. {
  23. m_queue.append(move(steps));
  24. if (!m_timer->is_active()) {
  25. m_timer->start();
  26. }
  27. }
  28. void process()
  29. {
  30. while (m_queue.size() > 0) {
  31. auto steps = m_queue.take_first();
  32. steps();
  33. }
  34. }
  35. private:
  36. Vector<JS::SafeFunction<void()>> m_queue;
  37. RefPtr<Core::Timer> m_timer;
  38. };
  39. }