MCPcopy Create free account
hub / github.com/Feather-2/Burner-X / FormulaPostProcessorAsync

Class FormulaPostProcessorAsync

js/processing/formula_post_processor_async.js:12–468  ·  view source on GitHub ↗

* 异步公式后处理器 * 使用 Web Worker 在后台渲染 KaTeX 公式

Source from the content-addressed store, hash-verified

10 * 使用 Web Worker 在后台渲染 KaTeX 公式
11 */
12 class FormulaPostProcessorAsync {
13 constructor() {
14 this.worker = null;
15 this.workerReady = false;
16 this.pendingCallbacks = new Map();
17 this.requestId = 0;
18 this.initWorker();
19 }
20
21 /**
22 * 初始化 Web Worker(使用 Blob URL 支持 file:// 协议)
23 */
24 initWorker() {
25 try {
26 // 创建内联 Worker 代码(Blob URL 方案,支持 file:// 协议)
27 const workerCode = this.getWorkerCode();
28 const blob = new Blob([workerCode], { type: 'application/javascript' });
29 const workerUrl = URL.createObjectURL(blob);
30
31 this.worker = new Worker(workerUrl);
32
33 // 清理 Blob URL(Worker 已创建,不再需要)
34 URL.revokeObjectURL(workerUrl);
35
36 this.worker.onmessage = (e) => {
37 const { type } = e.data;
38
39 if (type === 'ready') {
40 this.workerReady = true;
41 console.log('[FormulaPostProcessorAsync] Worker ready (Blob URL)');
42 return;
43 }
44
45 if (type === 'batch_complete') {
46 const { batchId, results } = e.data;
47 const callback = this.pendingCallbacks.get(batchId);
48 if (callback) {
49 callback(results);
50 this.pendingCallbacks.delete(batchId);
51 }
52 return;
53 }
54
55 if (type === 'error') {
56 console.error('[FormulaPostProcessorAsync] Worker error:', e.data.error);
57 return;
58 }
59 };
60
61 this.worker.onerror = (error) => {
62 console.error('[FormulaPostProcessorAsync] Worker error:', error);
63 this.workerReady = false;
64 };
65
66 } catch (error) {
67 console.warn('[FormulaPostProcessorAsync] Failed to create Worker, falling back to sync:', error);
68 this.workerReady = false;
69 }

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected