-
Notifications
You must be signed in to change notification settings - Fork 134
fix(spell-check): recreate worker after dispose to survive SuperDoc reinit #2933
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -1,4 +1,5 @@ | ||||||
| import type { TypoWorkerCancelMessage, TypoWorkerIssue, TypoWorkerRequest, TypoWorkerResponse } from './typoWorkerMessages'; | ||||||
| import typoWorkerUrl from './typoWorker?worker&url'; | ||||||
|
|
||||||
| type PendingRequest = { | ||||||
| resolve: (value: { issues: TypoWorkerIssue[] }) => void; | ||||||
|
|
@@ -18,7 +19,18 @@ function createAbortError(): DOMException | Error { | |||||
|
|
||||||
| export async function createTypoJsProvider() { | ||||||
| // Run Typo.js work inside a dedicated worker to avoid UI stalls. | ||||||
| const worker = new Worker(new URL('./typoWorker.ts', import.meta.url), { type: 'module' }); | ||||||
| // Worker is lazily created/recreated to survive dispose() calls. | ||||||
| let worker: Worker | null = null; | ||||||
|
|
||||||
| function ensureWorker(): Worker { | ||||||
| if (!worker) { | ||||||
| worker = new Worker(typoWorkerUrl, { type: 'module' }); | ||||||
| worker.addEventListener('message', handleMessage); | ||||||
| worker.addEventListener('error', handleError); | ||||||
| } | ||||||
| return worker; | ||||||
| } | ||||||
|
|
||||||
| const pending = new Map<number, PendingRequest>(); | ||||||
| let nextRequestId = 0; | ||||||
|
|
||||||
|
|
@@ -48,8 +60,7 @@ export async function createTypoJsProvider() { | |||||
| pending.clear(); | ||||||
| }; | ||||||
|
|
||||||
| worker.addEventListener('message', handleMessage); | ||||||
| worker.addEventListener('error', handleError); | ||||||
| ensureWorker(); | ||||||
|
|
||||||
| return { | ||||||
| id: 'typo-js', | ||||||
|
|
@@ -83,7 +94,7 @@ export async function createTypoJsProvider() { | |||||
| pending.delete(requestId); | ||||||
| cleanup(); | ||||||
| const cancel: TypoWorkerCancelMessage = { type: 'cancel', id: requestId }; | ||||||
| worker.postMessage(cancel); | ||||||
| ensureWorker().postMessage(cancel); | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| reject(createAbortError()); | ||||||
| }; | ||||||
|
|
||||||
|
|
@@ -104,14 +115,17 @@ export async function createTypoJsProvider() { | |||||
| }, | ||||||
| }; | ||||||
|
|
||||||
| worker.postMessage(payload); | ||||||
| ensureWorker().postMessage(payload); | ||||||
| }); | ||||||
| }, | ||||||
|
|
||||||
| dispose() { | ||||||
| worker.removeEventListener('message', handleMessage); | ||||||
| worker.removeEventListener('error', handleError); | ||||||
| worker.terminate(); | ||||||
| if (worker) { | ||||||
| worker.removeEventListener('message', handleMessage); | ||||||
| worker.removeEventListener('error', handleError); | ||||||
| worker.terminate(); | ||||||
| worker = null; | ||||||
| } | ||||||
|
|
||||||
| for (const [, entry] of pending) { | ||||||
| entry.cleanup(); | ||||||
|
|
||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
worth saying why in the comment - the next reader has to open App.tsx to see the lifecycle.