ModuleMap.cpp 1.3 KB

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