forked from firefox-devtools/debugger
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcore.js
More file actions
174 lines (144 loc) · 3.93 KB
/
core.js
File metadata and controls
174 lines (144 loc) · 3.93 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
export type Resource<R: ResourceBound> = $ReadOnly<$Exact<R>>;
export type ResourceBound = {
+id: string
};
export type Id<R: ResourceBound> = $ElementType<R, "id">;
type ResourceSubset<R: ResourceBound> = $ReadOnly<{
+id: Id<R>,
...$Shape<$Rest<R, { +id: Id<R> }>>
}>;
export opaque type ResourceIdentity: { [string]: mixed } = {||};
export type ResourceValues<R: ResourceBound> = { [Id<R>]: R };
export opaque type ResourceState<R: ResourceBound> = {
identity: { [Id<R>]: ResourceIdentity },
values: ResourceValues<R>
};
export function createInitial<R: ResourceBound>(): ResourceState<R> {
return {
identity: {},
values: {}
};
}
export function insertResources<R: ResourceBound>(
state: ResourceState<R>,
resources: $ReadOnlyArray<R>
): ResourceState<R> {
if (resources.length === 0) {
return state;
}
state = {
identity: { ...state.identity },
values: { ...state.values }
};
for (const resource of resources) {
const { id } = resource;
if (state.identity[id]) {
throw new Error(`Resource "${id}" already exists, cannot insert`);
}
if (state.values[id]) {
throw new Error(
`Resource state corrupt: ${id} has value but no identity`
);
}
state.identity[resource.id] = makeIdentity();
state.values[resource.id] = resource;
}
return state;
}
export function removeResources<R: ResourceBound>(
state: ResourceState<R>,
resources: $ReadOnlyArray<ResourceSubset<R> | Id<R>>
): ResourceState<R> {
if (resources.length === 0) {
return state;
}
state = {
identity: { ...state.identity },
values: { ...state.values }
};
for (let id of resources) {
if (typeof id !== "string") {
id = id.id;
}
if (!state.identity[id]) {
throw new Error(`Resource "${id}" does not exists, cannot remove`);
}
if (!state.values[id]) {
throw new Error(
`Resource state corrupt: ${id} has identity but no value`
);
}
delete state.identity[id];
delete state.values[id];
}
return state;
}
export function updateResources<R: ResourceBound>(
state: ResourceState<R>,
resources: $ReadOnlyArray<ResourceSubset<R>>
): ResourceState<R> {
if (resources.length === 0) {
return state;
}
let didCopyValues = false;
for (const subset of resources) {
const { id } = subset;
if (!state.identity[id]) {
throw new Error(`Resource "${id}" does not exists, cannot update`);
}
if (!state.values[id]) {
throw new Error(
`Resource state corrupt: ${id} has identity but no value`
);
}
const existing = state.values[id];
const updated = {};
for (const field of Object.keys(subset)) {
if (field === "id") {
continue;
}
if (subset[field] !== existing[field]) {
updated[field] = subset[field];
}
}
if (Object.keys(updated).length > 0) {
if (!didCopyValues) {
didCopyValues = true;
state = {
identity: state.identity,
values: { ...state.values }
};
}
state.values[id] = { ...existing, ...updated };
}
}
return state;
}
export function makeIdentity(): ResourceIdentity {
return ({}: any);
}
export function getResourcePair<R: ResourceBound>(
state: ResourceState<R>,
id: Id<R>
): {
value: R,
identity: ResourceIdentity
} | null {
const value = state.values[id];
const identity = state.identity[id];
if ((value && !identity) || (!value && identity)) {
throw new Error(
`Resource state corrupt: ${id} has mismatched value and identity`
);
}
return value ? { value, identity } : null;
}
export function getResourceValues<R: ResourceBound>(
state: ResourceState<R>
): ResourceValues<R> {
return state.values;
}