@forcecalendar/core 2.1.64 → 2.1.65
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/core/index.js +1 -1
- package/core/search/SearchWorkerManager.js +110 -25
- package/package.json +1 -1
package/core/index.js
CHANGED
|
@@ -28,7 +28,7 @@ export { RRuleParser } from './events/RRuleParser.js';
|
|
|
28
28
|
export { EnhancedCalendar } from './integration/EnhancedCalendar.js';
|
|
29
29
|
|
|
30
30
|
// Version — keep in sync with package.json
|
|
31
|
-
export const VERSION = '2.1.
|
|
31
|
+
export const VERSION = '2.1.65';
|
|
32
32
|
|
|
33
33
|
// Default export
|
|
34
34
|
export { Calendar as default } from './calendar/Calendar.js';
|
|
@@ -9,6 +9,8 @@ export class SearchWorkerManager {
|
|
|
9
9
|
this.workerSupported = typeof Worker !== 'undefined';
|
|
10
10
|
this.worker = null;
|
|
11
11
|
this.indexReady = false;
|
|
12
|
+
this.indexMode = 'none';
|
|
13
|
+
this.workerExpectedCount = 0;
|
|
12
14
|
this.pendingSearches = [];
|
|
13
15
|
|
|
14
16
|
// Fallback to main thread if workers not available
|
|
@@ -19,7 +21,8 @@ export class SearchWorkerManager {
|
|
|
19
21
|
chunkSize: 100, // Events per indexing batch
|
|
20
22
|
maxWorkers: 4, // Max parallel workers
|
|
21
23
|
indexThreshold: 1000, // Use workers above this event count
|
|
22
|
-
cacheSize: 50 // LRU cache for search results
|
|
24
|
+
cacheSize: 50, // LRU cache for search results
|
|
25
|
+
searchTimeout: 10000 // Worker search timeout in milliseconds
|
|
23
26
|
};
|
|
24
27
|
|
|
25
28
|
// Search result cache
|
|
@@ -35,7 +38,7 @@ export class SearchWorkerManager {
|
|
|
35
38
|
initializeWorker() {
|
|
36
39
|
if (!this.workerSupported) {
|
|
37
40
|
// Use InvertedIndex as fallback
|
|
38
|
-
this.
|
|
41
|
+
this.ensureFallbackIndex();
|
|
39
42
|
return;
|
|
40
43
|
}
|
|
41
44
|
|
|
@@ -51,13 +54,14 @@ export class SearchWorkerManager {
|
|
|
51
54
|
events[event.id] = event;
|
|
52
55
|
|
|
53
56
|
// Index each field
|
|
54
|
-
const fields = ['title', 'description', 'location', 'category'];
|
|
57
|
+
const fields = ['title', 'description', 'location', 'category', 'categories'];
|
|
55
58
|
for (const field of fields) {
|
|
56
59
|
const value = event[field];
|
|
57
60
|
if (!value) continue;
|
|
58
61
|
|
|
59
62
|
// Tokenize and index
|
|
60
|
-
const
|
|
63
|
+
const values = Array.isArray(value) ? value : [value];
|
|
64
|
+
const tokens = values.flatMap(item => tokenize(String(item).toLowerCase()));
|
|
61
65
|
for (const token of tokens) {
|
|
62
66
|
if (!index[token]) {
|
|
63
67
|
index[token] = new Set();
|
|
@@ -163,11 +167,19 @@ export class SearchWorkerManager {
|
|
|
163
167
|
};
|
|
164
168
|
`;
|
|
165
169
|
|
|
166
|
-
|
|
167
|
-
const blob = new Blob([workerCode], { type: 'application/javascript' });
|
|
168
|
-
const workerUrl = URL.createObjectURL(blob);
|
|
169
|
-
|
|
170
|
+
let workerUrl = null;
|
|
170
171
|
try {
|
|
172
|
+
if (
|
|
173
|
+
typeof Blob === 'undefined' ||
|
|
174
|
+
typeof URL === 'undefined' ||
|
|
175
|
+
typeof URL.createObjectURL !== 'function'
|
|
176
|
+
) {
|
|
177
|
+
throw new Error('Blob workers are not supported in this environment');
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
// Create worker from blob
|
|
181
|
+
const blob = new Blob([workerCode], { type: 'application/javascript' });
|
|
182
|
+
workerUrl = URL.createObjectURL(blob);
|
|
171
183
|
this.worker = new Worker(workerUrl);
|
|
172
184
|
this.setupWorkerHandlers();
|
|
173
185
|
|
|
@@ -180,10 +192,24 @@ export class SearchWorkerManager {
|
|
|
180
192
|
// Clean up blob URL
|
|
181
193
|
URL.revokeObjectURL(workerUrl);
|
|
182
194
|
} catch (error) {
|
|
195
|
+
if (workerUrl) {
|
|
196
|
+
URL.revokeObjectURL(workerUrl);
|
|
197
|
+
}
|
|
183
198
|
console.warn('Worker creation failed, falling back to main thread:', error);
|
|
184
199
|
this.workerSupported = false;
|
|
200
|
+
this.ensureFallbackIndex();
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* Ensure a fallback index exists.
|
|
206
|
+
* @private
|
|
207
|
+
*/
|
|
208
|
+
ensureFallbackIndex() {
|
|
209
|
+
if (!this.fallbackIndex) {
|
|
185
210
|
this.fallbackIndex = new InvertedIndex();
|
|
186
211
|
}
|
|
212
|
+
return this.fallbackIndex;
|
|
187
213
|
}
|
|
188
214
|
|
|
189
215
|
/**
|
|
@@ -191,7 +217,7 @@ export class SearchWorkerManager {
|
|
|
191
217
|
*/
|
|
192
218
|
setupWorkerHandlers() {
|
|
193
219
|
this.worker.onmessage = e => {
|
|
194
|
-
const { type
|
|
220
|
+
const { type } = e.data;
|
|
195
221
|
|
|
196
222
|
switch (type) {
|
|
197
223
|
case 'ready':
|
|
@@ -200,8 +226,10 @@ export class SearchWorkerManager {
|
|
|
200
226
|
break;
|
|
201
227
|
|
|
202
228
|
case 'indexed':
|
|
203
|
-
|
|
204
|
-
|
|
229
|
+
if (e.data.count >= this.workerExpectedCount) {
|
|
230
|
+
this.indexMode = 'worker';
|
|
231
|
+
this.processPendingSearches();
|
|
232
|
+
}
|
|
205
233
|
break;
|
|
206
234
|
|
|
207
235
|
case 'results':
|
|
@@ -212,9 +240,12 @@ export class SearchWorkerManager {
|
|
|
212
240
|
|
|
213
241
|
this.worker.onerror = error => {
|
|
214
242
|
console.error('Worker error:', error);
|
|
243
|
+
this.rejectPendingSearches(new Error('Search worker failed'));
|
|
215
244
|
// Fallback to main thread
|
|
216
245
|
this.workerSupported = false;
|
|
217
|
-
this.
|
|
246
|
+
this.worker = null;
|
|
247
|
+
this.ensureFallbackIndex().buildIndex(this.eventStore.getAllEvents());
|
|
248
|
+
this.indexMode = 'fallback';
|
|
218
249
|
};
|
|
219
250
|
}
|
|
220
251
|
|
|
@@ -223,17 +254,26 @@ export class SearchWorkerManager {
|
|
|
223
254
|
*/
|
|
224
255
|
async indexEvents() {
|
|
225
256
|
const events = this.eventStore.getAllEvents();
|
|
257
|
+
this.searchCache.clear();
|
|
258
|
+
this.cacheOrder = [];
|
|
226
259
|
|
|
227
260
|
// Use main thread for small datasets
|
|
228
261
|
if (events.length < this.config.indexThreshold) {
|
|
229
|
-
|
|
230
|
-
|
|
262
|
+
this.ensureFallbackIndex().buildIndex(events);
|
|
263
|
+
this.indexMode = 'fallback';
|
|
264
|
+
|
|
265
|
+
if (this.worker && this.indexReady) {
|
|
266
|
+
this.worker.postMessage({ type: 'clear' });
|
|
231
267
|
}
|
|
232
268
|
return;
|
|
233
269
|
}
|
|
234
270
|
|
|
235
271
|
// Chunk events for worker
|
|
236
272
|
if (this.worker && this.indexReady) {
|
|
273
|
+
this.indexMode = 'worker-indexing';
|
|
274
|
+
this.workerExpectedCount = events.length;
|
|
275
|
+
this.worker.postMessage({ type: 'clear' });
|
|
276
|
+
|
|
237
277
|
for (let i = 0; i < events.length; i += this.config.chunkSize) {
|
|
238
278
|
const chunk = events.slice(i, i + this.config.chunkSize);
|
|
239
279
|
this.worker.postMessage({
|
|
@@ -241,7 +281,11 @@ export class SearchWorkerManager {
|
|
|
241
281
|
data: { events: chunk }
|
|
242
282
|
});
|
|
243
283
|
}
|
|
284
|
+
return;
|
|
244
285
|
}
|
|
286
|
+
|
|
287
|
+
this.ensureFallbackIndex().buildIndex(events);
|
|
288
|
+
this.indexMode = 'fallback';
|
|
245
289
|
}
|
|
246
290
|
|
|
247
291
|
/**
|
|
@@ -257,9 +301,13 @@ export class SearchWorkerManager {
|
|
|
257
301
|
|
|
258
302
|
// Use appropriate search method
|
|
259
303
|
let results;
|
|
260
|
-
if (this.worker && this.
|
|
261
|
-
|
|
262
|
-
|
|
304
|
+
if (this.worker && this.indexMode === 'worker') {
|
|
305
|
+
try {
|
|
306
|
+
results = await this.workerSearch(query, options);
|
|
307
|
+
} catch {
|
|
308
|
+
results = this.directSearch(query, options);
|
|
309
|
+
}
|
|
310
|
+
} else if (this.fallbackIndex && this.indexMode === 'fallback') {
|
|
263
311
|
results = this.fallbackIndex.search(query, options);
|
|
264
312
|
} else {
|
|
265
313
|
// Direct search as last resort
|
|
@@ -276,14 +324,20 @@ export class SearchWorkerManager {
|
|
|
276
324
|
* Search using worker
|
|
277
325
|
*/
|
|
278
326
|
workerSearch(query, options) {
|
|
279
|
-
return new Promise(resolve => {
|
|
327
|
+
return new Promise((resolve, reject) => {
|
|
280
328
|
const searchId = Date.now() + Math.random();
|
|
329
|
+
const timeoutId = setTimeout(() => {
|
|
330
|
+
this.pendingSearches = this.pendingSearches.filter(search => search.id !== searchId);
|
|
331
|
+
reject(new Error('Search worker timed out'));
|
|
332
|
+
}, options.timeout || this.config.searchTimeout);
|
|
281
333
|
|
|
282
334
|
this.pendingSearches.push({
|
|
283
335
|
id: searchId,
|
|
284
336
|
query,
|
|
285
337
|
options,
|
|
286
|
-
resolve
|
|
338
|
+
resolve,
|
|
339
|
+
reject,
|
|
340
|
+
timeoutId
|
|
287
341
|
});
|
|
288
342
|
|
|
289
343
|
this.worker.postMessage({
|
|
@@ -309,14 +363,23 @@ export class SearchWorkerManager {
|
|
|
309
363
|
let score = 0;
|
|
310
364
|
|
|
311
365
|
// Check each field
|
|
312
|
-
const fields = options.fields || [
|
|
366
|
+
const fields = options.fields || [
|
|
367
|
+
'title',
|
|
368
|
+
'description',
|
|
369
|
+
'location',
|
|
370
|
+
'category',
|
|
371
|
+
'categories'
|
|
372
|
+
];
|
|
313
373
|
for (const field of fields) {
|
|
314
374
|
const value = event[field];
|
|
315
375
|
if (!value) continue;
|
|
316
376
|
|
|
317
|
-
const
|
|
318
|
-
|
|
319
|
-
|
|
377
|
+
const values = Array.isArray(value) ? value : [value];
|
|
378
|
+
for (const item of values) {
|
|
379
|
+
const valueLower = String(item).toLowerCase();
|
|
380
|
+
if (valueLower.includes(queryLower)) {
|
|
381
|
+
score += field === 'title' ? 20 : 10;
|
|
382
|
+
}
|
|
320
383
|
}
|
|
321
384
|
}
|
|
322
385
|
|
|
@@ -340,6 +403,7 @@ export class SearchWorkerManager {
|
|
|
340
403
|
handleSearchResults(data) {
|
|
341
404
|
const pending = this.pendingSearches.find(s => s.id === data.id);
|
|
342
405
|
if (pending) {
|
|
406
|
+
clearTimeout(pending.timeoutId);
|
|
343
407
|
pending.resolve(data.results);
|
|
344
408
|
this.pendingSearches = this.pendingSearches.filter(s => s.id !== data.id);
|
|
345
409
|
}
|
|
@@ -366,6 +430,10 @@ export class SearchWorkerManager {
|
|
|
366
430
|
* Cache search results with LRU eviction
|
|
367
431
|
*/
|
|
368
432
|
cacheResults(key, results) {
|
|
433
|
+
if (this.searchCache.has(key)) {
|
|
434
|
+
this.cacheOrder = this.cacheOrder.filter(existingKey => existingKey !== key);
|
|
435
|
+
}
|
|
436
|
+
|
|
369
437
|
// Add to cache
|
|
370
438
|
this.searchCache.set(key, results);
|
|
371
439
|
this.cacheOrder.push(key);
|
|
@@ -383,6 +451,9 @@ export class SearchWorkerManager {
|
|
|
383
451
|
clear() {
|
|
384
452
|
this.searchCache.clear();
|
|
385
453
|
this.cacheOrder = [];
|
|
454
|
+
this.indexMode = 'none';
|
|
455
|
+
this.workerExpectedCount = 0;
|
|
456
|
+
this.rejectPendingSearches(new Error('Search index was cleared'));
|
|
386
457
|
|
|
387
458
|
if (this.worker) {
|
|
388
459
|
this.worker.postMessage({ type: 'clear' });
|
|
@@ -402,6 +473,18 @@ export class SearchWorkerManager {
|
|
|
402
473
|
}
|
|
403
474
|
this.clear();
|
|
404
475
|
}
|
|
476
|
+
|
|
477
|
+
/**
|
|
478
|
+
* Reject all pending worker searches.
|
|
479
|
+
* @private
|
|
480
|
+
*/
|
|
481
|
+
rejectPendingSearches(error) {
|
|
482
|
+
for (const search of this.pendingSearches) {
|
|
483
|
+
clearTimeout(search.timeoutId);
|
|
484
|
+
search.reject(error);
|
|
485
|
+
}
|
|
486
|
+
this.pendingSearches = [];
|
|
487
|
+
}
|
|
405
488
|
}
|
|
406
489
|
|
|
407
490
|
/**
|
|
@@ -416,7 +499,8 @@ export class InvertedIndex {
|
|
|
416
499
|
title: 2.0,
|
|
417
500
|
description: 1.0,
|
|
418
501
|
location: 1.5,
|
|
419
|
-
category: 1.5
|
|
502
|
+
category: 1.5,
|
|
503
|
+
categories: 1.5
|
|
420
504
|
};
|
|
421
505
|
}
|
|
422
506
|
|
|
@@ -434,7 +518,8 @@ export class InvertedIndex {
|
|
|
434
518
|
const value = event[field];
|
|
435
519
|
if (!value) continue;
|
|
436
520
|
|
|
437
|
-
const
|
|
521
|
+
const values = Array.isArray(value) ? value : [value];
|
|
522
|
+
const tokens = values.flatMap(item => this.tokenize(String(item)));
|
|
438
523
|
for (const token of tokens) {
|
|
439
524
|
if (!this.index.has(token)) {
|
|
440
525
|
this.index.set(token, new Map());
|