Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/core/worker/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ export class DocumentClient {
private urls = new Map<string, Promise<string>>()
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) {
Expand Down
15 changes: 12 additions & 3 deletions src/react/xlsx/XlsxView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<number, number>()
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<string, string | number>
Expand All @@ -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) */
Expand Down
15 changes: 15 additions & 0 deletions test/client-closed.test.ts
Original file line number Diff line number Diff line change
@@ -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')
})
})