forked from firefox-devtools/debugger
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbase-query.js
More file actions
174 lines (150 loc) · 4.88 KB
/
base-query.js
File metadata and controls
174 lines (150 loc) · 4.88 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at <http://mozilla.org/MPL/2.0/>. */
// @flow
import {
getResourceValues,
getResourcePair,
makeIdentity,
type ResourceBound,
type Id,
type ResourceState,
type ResourceValues,
type ResourceIdentity
} from "./core";
import { arrayShallowEqual } from "./compare";
export type ResourceQuery<R: ResourceBound, Args, Reduced> = (
ResourceState<R>,
Args
) => Reduced;
export type QueryFilter<R: ResourceBound, Args> = (
ResourceValues<R>,
Args
) => Array<Id<R>>;
export type QueryMap<R: ResourceBound, Args, Mapped> =
| QueryMapNoArgs<R, Mapped>
| QueryMapWithArgs<R, Args, Mapped>;
export type QueryMapNoArgs<R: ResourceBound, Mapped> = {
+needsArgs?: false,
(R, ResourceIdentity): Mapped
};
export type QueryMapWithArgs<R: ResourceBound, Args, Mapped> = {
+needsArgs: true,
(R, ResourceIdentity, Args): Mapped
};
export type QueryReduce<R: ResourceBound, Args, Mapped, Reduced> = (
$ReadOnlyArray<Mapped>,
$ReadOnlyArray<Id<R>>,
Args
) => Reduced;
export type QueryContext<Args> = {
args: Args,
identMap: WeakMap<ResourceIdentity, ResourceIdentity>
};
export type QueryResult<Mapped, Reduced> = {
mapped: Array<Mapped>,
reduced: Reduced
};
export type QueryResultCompare<Reduced> = (Reduced, Reduced) => boolean;
export type QueryCacheHandler<R: ResourceBound, Args, Mapped, Reduced> = (
ResourceState<R>,
QueryContext<Args>,
QueryResult<Mapped, Reduced> | null
) => QueryResult<Mapped, Reduced>;
export type QueryCache<R: ResourceBound, Args, Mapped, Reduced> = (
handler: QueryCacheHandler<R, Args, Mapped, Reduced>
) => ResourceQuery<R, Args, Reduced>;
export function makeMapWithArgs<R: ResourceBound, Args, Mapped>(
map: (R, ResourceIdentity, Args) => Mapped
): QueryMapWithArgs<R, Args, Mapped> {
const wrapper = (resource, identity, args) => map(resource, identity, args);
wrapper.needsArgs = true;
return wrapper;
}
export function makeResourceQuery<R: ResourceBound, Args, Mapped, Reduced>({
cache,
filter,
map,
reduce,
resultCompare
}: {|
cache: QueryCache<R, Args, Mapped, Reduced>,
filter: QueryFilter<R, Args>,
map: QueryMap<R, Args, Mapped>,
reduce: QueryReduce<R, Args, Mapped, Reduced>,
resultCompare: QueryResultCompare<Reduced>
|}): ResourceQuery<R, Args, Reduced> {
const loadResource = makeResourceMapper(map);
return cache((state, context, existing) => {
const ids = filter(getResourceValues(state), context.args);
const mapped = ids.map(id => loadResource(state, id, context));
if (existing && arrayShallowEqual(existing.mapped, mapped)) {
// If the items are exactly the same as the existing ones, we return
// early to reuse the existing result.
return existing;
}
const reduced = reduce(mapped, ids, context.args);
if (existing && resultCompare(existing.reduced, reduced)) {
return existing;
}
return { mapped, reduced };
});
}
type ResourceLoader<R: ResourceBound, Args, Mapped> = (
ResourceState<R>,
Id<R>,
QueryContext<Args>
) => Mapped;
function makeResourceMapper<R: ResourceBound, Args, Mapped>(
map: QueryMap<R, Args, Mapped>
): ResourceLoader<R, Args, Mapped> {
return map.needsArgs
? makeResourceArgsMapper(map)
: makeResourceNoArgsMapper(map);
}
/**
* Resources loaded when things care about arguments need to be given a
* special ResourceIdentity object that correlates with both the resource
* _and_ the arguments being passed to the query. That means they need extra
* logic when loading those resources.
*/
function makeResourceArgsMapper<R: ResourceBound, Args, Mapped>(
map: QueryMapWithArgs<R, Args, Mapped>
): ResourceLoader<R, Args, Mapped> {
const mapper = (value, identity, context) =>
map(value, getIdentity(context.identMap, identity), context.args);
return (state, id, context) => getCachedResource(state, id, context, mapper);
}
function makeResourceNoArgsMapper<R: ResourceBound, Args, Mapped>(
map: QueryMapNoArgs<R, Mapped>
): ResourceLoader<R, Args, Mapped> {
const mapper = (value, identity, context) => map(value, identity);
return (state, id, context) => getCachedResource(state, id, context, mapper);
}
function getCachedResource<R: ResourceBound, Args, Mapped>(
state: ResourceState<R>,
id: Id<R>,
context: QueryContext<Args>,
map: (
value: R,
identity: ResourceIdentity,
context: QueryContext<Args>
) => Mapped
): Mapped {
const pair = getResourcePair(state, id);
if (!pair) {
throw new Error(`Resource ${id} does not exist`);
}
return map(pair.value, pair.identity, context);
}
function getIdentity(
identMap: WeakMap<ResourceIdentity, ResourceIdentity>,
identity: ResourceIdentity
) {
let ident = identMap.get(identity);
if (!ident) {
ident = makeIdentity();
identMap.set(identity, ident);
}
return ident;
}