forked from firefox-devtools/debugger
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquery-cache.js
More file actions
148 lines (131 loc) · 4.13 KB
/
query-cache.js
File metadata and controls
148 lines (131 loc) · 4.13 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
/* 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 type { ResourceBound, ResourceState } from "./core";
import type {
ResourceQuery,
QueryCacheHandler,
QueryContext,
QueryResult
} from "./base-query";
import { strictEqual, shallowEqual } from "./compare";
export type WeakArgsBound =
| $ReadOnly<{ [string]: mixed }>
| $ReadOnlyArray<mixed>;
export type ShallowArgsBound =
| $ReadOnly<{ [string]: mixed }>
| $ReadOnlyArray<mixed>;
/**
* A query 'cache' function that uses the identity of the arguments object to
* cache data for the query itself.
*/
export function queryCacheWeak<
R: ResourceBound,
Args: WeakArgsBound,
Mapped,
Reduced
>(
handler: QueryCacheHandler<R, Args, Mapped, Reduced>
): ResourceQuery<R, Args, Reduced> {
const cache = new WeakMap();
return makeCacheFunction({
handler,
// The WeakMap will only return entries for the exact object,
// so there is no need to compare at all.
compareArgs: () => true,
getEntry: args => cache.get(args) || null,
setEntry: (args, entry) => {
cache.set(args, entry);
}
});
}
/**
* A query 'cache' function that uses shallow comparison to cache the most
* recent calculated result based on the value of the argument.
*/
export function queryCacheShallow<
R: ResourceBound,
// We require args to be an object here because if you're using a primitive
// then you should be using queryCacheStrict instead.
Args: ShallowArgsBound,
Mapped,
Reduced
>(
handler: QueryCacheHandler<R, Args, Mapped, Reduced>
): ResourceQuery<R, Args, Reduced> {
let latestEntry = null;
return makeCacheFunction({
handler,
compareArgs: shallowEqual,
getEntry: () => latestEntry,
setEntry: (args, entry) => {
latestEntry = entry;
}
});
}
/**
* A query 'cache' function that uses strict comparison to cache the most
* recent calculated result based on the value of the argument.
*/
export function queryCacheStrict<R: ResourceBound, Args, Mapped, Reduced>(
handler: QueryCacheHandler<R, Args, Mapped, Reduced>
): ResourceQuery<R, Args, Reduced> {
let latestEntry = null;
return makeCacheFunction({
handler,
compareArgs: strictEqual,
getEntry: () => latestEntry,
setEntry: (args, entry) => {
latestEntry = entry;
}
});
}
type CacheEntry<R: ResourceBound, Args, Mapped, Reduced> = {
context: QueryContext<Args>,
state: ResourceState<R>,
result: QueryResult<Mapped, Reduced>
};
type CacheFunctionInfo<R: ResourceBound, Args, Mapped, Reduced> = {|
// The handler to call when the args or the state are different from
// those in the entry for the arguments.
handler: QueryCacheHandler<R, Args, Mapped, Reduced>,
// Compare two sets of arguments to decide whether or not they should be
// treated as the same set of arguments from the standpoint of caching.
compareArgs: (a: Args, b: Args) => boolean,
getEntry: (args: Args) => CacheEntry<R, Args, Mapped, Reduced> | null,
setEntry: (args: Args, entry: CacheEntry<R, Args, Mapped, Reduced>) => void
|};
function makeCacheFunction<R: ResourceBound, Args, Mapped, Reduced>(
info: CacheFunctionInfo<R, Args, Mapped, Reduced>
): ResourceQuery<R, Args, Reduced> {
const { handler, compareArgs, getEntry, setEntry } = info;
return (state, args: Args) => {
let entry = getEntry(args);
const sameArgs = !!entry && compareArgs(entry.context.args, args);
const sameState = !!entry && entry.state === state;
if (!entry || !sameArgs || !sameState) {
const context =
!entry || !sameArgs
? {
args,
identMap: new WeakMap()
}
: entry.context;
const result = handler(state, context, entry ? entry.result : null);
if (entry) {
entry.context = context;
entry.state = state;
entry.result = result;
} else {
entry = {
context,
state,
result
};
setEntry(args, entry);
}
}
return entry.result.reduced;
};
}