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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
|
/* eslint-env browser */
import {promiseWithResolvers} from '../../shared-util/sugar.js';
import {dispatchInternalEvent} from '../client-util.js';
export const info = {
id: 'wikiSearchInfo',
state: {
worker: null,
workerReadyPromise: null,
workerReadyPromiseResolvers: null,
workerActionCounter: 0,
workerActionPromiseResolverMap: new Map(),
downloads: Object.create(null),
},
event: {
whenWorkerAlive: [],
whenWorkerReady: [],
whenWorkerFailsToInitialize: [],
whenWorkerHasRuntimeError: [],
whenDownloadBegins: [],
whenDownloadsBegin: [],
whenDownloadProgresses: [],
whenDownloadEnds: [],
},
};
export async function initializeSearchWorker() {
const {state} = info;
if (state.worker) {
return await state.workerReadyPromise;
}
state.worker =
new Worker(
import.meta.resolve('../search-worker.js'),
{type: 'module'});
state.worker.onmessage = handleSearchWorkerMessage;
const {promise, resolve, reject} = promiseWithResolvers();
state.workerReadyPromiseResolvers = {resolve, reject};
return await (state.workerReadyPromise = promise);
}
function handleSearchWorkerMessage(message) {
switch (message.data.kind) {
case 'status':
handleSearchWorkerStatusMessage(message);
break;
case 'result':
handleSearchWorkerResultMessage(message);
break;
case 'download-begun':
handleSearchWorkerDownloadBegunMessage(message);
break;
case 'download-progress':
handleSearchWorkerDownloadProgressMessage(message);
break;
case 'download-complete':
handleSearchWorkerDownloadCompleteMessage(message);
break;
default:
console.warn(`Unknown message kind "${message.data.kind}" <- from search worker`);
break;
}
}
function handleSearchWorkerStatusMessage(message) {
const {state, event} = info;
switch (message.data.status) {
case 'alive':
console.debug(`Search worker is alive, but not yet ready.`);
dispatchInternalEvent(event, 'whenWorkerAlive');
break;
case 'ready':
console.debug(`Search worker has loaded corpuses and is ready.`);
state.workerReadyPromiseResolvers.resolve(state.worker);
dispatchInternalEvent(event, 'whenWorkerReady');
break;
case 'setup-error':
console.debug(`Search worker failed to initialize.`);
state.workerReadyPromiseResolvers.reject(new Error('Received "setup-error" status from worker'));
dispatchInternalEvent(event, 'whenWorkerFailsToInitialize');
break;
case 'runtime-error':
console.debug(`Search worker had an uncaught runtime error.`);
dispatchInternalEvent(event, 'whenWorkerHasRuntimeError');
break;
default:
console.warn(`Unknown status "${message.data.status}" <- from search worker`);
break;
}
}
function handleSearchWorkerResultMessage(message) {
const {state} = info;
const {id} = message.data;
if (!id) {
console.warn(`Result without id <- from search worker:`, message.data);
return;
}
if (!state.workerActionPromiseResolverMap.has(id)) {
console.warn(`Runaway result id <- from search worker:`, message.data);
return;
}
const {resolve, reject} =
state.workerActionPromiseResolverMap.get(id);
switch (message.data.status) {
case 'resolve':
resolve(message.data.value);
break;
case 'reject':
reject(message.data.value);
break;
default:
console.warn(`Unknown result status "${message.data.status}" <- from search worker`);
return;
}
state.workerActionPromiseResolverMap.delete(id);
}
function handleSearchWorkerDownloadBegunMessage(message) {
const {event} = info;
const {context: contextKey, keys} = message.data;
const context = getSearchWorkerDownloadContext(contextKey, true);
for (const key of keys) {
context[key] = 0.00;
dispatchInternalEvent(event, 'whenDownloadBegins', {
context: contextKey,
key,
});
}
dispatchInternalEvent(event, 'whenDownloadsBegin', {
context: contextKey,
keys,
});
}
function handleSearchWorkerDownloadProgressMessage(message) {
const {event} = info;
const {context: contextKey, key, progress} = message.data;
const context = getSearchWorkerDownloadContext(contextKey);
context[key] = progress;
dispatchInternalEvent(event, 'whenDownloadProgresses', {
context: contextKey,
key,
progress,
});
}
function handleSearchWorkerDownloadCompleteMessage(message) {
const {event} = info;
const {context: contextKey, key} = message.data;
const context = getSearchWorkerDownloadContext(contextKey);
context[key] = 1.00;
dispatchInternalEvent(event, 'whenDownloadEnds', {
context: contextKey,
key,
});
}
export function getSearchWorkerDownloadContext(context, initialize = false) {
const {state} = info;
if (context in state.downloads) {
return state.downloads[context];
}
if (!initialize) {
return null;
}
return state.downloads[context] = Object.create(null);
}
export async function postSearchWorkerAction(action, options) {
const {state} = info;
const worker = await initializeSearchWorker();
const id = ++state.workerActionCounter;
const {promise, resolve, reject} = promiseWithResolvers();
state.workerActionPromiseResolverMap.set(id, {resolve, reject});
worker.postMessage({
kind: 'action',
action: action,
id,
options,
});
return await promise;
}
export async function searchAll(query, options = {}) {
return await postSearchWorkerAction('search', {
query,
options,
});
}
|