summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibWeb/HTML/Scripting/ModuleMap.h
blob: 7d177b26954a3fddbe9e72cbeec1069a26295127 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*
 * Copyright (c) 2022, networkException <networkexception@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#pragma once

#include <LibWeb/HTML/Scripting/ModuleScript.h>

namespace Web::HTML {

class ModuleLocationTuple {
public:
    ModuleLocationTuple(AK::URL url, DeprecatedString type)
        : m_url(move(url))
        , m_type(move(type))
    {
    }

    AK::URL const& url() const { return m_url; };
    DeprecatedString const& type() const { return m_type; }

    bool operator==(ModuleLocationTuple const& other) const
    {
        return other.url() == m_url && other.type() == m_type;
    };

private:
    AK::URL m_url;
    DeprecatedString m_type;
};

// https://html.spec.whatwg.org/multipage/webappapis.html#module-map
class ModuleMap {
    AK_MAKE_NONCOPYABLE(ModuleMap);

public:
    ModuleMap() = default;
    ~ModuleMap() = default;

    enum class EntryType {
        Fetching,
        Failed,
        ModuleScript
    };

    struct Entry {
        EntryType type;
        JavaScriptModuleScript* module_script;
    };

    bool is_fetching(AK::URL const& url, DeprecatedString const& type) const;
    bool is_failed(AK::URL const& url, DeprecatedString const& type) const;

    bool is(AK::URL const& url, DeprecatedString const& type, EntryType) const;

    Optional<Entry> get(AK::URL const& url, DeprecatedString const& type) const;

    AK::HashSetResult set(AK::URL const& url, DeprecatedString const& type, Entry);

    void wait_for_change(AK::URL const& url, DeprecatedString const& type, Function<void(Entry)> callback);

private:
    HashMap<ModuleLocationTuple, Entry> m_values;
    HashMap<ModuleLocationTuple, Vector<Function<void(Entry)>>> m_callbacks;
};

}

namespace AK {

template<>
struct Traits<Web::HTML::ModuleLocationTuple> : public GenericTraits<Web::HTML::ModuleLocationTuple> {
    static unsigned hash(Web::HTML::ModuleLocationTuple const& tuple)
    {
        return pair_int_hash(tuple.url().to_deprecated_string().hash(), tuple.type().hash());
    }
};

}