ModuleMap.cpp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*
  2. * Copyright (c) 2022-2023, networkException <networkexception@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <LibWeb/HTML/Scripting/ModuleMap.h>
  7. namespace Web::HTML {
  8. JS_DEFINE_ALLOCATOR(ModuleMap);
  9. void ModuleMap::visit_edges(Visitor& visitor)
  10. {
  11. Base::visit_edges(visitor);
  12. for (auto& it : m_values)
  13. visitor.visit(it.value.module_script);
  14. for (auto const& it : m_callbacks)
  15. for (auto const& callback : it.value)
  16. visitor.visit(callback);
  17. }
  18. bool ModuleMap::is_fetching(URL::URL const& url, ByteString const& type) const
  19. {
  20. return is(url, type, EntryType::Fetching);
  21. }
  22. bool ModuleMap::is_failed(URL::URL const& url, ByteString const& type) const
  23. {
  24. return is(url, type, EntryType::Failed);
  25. }
  26. bool ModuleMap::is(URL::URL const& url, ByteString const& type, EntryType entry_type) const
  27. {
  28. auto value = m_values.get({ url, type });
  29. if (!value.has_value())
  30. return false;
  31. return value->type == entry_type;
  32. }
  33. Optional<ModuleMap::Entry> ModuleMap::get(URL::URL const& url, ByteString const& type) const
  34. {
  35. return m_values.get({ url, type });
  36. }
  37. AK::HashSetResult ModuleMap::set(URL::URL const& url, ByteString const& type, Entry entry)
  38. {
  39. // NOTE: Re-entering this function while firing wait_for_change callbacks is not allowed.
  40. VERIFY(!m_firing_callbacks);
  41. auto value = m_values.set({ url, type }, entry);
  42. auto callbacks = m_callbacks.get({ url, type });
  43. if (callbacks.has_value()) {
  44. m_firing_callbacks = true;
  45. for (auto const& callback : *callbacks)
  46. callback->function()(entry);
  47. m_firing_callbacks = false;
  48. }
  49. return value;
  50. }
  51. void ModuleMap::wait_for_change(JS::Heap& heap, URL::URL const& url, ByteString const& type, Function<void(Entry)> callback)
  52. {
  53. m_callbacks.ensure({ url, type }).append(JS::create_heap_function(heap, move(callback)));
  54. }
  55. }