ModuleMap.cpp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /*
  2. * Copyright (c) 2022, 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. }
  14. }
  15. bool ModuleMap::is_fetching(AK::URL const& url, DeprecatedString const& type) const
  16. {
  17. return is(url, type, EntryType::Fetching);
  18. }
  19. bool ModuleMap::is_failed(AK::URL const& url, DeprecatedString const& type) const
  20. {
  21. return is(url, type, EntryType::Failed);
  22. }
  23. bool ModuleMap::is(AK::URL const& url, DeprecatedString const& type, EntryType entry_type) const
  24. {
  25. auto value = m_values.get({ url, type });
  26. if (!value.has_value())
  27. return false;
  28. return value->type == entry_type;
  29. }
  30. Optional<ModuleMap::Entry> ModuleMap::get(AK::URL const& url, DeprecatedString const& type) const
  31. {
  32. return m_values.get({ url, type });
  33. }
  34. AK::HashSetResult ModuleMap::set(AK::URL const& url, DeprecatedString const& type, Entry entry)
  35. {
  36. auto callbacks = m_callbacks.get({ url, type });
  37. if (callbacks.has_value())
  38. for (auto const& callback : *callbacks)
  39. callback(entry);
  40. return m_values.set({ url, type }, entry);
  41. }
  42. void ModuleMap::wait_for_change(AK::URL const& url, DeprecatedString const& type, Function<void(Entry)> callback)
  43. {
  44. m_callbacks.ensure({ url, type }).append(move(callback));
  45. }
  46. }