diff --git a/src/core/worker/client.ts b/src/core/worker/client.ts index 656653e..f09c2a7 100644 --- a/src/core/worker/client.ts +++ b/src/core/worker/client.ts @@ -38,6 +38,11 @@ export class DocumentClient { private urls = new Map>() private destroyed = false + /** the document was closed (`destroy`): every call now fails with "Document closed" */ + get closed(): boolean { + return this.destroyed + } + private constructor(opts: OpenOptions) { const factory = opts.worker === false ? undefined : opts.worker ?? defaultWorkerFactory if (factory) { diff --git a/src/react/xlsx/XlsxView.tsx b/src/react/xlsx/XlsxView.tsx index 3bec6c7..6d10490 100644 --- a/src/react/xlsx/XlsxView.tsx +++ b/src/react/xlsx/XlsxView.tsx @@ -1461,10 +1461,16 @@ function SheetGrid({ store, sheet, meta, client, edit, setEdit, hold, onSheet }: /** columns that show #### for the numbers written in `range` are widened, as Excel does (a width set by hand stays) */ const widen = async (range: Range, at = sheet) => { - if (!editable || at !== sheet) return + // it runs after the edit's result: the document may have been closed (another one opened) meanwhile + if (!editable || at !== sheet || client.closed) return const live = store.sheet(sheet) const need = new Map() - for (const [, cells] of await rowsOf(range.r0, Math.min(range.r1, range.r0 + 999))) { + const rows = await rowsOf(range.r0, Math.min(range.r1, range.r0 + 999)).catch((e: Error) => { + if (client.closed) return null + throw e + }) + if (!rows) return + for (const [, cells] of rows) { for (const [c, text, si, kind] of cells) { if (c < range.c0 || c > range.c1 || kind !== 1 || !text || (meta.formats[si] ?? 'General') === 'General' || live.colAxis.size(c) !== live.colAxis.def) continue const css = (styles[si] ?? {}) as Record @@ -1477,7 +1483,10 @@ function SheetGrid({ store, sheet, meta, client, edit, setEdit, hold, onSheet }: need.set(c, Math.max(need.get(c) ?? 0, w)) } } - for (const [c, px] of need) await sheetOp({ op: 'colWidth', c0: c, c1: c, px: Math.min(px, 400) }) + for (const [c, px] of need) { + if (client.closed) return + await sheetOp({ op: 'colWidth', c0: c, c1: c, px: Math.min(px, 400) }) + } } /** a column as wide as its longest text (double click on its edge) */ diff --git a/test/client-closed.test.ts b/test/client-closed.test.ts new file mode 100644 index 0000000..e3314aa --- /dev/null +++ b/test/client-closed.test.ts @@ -0,0 +1,15 @@ +import { describe, expect, test } from 'bun:test' +import { DocumentClient } from '../src/core/worker/client' + +const S = (n: string) => `${import.meta.dir}/../playground/public/samples/${n}` + +describe('a closed document', () => { + test('says it is closed, so work that outlives it (a column fit after an edit) can stop', async () => { + const client = await DocumentClient.open(Bun.file(S('sample.xlsx')) as unknown as Blob, { worker: false }) + expect(client.closed).toBe(false) + await client.call('xlsx.block', 0, 0, 10) + client.destroy() + expect(client.closed).toBe(true) + await expect(client.call('xlsx.block', 0, 0, 10)).rejects.toThrow('Document closed') + }) +})