ModuleMap.cpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. visitor.visit(it.value);
  16. }
  17. bool ModuleMap::is_fetching(URL::URL const& url, ByteString const& type) const
  18. {
  19. return is(url, type, EntryType::Fetching);
  20. }
  21. bool ModuleMap::is_failed(URL::URL const& url, ByteString const& type) const
  22. {
  23. return is(url, type, EntryType::Failed);
  24. }
  25. bool ModuleMap::is(URL::URL const& url, ByteString const& type, EntryType entry_type) const
  26. {
  27. auto value = m_values.get({ url, type });
  28. if (!value.has_value())
  29. return false;
  30. return value->type == entry_type;
  31. }
  32. Optional<ModuleMap::Entry> ModuleMap::get(URL::URL const& url, ByteString const& type) const
  33. {
  34. return m_values.get({ url, type });
  35. }
  36. AK::HashSetResult ModuleMap::set(URL::URL const& url, ByteString const& type, Entry entry)
  37. {
  38. // NOTE: Re-entering this function while firing wait_for_change callbacks is not allowed.
  39. VERIFY(!m_firing_callbacks);
  40. auto value = m_values.set({ url, type }, entry);
  41. auto callbacks = m_callbacks.get({ url, type });
  42. if (callbacks.has_value()) {
  43. m_firing_callbacks = true;
  44. for (auto const& callback : *callbacks)
  45. callback->function()(entry);
  46. m_firing_callbacks = false;
  47. }
  48. return value;
  49. }
  50. void ModuleMap::wait_for_change(JS::Heap& heap, URL::URL const& url, ByteString const& type, Function<void(Entry)> callback)
  51. {
  52. m_callbacks.ensure({ url, type }).append(JS::create_heap_function(heap, move(callback)));
  53. }
  54. }