@lincyaw/workbench 0.2.0-rc.124 → 0.2.0-rc.126

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lincyaw/workbench",
3
- "version": "0.2.0-rc.124",
3
+ "version": "0.2.0-rc.126",
4
4
  "description": "Presmium's workbench panels in React (the documents, a document edited on its rendered page with comments and version history) with the workspace service's client and the preview protocol's workbench side; @lincyaw/dsh places them in presmium studio (experimental)",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {
@@ -55,8 +55,8 @@
55
55
  "@tiptap/pm": "3.32.0",
56
56
  "katex": "0.18.10",
57
57
  "react-resizable-panels": "4.14.5",
58
- "@lincyaw/design": "1.0.0-rc.123",
59
- "@lincyaw/kernel": "1.0.0-rc.123"
58
+ "@lincyaw/design": "1.0.0-rc.124",
59
+ "@lincyaw/kernel": "1.0.0-rc.124"
60
60
  },
61
61
  "peerDependencies": {
62
62
  "react": "^18.3.1",
@@ -67,6 +67,6 @@
67
67
  "@types/react-dom": "18.3.7",
68
68
  "react": "^18.3.1",
69
69
  "react-dom": "^18.3.1",
70
- "@lincyaw/compiler": "1.0.0-rc.123"
70
+ "@lincyaw/compiler": "1.0.0-rc.124"
71
71
  }
72
72
  }
@@ -2,7 +2,7 @@ import { isRecord, randomUUID, validateDocument } from '@lincyaw/kernel'
2
2
  import type { PresmiumDocument } from '@lincyaw/kernel'
3
3
  import { DocumentDraftStore } from './document-draft-store.ts'
4
4
  import { sha256 as tokenOf } from './sha256.ts'
5
- import type { DraftStoreCommit, DraftStoreOperation } from './document-draft-store.ts'
5
+ import type { DraftStoreCommit, DraftStoreOperation, DraftStoreResult } from './document-draft-store.ts'
6
6
 
7
7
  export interface DocumentDraftReference { id: string, revision: number, token: string }
8
8
  export interface DocumentDraft {
@@ -47,10 +47,13 @@ function documentDraft(value: unknown, documentId: string): DocumentDraft | unde
47
47
  pendingHash: value.pendingHash, composing: value.composing, origins: value.origins }
48
48
  }
49
49
 
50
+ interface ParsedDraft { raw: string, documentId: string, value: StoredDocumentDraft, token?: string }
51
+
50
52
  /** Strictly committed document backups, with old localStorage copies identified as unconfirmed rescue material. */
