ModuleMap.cpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. void ModuleMap::visit_edges(Visitor& visitor)
  9. {
  10. Base::visit_edges(visitor);
  11. for (auto& it : m_values)
  12. visitor.visit(it.value.module_script);
  13. for (auto const& it : m_callbacks)
  14. for (auto const& callback : it.value)
  15. visitor.visit(callback);
  16. }
  17. bool ModuleMap::is_fetching(AK::URL const& url, DeprecatedString const& type) const
  18. {
  19. return is(url, type, EntryType::Fetching);
  20. }
  21. bool ModuleMap::is_failed(AK::URL const& url, DeprecatedString const& type) const
  22. {
  23. return is(url, type, EntryType::Failed);
  24. }
  25. bool ModuleMap::is(AK::URL const& url, DeprecatedString 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(AK::URL const& url, DeprecatedString const& type) const
  33. {
  34. return m_values.get({ url, type });
  35. }
  36. AK::HashSetResult ModuleMap::set(AK::URL const& url, DeprecatedString const& type, Entry entry)
  37. {
  38. auto value = m_values.set({ url, type }, entry);
  39. auto callbacks = m_callbacks.get({ url, type });
  40. if (callbacks.has_value())
  41. for (auto const& callback : *callbacks)
  42. callback->function()(entry);
  43. return value;
  44. }
  45. void ModuleMap::wait_for_change(JS::Heap& heap, AK::URL const& url, DeprecatedString const& type, Function<void(Entry)> callback)
  46. {
  47. m_callbacks.ensure({ url, type }).append(JS::create_heap_function(heap, move(callback)));
  48. }
  49. }