@pierre/diffs 1.2.0-beta.6 → 1.2.1
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/dist/components/CodeView.d.ts +16 -4
- package/dist/components/CodeView.d.ts.map +1 -1
- package/dist/components/CodeView.js +108 -31
- package/dist/components/CodeView.js.map +1 -1
- package/dist/components/File.d.ts +5 -0
- package/dist/components/File.d.ts.map +1 -1
- package/dist/components/File.js +54 -10
- package/dist/components/File.js.map +1 -1
- package/dist/components/FileDiff.d.ts +5 -1
- package/dist/components/FileDiff.d.ts.map +1 -1
- package/dist/components/FileDiff.js +56 -14
- package/dist/components/FileDiff.js.map +1 -1
- package/dist/components/FileStream.js +1 -0
- package/dist/components/FileStream.js.map +1 -1
- package/dist/components/UnresolvedFile.d.ts.map +1 -1
- package/dist/components/UnresolvedFile.js +1 -1
- package/dist/components/VirtualizedFileDiff.js +2 -2
- package/dist/components/VirtualizedFileDiff.js.map +1 -1
- package/dist/components/Virtualizer.js +1 -1
- package/dist/components/Virtualizer.js.map +1 -1
- package/dist/constants.d.ts.map +1 -1
- package/dist/highlighter/shared_highlighter.js +14 -0
- package/dist/highlighter/shared_highlighter.js.map +1 -1
- package/dist/index.d.ts +5 -1
- package/dist/index.js +7 -3
- package/dist/renderers/DiffHunksRenderer.d.ts +2 -0
- package/dist/renderers/DiffHunksRenderer.d.ts.map +1 -1
- package/dist/renderers/DiffHunksRenderer.js +34 -21
- package/dist/renderers/DiffHunksRenderer.js.map +1 -1
- package/dist/renderers/FileRenderer.d.ts +3 -0
- package/dist/renderers/FileRenderer.d.ts.map +1 -1
- package/dist/renderers/FileRenderer.js +37 -23
- package/dist/renderers/FileRenderer.js.map +1 -1
- package/dist/style.js +1 -1
- package/dist/style.js.map +1 -1
- package/dist/types.d.ts +1 -1
- package/dist/types.d.ts.map +1 -1
- package/dist/utils/areDiffTargetsEqual.d.ts +7 -0
- package/dist/utils/areDiffTargetsEqual.d.ts.map +1 -0
- package/dist/utils/areDiffTargetsEqual.js +8 -0
- package/dist/utils/areDiffTargetsEqual.js.map +1 -0
- package/dist/utils/areFileRenderOptionsEqual.d.ts +7 -0
- package/dist/utils/areFileRenderOptionsEqual.d.ts.map +1 -0
- package/dist/utils/areFileRenderOptionsEqual.js +10 -0
- package/dist/utils/areFileRenderOptionsEqual.js.map +1 -0
- package/dist/utils/areWorkerStatsEqual.js +1 -1
- package/dist/utils/areWorkerStatsEqual.js.map +1 -1
- package/dist/utils/detachString.d.ts +2 -1
- package/dist/utils/detachString.d.ts.map +1 -1
- package/dist/utils/detachString.js +12 -10
- package/dist/utils/detachString.js.map +1 -1
- package/dist/utils/isStyleNode.d.ts +5 -0
- package/dist/utils/isStyleNode.d.ts.map +1 -0
- package/dist/utils/isStyleNode.js +10 -0
- package/dist/utils/isStyleNode.js.map +1 -0
- package/dist/utils/parsePatchFiles.d.ts +1 -7
- package/dist/utils/parsePatchFiles.d.ts.map +1 -1
- package/dist/utils/parsePatchFiles.js +18 -4
- package/dist/utils/parsePatchFiles.js.map +1 -1
- package/dist/utils/prefersReducedMotion.d.ts +5 -0
- package/dist/utils/prefersReducedMotion.d.ts.map +1 -0
- package/dist/utils/prefersReducedMotion.js +9 -0
- package/dist/utils/prefersReducedMotion.js.map +1 -0
- package/dist/worker/WorkerPoolManager.d.ts +33 -5
- package/dist/worker/WorkerPoolManager.d.ts.map +1 -1
- package/dist/worker/WorkerPoolManager.js +278 -79
- package/dist/worker/WorkerPoolManager.js.map +1 -1
- package/dist/worker/types.d.ts +7 -3
- package/dist/worker/types.d.ts.map +1 -1
- package/dist/worker/worker-portable.js +9 -9
- package/dist/worker/worker-portable.js.map +1 -1
- package/dist/worker/worker.js +3 -0
- package/dist/worker/worker.js.map +1 -1
- package/package.json +2 -6
|
@@ -4,11 +4,13 @@ import { attachResolvedThemes } from "../highlighter/themes/attachResolvedThemes
|
|
|
4
4
|
import { getSharedHighlighter } from "../highlighter/shared_highlighter.js";
|
|
5
5
|
import { getThemes } from "../utils/getThemes.js";
|
|
6
6
|
import { hasResolvedThemes } from "../highlighter/themes/hasResolvedThemes.js";
|
|
7
|
+
import { areFileRenderOptionsEqual } from "../utils/areFileRenderOptionsEqual.js";
|
|
8
|
+
import { areFilesEqual } from "../utils/areFilesEqual.js";
|
|
7
9
|
import { getCustomExtensionsMap, getCustomExtensionsVersion, getFiletypeFromFileName } from "../utils/getFiletypeFromFileName.js";
|
|
8
10
|
import { isFilePlainText } from "../utils/isFilePlainText.js";
|
|
9
11
|
import { renderFileWithHighlighter } from "../utils/renderFileWithHighlighter.js";
|
|
10
|
-
import { areFilesEqual } from "../utils/areFilesEqual.js";
|
|
11
12
|
import { areDiffRenderOptionsEqual } from "../utils/areDiffRenderOptionsEqual.js";
|
|
13
|
+
import { areDiffTargetsEqual } from "../utils/areDiffTargetsEqual.js";
|
|
12
14
|
import { isDiffPlainText } from "../utils/isDiffPlainText.js";
|
|
13
15
|
import { renderDiffWithHighlighter } from "../utils/renderDiffWithHighlighter.js";
|
|
14
16
|
import { getResolvedLanguages } from "../highlighter/languages/getResolvedLanguages.js";
|
|
@@ -29,14 +31,17 @@ var WorkerPoolManager = class {
|
|
|
29
31
|
highlighter;
|
|
30
32
|
preferredHighlighter;
|
|
31
33
|
renderOptions;
|
|
34
|
+
renderOptionsVersion = 0;
|
|
32
35
|
initialized = false;
|
|
33
36
|
workers = [];
|
|
34
|
-
|
|
35
|
-
|
|
37
|
+
queuedTasks = [];
|
|
38
|
+
queuedTaskByInstance = /* @__PURE__ */ new Map();
|
|
39
|
+
taskByHighlightKey = /* @__PURE__ */ new Map();
|
|
40
|
+
activeTaskById = /* @__PURE__ */ new Map();
|
|
41
|
+
activeRequestByInstance = /* @__PURE__ */ new Map();
|
|
36
42
|
nextRequestId = 0;
|
|
37
43
|
themeSubscribers = /* @__PURE__ */ new Set();
|
|
38
44
|
workersFailed = false;
|
|
39
|
-
instanceRequestMap = /* @__PURE__ */ new Map();
|
|
40
45
|
statSubscribers = /* @__PURE__ */ new Set();
|
|
41
46
|
fileCache;
|
|
42
47
|
diffCache;
|
|
@@ -117,6 +122,7 @@ var WorkerPoolManager = class {
|
|
|
117
122
|
}
|
|
118
123
|
if (!this.isCurrentLifecycle(lifecycleGeneration)) return;
|
|
119
124
|
this.renderOptions = newRenderOptions;
|
|
125
|
+
this.renderOptionsVersion++;
|
|
120
126
|
this.diffCache.clear();
|
|
121
127
|
this.fileCache.clear();
|
|
122
128
|
for (const instance of this.themeSubscribers) instance.rerender();
|
|
@@ -160,7 +166,7 @@ var WorkerPoolManager = class {
|
|
|
160
166
|
reject,
|
|
161
167
|
requestStart: Date.now()
|
|
162
168
|
};
|
|
163
|
-
this.
|
|
169
|
+
this.activeTaskById.set(id, task);
|
|
164
170
|
managedWorker.worker.postMessage(task.request);
|
|
165
171
|
}));
|
|
166
172
|
}
|
|
@@ -197,13 +203,17 @@ var WorkerPoolManager = class {
|
|
|
197
203
|
const stats = this.getStats();
|
|
198
204
|
for (const callback of this.statSubscribers) callback(stats);
|
|
199
205
|
};
|
|
200
|
-
|
|
201
|
-
this.
|
|
202
|
-
const requestId = this.
|
|
206
|
+
cleanUpTasks(instance) {
|
|
207
|
+
this.detachInstanceFromQueuedTasks(instance);
|
|
208
|
+
const requestId = this.activeRequestByInstance.get(instance);
|
|
203
209
|
if (requestId != null) {
|
|
204
|
-
this.
|
|
205
|
-
|
|
210
|
+
const task = this.activeTaskById.get(requestId);
|
|
211
|
+
if (isRenderTask(task)) {
|
|
212
|
+
this.detachInstanceFromRenderTask(task, instance);
|
|
213
|
+
if (!task.primeCache && task.instances.size === 0) this.removeActiveTask(task);
|
|
214
|
+
} else this.activeTaskById.delete(requestId);
|
|
206
215
|
}
|
|
216
|
+
this.activeRequestByInstance.delete(instance);
|
|
207
217
|
this.queueBroadcastStateChanges();
|
|
208
218
|
}
|
|
209
219
|
isInitialized() {
|
|
@@ -305,7 +315,7 @@ var WorkerPoolManager = class {
|
|
|
305
315
|
reject,
|
|
306
316
|
requestStart: Date.now()
|
|
307
317
|
};
|
|
308
|
-
this.
|
|
318
|
+
this.activeTaskById.set(id, task);
|
|
309
319
|
this.executeTask(managedWorker, task);
|
|
310
320
|
}));
|
|
311
321
|
}
|
|
@@ -313,25 +323,45 @@ var WorkerPoolManager = class {
|
|
|
313
323
|
}
|
|
314
324
|
drainQueue = () => {
|
|
315
325
|
this._queuedDrain = void 0;
|
|
316
|
-
if (this.initialized !== true || this.
|
|
317
|
-
for (
|
|
318
|
-
|
|
326
|
+
if (this.initialized !== true || this.queuedTasks.length === 0) return;
|
|
327
|
+
for (let i = 0; i < this.queuedTasks.length;) {
|
|
328
|
+
const task = this.queuedTasks[i];
|
|
329
|
+
if (this.hasActiveRequest(task)) {
|
|
330
|
+
i++;
|
|
331
|
+
continue;
|
|
332
|
+
}
|
|
319
333
|
const langs = getLangsFromTask(task);
|
|
320
334
|
const availableWorker = this.getAvailableWorker(langs);
|
|
321
335
|
if (availableWorker == null) break;
|
|
336
|
+
this.queuedTasks.splice(i, 1);
|
|
322
337
|
this.assignWorkerToTask(task, availableWorker);
|
|
323
338
|
this.resolveLanguagesAndExecuteTask(availableWorker, task, langs);
|
|
324
339
|
}
|
|
325
340
|
this.queueBroadcastStateChanges();
|
|
326
341
|
};
|
|
327
342
|
highlightFileAST(instance, file) {
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
this.submitTask(instance, {
|
|
343
|
+
const cachedResult = this.getFileResultCache(file);
|
|
344
|
+
if (isFilePlainText(file) || cachedResult != null && areFileRenderOptionsEqual(cachedResult.options, this.getFileRenderOptions())) return;
|
|
345
|
+
if (!this.hasMatchingFileInstanceTask(instance, file)) this.submitTask(instance, {
|
|
331
346
|
type: "file",
|
|
332
347
|
file
|
|
333
348
|
});
|
|
334
349
|
}
|
|
350
|
+
primeFileHighlightCache(file) {
|
|
351
|
+
if (file.cacheKey == null) {
|
|
352
|
+
console.warn(`WorkerPoolManager.primeFileHighlightCache: priming highlight cache requires file.cacheKey; skipping "${file.name}".`);
|
|
353
|
+
return;
|
|
354
|
+
}
|
|
355
|
+
const cachedResult = this.getFileResultCache(file);
|
|
356
|
+
const highlightKey = this.getFileHighlightKey(file);
|
|
357
|
+
if (highlightKey == null || isFilePlainText(file) || cachedResult != null && areFileRenderOptionsEqual(cachedResult.options, this.getFileRenderOptions())) return;
|
|
358
|
+
const existingTask = this.getTaskByHighlightKey(highlightKey);
|
|
359
|
+
if (existingTask != null) existingTask.primeCache = true;
|
|
360
|
+
else this.submitCacheTask({
|
|
361
|
+
type: "file",
|
|
362
|
+
file
|
|
363
|
+
}, highlightKey);
|
|
364
|
+
}
|
|
335
365
|
getPlainFileAST(file, startingLine, totalLines, lines) {
|
|
336
366
|
if (this.highlighter == null) {
|
|
337
367
|
this.queueInitialization();
|
|
@@ -345,13 +375,28 @@ var WorkerPoolManager = class {
|
|
|
345
375
|
});
|
|
346
376
|
}
|
|
347
377
|
highlightDiffAST(instance, diff) {
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
this.submitTask(instance, {
|
|
378
|
+
const cachedResult = this.getDiffResultCache(diff);
|
|
379
|
+
if (isDiffPlainText(diff) || cachedResult != null && areDiffRenderOptionsEqual(cachedResult.options, this.getDiffRenderOptions())) return;
|
|
380
|
+
if (!this.hasMatchingDiffInstanceTask(instance, diff)) this.submitTask(instance, {
|
|
351
381
|
type: "diff",
|
|
352
382
|
diff
|
|
353
383
|
});
|
|
354
384
|
}
|
|
385
|
+
primeDiffHighlightCache(diff) {
|
|
386
|
+
if (diff.cacheKey == null) {
|
|
387
|
+
console.warn(`WorkerPoolManager.primeDiffHighlightCache: priming highlight cache requires diff.cacheKey; skipping "${diff.prevName ?? diff.name}" -> "${diff.name}".`);
|
|
388
|
+
return;
|
|
389
|
+
}
|
|
390
|
+
const cachedResult = this.getDiffResultCache(diff);
|
|
391
|
+
const highlightKey = this.getDiffHighlightKey(diff);
|
|
392
|
+
if (highlightKey == null || isDiffPlainText(diff) || cachedResult != null && areDiffRenderOptionsEqual(cachedResult.options, this.getDiffRenderOptions())) return;
|
|
393
|
+
const existingTask = this.getTaskByHighlightKey(highlightKey);
|
|
394
|
+
if (existingTask != null) existingTask.primeCache = true;
|
|
395
|
+
else this.submitCacheTask({
|
|
396
|
+
type: "diff",
|
|
397
|
+
diff
|
|
398
|
+
}, highlightKey);
|
|
399
|
+
}
|
|
355
400
|
getPlainDiffAST(diff, startingLine, totalLines, expandedHunks, collapsedContextThreshold) {
|
|
356
401
|
return this.highlighter != null ? renderDiffWithHighlighter(diff, this.highlighter, this.renderOptions, {
|
|
357
402
|
forcePlainText: true,
|
|
@@ -363,13 +408,15 @@ var WorkerPoolManager = class {
|
|
|
363
408
|
}
|
|
364
409
|
terminate() {
|
|
365
410
|
this.lifecycleGeneration++;
|
|
366
|
-
this.
|
|
411
|
+
this.cancelActiveWorkerTasks();
|
|
367
412
|
this.terminateWorkers();
|
|
368
413
|
this.fileCache.clear();
|
|
369
414
|
this.diffCache.clear();
|
|
370
|
-
this.
|
|
371
|
-
this.
|
|
372
|
-
this.
|
|
415
|
+
this.activeRequestByInstance.clear();
|
|
416
|
+
this.queuedTasks.length = 0;
|
|
417
|
+
this.queuedTaskByInstance.clear();
|
|
418
|
+
this.taskByHighlightKey.clear();
|
|
419
|
+
this.activeTaskById.clear();
|
|
373
420
|
this.highlighter = void 0;
|
|
374
421
|
this.initialized = false;
|
|
375
422
|
this.workersFailed = false;
|
|
@@ -383,9 +430,9 @@ var WorkerPoolManager = class {
|
|
|
383
430
|
console.error(error);
|
|
384
431
|
});
|
|
385
432
|
}
|
|
386
|
-
|
|
433
|
+
cancelActiveWorkerTasks() {
|
|
387
434
|
const error = new WorkerPoolTerminatedError();
|
|
388
|
-
for (const task of this.
|
|
435
|
+
for (const task of this.activeTaskById.values()) if ("reject" in task) task.reject(error);
|
|
389
436
|
}
|
|
390
437
|
terminateWorkers() {
|
|
391
438
|
for (const managedWorker of this.workers) managedWorker.worker.terminate();
|
|
@@ -401,14 +448,57 @@ var WorkerPoolManager = class {
|
|
|
401
448
|
totalWorkers: this.workers.length,
|
|
402
449
|
workersFailed: this.workersFailed,
|
|
403
450
|
busyWorkers: this.workers.filter((w) => w.request_id != null).length,
|
|
404
|
-
queuedTasks: this.
|
|
405
|
-
|
|
451
|
+
queuedTasks: this.queuedTasks.length,
|
|
452
|
+
activeTasks: this.activeTaskById.size,
|
|
406
453
|
themeSubscribers: this.themeSubscribers.size,
|
|
407
454
|
fileCacheSize: this.fileCache.size,
|
|
408
455
|
diffCacheSize: this.diffCache.size
|
|
409
456
|
};
|
|
410
457
|
}
|
|
411
458
|
submitTask(instance, request) {
|
|
459
|
+
if (this.initialized === false) this.queueInitialization();
|
|
460
|
+
const highlightKey = this.getHighlightKeyForRequest(request);
|
|
461
|
+
const existingTask = highlightKey != null ? this.getTaskByHighlightKey(highlightKey) : void 0;
|
|
462
|
+
if (existingTask != null) {
|
|
463
|
+
this.detachInstanceFromQueuedTasks(instance, existingTask);
|
|
464
|
+
this.addInstanceToTask(existingTask, instance);
|
|
465
|
+
this.queueBroadcastStateChanges();
|
|
466
|
+
return;
|
|
467
|
+
}
|
|
468
|
+
this.detachInstanceFromQueuedTasks(instance);
|
|
469
|
+
const id = this.generateRequestId();
|
|
470
|
+
const requestStart = Date.now();
|
|
471
|
+
const task = (() => {
|
|
472
|
+
switch (request.type) {
|
|
473
|
+
case "file": return {
|
|
474
|
+
type: "file",
|
|
475
|
+
id,
|
|
476
|
+
request: {
|
|
477
|
+
...request,
|
|
478
|
+
id
|
|
479
|
+
},
|
|
480
|
+
instances: new Set([instance]),
|
|
481
|
+
primeCache: false,
|
|
482
|
+
highlightKey,
|
|
483
|
+
requestStart
|
|
484
|
+
};
|
|
485
|
+
case "diff": return {
|
|
486
|
+
type: "diff",
|
|
487
|
+
id,
|
|
488
|
+
request: {
|
|
489
|
+
...request,
|
|
490
|
+
id
|
|
491
|
+
},
|
|
492
|
+
instances: new Set([instance]),
|
|
493
|
+
primeCache: false,
|
|
494
|
+
highlightKey,
|
|
495
|
+
requestStart
|
|
496
|
+
};
|
|
497
|
+
}
|
|
498
|
+
})();
|
|
499
|
+
this.enqueueRenderTask(task, instance);
|
|
500
|
+
}
|
|
501
|
+
submitCacheTask(request, highlightKey) {
|
|
412
502
|
if (this.initialized === false) this.queueInitialization();
|
|
413
503
|
const id = this.generateRequestId();
|
|
414
504
|
const requestStart = Date.now();
|
|
@@ -421,7 +511,9 @@ var WorkerPoolManager = class {
|
|
|
421
511
|
...request,
|
|
422
512
|
id
|
|
423
513
|
},
|
|
424
|
-
|
|
514
|
+
instances: /* @__PURE__ */ new Set(),
|
|
515
|
+
primeCache: true,
|
|
516
|
+
highlightKey,
|
|
425
517
|
requestStart
|
|
426
518
|
};
|
|
427
519
|
case "diff": return {
|
|
@@ -431,12 +523,19 @@ var WorkerPoolManager = class {
|
|
|
431
523
|
...request,
|
|
432
524
|
id
|
|
433
525
|
},
|
|
434
|
-
|
|
526
|
+
instances: /* @__PURE__ */ new Set(),
|
|
527
|
+
primeCache: true,
|
|
528
|
+
highlightKey,
|
|
435
529
|
requestStart
|
|
436
530
|
};
|
|
437
531
|
}
|
|
438
532
|
})();
|
|
439
|
-
this.
|
|
533
|
+
this.enqueueRenderTask(task);
|
|
534
|
+
}
|
|
535
|
+
enqueueRenderTask(task, instance) {
|
|
536
|
+
this.queuedTasks.push(task);
|
|
537
|
+
if (instance != null) this.queuedTaskByInstance.set(instance, task);
|
|
538
|
+
if (task.highlightKey != null) this.taskByHighlightKey.set(task.highlightKey, task);
|
|
440
539
|
this.queueDrain();
|
|
441
540
|
}
|
|
442
541
|
async resolveLanguagesAndExecuteTask(availableWorker, task, langs) {
|
|
@@ -444,57 +543,65 @@ var WorkerPoolManager = class {
|
|
|
444
543
|
const workerMissingLangs = langs.filter((lang) => !availableWorker.langs.has(lang));
|
|
445
544
|
if (workerMissingLangs.length > 0) if (hasResolvedLanguages(workerMissingLangs)) task.request.resolvedLanguages = getResolvedLanguages(workerMissingLangs);
|
|
446
545
|
else task.request.resolvedLanguages = await resolveLanguages(workerMissingLangs);
|
|
546
|
+
if (!this.activeTaskById.has(task.id)) {
|
|
547
|
+
if (availableWorker.request_id === task.id) {
|
|
548
|
+
this.cleanWorkerAndTask(availableWorker, task);
|
|
549
|
+
this.queueBroadcastStateChanges();
|
|
550
|
+
if (this.queuedTasks.length > 0) this.queueDrain();
|
|
551
|
+
}
|
|
552
|
+
return;
|
|
553
|
+
}
|
|
447
554
|
this.executeTask(availableWorker, task);
|
|
448
555
|
} catch {
|
|
449
556
|
this.cleanWorkerAndTask(availableWorker, task);
|
|
557
|
+
this.queueBroadcastStateChanges();
|
|
558
|
+
if (this.queuedTasks.length > 0) this.queueDrain();
|
|
450
559
|
}
|
|
451
560
|
}
|
|
452
561
|
handleWorkerMessage(managedWorker, response) {
|
|
453
|
-
const task = this.
|
|
562
|
+
const task = this.activeTaskById.get(response.id);
|
|
454
563
|
try {
|
|
455
564
|
if (task == null) throw IGNORE_RESPONSE;
|
|
456
565
|
else if (response.type === "error") {
|
|
457
566
|
const error = new Error(response.error);
|
|
458
567
|
if (response.stack) error.stack = response.stack;
|
|
459
568
|
if ("reject" in task) task.reject(error);
|
|
460
|
-
else task.
|
|
569
|
+
else if (isRenderTask(task)) this.notifyHighlightError(task, error);
|
|
570
|
+
else throw new Error("handleWorkerMessage: unknown task type");
|
|
461
571
|
throw error;
|
|
462
|
-
} else {
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
instance.onHighlightSuccess(request.diff, result, options);
|
|
496
|
-
break;
|
|
497
|
-
}
|
|
572
|
+
} else switch (response.requestType) {
|
|
573
|
+
case "initialize":
|
|
574
|
+
if (task.type !== "initialize") throw new Error("handleWorkerMessage: task/response dont match");
|
|
575
|
+
this.syncCustomExtensionVersion(managedWorker, task.request);
|
|
576
|
+
task.resolve();
|
|
577
|
+
break;
|
|
578
|
+
case "set-render-options":
|
|
579
|
+
if (task.type !== "set-render-options") throw new Error("handleWorkerMessage: task/response dont match");
|
|
580
|
+
task.resolve();
|
|
581
|
+
break;
|
|
582
|
+
case "file": {
|
|
583
|
+
if (task.type !== "file") throw new Error("handleWorkerMessage: task/response dont match");
|
|
584
|
+
const { result, options } = response;
|
|
585
|
+
const { request } = task;
|
|
586
|
+
this.syncCustomExtensionVersion(managedWorker, request);
|
|
587
|
+
if (request.file.cacheKey != null) this.fileCache.set(request.file.cacheKey, {
|
|
588
|
+
result,
|
|
589
|
+
options
|
|
590
|
+
});
|
|
591
|
+
this.notifyFileInstances(task, result, options);
|
|
592
|
+
break;
|
|
593
|
+
}
|
|
594
|
+
case "diff": {
|
|
595
|
+
if (task.type !== "diff") throw new Error("handleWorkerMessage: task/response dont match");
|
|
596
|
+
const { result, options } = response;
|
|
597
|
+
const { request } = task;
|
|
598
|
+
this.syncCustomExtensionVersion(managedWorker, request);
|
|
599
|
+
if (request.diff.cacheKey != null) this.diffCache.set(request.diff.cacheKey, {
|
|
600
|
+
result,
|
|
601
|
+
options
|
|
602
|
+
});
|
|
603
|
+
this.notifyDiffInstances(task, result, options);
|
|
604
|
+
break;
|
|
498
605
|
}
|
|
499
606
|
}
|
|
500
607
|
} catch (error) {
|
|
@@ -502,7 +609,7 @@ var WorkerPoolManager = class {
|
|
|
502
609
|
}
|
|
503
610
|
this.cleanWorkerAndTask(managedWorker, task);
|
|
504
611
|
this.queueBroadcastStateChanges();
|
|
505
|
-
if (this.
|
|
612
|
+
if (this.queuedTasks.length > 0) this.queueDrain();
|
|
506
613
|
}
|
|
507
614
|
_queuedDrain;
|
|
508
615
|
queueDrain() {
|
|
@@ -512,30 +619,34 @@ var WorkerPoolManager = class {
|
|
|
512
619
|
}
|
|
513
620
|
assignWorkerToTask(task, managedWorker) {
|
|
514
621
|
managedWorker.request_id = task.id;
|
|
515
|
-
if (
|
|
516
|
-
this.
|
|
517
|
-
this.
|
|
622
|
+
if (isRenderTask(task)) {
|
|
623
|
+
this.clearQueuedInstanceRequests(task);
|
|
624
|
+
this.trackInstanceRequests(task);
|
|
518
625
|
}
|
|
519
|
-
this.
|
|
626
|
+
this.activeTaskById.set(task.id, task);
|
|
520
627
|
}
|
|
521
628
|
cleanWorkerAndTask(managedWorker, task) {
|
|
522
629
|
managedWorker.request_id = void 0;
|
|
523
630
|
if (task != null) {
|
|
524
|
-
if (
|
|
525
|
-
|
|
631
|
+
if (isRenderTask(task)) {
|
|
632
|
+
this.clearInstanceRequests(task);
|
|
633
|
+
this.clearHighlightKey(task);
|
|
634
|
+
}
|
|
635
|
+
this.activeTaskById.delete(task.id);
|
|
526
636
|
}
|
|
527
637
|
}
|
|
528
638
|
executeTask(managedWorker, task) {
|
|
529
639
|
if (shouldSyncCustomExtensions(task.request)) this.maybeAttachCustomExtensions(managedWorker, task.request);
|
|
530
|
-
this.assignWorkerToTask(task, managedWorker);
|
|
640
|
+
if (!this.activeTaskById.has(task.id)) this.assignWorkerToTask(task, managedWorker);
|
|
531
641
|
for (const lang of getLangsFromTask(task)) managedWorker.langs.add(lang);
|
|
532
642
|
try {
|
|
533
643
|
managedWorker.worker.postMessage(task.request);
|
|
534
644
|
} catch (error) {
|
|
535
|
-
this.cleanWorkerAndTask(managedWorker, task);
|
|
536
645
|
console.error("Failed to post message to worker:", error);
|
|
537
|
-
if (
|
|
646
|
+
if (isRenderTask(task)) this.notifyHighlightError(task, error);
|
|
538
647
|
else if ("reject" in task) task.reject(error);
|
|
648
|
+
this.cleanWorkerAndTask(managedWorker, task);
|
|
649
|
+
if (this.queuedTasks.length > 0) this.queueDrain();
|
|
539
650
|
}
|
|
540
651
|
this.queueBroadcastStateChanges();
|
|
541
652
|
}
|
|
@@ -565,6 +676,88 @@ var WorkerPoolManager = class {
|
|
|
565
676
|
}
|
|
566
677
|
return worker;
|
|
567
678
|
}
|
|
679
|
+
getFileHighlightKey(file) {
|
|
680
|
+
if (file.cacheKey == null) return;
|
|
681
|
+
return `file:${file.cacheKey}:${this.renderOptionsVersion}`;
|
|
682
|
+
}
|
|
683
|
+
getDiffHighlightKey(diff) {
|
|
684
|
+
if (diff.cacheKey == null) return;
|
|
685
|
+
return `diff:${diff.cacheKey}:${this.renderOptionsVersion}`;
|
|
686
|
+
}
|
|
687
|
+
getHighlightKeyForRequest(request) {
|
|
688
|
+
switch (request.type) {
|
|
689
|
+
case "file": return this.getFileHighlightKey(request.file);
|
|
690
|
+
case "diff": return this.getDiffHighlightKey(request.diff);
|
|
691
|
+
}
|
|
692
|
+
}
|
|
693
|
+
hasActiveRequest(task) {
|
|
694
|
+
for (const instance of getInstances(task)) if (this.activeRequestByInstance.has(instance)) return true;
|
|
695
|
+
return false;
|
|
696
|
+
}
|
|
697
|
+
addInstanceToTask(task, instance) {
|
|
698
|
+
if (task.type === "file") task.instances.add(instance);
|
|
699
|
+
else task.instances.add(instance);
|
|
700
|
+
if (this.activeTaskById.has(task.id)) this.activeRequestByInstance.set(instance, task.id);
|
|
701
|
+
else this.queuedTaskByInstance.set(instance, task);
|
|
702
|
+
}
|
|
703
|
+
detachInstanceFromQueuedTasks(instance, exceptTask) {
|
|
704
|
+
const task = this.queuedTaskByInstance.get(instance);
|
|
705
|
+
if (task == null || task === exceptTask) return;
|
|
706
|
+
this.queuedTaskByInstance.delete(instance);
|
|
707
|
+
this.detachInstanceFromRenderTask(task, instance);
|
|
708
|
+
if (!task.primeCache && task.instances.size === 0) this.removeQueuedTask(task);
|
|
709
|
+
}
|
|
710
|
+
detachInstanceFromRenderTask(task, instance) {
|
|
711
|
+
if (task.type === "file") task.instances.delete(instance);
|
|
712
|
+
else task.instances.delete(instance);
|
|
713
|
+
}
|
|
714
|
+
removeQueuedTask(task) {
|
|
715
|
+
const index = this.queuedTasks.indexOf(task);
|
|
716
|
+
if (index !== -1) this.queuedTasks.splice(index, 1);
|
|
717
|
+
this.clearQueuedInstanceRequests(task);
|
|
718
|
+
this.clearHighlightKey(task);
|
|
719
|
+
}
|
|
720
|
+
removeActiveTask(task) {
|
|
721
|
+
this.clearInstanceRequests(task);
|
|
722
|
+
this.clearHighlightKey(task);
|
|
723
|
+
this.activeTaskById.delete(task.id);
|
|
724
|
+
}
|
|
725
|
+
clearQueuedInstanceRequests(task) {
|
|
726
|
+
for (const instance of getInstances(task)) if (this.queuedTaskByInstance.get(instance) === task) this.queuedTaskByInstance.delete(instance);
|
|
727
|
+
}
|
|
728
|
+
clearHighlightKey(task) {
|
|
729
|
+
if (task.highlightKey != null && this.taskByHighlightKey.get(task.highlightKey) === task) this.taskByHighlightKey.delete(task.highlightKey);
|
|
730
|
+
}
|
|
731
|
+
trackInstanceRequests(task) {
|
|
732
|
+
for (const instance of getInstances(task)) this.activeRequestByInstance.set(instance, task.id);
|
|
733
|
+
}
|
|
734
|
+
clearInstanceRequests(task) {
|
|
735
|
+
for (const instance of getInstances(task)) if (this.activeRequestByInstance.get(instance) === task.id) this.activeRequestByInstance.delete(instance);
|
|
736
|
+
}
|
|
737
|
+
notifyFileInstances(task, result, options) {
|
|
738
|
+
for (const instance of task.instances) if (this.activeRequestByInstance.get(instance) === task.id) instance.onHighlightSuccess(task.request.file, result, options);
|
|
739
|
+
}
|
|
740
|
+
notifyDiffInstances(task, result, options) {
|
|
741
|
+
for (const instance of task.instances) if (this.activeRequestByInstance.get(instance) === task.id) instance.onHighlightSuccess(task.request.diff, result, options);
|
|
742
|
+
}
|
|
743
|
+
notifyHighlightError(task, error) {
|
|
744
|
+
for (const instance of getInstances(task)) if (this.activeRequestByInstance.get(instance) === task.id) instance.onHighlightError(error);
|
|
745
|
+
}
|
|
746
|
+
hasMatchingFileInstanceTask(instance, file) {
|
|
747
|
+
for (const task of this.iterateRenderTasks()) if (task.type === "file" && task.instances.has(instance) && areFilesEqual(file, task.request.file)) return true;
|
|
748
|
+
return false;
|
|
749
|
+
}
|
|
750
|
+
hasMatchingDiffInstanceTask(instance, diff) {
|
|
751
|
+
for (const task of this.iterateRenderTasks()) if (task.type === "diff" && task.instances.has(instance) && areDiffTargetsEqual(task.request.diff, diff)) return true;
|
|
752
|
+
return false;
|
|
753
|
+
}
|
|
754
|
+
getTaskByHighlightKey(highlightKey) {
|
|
755
|
+
return this.taskByHighlightKey.get(highlightKey);
|
|
756
|
+
}
|
|
757
|
+
*iterateRenderTasks() {
|
|
758
|
+
for (const task of this.queuedTasks) yield task;
|
|
759
|
+
for (const task of this.activeTaskById.values()) if (isRenderTask(task)) yield task;
|
|
760
|
+
}
|
|
568
761
|
generateRequestId() {
|
|
569
762
|
return `req_${++this.nextRequestId}`;
|
|
570
763
|
}
|
|
@@ -587,6 +780,12 @@ function getLangsFromTask(task) {
|
|
|
587
780
|
langs.delete("text");
|
|
588
781
|
return Array.from(langs);
|
|
589
782
|
}
|
|
783
|
+
function isRenderTask(task) {
|
|
784
|
+
return task?.type === "file" || task?.type === "diff";
|
|
785
|
+
}
|
|
786
|
+
function getInstances(task) {
|
|
787
|
+
return task.instances;
|
|
788
|
+
}
|
|
590
789
|
|
|
591
790
|
//#endregion
|
|
592
791
|
export { WorkerPoolManager };
|