51
53
  export class DocumentDrafts {
52
54
  readonly #owner = randomUUID()
53
55
  readonly #owned = new Map<string, { identity: string, revision: number, raw: string }>()
56
+ readonly #parsed = new Map<string, ParsedDraft[]>()
54
57
  #store?: DocumentDraftStore
55
58
  #identity?: string
56
59
  #closed = false
@@ -93,25 +96,37 @@ export class DocumentDrafts {
93
96
  signal?.throwIfAborted()
94
97
  try {
95
98
  if (typeof raw !== 'string' || raw.length > MAX_CHARS) throw new Error('document-draft-size')
96
- const value: unknown = JSON.parse(raw)
97
- if (!isRecord(value) || value.format !== 2 || value.identity !== identity || !idRevision(value) || key !== keyOf(identity, value.id)
98
- || typeof value.owner !== 'string' || !value.owner || value.owner.length > 200 || typeof value.updatedAt !== 'number' || !Number.isSafeInteger(value.updatedAt) || value.updatedAt < 0 || Number.isNaN(new Date(value.updatedAt).getTime())) throw new Error('document-draft-data')
99
- const draft = documentDraft(value, documentId)
100
- if (!draft) throw new Error('document-draft-document')
101
- if (draft.baseHash.startsWith('json:')) draft.baseHash = await tokenOf(draft.baseHash.slice(5))
102
- if (draft.pendingHash?.startsWith('json:')) draft.pendingHash = await tokenOf(draft.pendingHash.slice(5))
103
- const token = await tokenOf(raw), marker = settledKey(identity, { id: value.id, revision: value.revision, token })
99
+ const { value, token } = await this.#parse(identity, documentId, key, raw)
100
+ const marker = settledKey(identity, { id: value.id, revision: value.revision, token })
104
101
  if (durable.get(marker) === token) continue
105
102
  if (source === 'rescue' && localStorage.getItem(marker) === token) continue
106
103
  const id = JSON.stringify([value.id, value.revision, token])
107
- if (!records.has(id)) records.set(id, { ...draft, format: 2, identity, id: value.id, owner: value.owner, revision: value.revision, updatedAt: value.updatedAt, token, source,
104
+ if (!records.has(id)) records.set(id, { ...value, token, source,
108
105
  owned: source === 'durable' && value.owner === this.#owner && (this.#owned.get(value.id)?.raw === raw || store.owns(key, raw)) })
109
106
  } catch { signal?.throwIfAborted(); failed = true }
110
107
  }
108
+ for (const key of this.#parsed.keys()) if (key.startsWith(prefix(identity)) && !values.some(value => value.key === key)) this.#parsed.delete(key)
111
109
  signal?.throwIfAborted()
112
110
  return { records: [...records.values()], failed }
113
111
  }
114
112
 
113
+ /** The same stored bytes are parsed and validated once: a long document's backup is read again after each of its writes. Contents are shared, never mutated. */
114
+ async #parse(identity: string, documentId: string, key: string, raw: string): Promise<{ value: StoredDocumentDraft, token: string }> {
115
+ const known = this.#parsed.get(key)?.find(entry => entry.raw === raw && entry.documentId === documentId)
116
+ if (known) return { value: known.value, token: known.token ??= await tokenOf(raw) }
117
+ const value: unknown = JSON.parse(raw)
118
+ if (!isRecord(value) || value.format !== 2 || value.identity !== identity || !idRevision(value) || key !== keyOf(identity, value.id)
119
+ || typeof value.owner !== 'string' || !value.owner || value.owner.length > 200 || typeof value.updatedAt !== 'number' || !Number.isSafeInteger(value.updatedAt) || value.updatedAt < 0 || Number.isNaN(new Date(value.updatedAt).getTime())) throw new Error('document-draft-data')
120
+ const draft = documentDraft(value, documentId)
121
+ if (!draft) throw new Error('document-draft-document')
122
+ if (draft.baseHash.startsWith('json:')) draft.baseHash = await tokenOf(draft.baseHash.slice(5))
123
+ if (draft.pendingHash?.startsWith('json:')) draft.pendingHash = await tokenOf(draft.pendingHash.slice(5))
124
+ const parsed: StoredDocumentDraft = { ...draft, format: 2, identity, id: value.id, owner: value.owner, revision: value.revision, updatedAt: value.updatedAt }
125
+ const token = await tokenOf(raw)
126
+ this.#remember(key, { raw, documentId, value: parsed, token })
127
+ return { value: parsed, token }
128
+ }
129
+
115
130
  #record(identity: string, draft: DocumentDraft, previous?: StoredDocumentDraft): { record: StoredDocumentDraft, raw: string } | undefined {
116
131
  const store = this.#storage(identity)
117
132
  if (!store || !draftShape(draft)) return
@@ -124,14 +139,30 @@ export class DocumentDrafts {
124
139
  catch { return }
125
140
  }
126
141
 
142
+ // Storage holds this tab's committed write while later ones are in flight or waiting, so each key keeps those bytes, newest first.
143
+ #remember(key: string, entry: ParsedDraft): void {
144
+ this.#parsed.set(key, [entry, ...(this.#parsed.get(key) ?? []).filter(known => known.raw !== entry.raw)].slice(0, 8))
145
+ }
146
+ #settled(key: string, raw: string, result: DraftStoreResult): void {
147
+ const entries = this.#parsed.get(key), index = entries?.findIndex(entry => entry.raw === raw) ?? -1
148
+ if (!entries || index < 0) return
149
+ this.#parsed.set(key, result === 'committed' ? entries.slice(0, index + 1) : entries.filter((_, position) => position !== index))
150
+ }
151
+
152
+ // This tab's own bytes come from its open document, so reading them back does not parse them again.
153
+ #own(identity: string, next: { record: StoredDocumentDraft, raw: string }): void {
154
+ this.#owned.set(next.record.id, { identity, revision: next.record.revision, raw: next.raw })
155
+ this.#remember(keyOf(identity, next.record.id), { raw: next.raw, documentId: next.record.content.id, value: next.record })
156
+ }
157
+
127
158
  /** Admission is immediate; only done='committed' confirms a durable backup. */
128
159
  write(identity: string, draft: DocumentDraft, previous?: StoredDocumentDraft): DocumentDraftCommit | undefined {
129
160
  const next = this.#record(identity, draft, previous)
130
161
  if (!next) return
131
162
  const commit = this.#store!.put(keyOf(identity, next.record.id), next.raw)
132
163
  if (!commit) return
133
- this.#owned.set(next.record.id, { identity, revision: next.record.revision, raw: next.raw })
134
- void commit.done.then(() => this.#touch())
164
+ this.#own(identity, next)
165
+ void commit.done.then(result => { this.#settled(keyOf(identity, next.record.id), next.raw, result); this.#touch() })
135
166
  if (previous && previous.id !== next.record.id) this.archive(previous)
136
167
  return { ...commit, record: next.record, clearing: false }
137
168
  }
@@ -139,7 +170,7 @@ export class DocumentDrafts {
139
170
  async fork(identity: string, draft: DocumentDraft, operation?: DraftStoreOperation): Promise<StoredDocumentDraft | undefined> {
140
171
  const next = this.#record(identity, draft)
141
172
  if (!next || !await this.#store!.create(keyOf(identity, next.record.id), next.raw, operation)) return
142
- this.#owned.set(next.record.id, { identity, revision: next.record.revision, raw: next.raw })
173
+ this.#own(identity, next)
143
174
  this.#touch()
144
175
  return next.record
145
176
  }