-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
172 lines (147 loc) · 4.43 KB
/
index.ts
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
import { readFile } from 'node:fs/promises';
import { createRoot } from 'solid-js';
import { unwrap } from 'solid-js/store';
import type { Store } from 'solid-js/store';
import { ToastState } from '../../src/stores/Toast';
import type { BookRaw, BookState } from '../../src/stores/BookStore';
type FnVoid = () => void;
type Maybe<T> = T | undefined;
type Resolve<T> = (value: T) => void;
type Reject = (reason: unknown) => void;
type Done<T> = (data: Maybe<T>, err?: unknown) => void;
type Factory<T> = (done: Done<T>) => Maybe<FnVoid>;
const TOAST_SILENCE = 'CRICKETS';
async function rootAndRun<T>(
timeoutMs: number,
factory: Factory<T>
): Promise<T> {
let disposeFn: Maybe<FnVoid>;
let timeoutId: Maybe<ReturnType<typeof setTimeout>>;
try {
return await new Promise(executor);
} finally {
if (disposeFn) {
disposeFn();
disposeFn = undefined;
}
if (timeoutId) {
clearTimeout(timeoutId);
timeoutId = undefined;
}
}
// ---
function executor(resolve: Resolve<T>, reject: Reject) {
createRoot((dispose) => {
disposeFn = dispose;
timeoutId = setTimeout(timeout, timeoutMs);
// queueMicrotask/setTimeout allows `setup` to finish
// before exercising the reactive graph with `run`
const run = factory(done);
if (typeof run === 'function') queueMicrotask(run);
});
// ---
function timeout() {
timeoutId = undefined;
reject(new Error('Timed out'));
}
function done(data: Maybe<T>, reason: unknown) {
// eslint-disable-next-line eqeqeq
if (data != undefined) resolve(data);
else reject(reason);
}
}
}
function constantFetcher(
data: BookRaw[]
): (s: unknown, i: unknown) => Promise<BookRaw[]> {
return (_source: unknown, _info: unknown) => {
return Promise.resolve(data);
};
}
function cycleFetcher(
data: BookRaw[][]
): (s: unknown, i: unknown) => Promise<BookRaw[]> {
const last = data.length - 1;
let next = 0;
const cycle = () => {
next = next < last ? next + 1 : 0;
};
return (_source: unknown, _info: unknown) => {
const payload = data[next];
cycle();
return Promise.resolve(payload);
};
}
const jsonBooks = `[
{"id":"978-0641723445","cat":["book","hardcover"],"name":"The Lightning Thief",
"author":"Rick Riordan","series":"Percy Jackson and the Olympians","sequence":1,
"genre":"fantasy","in_stock":true,"price":12.5,"pages":384},
{"id":"978-1423103349","cat":["book","paperback"],"name":"The Sea of Monsters",
"author":"Rick Riordan","series":"Percy Jackson and the Olympians","sequence":2,
"genre":"fantasy","in_stock":true,"price":6.49,"pages":304},
{"id":"978-1857995879","cat":["book","paperback"],"name":"Sophie's World : The Greek Philosophers",
"author":"Jostein Gaarder","sequence":1,
"genre":"fantasy","in_stock":true,"price":3.07,"pages":64},
{"id":"978-1933988177","cat":["book","paperback"],"name":"Lucene in Action, Second Edition",
"author":"Michael McCandless","sequence":1,
"genre":"IT","in_stock":true,"price":30.5,"pages":475}
]`;
function fakeSimpleFetcher(
_source: unknown,
_info: unknown
): Promise<BookRaw[]> {
return Promise.resolve(JSON.parse(jsonBooks));
}
function makeFileFetcher(fullpath: string) {
return (_source: unknown, _info: unknown) => {
return readFile(fullpath, { encoding: 'utf8' }).then((json) =>
JSON.parse(json)
);
};
}
function makeCachedFetcher(fullpath: string) {
let result: Promise<BookRaw[]> | undefined;
const fileFetcher = makeFileFetcher(fullpath);
return (source: unknown, info: unknown) => {
if (result) return result;
result = fileFetcher(source, info);
return result;
};
}
const nullStorage = {
load() {
return Promise.resolve('[]');
},
save(_json: string) {
return Promise.resolve();
},
};
Object.freeze(nullStorage);
function booksSizes(books: Store<BookState>): [number, number] {
const data = unwrap(books);
let size = 0;
let available = 0;
for (const id in data) {
size += 1;
if (data[id].isAvailable) available += 1;
}
return [size, available];
}
function lastMessage(state?: ToastState): string {
if (state == undefined) return TOAST_SILENCE;
if (state.messages.length === 0) return TOAST_SILENCE;
return state.messages[state.messages.length - 1];
}
export {
booksSizes,
constantFetcher,
cycleFetcher,
fakeSimpleFetcher,
lastMessage,
makeCachedFetcher,
makeFileFetcher,
nullStorage,
rootAndRun,
TOAST_SILENCE,
};
export type { Done, Factory };