-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate-test-module-cache.js
More file actions
143 lines (120 loc) · 2.85 KB
/
Copy pathcreate-test-module-cache.js
File metadata and controls
143 lines (120 loc) · 2.85 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
import invariant from "tiny-invariant";
import {
IS_MODULE,
MODULE_FETCH,
IS_MODULE_CACHE,
MODULE_CACHE_FETCH,
MODULE_PENDING,
MODULE_SUCCESS,
MODULE_FAILURE,
} from "./constants";
export default function createTestResourceCache() {
const entries = new Map();
function fetch(module) {
invariant(
module && typeof module === "object" && module[IS_MODULE],
"expected module to be an object returned by `createModule`",
);
const entry = entries.get(module);
if (entry) {
return entry;
} else {
return createEntry(module);
}
}
function createEntry(module) {
let subscribers = [];
function listen(cb) {
if (subscribers) {
subscribers.push(cb);
}
return () => {
if (subscribers) {
const index = subscribers.indexOf(cb);
if (index > -1) {
subscribers.splice(index, 1);
}
}
};
}
async function wait() {
if (subscribers) {
const data = await module[MODULE_FETCH]();
if (subscribers) {
const successEntry = {
status: MODULE_SUCCESS,
module: data,
};
entries.set(module, successEntry);
for (const cb of subscribers) {
cb(successEntry);
}
subscribers = null;
}
}
}
function fail(error) {
if (subscribers) {
const failureEntry = {
status: MODULE_FAILURE,
error,
};
entries.set(module, failureEntry);
for (const cb of subscribers) {
cb(failureEntry);
}
subscribers = null;
}
}
const pendingEntry = {
status: MODULE_PENDING,
listen,
wait,
fail,
};
entries.set(module, pendingEntry);
return pendingEntry;
}
function preload(module) {
const entry = fetch(module);
if (entry.status === MODULE_PENDING) {
return new Promise(resolve => {
entry.listen(resolve);
});
} else {
return Promise.resolve(entry);
}
}
function wait(module) {
const entry = getPendingEntry(module);
return entry.wait();
}
function waitAll() {
return Promise.all(
[...entries.values()]
.filter(entry => entry.status === MODULE_PENDING)
.map(entry => entry.wait()),
).then(() => {});
}
function fail(module, error) {
const entry = getPendingEntry(module);
return entry.fail(error);
}
function getPendingEntry(module) {
const entry = entries.get(module);
invariant(entry, "expected module to have been fetched");
invariant(
entry.status === MODULE_PENDING,
"expected module not to have been waited on before",
);
return entry;
}
return {
[IS_MODULE_CACHE]: true,
[MODULE_CACHE_FETCH]: fetch,
preload,
wait,
waitAll,
fail,
};
}