@tearleads/client-sdk 0.1.87 → 0.1.88

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.
Files changed (120) hide show
  1. package/Tearleads.js +1 -1
  2. package/Tearleads.js.map +1 -1
  3. package/client/containerContents/documentDiscovery.js +16 -2
  4. package/client/containerContents/documentDiscovery.js.map +1 -1
  5. package/client/containerContents/index.d.ts +1 -1
  6. package/client/containerContents/index.js +1 -0
  7. package/client/containerContents/index.js.map +1 -1
  8. package/client/index.d.ts +1 -1
  9. package/client/index.js.map +1 -1
  10. package/client/organizations/index.d.ts +4 -2
  11. package/client/organizations/index.js +6 -3
  12. package/client/organizations/index.js.map +1 -1
  13. package/client/organizations/organizationMemberships.d.ts +30 -0
  14. package/client/organizations/organizationMemberships.js +132 -0
  15. package/client/organizations/organizationMemberships.js.map +1 -0
  16. package/client/organizations/organizationsTypes.d.ts +3 -0
  17. package/client/organizations/organizationsTypes.js.map +1 -1
  18. package/data/persistence/container-contents/containerStructuralRepair.js +19 -14
  19. package/data/persistence/container-contents/containerStructuralRepair.js.map +1 -1
  20. package/data/persistence/container-contents/exclusiveDocumentPlacements.js +60 -0
  21. package/data/persistence/container-contents/exclusiveDocumentPlacements.js.map +1 -0
  22. package/data/sqlite/schema/index.d.ts +71 -0
  23. package/data/sqlite/schema/index.js +3 -0
  24. package/data/sqlite/schema/index.js.map +1 -1
  25. package/data/sqlite/schema/organizationSyncPauseSchema.js +32 -0
  26. package/data/sqlite/schema/organizationSyncPauseSchema.js.map +1 -0
  27. package/data/sync/organizationSyncPauses.d.ts +59 -0
  28. package/data/sync/organizationSyncPauses.js +149 -0
  29. package/data/sync/organizationSyncPauses.js.map +1 -0
  30. package/index.d.ts +2 -2
  31. package/index.js.map +1 -1
  32. package/internal/api-client/ApiClient.d.ts +11 -2
  33. package/internal/api-client/ApiClient.js +9 -1
  34. package/internal/api-client/ApiClient.js.map +1 -1
  35. package/internal/api-client/routes/organizations/memberships.js +17 -0
  36. package/internal/api-client/routes/organizations/memberships.js.map +1 -0
  37. package/internal/validators/operation/index.d.ts +1 -0
  38. package/internal/validators/operation/index.js +1 -0
  39. package/internal/validators/operation/index.js.map +1 -1
  40. package/internal/validators/operation/organizationMemberships.d.ts +240 -0
  41. package/internal/validators/operation/organizationMemberships.js +51 -0
  42. package/internal/validators/operation/organizationMemberships.js.map +1 -0
  43. package/internal/validators/operation/registry.d.ts +3 -0
  44. package/internal/validators/operation/registry.js +3 -0
  45. package/internal/validators/operation/registry.js.map +1 -1
  46. package/internal/validators/realtime/index.js +1 -1
  47. package/internal/validators/realtime/index.js.map +1 -1
  48. package/internal/validators/realtime/wsInvalidationHints.js +8 -0
  49. package/internal/validators/realtime/wsInvalidationHints.js.map +1 -1
  50. package/internal/validators/request/index.d.ts +1 -1
  51. package/internal/validators/request/index.js +1 -1
  52. package/internal/validators/request/index.js.map +1 -1
  53. package/internal/validators/request/organization.d.ts +6 -0
  54. package/internal/validators/request/organization.js +7 -0
  55. package/internal/validators/request/organization.js.map +1 -1
  56. package/internal/validators/response/index.d.ts +1 -0
  57. package/internal/validators/response/index.js +1 -0
  58. package/internal/validators/response/index.js.map +1 -1
  59. package/internal/validators/response/organizationMembership.d.ts +56 -0
  60. package/internal/validators/response/organizationMembership.js +26 -0
  61. package/internal/validators/response/organizationMembership.js.map +1 -0
  62. package/package.json +1 -1
  63. package/stores/container-contents/containerContentsStore.d.ts +1 -1
  64. package/stores/container-contents/containerContentsStore.js +2 -0
  65. package/stores/container-contents/containerContentsStore.js.map +1 -1
  66. package/stores/container-contents/operations/forgetOrganizationContentOperation.js +76 -0
  67. package/stores/container-contents/operations/forgetOrganizationContentOperation.js.map +1 -0
  68. package/stores/container-contents/types.d.ts +6 -0
  69. package/stores/container-contents/types.js.map +1 -1
  70. package/stores/documents/documentStore/attachments/attachmentRemoval.js +3 -4
  71. package/stores/documents/documentStore/attachments/attachmentRemoval.js.map +1 -1
  72. package/stores/documents/documentStore/attachments/index.js +5 -7
  73. package/stores/documents/documentStore/attachments/index.js.map +1 -1
  74. package/stores/documents/documentStore/index.js +16 -12
  75. package/stores/documents/documentStore/index.js.map +1 -1
  76. package/stores/documents/documentStore/initialization/index.js +9 -0
  77. package/stores/documents/documentStore/initialization/index.js.map +1 -1
  78. package/stores/documents/documentStore/mutations.js +5 -7
  79. package/stores/documents/documentStore/mutations.js.map +1 -1
  80. package/stores/documents/documentStore/rotation/index.js +2 -1
  81. package/stores/documents/documentStore/rotation/index.js.map +1 -1
  82. package/stores/documents/documentStore/rows/index.js +3 -4
  83. package/stores/documents/documentStore/rows/index.js.map +1 -1
  84. package/stores/documents/documentStore/state.js +18 -0
  85. package/stores/documents/documentStore/state.js.map +1 -1
  86. package/stores/documents/documentStore/sync/index.js +5 -2
  87. package/stores/documents/documentStore/sync/index.js.map +1 -1
  88. package/stores/documents/registry.d.ts +32 -0
  89. package/stores/documents/registry.js +79 -4
  90. package/stores/documents/registry.js.map +1 -1
  91. package/stores/documents/removedDocumentStore.js +38 -0
  92. package/stores/documents/removedDocumentStore.js.map +1 -0
  93. package/stores/documents/types.d.ts +2 -0
  94. package/stores/documents/types.js.map +1 -1
  95. package/sync/reconciliation/service.js +2 -2
  96. package/sync/reconciliation/service.js.map +1 -1
  97. package/sync/reconciliation/serviceTypes.d.ts +8 -1
  98. package/sync/reconciliation/serviceTypes.js.map +1 -1
  99. package/workflows/container-contents/pendingWrites/index.d.ts +5 -0
  100. package/workflows/container-contents/pendingWrites/index.js +7 -0
  101. package/workflows/container-contents/pendingWrites/index.js.map +1 -1
  102. package/workflows/container-contents/remoteHydration/index.js +7 -1
  103. package/workflows/container-contents/remoteHydration/index.js.map +1 -1
  104. package/workflows/container-contents/remoteHydration/remoteContainers/organizationSyncPauseFilter.js +13 -0
  105. package/workflows/container-contents/remoteHydration/remoteContainers/organizationSyncPauseFilter.js.map +1 -0
  106. package/workflows/container-contents/remoteHydration/remoteContainers/remoteContainerPageHydration.js +13 -2
  107. package/workflows/container-contents/remoteHydration/remoteContainers/remoteContainerPageHydration.js.map +1 -1
  108. package/workflows/container-contents/syncPause/discoveryGuard.js +31 -0
  109. package/workflows/container-contents/syncPause/discoveryGuard.js.map +1 -0
  110. package/workflows/container-contents/syncPause/documentRemoval.js +154 -0
  111. package/workflows/container-contents/syncPause/documentRemoval.js.map +1 -0
  112. package/workflows/organizations/index.d.ts +1 -0
  113. package/workflows/organizations/index.js +1 -0
  114. package/workflows/organizations/index.js.map +1 -1
  115. package/workflows/organizations/memberships/organizationMemberships.d.ts +84 -0
  116. package/workflows/organizations/memberships/organizationMemberships.js +95 -0
  117. package/workflows/organizations/memberships/organizationMemberships.js.map +1 -0
  118. package/workflows/sync/index.d.ts +1 -0
  119. package/workflows/sync/index.js +1 -0
  120. package/workflows/sync/index.js.map +1 -1
@@ -1 +1 @@
1
- {"version":3,"file":"state.js","sourceRoot":"","sources":["state.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,qBAAqB,EAAE,MAAM,2CAA2C,CAAC;AAClF,OAAO,EAEL,sBAAsB,EACtB,uBAAuB,GACxB,MAAM,yCAAyC,CAAC;AACjD,OAAO,EACL,0BAA0B,EAC1B,uBAAuB,GAExB,MAAM,uCAAuC,CAAC;AAC/C,OAAO,EAEL,gBAAgB,EAChB,gBAAgB,GACjB,MAAM,yCAAyC,CAAC;AAIjD,OAAO,EACL,4BAA4B,EAC5B,6BAA6B,GAC9B,MAAM,kCAAkC,CAAC;AAE1C,OAAO,EAGL,uCAAuC,EAOvC,2BAA2B,GAC5B,MAAM,8BAA8B,CAAC;AAMtC,OAAO,EACL,qBAAqB,EACrB,wBAAwB,GACzB,MAAM,kCAAkC,CAAC;AAC1C,OAAO,EAAE,oBAAoB,EAAE,kBAAkB,EAAE,MAAM,oBAAoB,CAAC;AAqJ9E,MAAM,uBAAuB,GAAqB;IAChD,WAAW,EAAE,EAAE;IACf,wBAAwB,EAAE,EAAE;IAC5B,4BAA4B,EAAE,EAAE;IAChC,SAAS,EAAE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AK,CAAC,MAAM,EAAE,oBAAoB,CACnC;KACF,CAAC,CAAC;AACL,CAAC;AAED,MAAM,UAAU,wBAAwB,CACtC,KAAyB,EACzB,QAAoB;IAEpB,KAAK,CAAC,SAAS,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;IAC9B,OAAO,GAAG,EAAE,CAAC,KAAK,KAAK,CAAC,SAAS,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;AACrD,CAAC","sourcesContent":["import type { createDocument } from \"@tearleads/loro\";\nimport type { DocumentWriterProjectionResponse } from \"@tearleads/validators/response\";\nimport { DEFAULT_DOCUMENT_KIND } from \"../../../data/documents/documentConstants\";\nimport {\n type DocumentAttachment,\n getDocumentAttachments,\n sameDocumentAttachments,\n} from \"../../../data/documents/documentContent\";\nimport {\n projectStoredDocumentState,\n readStoredDocumentState,\n type StoredDocumentKind,\n} from \"../../../data/documents/documentKinds\";\nimport {\n type DocumentRow,\n listDocumentRows,\n sameDocumentRows,\n} from \"../../../data/documents/documentRowList\";\nimport type { DocumentSummary } from \"../../../data/documents/documentSummary\";\nimport type { DocumentSyncPullContinuation } from \"../../../data/documents/shared/sync/syncPagination\";\nimport type { SyncRemoteDocumentResult } from \"../../../data/documents/shared/types\";\nimport {\n canWriteEffectiveAccessLevel,\n normalizeEffectiveAccessLevel,\n} from \"../../../data/keying/accessLevel\";\nimport type { DomainScope } from \"../../../data/shared/domainScope\";\nimport {\n type AttachmentRemovalRows,\n type AttachmentStagingRows,\n createDocumentProjectionUserKeyResolver,\n type DocumentProjectionUserKeyResolver,\n type DocumentRecord,\n type DocumentSyncLane,\n type DocumentsPersistence,\n type PendingAttachmentRecord,\n type PendingUpdateInsert,\n resolveDocumentCreateAuthor,\n} from \"../../../workflows/documents\";\nimport type {\n DocumentSnapshot,\n DocumentStoreOpenOptions,\n DocumentsRuntime,\n} from \"../types\";\nimport {\n getAttachmentStatuses,\n getAttachmentStorageKeys,\n} from \"./attachments/attachmentSnapshot\";\nimport { sameValidationIssues, shallowEqualRecord } from \"./snapshotEquality\";\nexport type DocumentState = Awaited<ReturnType<typeof createDocument>>;\nexport type EncapsulationKeyPair = NonNullable<\n DocumentsRuntime[\"crypto\"][\"encapsulationKeyPair\"]\n>;\nexport type DocumentAttachmentBinding = NonNullable<\n Awaited<ReturnType<DocumentsRuntime[\"apiClient\"][\"listDocumentAttachments\"]>>\n>[number];\nexport type PendingMutationSyncResult = {\n completed: boolean;\n nextRecord: DocumentRecord;\n};\nexport interface PersistedDocumentRecord {\n creationSuperseded?: true;\n historyRestoreState?:\n | Awaited<ReturnType<DocumentsPersistence[\"loadHistoryRestoreState\"]>>\n | undefined;\n pullContinuationSuperseded?: true;\n record: DocumentRecord;\n syncIdentitySuperseded?: true;\n updatedAt?: string | undefined;\n}\nexport interface SaveDocumentRecordOptions {\n acceptedPendingUpdateIds?: readonly string[] | undefined;\n attachmentRemoval?: AttachmentRemovalRows | undefined;\n attachmentStaging?: AttachmentStagingRows | undefined;\n clearSyncFailure?: boolean | undefined;\n expectedSyncState?:\n | {\n pullContinuation: DocumentSyncPullContinuation | null;\n record: DocumentRecord;\n }\n | undefined;\n historyCheckpoint?:\n | {\n coveredTailIds: readonly string[];\n endVersionVector: string;\n pruneCoveredLocalState: boolean;\n snapshot: string;\n }\n | undefined;\n historyUpdateOrigin?: \"local\" | \"remote\" | undefined;\n historyUpdates?: readonly string[] | undefined;\n pendingUpdate?: Omit<PendingUpdateInsert, \"localId\"> | undefined;\n pendingBaseVersionOverride?: string | null | undefined;\n preserveSnapshotStructuredFields?: boolean | undefined;\n preserveSnapshotText?: boolean | undefined;\n stillCurrent?: (() => boolean) | undefined;\n}\nexport interface DocumentSyncAttempt {\n consumedPullContinuation: DocumentSyncPullContinuation | null;\n outgoingUpdateCount: number;\n requestRecord: DocumentRecord;\n synced: SyncRemoteDocumentResult;\n /** `writerProjectionGeneration` when the request left; gates the install. */\n writerProjectionGeneration: number;\n}\nexport interface DocumentStorePersistenceEffects {\n emitPersistedDocument: (\n domainScope: DomainScope,\n persistedDocument: DocumentSummary,\n placementChanged?: boolean | undefined,\n ) => void;\n registerDocumentIdentity: (\n domainScope: DomainScope,\n localId: string,\n documentId: string | null,\n ) => void;\n}\nexport interface DocumentStoreState {\n attachmentBlobIdBySlotId: Record<string, string | null>;\n /** Plaintext digest of the held bytes per slot; see `getAttachmentStatuses`. */\n attachmentContentSha256BySlotId: Record<string, string>;\n attachmentStorageKeyBySlotId: Record<string, string>;\n /** Creation's birth rides the first attach; see `deferredInitialUpdate.ts`. */\n deferInitialUpdate?: boolean;\n doc: DocumentState | null;\n effects: DocumentStorePersistenceEffects;\n initialDocumentId: string | null;\n /** Fields and rows a created record starts with; see `initialContent.ts`. */\n initialContent?: Pick<\n DocumentStoreOpenOptions,\n \"initialRows\" | \"initialStructuredFields\"\n >;\n initialDocumentKind: StoredDocumentKind;\n initialText: string;\n initializePromise: Promise<void> | null;\n initialized: boolean;\n lastEventCount: number;\n /** Invalidates queued local-write closures when this store is cleared. */\n localWriteGeneration: number;\n locallyAcceptedUpdateIds: Set<string>;\n localId: string;\n listeners: Set<() => void>;\n pendingAttachments: PendingAttachmentRecord[];\n /**\n * Oplog version through which every op in `doc` is already durably enqueued as\n * a pending update or synced from a remote peer. It advances only after an\n * edit's enqueue+persist succeeds, so a later edit can recover any orphaned\n * delta left by a failed write instead of silently dropping it from sync.\n * `null` only before initialization sets it to the loaded doc's version.\n */\n pendingBaseVersion: string | null;\n /**\n * Local text/structured writes queued on `writeChain` but not yet drained.\n * While positive, sync MUST preserve the optimistic text/fields because `doc`\n * can lag the latest keystroke. Remote merges remain in `doc` and publish when\n * the final write settles.\n */\n pendingLocalWrites: number;\n persistence: DocumentsPersistence;\n /**\n * Durable continuation for a bounded remote pull, mirrored from the document\n * record so the live lane can advance it without re-reading SQLite.\n */\n pullContinuation: DocumentSyncPullContinuation | null;\n record: DocumentRecord | null;\n /** Served bindings hydration refused for a held copy; see `hydrate.ts`. */\n rejectedServedAttachmentBindings: Set<string>;\n /**\n * Consecutive completed sync passes that re-keyed conflicted pending updates\n * without settling anything. Bounds the rekey-driven self re-arm so a server\n * that keeps conflicting fresh ids cannot drive a network-speed loop; see\n * shouldReArmAfterOutgoingSettlement.\n */\n rekeyOnlyPassCount: number;\n resolveProjectionUserKey: DocumentProjectionUserKeyResolver;\n /** Latest remote-update sequence durably applied by a completed probe. */\n remoteUpdateCompletedSignalSeq: number;\n /** A cancelled remote-only owner blocks late responses until fresh work. */\n remoteSyncBlocked: boolean;\n scheduleStartupRemoteSync: boolean;\n remoteUpdatePending: boolean;\n /** Sequence used to keep an older pass from clearing a newer remote event. */\n remoteUpdateSignalSeq: number;\n runtime: DocumentsRuntime;\n snapshot: DocumentSnapshot;\n syncLane: DocumentSyncLane | null;\n /** Parked behind another member's ancestor repair; a path hint re-runs. */\n awaitingAncestorRepair: boolean;\n writeChain: Promise<void>;\n writerProjection: DocumentWriterProjectionResponse | null;\n /**\n * Bumped whenever a realtime hint drops `writerProjection`; an operation that\n * fetched or was handed a projection installs it only if this is unchanged\n * since it started, so a pre-hint answer never lands after the hint.\n */\n writerProjectionGeneration: number;\n}\nconst EMPTY_DOCUMENT_SNAPSHOT: DocumentSnapshot = {\n attachments: [],\n attachmentStatusBySlotId: {},\n attachmentStorageKeyBySlotId: {},\n canAttach: false,\n canWrite: true,\n currentAuthorId: null,\n documentId: null,\n documentKind: DEFAULT_DOCUMENT_KIND,\n effectiveAccessLevel: \"admin\",\n fieldValidationIssues: [],\n ready: false,\n rows: [],\n structuredFields: {},\n text: \"\",\n title: \"\",\n syncing: false,\n};\n\nexport function createDocumentStoreState(\n localId: string,\n initialRuntime: DocumentsRuntime,\n persistence: DocumentsPersistence,\n effects: DocumentStorePersistenceEffects,\n initialDocumentId: string | null,\n initialText = \"\",\n initialDocumentKind: StoredDocumentKind = DEFAULT_DOCUMENT_KIND,\n scheduleStartupRemoteSync = true,\n): DocumentStoreState {\n return {\n attachmentBlobIdBySlotId: {},\n attachmentContentSha256BySlotId: {},\n attachmentStorageKeyBySlotId: {},\n doc: null,\n effects,\n initialDocumentId,\n initialDocumentKind,\n initialText,\n initializePromise: null,\n initialized: false,\n lastEventCount: 0,\n localWriteGeneration: 0,\n locallyAcceptedUpdateIds: new Set(),\n localId,\n listeners: new Set(),\n pendingAttachments: [],\n pendingBaseVersion: null,\n pendingLocalWrites: 0,\n persistence,\n pullContinuation: null,\n record: null,\n rejectedServedAttachmentBindings: new Set(),\n rekeyOnlyPassCount: 0,\n resolveProjectionUserKey:\n createDocumentProjectionUserKeyResolver(initialRuntime),\n remoteUpdateCompletedSignalSeq: 0,\n remoteSyncBlocked: !scheduleStartupRemoteSync,\n scheduleStartupRemoteSync,\n awaitingAncestorRepair: false,\n remoteUpdatePending: false,\n remoteUpdateSignalSeq: 0,\n runtime: initialRuntime,\n snapshot: { ...EMPTY_DOCUMENT_SNAPSHOT },\n syncLane: null,\n writeChain: Promise.resolve(),\n writerProjection: null,\n writerProjectionGeneration: 0,\n };\n}\n\nfunction emitDocumentStore(state: DocumentStoreState) {\n for (const listener of state.listeners) {\n listener();\n }\n}\n\nexport function setDocumentSnapshot(\n state: DocumentStoreState,\n next: DocumentSnapshot,\n) {\n if (\n sameDocumentAttachments(state.snapshot.attachments, next.attachments) &&\n shallowEqualRecord(\n state.snapshot.attachmentStatusBySlotId,\n next.attachmentStatusBySlotId,\n ) &&\n shallowEqualRecord(\n state.snapshot.attachmentStorageKeyBySlotId,\n next.attachmentStorageKeyBySlotId,\n ) &&\n state.snapshot.canAttach === next.canAttach &&\n state.snapshot.canWrite === next.canWrite &&\n state.snapshot.currentAuthorId === next.currentAuthorId &&\n state.snapshot.documentId === next.documentId &&\n state.snapshot.documentKind === next.documentKind &&\n state.snapshot.effectiveAccessLevel === next.effectiveAccessLevel &&\n sameValidationIssues(\n state.snapshot.fieldValidationIssues,\n next.fieldValidationIssues,\n ) &&\n state.snapshot.ready === next.ready &&\n sameDocumentRows(state.snapshot.rows, next.rows) &&\n shallowEqualRecord(\n state.snapshot.structuredFields,\n next.structuredFields,\n ) &&\n state.snapshot.text === next.text &&\n state.snapshot.title === next.title &&\n state.snapshot.syncing === next.syncing\n ) {\n return;\n }\n\n state.snapshot = next;\n emitDocumentStore(state);\n}\n\nfunction clearDocumentStoreState(\n state: DocumentStoreState,\n { initialized }: { initialized: boolean },\n) {\n state.deferInitialUpdate = false;\n state.doc = null;\n state.localWriteGeneration += 1;\n state.record = null;\n state.pendingAttachments = [];\n state.pendingBaseVersion = null;\n state.pendingLocalWrites = 0;\n state.pullContinuation = null;\n state.attachmentBlobIdBySlotId = {};\n state.attachmentContentSha256BySlotId = {};\n state.attachmentStorageKeyBySlotId = {};\n state.locallyAcceptedUpdateIds = new Set();\n state.rejectedServedAttachmentBindings = new Set();\n state.remoteUpdateCompletedSignalSeq = 0;\n state.remoteSyncBlocked = !state.scheduleStartupRemoteSync;\n state.awaitingAncestorRepair = false;\n state.remoteUpdatePending = false;\n state.remoteUpdateSignalSeq = 0;\n state.writerProjection = null;\n state.initialized = initialized;\n state.initializePromise = null;\n state.writeChain = Promise.resolve();\n setDocumentSnapshot(state, { ...EMPTY_DOCUMENT_SNAPSHOT });\n}\n\nexport function resetDocumentStore(state: DocumentStoreState) {\n clearDocumentStoreState(state, { initialized: false });\n}\n\nexport function markDocumentStoreRemoved(state: DocumentStoreState) {\n clearDocumentStoreState(state, { initialized: true });\n}\n\nexport function canAttachFiles(state: DocumentStoreState): boolean {\n return (\n canWriteDocument(state) &&\n state.runtime.infra.dbStatus === \"ready\" &&\n !!state.runtime.crypto.encapsulationKeyPair\n );\n}\n\nexport function canWriteDocument(state: DocumentStoreState): boolean {\n return canWriteEffectiveAccessLevel(state.record?.effectiveAccessLevel);\n}\n\nfunction getSnapshotAttachments(\n state: DocumentStoreState,\n currentDoc: DocumentState | null = state.doc,\n): DocumentAttachment[] {\n return currentDoc ? getDocumentAttachments(currentDoc) : [];\n}\n\nfunction getSnapshotRows(\n state: DocumentStoreState,\n currentDoc: DocumentState | null = state.doc,\n): DocumentRow[] {\n return currentDoc ? listDocumentRows(currentDoc) : [];\n}\n\nfunction resolveCurrentAuthorId(state: DocumentStoreState): string | null {\n return resolveDocumentCreateAuthor(state.runtime)?.signerUserId ?? null;\n}\n\nexport function setReadySnapshot(\n state: DocumentStoreState,\n currentDoc: DocumentState,\n syncing: boolean,\n textOverride?: string,\n structuredFieldsOverride?: DocumentSnapshot[\"structuredFields\"],\n) {\n const attachments = getSnapshotAttachments(state, currentDoc);\n const rows = getSnapshotRows(state, currentDoc);\n const documentState = readStoredDocumentState(\n currentDoc,\n state.runtime.infra.documentProjectors,\n );\n const text = textOverride ?? documentState.text;\n const structuredFields =\n structuredFieldsOverride ?? documentState.structuredFields;\n const projectedState =\n textOverride === undefined && structuredFieldsOverride === undefined\n ? documentState\n : projectStoredDocumentState(\n {\n documentKind: documentState.documentKind,\n structuredFields,\n text,\n rows: rows.map((row) => ({ id: row.id, fields: row.fields })),\n },\n state.runtime.infra.documentProjectors,\n );\n\n setDocumentSnapshot(state, {\n attachments,\n attachmentStatusBySlotId: getAttachmentStatuses({\n attachments,\n contentSha256BySlotId: state.attachmentContentSha256BySlotId,\n pendingSlotIds: new Set(\n state.pendingAttachments.map((attachment) => attachment.slotId),\n ),\n }),\n attachmentStorageKeyBySlotId: getAttachmentStorageKeys(\n state.attachmentStorageKeyBySlotId,\n attachments,\n ),\n canAttach: canAttachFiles(state),\n canWrite: canWriteDocument(state),\n currentAuthorId: resolveCurrentAuthorId(state),\n documentId: state.record?.documentId ?? null,\n documentKind: projectedState.documentKind,\n effectiveAccessLevel: normalizeEffectiveAccessLevel(\n state.record?.effectiveAccessLevel,\n ),\n fieldValidationIssues: projectedState.fieldValidationIssues,\n ready: true,\n rows,\n structuredFields: projectedState.structuredFields,\n text,\n title: projectedState.title,\n syncing,\n });\n}\n\nexport function setDocumentSyncing(\n state: DocumentStoreState,\n syncing: boolean,\n) {\n setDocumentSnapshot(state, { ...state.snapshot, syncing });\n}\n\nexport function refreshAttachabilitySnapshot(state: DocumentStoreState) {\n if (!state.snapshot.ready) {\n return;\n }\n\n setDocumentSnapshot(state, {\n ...state.snapshot,\n canAttach: canAttachFiles(state),\n canWrite: canWriteDocument(state),\n effectiveAccessLevel: normalizeEffectiveAccessLevel(\n state.record?.effectiveAccessLevel,\n ),\n });\n}\n\nexport function subscribeToDocumentStore(\n state: DocumentStoreState,\n listener: () => void,\n) {\n state.listeners.add(listener);\n return () => void state.listeners.delete(listener);\n}\n"]}
1
+ {"version":3,"file":"state.js","sourceRoot":"","sources":["state.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,qBAAqB,EAAE,MAAM,2CAA2C,CAAC;AAClF,OAAO,EAEL,sBAAsB,EACtB,uBAAuB,GACxB,MAAM,yCAAyC,CAAC;AACjD,OAAO,EACL,0BAA0B,EAC1B,uBAAuB,GAExB,MAAM,uCAAuC,CAAC;AAC/C,OAAO,EAEL,gBAAgB,EAChB,gBAAgB,GACjB,MAAM,yCAAyC,CAAC;AAIjD,OAAO,EACL,4BAA4B,EAC5B,6BAA6B,GAC9B,MAAM,kCAAkC,CAAC;AAE1C,OAAO,EAGL,uCAAuC,EAOvC,2BAA2B,GAC5B,MAAM,8BAA8B,CAAC;AAMtC,OAAO,EACL,qBAAqB,EACrB,wBAAwB,GACzB,MAAM,kCAAkC,CAAC;AAC1C,OAAO,EAAE,oBAAoB,EAAE,kBAAkB,EAAE,MAAM,oBAAoB,CAAC;AAuJ9E,MAAM,uBAAuB,GAAqB;IAChD,WAAW,EAAE,EAAE;IACf,wBAAwB,EAAE,EAAE;IAC5B,4BAA4B,EAAE,EAAE;IAChC,SAAS,EAAE,KAAK;IAChB,QAAQ,EAAE,IAAI;IACd,eAAe,EAAE,IAAI;IACrB,UAAU,EAAE,IAAI;IAChB,YAAY,EAAE,qBAAqB;IACnC,oBAAoB,EAAE,OAAO;IAC7B,qBAAqB,EAAE,EAAE;IACzB,KAAK,EAAE,KAAK;IACZ,IAAI,EAAE,EAAE;IACR,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,GAAG,EAAE,CAAC,KAAK,KAAK,CAAC,SAAS,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;AACrD,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,gBAAgB,CAC9B,KAAyB,EACzB,KAAiB;IAEjB,KAAK,CAAC,YAAY,IAAI,CAAC,CAAC;IACxB,MAAM,MAAM,GAAG,GAAG,EAAE;QAClB,KAAK,CAAC,YAAY,IAAI,CAAC,CAAC;IAC1B,CAAC,CAAC;IACF,KAAK,KAAK,CAAC,IAAI,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAChC,OAAO,KAAK,CAAC;AACf,CAAC;AAED,4EAA4E;AAC5E,MAAM,UAAU,gBAAgB,CAC9B,KAAyB,EACzB,IAAmB;IAEnB,KAAK,CAAC,UAAU,GAAG,gBAAgB,CAAC,KAAK,EAAE,IAAI,CAAC,CAAC;IACjD,OAAO,KAAK,CAAC,UAAU,CAAC;AAC1B,CAAC","sourcesContent":["import type { createDocument } from \"@tearleads/loro\";\nimport type { DocumentWriterProjectionResponse } from \"@tearleads/validators/response\";\nimport { DEFAULT_DOCUMENT_KIND } from \"../../../data/documents/documentConstants\";\nimport {\n type DocumentAttachment,\n getDocumentAttachments,\n sameDocumentAttachments,\n} from \"../../../data/documents/documentContent\";\nimport {\n projectStoredDocumentState,\n readStoredDocumentState,\n type StoredDocumentKind,\n} from \"../../../data/documents/documentKinds\";\nimport {\n type DocumentRow,\n listDocumentRows,\n sameDocumentRows,\n} from \"../../../data/documents/documentRowList\";\nimport type { DocumentSummary } from \"../../../data/documents/documentSummary\";\nimport type { DocumentSyncPullContinuation } from \"../../../data/documents/shared/sync/syncPagination\";\nimport type { SyncRemoteDocumentResult } from \"../../../data/documents/shared/types\";\nimport {\n canWriteEffectiveAccessLevel,\n normalizeEffectiveAccessLevel,\n} from \"../../../data/keying/accessLevel\";\nimport type { DomainScope } from \"../../../data/shared/domainScope\";\nimport {\n type AttachmentRemovalRows,\n type AttachmentStagingRows,\n createDocumentProjectionUserKeyResolver,\n type DocumentProjectionUserKeyResolver,\n type DocumentRecord,\n type DocumentSyncLane,\n type DocumentsPersistence,\n type PendingAttachmentRecord,\n type PendingUpdateInsert,\n resolveDocumentCreateAuthor,\n} from \"../../../workflows/documents\";\nimport type {\n DocumentSnapshot,\n DocumentStoreOpenOptions,\n DocumentsRuntime,\n} from \"../types\";\nimport {\n getAttachmentStatuses,\n getAttachmentStorageKeys,\n} from \"./attachments/attachmentSnapshot\";\nimport { sameValidationIssues, shallowEqualRecord } from \"./snapshotEquality\";\nexport type DocumentState = Awaited<ReturnType<typeof createDocument>>;\nexport type EncapsulationKeyPair = NonNullable<\n DocumentsRuntime[\"crypto\"][\"encapsulationKeyPair\"]\n>;\nexport type DocumentAttachmentBinding = NonNullable<\n Awaited<ReturnType<DocumentsRuntime[\"apiClient\"][\"listDocumentAttachments\"]>>\n>[number];\nexport type PendingMutationSyncResult = {\n completed: boolean;\n nextRecord: DocumentRecord;\n};\nexport interface PersistedDocumentRecord {\n creationSuperseded?: true;\n historyRestoreState?:\n | Awaited<ReturnType<DocumentsPersistence[\"loadHistoryRestoreState\"]>>\n | undefined;\n pullContinuationSuperseded?: true;\n record: DocumentRecord;\n syncIdentitySuperseded?: true;\n updatedAt?: string | undefined;\n}\nexport interface SaveDocumentRecordOptions {\n acceptedPendingUpdateIds?: readonly string[] | undefined;\n attachmentRemoval?: AttachmentRemovalRows | undefined;\n attachmentStaging?: AttachmentStagingRows | undefined;\n clearSyncFailure?: boolean | undefined;\n expectedSyncState?:\n | {\n pullContinuation: DocumentSyncPullContinuation | null;\n record: DocumentRecord;\n }\n | undefined;\n historyCheckpoint?:\n | {\n coveredTailIds: readonly string[];\n endVersionVector: string;\n pruneCoveredLocalState: boolean;\n snapshot: string;\n }\n | undefined;\n historyUpdateOrigin?: \"local\" | \"remote\" | undefined;\n historyUpdates?: readonly string[] | undefined;\n pendingUpdate?: Omit<PendingUpdateInsert, \"localId\"> | undefined;\n pendingBaseVersionOverride?: string | null | undefined;\n preserveSnapshotStructuredFields?: boolean | undefined;\n preserveSnapshotText?: boolean | undefined;\n stillCurrent?: (() => boolean) | undefined;\n}\nexport interface DocumentSyncAttempt {\n consumedPullContinuation: DocumentSyncPullContinuation | null;\n outgoingUpdateCount: number;\n requestRecord: DocumentRecord;\n synced: SyncRemoteDocumentResult;\n /** `writerProjectionGeneration` when the request left; gates the install. */\n writerProjectionGeneration: number;\n}\nexport interface DocumentStorePersistenceEffects {\n emitPersistedDocument: (\n domainScope: DomainScope,\n persistedDocument: DocumentSummary,\n placementChanged?: boolean | undefined,\n ) => void;\n registerDocumentIdentity: (\n domainScope: DomainScope,\n localId: string,\n documentId: string | null,\n ) => void;\n}\nexport interface DocumentStoreState {\n attachmentBlobIdBySlotId: Record<string, string | null>;\n /** Plaintext digest of the held bytes per slot; see `getAttachmentStatuses`. */\n attachmentContentSha256BySlotId: Record<string, string>;\n attachmentStorageKeyBySlotId: Record<string, string>;\n /** Creation's birth rides the first attach; see `deferredInitialUpdate.ts`. */\n deferInitialUpdate?: boolean;\n doc: DocumentState | null;\n effects: DocumentStorePersistenceEffects;\n initialDocumentId: string | null;\n /** Fields and rows a created record starts with; see `initialContent.ts`. */\n initialContent?: Pick<\n DocumentStoreOpenOptions,\n \"initialRows\" | \"initialStructuredFields\"\n >;\n initialDocumentKind: StoredDocumentKind;\n initialText: string;\n initializePromise: Promise<void> | null;\n initialized: boolean;\n lastEventCount: number;\n /** Invalidates queued local-write closures when this store is cleared. */\n localWriteGeneration: number;\n locallyAcceptedUpdateIds: Set<string>;\n localId: string;\n listeners: Set<() => void>;\n pendingAttachments: PendingAttachmentRecord[];\n /**\n * Oplog version through which every op in `doc` is already durably enqueued as\n * a pending update or synced from a remote peer. It advances only after an\n * edit's enqueue+persist succeeds, so a later edit can recover any orphaned\n * delta left by a failed write instead of silently dropping it from sync.\n * `null` only before initialization sets it to the loaded doc's version.\n */\n pendingBaseVersion: string | null;\n /**\n * Local text/structured writes queued on `writeChain` but not yet drained.\n * While positive, sync MUST preserve the optimistic text/fields because `doc`\n * can lag the latest keystroke. Remote merges remain in `doc` and publish when\n * the final write settles.\n */\n pendingLocalWrites: number;\n /** Local writes called or chained but unsettled; zero means all in SQLite. */\n queuedWrites: number;\n persistence: DocumentsPersistence;\n /**\n * Durable continuation for a bounded remote pull, mirrored from the document\n * record so the live lane can advance it without re-reading SQLite.\n */\n pullContinuation: DocumentSyncPullContinuation | null;\n record: DocumentRecord | null;\n /** Served bindings hydration refused for a held copy; see `hydrate.ts`. */\n rejectedServedAttachmentBindings: Set<string>;\n /**\n * Consecutive completed sync passes that re-keyed conflicted pending updates\n * without settling anything. Bounds the rekey-driven self re-arm so a server\n * that keeps conflicting fresh ids cannot drive a network-speed loop; see\n * shouldReArmAfterOutgoingSettlement.\n */\n rekeyOnlyPassCount: number;\n resolveProjectionUserKey: DocumentProjectionUserKeyResolver;\n /** Latest remote-update sequence durably applied by a completed probe. */\n remoteUpdateCompletedSignalSeq: number;\n /** A cancelled remote-only owner blocks late responses until fresh work. */\n remoteSyncBlocked: boolean;\n scheduleStartupRemoteSync: boolean;\n remoteUpdatePending: boolean;\n /** Sequence used to keep an older pass from clearing a newer remote event. */\n remoteUpdateSignalSeq: number;\n runtime: DocumentsRuntime;\n snapshot: DocumentSnapshot;\n syncLane: DocumentSyncLane | null;\n /** Parked behind another member's ancestor repair; a path hint re-runs. */\n awaitingAncestorRepair: boolean;\n writeChain: Promise<void>;\n writerProjection: DocumentWriterProjectionResponse | null;\n /**\n * Bumped whenever a realtime hint drops `writerProjection`; an operation that\n * fetched or was handed a projection installs it only if this is unchanged\n * since it started, so a pre-hint answer never lands after the hint.\n */\n writerProjectionGeneration: number;\n}\nconst EMPTY_DOCUMENT_SNAPSHOT: DocumentSnapshot = {\n attachments: [],\n attachmentStatusBySlotId: {},\n attachmentStorageKeyBySlotId: {},\n canAttach: false,\n canWrite: true,\n currentAuthorId: null,\n documentId: null,\n documentKind: DEFAULT_DOCUMENT_KIND,\n effectiveAccessLevel: \"admin\",\n fieldValidationIssues: [],\n ready: false,\n rows: [],\n structuredFields: {},\n text: \"\",\n title: \"\",\n syncing: false,\n};\n\nexport function createDocumentStoreState(\n localId: string,\n initialRuntime: DocumentsRuntime,\n persistence: DocumentsPersistence,\n effects: DocumentStorePersistenceEffects,\n initialDocumentId: string | null,\n initialText = \"\",\n initialDocumentKind: StoredDocumentKind = DEFAULT_DOCUMENT_KIND,\n scheduleStartupRemoteSync = true,\n): DocumentStoreState {\n return {\n attachmentBlobIdBySlotId: {},\n attachmentContentSha256BySlotId: {},\n attachmentStorageKeyBySlotId: {},\n doc: null,\n effects,\n initialDocumentId,\n initialDocumentKind,\n initialText,\n initializePromise: null,\n initialized: false,\n lastEventCount: 0,\n localWriteGeneration: 0,\n locallyAcceptedUpdateIds: new Set(),\n localId,\n listeners: new Set(),\n pendingAttachments: [],\n pendingBaseVersion: null,\n pendingLocalWrites: 0,\n persistence,\n queuedWrites: 0,\n pullContinuation: null,\n record: null,\n rejectedServedAttachmentBindings: new Set(),\n rekeyOnlyPassCount: 0,\n resolveProjectionUserKey:\n createDocumentProjectionUserKeyResolver(initialRuntime),\n remoteUpdateCompletedSignalSeq: 0,\n remoteSyncBlocked: !scheduleStartupRemoteSync,\n scheduleStartupRemoteSync,\n awaitingAncestorRepair: false,\n remoteUpdatePending: false,\n remoteUpdateSignalSeq: 0,\n runtime: initialRuntime,\n snapshot: { ...EMPTY_DOCUMENT_SNAPSHOT },\n syncLane: null,\n writeChain: Promise.resolve(),\n writerProjection: null,\n writerProjectionGeneration: 0,\n };\n}\n\nfunction emitDocumentStore(state: DocumentStoreState) {\n for (const listener of state.listeners) {\n listener();\n }\n}\n\nexport function setDocumentSnapshot(\n state: DocumentStoreState,\n next: DocumentSnapshot,\n) {\n if (\n sameDocumentAttachments(state.snapshot.attachments, next.attachments) &&\n shallowEqualRecord(\n state.snapshot.attachmentStatusBySlotId,\n next.attachmentStatusBySlotId,\n ) &&\n shallowEqualRecord(\n state.snapshot.attachmentStorageKeyBySlotId,\n next.attachmentStorageKeyBySlotId,\n ) &&\n state.snapshot.canAttach === next.canAttach &&\n state.snapshot.canWrite === next.canWrite &&\n state.snapshot.currentAuthorId === next.currentAuthorId &&\n state.snapshot.documentId === next.documentId &&\n state.snapshot.documentKind === next.documentKind &&\n state.snapshot.effectiveAccessLevel === next.effectiveAccessLevel &&\n sameValidationIssues(\n state.snapshot.fieldValidationIssues,\n next.fieldValidationIssues,\n ) &&\n state.snapshot.ready === next.ready &&\n sameDocumentRows(state.snapshot.rows, next.rows) &&\n shallowEqualRecord(\n state.snapshot.structuredFields,\n next.structuredFields,\n ) &&\n state.snapshot.text === next.text &&\n state.snapshot.title === next.title &&\n state.snapshot.syncing === next.syncing\n ) {\n return;\n }\n\n state.snapshot = next;\n emitDocumentStore(state);\n}\n\nfunction clearDocumentStoreState(\n state: DocumentStoreState,\n { initialized }: { initialized: boolean },\n) {\n state.deferInitialUpdate = false;\n state.doc = null;\n state.localWriteGeneration += 1;\n state.record = null;\n state.pendingAttachments = [];\n state.pendingBaseVersion = null;\n state.pendingLocalWrites = 0;\n state.pullContinuation = null;\n state.attachmentBlobIdBySlotId = {};\n state.attachmentContentSha256BySlotId = {};\n state.attachmentStorageKeyBySlotId = {};\n state.locallyAcceptedUpdateIds = new Set();\n state.rejectedServedAttachmentBindings = new Set();\n state.remoteUpdateCompletedSignalSeq = 0;\n state.remoteSyncBlocked = !state.scheduleStartupRemoteSync;\n state.awaitingAncestorRepair = false;\n state.remoteUpdatePending = false;\n state.remoteUpdateSignalSeq = 0;\n state.writerProjection = null;\n state.initialized = initialized;\n state.initializePromise = null;\n state.writeChain = Promise.resolve();\n setDocumentSnapshot(state, { ...EMPTY_DOCUMENT_SNAPSHOT });\n}\n\nexport function resetDocumentStore(state: DocumentStoreState) {\n clearDocumentStoreState(state, { initialized: false });\n}\n\nexport function markDocumentStoreRemoved(state: DocumentStoreState) {\n clearDocumentStoreState(state, { initialized: true });\n}\n\nexport function canAttachFiles(state: DocumentStoreState): boolean {\n return (\n canWriteDocument(state) &&\n state.runtime.infra.dbStatus === \"ready\" &&\n !!state.runtime.crypto.encapsulationKeyPair\n );\n}\n\nexport function canWriteDocument(state: DocumentStoreState): boolean {\n return canWriteEffectiveAccessLevel(state.record?.effectiveAccessLevel);\n}\n\nfunction getSnapshotAttachments(\n state: DocumentStoreState,\n currentDoc: DocumentState | null = state.doc,\n): DocumentAttachment[] {\n return currentDoc ? getDocumentAttachments(currentDoc) : [];\n}\n\nfunction getSnapshotRows(\n state: DocumentStoreState,\n currentDoc: DocumentState | null = state.doc,\n): DocumentRow[] {\n return currentDoc ? listDocumentRows(currentDoc) : [];\n}\n\nfunction resolveCurrentAuthorId(state: DocumentStoreState): string | null {\n return resolveDocumentCreateAuthor(state.runtime)?.signerUserId ?? null;\n}\n\nexport function setReadySnapshot(\n state: DocumentStoreState,\n currentDoc: DocumentState,\n syncing: boolean,\n textOverride?: string,\n structuredFieldsOverride?: DocumentSnapshot[\"structuredFields\"],\n) {\n const attachments = getSnapshotAttachments(state, currentDoc);\n const rows = getSnapshotRows(state, currentDoc);\n const documentState = readStoredDocumentState(\n currentDoc,\n state.runtime.infra.documentProjectors,\n );\n const text = textOverride ?? documentState.text;\n const structuredFields =\n structuredFieldsOverride ?? documentState.structuredFields;\n const projectedState =\n textOverride === undefined && structuredFieldsOverride === undefined\n ? documentState\n : projectStoredDocumentState(\n {\n documentKind: documentState.documentKind,\n structuredFields,\n text,\n rows: rows.map((row) => ({ id: row.id, fields: row.fields })),\n },\n state.runtime.infra.documentProjectors,\n );\n\n setDocumentSnapshot(state, {\n attachments,\n attachmentStatusBySlotId: getAttachmentStatuses({\n attachments,\n contentSha256BySlotId: state.attachmentContentSha256BySlotId,\n pendingSlotIds: new Set(\n state.pendingAttachments.map((attachment) => attachment.slotId),\n ),\n }),\n attachmentStorageKeyBySlotId: getAttachmentStorageKeys(\n state.attachmentStorageKeyBySlotId,\n attachments,\n ),\n canAttach: canAttachFiles(state),\n canWrite: canWriteDocument(state),\n currentAuthorId: resolveCurrentAuthorId(state),\n documentId: state.record?.documentId ?? null,\n documentKind: projectedState.documentKind,\n effectiveAccessLevel: normalizeEffectiveAccessLevel(\n state.record?.effectiveAccessLevel,\n ),\n fieldValidationIssues: projectedState.fieldValidationIssues,\n ready: true,\n rows,\n structuredFields: projectedState.structuredFields,\n text,\n title: projectedState.title,\n syncing,\n });\n}\n\nexport function setDocumentSyncing(\n state: DocumentStoreState,\n syncing: boolean,\n) {\n setDocumentSnapshot(state, { ...state.snapshot, syncing });\n}\n\nexport function refreshAttachabilitySnapshot(state: DocumentStoreState) {\n if (!state.snapshot.ready) {\n return;\n }\n\n setDocumentSnapshot(state, {\n ...state.snapshot,\n canAttach: canAttachFiles(state),\n canWrite: canWriteDocument(state),\n effectiveAccessLevel: normalizeEffectiveAccessLevel(\n state.record?.effectiveAccessLevel,\n ),\n });\n}\n\nexport function subscribeToDocumentStore(\n state: DocumentStoreState,\n listener: () => void,\n) {\n state.listeners.add(listener);\n return () => void state.listeners.delete(listener);\n}\n\n/**\n * Counts `write` in `queuedWrites` until it settles. Counted synchronously, so\n * a write started while a removal runs is visible to its commit guard.\n */\nexport function countQueuedWrite<T>(\n state: DocumentStoreState,\n write: Promise<T>,\n): Promise<T> {\n state.queuedWrites += 1;\n const settle = () => {\n state.queuedWrites -= 1;\n };\n void write.then(settle, settle);\n return write;\n}\n\n/** Installs `link` as the store's write chain, counted until it settles. */\nexport function chainQueuedWrite(\n state: DocumentStoreState,\n link: Promise<void>,\n): Promise<void> {\n state.writeChain = countQueuedWrite(state, link);\n return state.writeChain;\n}\n"]}
@@ -1,6 +1,6 @@
1
1
  import { reportAndRethrowKeyingVerificationError } from "../../../../data/keyingProjectionVerification/error.js";
2
2
  import { isDatabaseUnavailableError, registerDocumentSyncLane, resolveDocumentCreateAuthor, } from "../../../../workflows/documents/index.js";
3
- import { requestDocumentStoreSync } from "../../registry.js";
3
+ import { onDocumentStoreRetired, requestDocumentStoreSync, } from "../../registry.js";
4
4
  import { awaitInitializationForSync } from "../initialization/index.js";
5
5
  import { listPendingUpdates } from "../persistence.js";
6
6
  import { setDocumentSyncing, } from "../state.js";
@@ -10,7 +10,7 @@ import { cleanupPreRegisteredUpdateIdsOnFailure, discardPreRegisteredUpdateIds,
10
10
  import { syncPendingAttachments } from "./syncAttachments.js";
11
11
  import { syncDetachedAttachmentBindings } from "./syncDetachedAttachments.js";
12
12
  import { finalizeDocumentSync } from "./syncFinalize.js";
13
- import { captureDocumentStoreAttachmentSyncGeneration, captureDocumentStoreRemoteSyncGeneration, captureDocumentStoreSyncGeneration, isDocumentStoreRemoteSyncBlocked, isDocumentStoreSyncGenerationCurrent, isDocumentStoreSyncLaneGenerationCurrent, } from "./syncGeneration.js";
13
+ import { captureDocumentStoreAttachmentSyncGeneration, captureDocumentStoreRemoteSyncGeneration, captureDocumentStoreSyncGeneration, invalidateDocumentStoreSyncLane, isDocumentStoreRemoteSyncBlocked, isDocumentStoreSyncGenerationCurrent, isDocumentStoreSyncLaneGenerationCurrent, } from "./syncGeneration.js";
14
14
  import { prepareDocumentOutgoingCoverage } from "./syncOutgoingCoverage.js";
15
15
  import { requestRemoteDocumentSync } from "./syncRequest.js";
16
16
  import { ensureSeededRemoteDocument } from "./syncSeededCreate.js";
@@ -272,6 +272,9 @@ async function runScheduledSyncLoop(state, syncLaneGeneration) {
272
272
  }
273
273
  }
274
274
  export function registerDocumentStoreSyncLane(state, getSyncLaneGeneration) {
275
+ // Retiring the store (its document removed under it) ends this lane's
276
+ // current and in-flight work; a replacement store registers its own.
277
+ onDocumentStoreRetired(state.runtime.state.domainScope, state.localId, () => invalidateDocumentStoreSyncLane(state));
275
278
  return registerDocumentSyncLane({
276
279
  domainScope: state.runtime.state.domainScope,
277
280
  localId: state.localId,
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,uCAAuC,EAAE,M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CAAC;gBACH,CAAC,CAAC,CAAC;gBACH,OAAO,OAAO,CAAC,OAAO,EAAE,CAAC;YAC3B,CAAC;YACD,OAAO,qBAAqB,CAAC,KAAK,EAAE,GAAG,EAAE,CACvC,wCAAwC,CAAC,KAAK,EAAE,kBAAkB,CAAC;gBACjE,CAAC,CAAC,oBAAoB,CAAC,KAAK,EAAE,kBAAkB,CAAC;gBACjD,CAAC,CAAC,OAAO,CAAC,OAAO,EAAE,CACtB,CAAC;QACJ,CAAC;KACF,CAAC,CAAC;AACL,CAAC","sourcesContent":["import { reportAndRethrowKeyingVerificationError } from \"../../../../data/keyingProjectionVerification/error\";\nimport {\n type DocumentRecord,\n type DocumentSyncLane,\n isDatabaseUnavailableError,\n type PendingUpdateRecord,\n registerDocumentSyncLane,\n resolveDocumentCreateAuthor,\n} from \"../../../../workflows/documents\";\nimport { requestDocumentStoreSync } from \"../../registry\";\nimport { awaitInitializationForSync } from \"../initialization\";\nimport { listPendingUpdates } from \"../persistence\";\nimport {\n type DocumentState,\n type DocumentStoreState,\n type DocumentSyncAttempt,\n type EncapsulationKeyPair,\n setDocumentSyncing,\n} from \"../state\";\nimport { logRevalidationUnavailable as logUnavailable } from \"./remoteRevalidationTelemetry\";\nimport {\n getActiveDocumentRemoteWork,\n runDocumentRemoteWork,\n} from \"./remoteWork\";\nimport {\n cleanupPreRegisteredUpdateIdsOnFailure,\n discardPreRegisteredUpdateIds,\n preRegisterMaterializedDocumentSyncUpdateIds,\n} from \"./syncAcceptedUpdateIds\";\nimport { syncPendingAttachments } from \"./syncAttachments\";\nimport { syncDetachedAttachmentBindings } from \"./syncDetachedAttachments\";\nimport { finalizeDocumentSync } from \"./syncFinalize\";\nimport {\n captureDocumentStoreAttachmentSyncGeneration,\n captureDocumentStoreRemoteSyncGeneration,\n captureDocumentStoreSyncGeneration,\n type DocumentStoreSyncGeneration,\n type DocumentStoreSyncLaneGeneration,\n isDocumentStoreRemoteSyncBlocked,\n isDocumentStoreSyncGenerationCurrent,\n isDocumentStoreSyncLaneGenerationCurrent,\n} from \"./syncGeneration\";\nimport { prepareDocumentOutgoingCoverage } from \"./syncOutgoingCoverage\";\nimport { requestRemoteDocumentSync } from \"./syncRequest\";\nimport { ensureSeededRemoteDocument } from \"./syncSeededCreate\";\nimport {\n isContainerAwaitingRemoteCreate,\n shouldSkipCleanScheduledDocumentSync,\n} from \"./syncShared\";\n\nfunction canRunScheduledSync(state: DocumentStoreState): boolean {\n return (\n // A deferred birth waits for its attach to carry it (see\n // deferredInitialUpdate.ts); a pass now would ship it on its own.\n state.deferInitialUpdate !== true &&\n state.doc !== null &&\n state.snapshot.ready &&\n state.runtime.state.online &&\n state.runtime.auth.isAuthenticated &&\n state.runtime.crypto.encapsulationKeyPair !== null &&\n resolveDocumentCreateAuthor(state.runtime) !== null\n );\n}\n\nasync function requestOutgoingDocumentBatch(input: {\n readonly currentDoc: DocumentState;\n readonly encapsulationKeyPair: EncapsulationKeyPair;\n readonly generation: DocumentStoreSyncGeneration;\n readonly pendingUpdates: PendingUpdateRecord[];\n readonly record: DocumentRecord;\n readonly state: DocumentStoreState;\n}): Promise<{\n sentUpdateIds: string[];\n syncAttempt: DocumentSyncAttempt | null;\n}> {\n const { planningUpdates, queuedUpdateCount } = prepareDocumentStoreSyncQueue(\n input.pendingUpdates,\n );\n const sentUpdateIds: string[] = [];\n const syncAttempt = await cleanupPreRegisteredUpdateIdsOnFailure(\n input.state,\n sentUpdateIds,\n () =>\n requestRemoteDocumentSync({\n currentDoc: input.currentDoc,\n currentRecord: input.record,\n encapsulationKeyPair: input.encapsulationKeyPair,\n generation: input.generation,\n onOutgoingUpdatesMaterialized: (updateIds) =>\n preRegisterMaterializedDocumentSyncUpdateIds(\n input.state,\n sentUpdateIds,\n updateIds,\n ),\n pendingUpdates: planningUpdates,\n queuedUpdateCount,\n state: input.state,\n unavailableWriterLogMessage:\n \"Documents: skipped sync because the writer context is unavailable.\",\n }),\n );\n return { sentUpdateIds, syncAttempt };\n}\n\n/**\n * Keep the full durable queue visible to stale-heal/checkpoint classification.\n * The workflow planner owns the final count and byte bounds, and the exact\n * materialized batch is pre-registered immediately before each submit.\n */\nexport function prepareDocumentStoreSyncQueue(\n pendingUpdates: PendingUpdateRecord[],\n): {\n planningUpdates: PendingUpdateRecord[];\n queuedUpdateCount: number;\n} {\n return {\n planningUpdates: pendingUpdates,\n queuedUpdateCount: pendingUpdates.length,\n };\n}\n\nexport function canSyncDetachedAttachmentBindings(\n record: DocumentRecord,\n pendingUpdateCount: number,\n): boolean {\n return (\n pendingUpdateCount === 0 &&\n (record.pullContinuation ?? null) === null &&\n !record.pullContinuationRecoveryRequired\n );\n}\n\nasync function syncDocumentState(\n state: DocumentStoreState,\n currentDoc: DocumentState,\n nextRecord: DocumentRecord,\n encapsulationKeyPair: EncapsulationKeyPair,\n generation: DocumentStoreSyncGeneration,\n): Promise<DocumentRecord> {\n // Snapshot the remote-update signal sequence before any await. The GET below\n // fetches server state as of its own snapshot; any remote event delivered\n // after this point describes an update that may not be in that response, so\n // finalizeDocumentSync must not clear the signal if this sequence has moved.\n const consumedRemoteUpdateSignalSeq = state.remoteUpdateSignalSeq;\n const wasRemoteProbe = state.remoteUpdatePending;\n let pendingUpdates = await listPendingUpdates(state);\n if (!isDocumentStoreSyncGenerationCurrent(state, generation)) {\n requestDocumentStoreSync(state);\n return state.record ?? nextRecord;\n }\n if (pendingUpdates.length === 0 && isDocumentStoreRemoteSyncBlocked(state)) {\n return nextRecord;\n }\n // Create the remote document even when nothing is queued to send: a note\n // created and never edited enqueues no updates, but its local row is still a\n // pending create the write queue reports — it must flush, not sit. Defer\n // while the container itself still awaits its remote create — with OR\n // without queued edits, since the create cannot succeed before its parent\n // exists and attempting it just records a spurious failure. The structural\n // lane primes this store again as soon as the container create lands.\n if (!nextRecord.documentId) {\n const containerAwaitsCreate = await isContainerAwaitingRemoteCreate(state);\n if (!isDocumentStoreSyncGenerationCurrent(state, generation)) {\n requestDocumentStoreSync(state);\n return state.record ?? nextRecord;\n }\n if (containerAwaitsCreate) return nextRecord;\n }\n const created = await ensureSeededRemoteDocument({\n consumedRemoteUpdateSignalSeq,\n currentDoc,\n encapsulationKeyPair,\n generation,\n nextRecord,\n pendingUpdates,\n state,\n });\n let nextRemoteRecord = created.record;\n pendingUpdates = created.pendingUpdates;\n if (!isDocumentStoreSyncGenerationCurrent(state, generation)) {\n requestDocumentStoreSync(state);\n return state.record ?? nextRecord;\n }\n if (!nextRemoteRecord?.documentId) return nextRecord;\n\n const preparedCoverage = await prepareDocumentOutgoingCoverage({\n currentDoc,\n generation,\n pendingUpdates,\n state,\n });\n if (!preparedCoverage) {\n requestDocumentStoreSync(state);\n return state.record ?? nextRemoteRecord;\n }\n nextRemoteRecord = preparedCoverage.record;\n pendingUpdates = preparedCoverage.pendingUpdates;\n\n if (\n shouldSkipCleanScheduledDocumentSync({\n currentRecord: nextRemoteRecord,\n pendingUpdates,\n state,\n })\n ) {\n return nextRemoteRecord;\n }\n\n // Pre-register the IDs we are about to send so the author's OWN\n // `document_update_created` echo (fanned back over realtime) is classified as\n // self-authored and never re-arms a redundant sync. Registering BEFORE the\n // network await closes the race where the echo lands before\n // finalizeDocumentSync records the accepted IDs — the gap that turned every\n // fast keystroke into an extra self-triggered sync.\n const { sentUpdateIds, syncAttempt } = await requestOutgoingDocumentBatch({\n currentDoc,\n encapsulationKeyPair,\n generation,\n pendingUpdates,\n record: nextRemoteRecord,\n state,\n });\n if (!isDocumentStoreSyncGenerationCurrent(state, generation)) {\n discardPreRegisteredUpdateIds(state, sentUpdateIds);\n requestDocumentStoreSync(state);\n return state.record ?? nextRemoteRecord;\n }\n if (!syncAttempt) {\n discardPreRegisteredUpdateIds(state, sentUpdateIds);\n logUnavailable(state, wasRemoteProbe);\n return nextRemoteRecord;\n }\n\n return cleanupPreRegisteredUpdateIdsOnFailure(state, sentUpdateIds, () =>\n finalizeDocumentSync(\n state,\n currentDoc,\n nextRemoteRecord,\n syncAttempt,\n consumedRemoteUpdateSignalSeq,\n generation,\n sentUpdateIds,\n wasRemoteProbe,\n ),\n );\n}\n\nasync function revalidateRemoteDocumentBeforeAttachments(\n state: DocumentStoreState,\n currentDoc: DocumentState,\n nextRecord: DocumentRecord,\n encapsulationKeyPair: EncapsulationKeyPair,\n generation: DocumentStoreSyncGeneration,\n): Promise<{ canSyncAttachments: boolean; nextRecord: DocumentRecord }> {\n if (state.pendingAttachments.length === 0 || !nextRecord.documentId) {\n return { canSyncAttachments: true, nextRecord };\n }\n\n const consumedRemoteUpdateSignalSeq = state.remoteUpdateSignalSeq;\n const wasRemoteProbe = state.remoteUpdatePending;\n const syncAttempt = await requestRemoteDocumentSync({\n state,\n currentDoc,\n currentRecord: nextRecord,\n encapsulationKeyPair,\n generation,\n pendingUpdates: [],\n unavailableWriterLogMessage:\n \"Documents: skipped pre-attachment revalidation because the writer context is unavailable.\",\n });\n if (!isDocumentStoreSyncGenerationCurrent(state, generation)) {\n requestDocumentStoreSync(state);\n return { canSyncAttachments: false, nextRecord };\n }\n if (!syncAttempt) {\n logUnavailable(state, wasRemoteProbe);\n return { canSyncAttachments: false, nextRecord };\n }\n\n const refreshedRecord = await finalizeDocumentSync(\n state,\n currentDoc,\n nextRecord,\n syncAttempt,\n consumedRemoteUpdateSignalSeq,\n generation,\n [],\n wasRemoteProbe,\n );\n return {\n canSyncAttachments:\n !syncAttempt.synced.hasIncompletePull &&\n refreshedRecord.pullContinuation == null &&\n refreshedRecord.pullContinuationRecoveryRequired !== true &&\n isDocumentStoreSyncGenerationCurrent(state, generation),\n nextRecord: refreshedRecord,\n };\n}\n\nasync function runDocumentSyncPass(\n state: DocumentStoreState,\n syncLaneGeneration: DocumentStoreSyncLaneGeneration,\n) {\n const currentDoc = state.doc;\n const encapsulationKeyPair = state.runtime.crypto.encapsulationKeyPair;\n let nextRecord = state.record;\n\n if (!currentDoc || !nextRecord || !encapsulationKeyPair) {\n return;\n }\n\n const generation = captureDocumentStoreRemoteSyncGeneration(\n state,\n currentDoc,\n syncLaneGeneration,\n );\n const attachmentGeneration = captureDocumentStoreAttachmentSyncGeneration(\n state,\n currentDoc,\n syncLaneGeneration,\n );\n if (!generation || !attachmentGeneration) {\n requestDocumentStoreSync(state);\n return;\n }\n\n const revalidation = await revalidateRemoteDocumentBeforeAttachments(\n state,\n currentDoc,\n nextRecord,\n encapsulationKeyPair,\n generation,\n );\n nextRecord = revalidation.nextRecord;\n if (!revalidation.canSyncAttachments) {\n return;\n }\n\n const attachmentResult = await syncPendingAttachments(\n state,\n nextRecord,\n encapsulationKeyPair,\n attachmentGeneration,\n );\n nextRecord = attachmentResult.nextRecord;\n if (state.pendingAttachments.length > 0) {\n return;\n }\n // A settled bind and the content update carrying its digest are two server\n // writes. Push the content in this same pass rather than re-arming a later\n // one, so a device lost between them is the only remaining window: peers\n // otherwise see the old digest against the new binding.\n\n nextRecord = await syncDocumentState(\n state,\n currentDoc,\n nextRecord,\n encapsulationKeyPair,\n generation,\n );\n\n if (!isDocumentStoreSyncGenerationCurrent(state, generation)) {\n requestDocumentStoreSync(state);\n return;\n }\n\n if (!state.doc || !state.record) return;\n\n const pendingUpdates = await listPendingUpdates(state);\n if (!isDocumentStoreSyncGenerationCurrent(state, generation)) {\n requestDocumentStoreSync(state);\n return;\n }\n if (!canSyncDetachedAttachmentBindings(nextRecord, pendingUpdates.length)) {\n return;\n }\n\n await syncDetachedAttachmentBindings(state, nextRecord, generation);\n if (!isDocumentStoreSyncGenerationCurrent(state, generation)) {\n requestDocumentStoreSync(state);\n }\n}\n\nasync function runScheduledSyncIteration(\n state: DocumentStoreState,\n syncLaneGeneration: DocumentStoreSyncLaneGeneration,\n) {\n if (!(await awaitInitializationForSync(state))) {\n return false;\n }\n if (!isDocumentStoreSyncLaneGenerationCurrent(state, syncLaneGeneration)) {\n return true;\n }\n\n if (!canRunScheduledSync(state)) {\n return true;\n }\n\n try {\n await runDocumentSyncPass(state, syncLaneGeneration);\n return true;\n } catch (error) {\n if (isDatabaseUnavailableError(error)) {\n return false;\n }\n\n await reportAndRethrowKeyingVerificationError(\n error,\n state.runtime.util.reportSecurityIncident,\n {\n objectId: state.record?.documentId ?? state.localId,\n objectKind: \"document\",\n operation: \"document.sync\",\n },\n );\n\n throw error;\n }\n}\n\nasync function runScheduledSyncLoop(\n state: DocumentStoreState,\n syncLaneGeneration: DocumentStoreSyncLaneGeneration,\n) {\n const syncingGeneration = captureDocumentStoreSyncGeneration(\n state,\n state.doc,\n );\n setDocumentSyncing(state, true);\n\n try {\n await runScheduledSyncIteration(state, syncLaneGeneration);\n } finally {\n if (\n syncingGeneration &&\n isDocumentStoreSyncGenerationCurrent(state, syncingGeneration) &&\n isDocumentStoreSyncLaneGenerationCurrent(state, syncLaneGeneration)\n ) {\n setDocumentSyncing(state, false);\n }\n }\n}\n\nexport function registerDocumentStoreSyncLane(\n state: DocumentStoreState,\n getSyncLaneGeneration: () => DocumentStoreSyncLaneGeneration | null,\n): DocumentSyncLane {\n return registerDocumentSyncLane({\n domainScope: state.runtime.state.domainScope,\n localId: state.localId,\n priority: () =>\n state.remoteUpdatePending || state.record?.snapshotEndVersion === \"\"\n ? -1\n : 0,\n logError: (message, error) => {\n // Read the runtime lazily: a store's runtime is replaced in place, and a\n // reference captured here would report into a torn-down host.\n const { log, logError } = state.runtime.util;\n // Return the host's result: both callbacks are declared `=> void` but may\n // be async, and the lane reporter only catches a rejection it is handed.\n return logError ? logError(message, error) : log(message);\n },\n run: () => {\n const syncLaneGeneration = getSyncLaneGeneration();\n if (!syncLaneGeneration) return Promise.resolve();\n const activeWork = getActiveDocumentRemoteWork(state);\n if (activeWork) {\n // Re-arm on settlement, not immediately: immediate re-requests would\n // spin this lane and starve other work while the HTTP request is held.\n void activeWork.then(() => {\n if (\n isDocumentStoreSyncLaneGenerationCurrent(state, syncLaneGeneration)\n ) {\n requestDocumentStoreSync(state);\n }\n });\n return Promise.resolve();\n }\n return runDocumentRemoteWork(state, () =>\n isDocumentStoreSyncLaneGenerationCurrent(state, syncLaneGeneration)\n ? runScheduledSyncLoop(state, syncLaneGeneration)\n : Promise.resolve(),\n );\n },\n });\n}\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,uCAAuC,EAAE,MAAM,qDAAqD,CAAC;AAC9G,OAAO,EAGL,0BAA0B,EAE1B,wBAAwB,EACxB,2BAA2B,GAC5B,MAAM,iCAAiC,CAAC;AACzC,OAAO,EACL,sBAAsB,EACtB,wBAAwB,GACzB,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAAE,0BAA0B,EAAE,MAAM,mBAAmB,CAAC;AAC/D,OAAO,EAAE,kBAAkB,EAAE,MAAM,gBAAgB,CAAC;AACpD,OAAO,EAKL,kBAAkB,GACnB,MAAM,UAAU,CAAC;AAClB,OAAO,EAAE,0BAA0B,IAAI,cAAc,EAAE,MAAM,+BAA+B,CAAC;AAC7F,OAAO,EACL,2BAA2B,EAC3B,qBAAqB,GACtB,MAAM,cAAc,CAAC;AACtB,OAAO,EACL,sCAAsC,EACtC,6BAA6B,EAC7B,4CAA4C,GAC7C,MAAM,yBAAyB,CAAC;AACjC,OAAO,EAAE,sBAAsB,EAAE,MAAM,mBAAmB,CAAC;AAC3D,OAAO,EAAE,8BAA8B,EAAE,MAAM,2BAA2B,CAAC;AAC3E,OAAO,EAAE,oBAAoB,EAAE,MAAM,gBAAgB,CAAC;AACtD,OAAO,EACL,4CAA4C,EAC5C,wCAAwC,EACxC,kCAAkC,EAGlC,+BAA+B,EAC/B,gCAAgC,EAChC,oCAAoC,EACpC,wCAAwC,GACzC,MAAM,kBAAkB,CAAC;AAC1B,OAAO,EAAE,+BAA+B,EAAE,MAAM,wBAAwB,CAAC;AACzE,OAAO,EAAE,yBAAyB,EAAE,MAAM,eAAe,CAAC;AAC1D,OAAO,EAAE,0BAA0B,EAAE,MAAM,oBAAoB,CAAC;AAChE,OAAO,EACL,+BAA+B,EAC/B,oCAAoC,GACrC,MAAM,cAAc,CAAC;AAEtB,SA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EAAE,CAAC;YACnD,IAAI,CAAC,kBAAkB;gBAAE,OAAO,OAAO,CAAC,OAAO,EAAE,CAAC;YAClD,MAAM,UAAU,GAAG,2BAA2B,CAAC,KAAK,CAAC,CAAC;YACtD,IAAI,UAAU,EAAE,CAAC;gBACf,qEAAqE;gBACrE,uEAAuE;gBACvE,KAAK,UAAU,CAAC,IAAI,CAAC,GAAG,EAAE;oBACxB,IACE,wCAAwC,CAAC,KAAK,EAAE,kBAAkB,CAAC,EACnE,CAAC;wBACD,wBAAwB,CAAC,KAAK,CAAC,CAAC;oBAClC,CAAC;gBACH,CAAC,CAAC,CAAC;gBACH,OAAO,OAAO,CAAC,OAAO,EAAE,CAAC;YAC3B,CAAC;YACD,OAAO,qBAAqB,CAAC,KAAK,EAAE,GAAG,EAAE,CACvC,wCAAwC,CAAC,KAAK,EAAE,kBAAkB,CAAC;gBACjE,CAAC,CAAC,oBAAoB,CAAC,KAAK,EAAE,kBAAkB,CAAC;gBACjD,CAAC,CAAC,OAAO,CAAC,OAAO,EAAE,CACtB,CAAC;QACJ,CAAC;KACF,CAAC,CAAC;AACL,CAAC","sourcesContent":["import { reportAndRethrowKeyingVerificationError } from \"../../../../data/keyingProjectionVerification/error\";\nimport {\n type DocumentRecord,\n type DocumentSyncLane,\n isDatabaseUnavailableError,\n type PendingUpdateRecord,\n registerDocumentSyncLane,\n resolveDocumentCreateAuthor,\n} from \"../../../../workflows/documents\";\nimport {\n onDocumentStoreRetired,\n requestDocumentStoreSync,\n} from \"../../registry\";\nimport { awaitInitializationForSync } from \"../initialization\";\nimport { listPendingUpdates } from \"../persistence\";\nimport {\n type DocumentState,\n type DocumentStoreState,\n type DocumentSyncAttempt,\n type EncapsulationKeyPair,\n setDocumentSyncing,\n} from \"../state\";\nimport { logRevalidationUnavailable as logUnavailable } from \"./remoteRevalidationTelemetry\";\nimport {\n getActiveDocumentRemoteWork,\n runDocumentRemoteWork,\n} from \"./remoteWork\";\nimport {\n cleanupPreRegisteredUpdateIdsOnFailure,\n discardPreRegisteredUpdateIds,\n preRegisterMaterializedDocumentSyncUpdateIds,\n} from \"./syncAcceptedUpdateIds\";\nimport { syncPendingAttachments } from \"./syncAttachments\";\nimport { syncDetachedAttachmentBindings } from \"./syncDetachedAttachments\";\nimport { finalizeDocumentSync } from \"./syncFinalize\";\nimport {\n captureDocumentStoreAttachmentSyncGeneration,\n captureDocumentStoreRemoteSyncGeneration,\n captureDocumentStoreSyncGeneration,\n type DocumentStoreSyncGeneration,\n type DocumentStoreSyncLaneGeneration,\n invalidateDocumentStoreSyncLane,\n isDocumentStoreRemoteSyncBlocked,\n isDocumentStoreSyncGenerationCurrent,\n isDocumentStoreSyncLaneGenerationCurrent,\n} from \"./syncGeneration\";\nimport { prepareDocumentOutgoingCoverage } from \"./syncOutgoingCoverage\";\nimport { requestRemoteDocumentSync } from \"./syncRequest\";\nimport { ensureSeededRemoteDocument } from \"./syncSeededCreate\";\nimport {\n isContainerAwaitingRemoteCreate,\n shouldSkipCleanScheduledDocumentSync,\n} from \"./syncShared\";\n\nfunction canRunScheduledSync(state: DocumentStoreState): boolean {\n return (\n // A deferred birth waits for its attach to carry it (see\n // deferredInitialUpdate.ts); a pass now would ship it on its own.\n state.deferInitialUpdate !== true &&\n state.doc !== null &&\n state.snapshot.ready &&\n state.runtime.state.online &&\n state.runtime.auth.isAuthenticated &&\n state.runtime.crypto.encapsulationKeyPair !== null &&\n resolveDocumentCreateAuthor(state.runtime) !== null\n );\n}\n\nasync function requestOutgoingDocumentBatch(input: {\n readonly currentDoc: DocumentState;\n readonly encapsulationKeyPair: EncapsulationKeyPair;\n readonly generation: DocumentStoreSyncGeneration;\n readonly pendingUpdates: PendingUpdateRecord[];\n readonly record: DocumentRecord;\n readonly state: DocumentStoreState;\n}): Promise<{\n sentUpdateIds: string[];\n syncAttempt: DocumentSyncAttempt | null;\n}> {\n const { planningUpdates, queuedUpdateCount } = prepareDocumentStoreSyncQueue(\n input.pendingUpdates,\n );\n const sentUpdateIds: string[] = [];\n const syncAttempt = await cleanupPreRegisteredUpdateIdsOnFailure(\n input.state,\n sentUpdateIds,\n () =>\n requestRemoteDocumentSync({\n currentDoc: input.currentDoc,\n currentRecord: input.record,\n encapsulationKeyPair: input.encapsulationKeyPair,\n generation: input.generation,\n onOutgoingUpdatesMaterialized: (updateIds) =>\n preRegisterMaterializedDocumentSyncUpdateIds(\n input.state,\n sentUpdateIds,\n updateIds,\n ),\n pendingUpdates: planningUpdates,\n queuedUpdateCount,\n state: input.state,\n unavailableWriterLogMessage:\n \"Documents: skipped sync because the writer context is unavailable.\",\n }),\n );\n return { sentUpdateIds, syncAttempt };\n}\n\n/**\n * Keep the full durable queue visible to stale-heal/checkpoint classification.\n * The workflow planner owns the final count and byte bounds, and the exact\n * materialized batch is pre-registered immediately before each submit.\n */\nexport function prepareDocumentStoreSyncQueue(\n pendingUpdates: PendingUpdateRecord[],\n): {\n planningUpdates: PendingUpdateRecord[];\n queuedUpdateCount: number;\n} {\n return {\n planningUpdates: pendingUpdates,\n queuedUpdateCount: pendingUpdates.length,\n };\n}\n\nexport function canSyncDetachedAttachmentBindings(\n record: DocumentRecord,\n pendingUpdateCount: number,\n): boolean {\n return (\n pendingUpdateCount === 0 &&\n (record.pullContinuation ?? null) === null &&\n !record.pullContinuationRecoveryRequired\n );\n}\n\nasync function syncDocumentState(\n state: DocumentStoreState,\n currentDoc: DocumentState,\n nextRecord: DocumentRecord,\n encapsulationKeyPair: EncapsulationKeyPair,\n generation: DocumentStoreSyncGeneration,\n): Promise<DocumentRecord> {\n // Snapshot the remote-update signal sequence before any await. The GET below\n // fetches server state as of its own snapshot; any remote event delivered\n // after this point describes an update that may not be in that response, so\n // finalizeDocumentSync must not clear the signal if this sequence has moved.\n const consumedRemoteUpdateSignalSeq = state.remoteUpdateSignalSeq;\n const wasRemoteProbe = state.remoteUpdatePending;\n let pendingUpdates = await listPendingUpdates(state);\n if (!isDocumentStoreSyncGenerationCurrent(state, generation)) {\n requestDocumentStoreSync(state);\n return state.record ?? nextRecord;\n }\n if (pendingUpdates.length === 0 && isDocumentStoreRemoteSyncBlocked(state)) {\n return nextRecord;\n }\n // Create the remote document even when nothing is queued to send: a note\n // created and never edited enqueues no updates, but its local row is still a\n // pending create the write queue reports — it must flush, not sit. Defer\n // while the container itself still awaits its remote create — with OR\n // without queued edits, since the create cannot succeed before its parent\n // exists and attempting it just records a spurious failure. The structural\n // lane primes this store again as soon as the container create lands.\n if (!nextRecord.documentId) {\n const containerAwaitsCreate = await isContainerAwaitingRemoteCreate(state);\n if (!isDocumentStoreSyncGenerationCurrent(state, generation)) {\n requestDocumentStoreSync(state);\n return state.record ?? nextRecord;\n }\n if (containerAwaitsCreate) return nextRecord;\n }\n const created = await ensureSeededRemoteDocument({\n consumedRemoteUpdateSignalSeq,\n currentDoc,\n encapsulationKeyPair,\n generation,\n nextRecord,\n pendingUpdates,\n state,\n });\n let nextRemoteRecord = created.record;\n pendingUpdates = created.pendingUpdates;\n if (!isDocumentStoreSyncGenerationCurrent(state, generation)) {\n requestDocumentStoreSync(state);\n return state.record ?? nextRecord;\n }\n if (!nextRemoteRecord?.documentId) return nextRecord;\n\n const preparedCoverage = await prepareDocumentOutgoingCoverage({\n currentDoc,\n generation,\n pendingUpdates,\n state,\n });\n if (!preparedCoverage) {\n requestDocumentStoreSync(state);\n return state.record ?? nextRemoteRecord;\n }\n nextRemoteRecord = preparedCoverage.record;\n pendingUpdates = preparedCoverage.pendingUpdates;\n\n if (\n shouldSkipCleanScheduledDocumentSync({\n currentRecord: nextRemoteRecord,\n pendingUpdates,\n state,\n })\n ) {\n return nextRemoteRecord;\n }\n\n // Pre-register the IDs we are about to send so the author's OWN\n // `document_update_created` echo (fanned back over realtime) is classified as\n // self-authored and never re-arms a redundant sync. Registering BEFORE the\n // network await closes the race where the echo lands before\n // finalizeDocumentSync records the accepted IDs — the gap that turned every\n // fast keystroke into an extra self-triggered sync.\n const { sentUpdateIds, syncAttempt } = await requestOutgoingDocumentBatch({\n currentDoc,\n encapsulationKeyPair,\n generation,\n pendingUpdates,\n record: nextRemoteRecord,\n state,\n });\n if (!isDocumentStoreSyncGenerationCurrent(state, generation)) {\n discardPreRegisteredUpdateIds(state, sentUpdateIds);\n requestDocumentStoreSync(state);\n return state.record ?? nextRemoteRecord;\n }\n if (!syncAttempt) {\n discardPreRegisteredUpdateIds(state, sentUpdateIds);\n logUnavailable(state, wasRemoteProbe);\n return nextRemoteRecord;\n }\n\n return cleanupPreRegisteredUpdateIdsOnFailure(state, sentUpdateIds, () =>\n finalizeDocumentSync(\n state,\n currentDoc,\n nextRemoteRecord,\n syncAttempt,\n consumedRemoteUpdateSignalSeq,\n generation,\n sentUpdateIds,\n wasRemoteProbe,\n ),\n );\n}\n\nasync function revalidateRemoteDocumentBeforeAttachments(\n state: DocumentStoreState,\n currentDoc: DocumentState,\n nextRecord: DocumentRecord,\n encapsulationKeyPair: EncapsulationKeyPair,\n generation: DocumentStoreSyncGeneration,\n): Promise<{ canSyncAttachments: boolean; nextRecord: DocumentRecord }> {\n if (state.pendingAttachments.length === 0 || !nextRecord.documentId) {\n return { canSyncAttachments: true, nextRecord };\n }\n\n const consumedRemoteUpdateSignalSeq = state.remoteUpdateSignalSeq;\n const wasRemoteProbe = state.remoteUpdatePending;\n const syncAttempt = await requestRemoteDocumentSync({\n state,\n currentDoc,\n currentRecord: nextRecord,\n encapsulationKeyPair,\n generation,\n pendingUpdates: [],\n unavailableWriterLogMessage:\n \"Documents: skipped pre-attachment revalidation because the writer context is unavailable.\",\n });\n if (!isDocumentStoreSyncGenerationCurrent(state, generation)) {\n requestDocumentStoreSync(state);\n return { canSyncAttachments: false, nextRecord };\n }\n if (!syncAttempt) {\n logUnavailable(state, wasRemoteProbe);\n return { canSyncAttachments: false, nextRecord };\n }\n\n const refreshedRecord = await finalizeDocumentSync(\n state,\n currentDoc,\n nextRecord,\n syncAttempt,\n consumedRemoteUpdateSignalSeq,\n generation,\n [],\n wasRemoteProbe,\n );\n return {\n canSyncAttachments:\n !syncAttempt.synced.hasIncompletePull &&\n refreshedRecord.pullContinuation == null &&\n refreshedRecord.pullContinuationRecoveryRequired !== true &&\n isDocumentStoreSyncGenerationCurrent(state, generation),\n nextRecord: refreshedRecord,\n };\n}\n\nasync function runDocumentSyncPass(\n state: DocumentStoreState,\n syncLaneGeneration: DocumentStoreSyncLaneGeneration,\n) {\n const currentDoc = state.doc;\n const encapsulationKeyPair = state.runtime.crypto.encapsulationKeyPair;\n let nextRecord = state.record;\n\n if (!currentDoc || !nextRecord || !encapsulationKeyPair) {\n return;\n }\n\n const generation = captureDocumentStoreRemoteSyncGeneration(\n state,\n currentDoc,\n syncLaneGeneration,\n );\n const attachmentGeneration = captureDocumentStoreAttachmentSyncGeneration(\n state,\n currentDoc,\n syncLaneGeneration,\n );\n if (!generation || !attachmentGeneration) {\n requestDocumentStoreSync(state);\n return;\n }\n\n const revalidation = await revalidateRemoteDocumentBeforeAttachments(\n state,\n currentDoc,\n nextRecord,\n encapsulationKeyPair,\n generation,\n );\n nextRecord = revalidation.nextRecord;\n if (!revalidation.canSyncAttachments) {\n return;\n }\n\n const attachmentResult = await syncPendingAttachments(\n state,\n nextRecord,\n encapsulationKeyPair,\n attachmentGeneration,\n );\n nextRecord = attachmentResult.nextRecord;\n if (state.pendingAttachments.length > 0) {\n return;\n }\n // A settled bind and the content update carrying its digest are two server\n // writes. Push the content in this same pass rather than re-arming a later\n // one, so a device lost between them is the only remaining window: peers\n // otherwise see the old digest against the new binding.\n\n nextRecord = await syncDocumentState(\n state,\n currentDoc,\n nextRecord,\n encapsulationKeyPair,\n generation,\n );\n\n if (!isDocumentStoreSyncGenerationCurrent(state, generation)) {\n requestDocumentStoreSync(state);\n return;\n }\n\n if (!state.doc || !state.record) return;\n\n const pendingUpdates = await listPendingUpdates(state);\n if (!isDocumentStoreSyncGenerationCurrent(state, generation)) {\n requestDocumentStoreSync(state);\n return;\n }\n if (!canSyncDetachedAttachmentBindings(nextRecord, pendingUpdates.length)) {\n return;\n }\n\n await syncDetachedAttachmentBindings(state, nextRecord, generation);\n if (!isDocumentStoreSyncGenerationCurrent(state, generation)) {\n requestDocumentStoreSync(state);\n }\n}\n\nasync function runScheduledSyncIteration(\n state: DocumentStoreState,\n syncLaneGeneration: DocumentStoreSyncLaneGeneration,\n) {\n if (!(await awaitInitializationForSync(state))) {\n return false;\n }\n if (!isDocumentStoreSyncLaneGenerationCurrent(state, syncLaneGeneration)) {\n return true;\n }\n\n if (!canRunScheduledSync(state)) {\n return true;\n }\n\n try {\n await runDocumentSyncPass(state, syncLaneGeneration);\n return true;\n } catch (error) {\n if (isDatabaseUnavailableError(error)) {\n return false;\n }\n\n await reportAndRethrowKeyingVerificationError(\n error,\n state.runtime.util.reportSecurityIncident,\n {\n objectId: state.record?.documentId ?? state.localId,\n objectKind: \"document\",\n operation: \"document.sync\",\n },\n );\n\n throw error;\n }\n}\n\nasync function runScheduledSyncLoop(\n state: DocumentStoreState,\n syncLaneGeneration: DocumentStoreSyncLaneGeneration,\n) {\n const syncingGeneration = captureDocumentStoreSyncGeneration(\n state,\n state.doc,\n );\n setDocumentSyncing(state, true);\n\n try {\n await runScheduledSyncIteration(state, syncLaneGeneration);\n } finally {\n if (\n syncingGeneration &&\n isDocumentStoreSyncGenerationCurrent(state, syncingGeneration) &&\n isDocumentStoreSyncLaneGenerationCurrent(state, syncLaneGeneration)\n ) {\n setDocumentSyncing(state, false);\n }\n }\n}\n\nexport function registerDocumentStoreSyncLane(\n state: DocumentStoreState,\n getSyncLaneGeneration: () => DocumentStoreSyncLaneGeneration | null,\n): DocumentSyncLane {\n // Retiring the store (its document removed under it) ends this lane's\n // current and in-flight work; a replacement store registers its own.\n onDocumentStoreRetired(state.runtime.state.domainScope, state.localId, () =>\n invalidateDocumentStoreSyncLane(state),\n );\n return registerDocumentSyncLane({\n domainScope: state.runtime.state.domainScope,\n localId: state.localId,\n priority: () =>\n state.remoteUpdatePending || state.record?.snapshotEndVersion === \"\"\n ? -1\n : 0,\n logError: (message, error) => {\n // Read the runtime lazily: a store's runtime is replaced in place, and a\n // reference captured here would report into a torn-down host.\n const { log, logError } = state.runtime.util;\n // Return the host's result: both callbacks are declared `=> void` but may\n // be async, and the lane reporter only catches a rejection it is handed.\n return logError ? logError(message, error) : log(message);\n },\n run: () => {\n const syncLaneGeneration = getSyncLaneGeneration();\n if (!syncLaneGeneration) return Promise.resolve();\n const activeWork = getActiveDocumentRemoteWork(state);\n if (activeWork) {\n // Re-arm on settlement, not immediately: immediate re-requests would\n // spin this lane and starve other work while the HTTP request is held.\n void activeWork.then(() => {\n if (\n isDocumentStoreSyncLaneGenerationCurrent(state, syncLaneGeneration)\n ) {\n requestDocumentStoreSync(state);\n }\n });\n return Promise.resolve();\n }\n return runDocumentRemoteWork(state, () =>\n isDocumentStoreSyncLaneGenerationCurrent(state, syncLaneGeneration)\n ? runScheduledSyncLoop(state, syncLaneGeneration)\n : Promise.resolve(),\n );\n },\n });\n}\n"]}
@@ -1,6 +1,15 @@
1
1
  import type { DomainScope } from "../../data/shared/domainScope.js";
2
2
  import type { DocumentStore, DocumentStoreFacade, PersistedDocumentListener } from "./types.js";
3
3
  interface DocumentStoreRegistry {
4
+ /** How the last open built each store's backing store, by store key. */
5
+ freshStoreFactories: Map<string, () => DocumentStore>;
6
+ /** Each backing store's way to stop its sync lane, by local id. */
7
+ retireHandlersByLocalId: Map<string, () => void>;
8
+ /**
9
+ * Stores whose document was removed under them, each with whether its
10
+ * removal still holds; see retireDocumentStore.
11
+ */
12
+ retiredStores: Map<string, () => boolean>;
4
13
  storeKeysByDocumentId: Map<string, string>;
5
14
  storeKeysByLocalId: Map<string, string>;
6
15
  storesByKey: Map<string, DocumentStoreFacade>;
@@ -10,6 +19,11 @@ export declare function getOrCreateDocumentStoreRegistry(domainScope: DomainScop
10
19
  export declare function resolveDocumentStoreKey(registry: DocumentStoreRegistry, localId: string, documentId: string | null): string;
11
20
  export declare function registerDocumentStore(domainScope: DomainScope, localId: string, store: DocumentStoreFacade, documentId: string | null): void;
12
21
  export declare function registerDocumentStoreIdentity(domainScope: DomainScope, localId: string, documentId: string | null): void;
22
+ /**
23
+ * Whether an open store holds local writes for the document that have not
24
+ * reached SQLite yet, which no persisted-write scan can see.
25
+ */
26
+ export declare function hasQueuedDocumentStoreWrites(domainScope: DomainScope, localId: string): boolean;
13
27
  export declare function requestDocumentStoreSync(state: {
14
28
  syncLane: {
15
29
  requestSync: () => void;
@@ -40,4 +54,22 @@ export declare function subscribeToPersistedDocuments(domainScope: DomainScope,
40
54
  export declare function emitPersistedDocumentDeletion(domainScope: DomainScope, localId: string): void;
41
55
  export declare function subscribeToPersistedDocumentDeletions(domainScope: DomainScope, listener: PersistedDocumentDeletionListener): () => void;
42
56
  export declare function requestDomainDocumentSync(domainScope: DomainScope): void;
57
+ /** Lets the backing store for `localId` stop its sync lane when retired. */
58
+ export declare function onDocumentStoreRetired(domainScope: DomainScope, localId: string, stopSyncLane: () => void): void;
59
+ /**
60
+ * Retires the store of a document a paused organization's cleanup removed.
61
+ * Its backing store's sync lane stops and the facade an editor may hold
62
+ * switches to a read-only stand-in, so nothing recreates or downloads the
63
+ * document. While `stillRemoved()` holds, reopening returns the stand-in;
64
+ * after that, the next open rebinds the facade to a fresh backing store that
65
+ * loads whatever SQLite holds by then, such as a copy rediscovered on resume.
66
+ */
67
+ export declare function retireDocumentStore(domainScope: DomainScope, localId: string, stillRemoved: () => boolean): void;
68
+ /**
69
+ * Undoes a retirement whose deletion did not commit: the document survives,
70
+ * so its facade gets a fresh backing store again.
71
+ */
72
+ export declare function restoreDocumentStore(domainScope: DomainScope, localId: string): void;
73
+ /** The registered store for a key, rebound once its removal has lifted. */
74
+ export declare function takeRegisteredDocumentStore(registry: DocumentStoreRegistry, storeKey: string, createFresh: () => DocumentStore): DocumentStoreFacade | undefined;
43
75
  export {};
@@ -1,4 +1,5 @@
1
1
  import { registerInternalDocumentRelink, relinkDocumentStoreWithCommitSideEffect, } from "./documentStore/internalRelink.js";
2
+ import { createRemovedDocumentStore } from "./removedDocumentStore.js";
2
3
  const documentStoreRegistriesByScope = new WeakMap();
3
4
  // Per-scope listener plumbing shared by the persisted-document and
4
5
  // persisted-document-deletion channels: emit fans out to the scope's
@@ -36,6 +37,9 @@ export function getOrCreateDocumentStoreRegistry(domainScope) {
36
37
  return existingRegistry;
37
38
  }
38
39
  const nextRegistry = {
40
+ freshStoreFactories: new Map(),
41
+ retireHandlersByLocalId: new Map(),
42
+ retiredStores: new Map(),
39
43
  storeKeysByDocumentId: new Map(),
40
44
  storeKeysByLocalId: new Map(),
41
45
  storesByKey: new Map(),
@@ -74,9 +78,27 @@ export function registerDocumentStoreIdentity(domainScope, localId, documentId)
74
78
  if (localStore) {
75
79
  documentStore?.rebindTo(localStore);
76
80
  registry.storesByKey.set(documentStoreKey, localStore);
81
+ // The store's retirement and how to rebuild it move with it.
82
+ moveKeyedEntry(registry.freshStoreFactories, localStoreKey, documentStoreKey);
83
+ moveKeyedEntry(registry.retiredStores, localStoreKey, documentStoreKey);
77
84
  }
78
85
  registry.storesByKey.delete(localStoreKey);
79
86
  }
87
+ function moveKeyedEntry(entries, fromKey, toKey) {
88
+ const value = entries.get(fromKey);
89
+ entries.delete(fromKey);
90
+ if (value !== undefined)
91
+ entries.set(toKey, value);
92
+ }
93
+ /**
94
+ * Whether an open store holds local writes for the document that have not
95
+ * reached SQLite yet, which no persisted-write scan can see.
96
+ */
97
+ export function hasQueuedDocumentStoreWrites(domainScope, localId) {
98
+ const registry = documentStoreRegistriesByScope.get(domainScope);
99
+ const storeKey = registry?.storeKeysByLocalId.get(localId) ?? localId;
100
+ return registry?.storesByKey.get(storeKey)?.hasQueuedWrites() ?? false;
101
+ }
80
102
  export function requestDocumentStoreSync(state) {
81
103
  state.syncLane?.requestSync();
82
104
  }
@@ -85,8 +107,9 @@ export function requestRegisteredDocumentRemoteSync(domainScope, localId, docume
85
107
  if (!registry) {
86
108
  return false;
87
109
  }
88
- const store = registry.storesByKey.get(resolveDocumentStoreKey(registry, localId, documentId));
89
- if (!store) {
110
+ const storeKey = resolveDocumentStoreKey(registry, localId, documentId);
111
+ const store = registry.storesByKey.get(storeKey);
112
+ if (!store || registry.retiredStores.has(storeKey)) {
90
113
  return false;
91
114
  }
92
115
  // Explicit refresh uses this path to recover missed websocket invalidations
@@ -158,6 +181,7 @@ export function createDocumentStoreFacade(initialStore) {
158
181
  discardLocalState: (expectedDocumentId) => targetStore.discardLocalState(expectedDocumentId),
159
182
  ensureInitialized: () => targetStore.ensureInitialized(),
160
183
  getSnapshot: () => targetStore.getSnapshot(),
184
+ hasQueuedWrites: () => targetStore.hasQueuedWrites(),
161
185
  removeAttachment: (slotId) => targetStore.removeAttachment(slotId),
162
186
  removeRow: (id) => targetStore.removeRow(id),
163
187
  replaceAttachment: (slotId, file) => targetStore.replaceAttachment(slotId, file),
@@ -199,8 +223,59 @@ export function requestDomainDocumentSync(domainScope) {
199
223
  if (!registry) {
200
224
  return;
201
225
  }
202
- for (const store of new Set(registry.storesByKey.values())) {
203
- store.requestSync();
226
+ for (const [storeKey, store] of registry.storesByKey) {
227
+ if (!registry.retiredStores.has(storeKey))
228
+ store.requestSync();
229
+ }
230
+ }
231
+ /** Lets the backing store for `localId` stop its sync lane when retired. */
232
+ export function onDocumentStoreRetired(domainScope, localId, stopSyncLane) {
233
+ getOrCreateDocumentStoreRegistry(domainScope).retireHandlersByLocalId.set(localId, stopSyncLane);
234
+ }
235
+ /**
236
+ * Retires the store of a document a paused organization's cleanup removed.
237
+ * Its backing store's sync lane stops and the facade an editor may hold
238
+ * switches to a read-only stand-in, so nothing recreates or downloads the
239
+ * document. While `stillRemoved()` holds, reopening returns the stand-in;
240
+ * after that, the next open rebinds the facade to a fresh backing store that
241
+ * loads whatever SQLite holds by then, such as a copy rediscovered on resume.
242
+ */
243
+ export function retireDocumentStore(domainScope, localId, stillRemoved) {
244
+ const registry = documentStoreRegistriesByScope.get(domainScope);
245
+ const storeKey = registry?.storeKeysByLocalId.get(localId) ?? localId;
246
+ const store = registry?.storesByKey.get(storeKey);
247
+ if (!registry || !store || registry.retiredStores.has(storeKey))
248
+ return;
249
+ registry.retireHandlersByLocalId.get(localId)?.();
250
+ store.rebindTo(createRemovedDocumentStore(store.getSnapshot()));
251
+ registry.retiredStores.set(storeKey, stillRemoved);
252
+ }
253
+ /**
254
+ * Undoes a retirement whose deletion did not commit: the document survives,
255
+ * so its facade gets a fresh backing store again.
256
+ */
257
+ export function restoreDocumentStore(domainScope, localId) {
258
+ const registry = documentStoreRegistriesByScope.get(domainScope);
259
+ const storeKey = registry?.storeKeysByLocalId.get(localId) ?? localId;
260
+ const store = registry?.storesByKey.get(storeKey);
261
+ if (!registry || !store || !registry.retiredStores.delete(storeKey))
262
+ return;
263
+ const createFresh = registry.freshStoreFactories.get(storeKey);
264
+ // Without a factory the next open rebinds it instead.
265
+ if (createFresh)
266
+ store.rebindTo(createFresh());
267
+ else
268
+ registry.retiredStores.set(storeKey, () => false);
269
+ }
270
+ /** The registered store for a key, rebound once its removal has lifted. */
271
+ export function takeRegisteredDocumentStore(registry, storeKey, createFresh) {
272
+ registry.freshStoreFactories.set(storeKey, createFresh);
273
+ const store = registry.storesByKey.get(storeKey);
274
+ const stillRemoved = registry.retiredStores.get(storeKey);
275
+ if (store && stillRemoved && !stillRemoved()) {
276
+ registry.retiredStores.delete(storeKey);
277
+ store.rebindTo(createFresh());
204
278
  }
279
+ return store;
205
280
  }
206
281
  //# sourceMappingURL=registry.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"registry.js","sourceRoot":"","sources":["registry.ts"],"names":[],"mappings":"AACA,OAAO,EACL,8BAA8B,EAC9B,uCAAuC,GACxC,MAAM,gCAAgC,CAAC;AAexC,MAAM,8BAA8B,GAAG,IAAI,OAAO,EAG/C,CAAC;AAGJ,mEAAmE;AACnE,qEAAqE;AACrE,wEAAwE;AACxE,SAAS,4BAA4B;IAGnC,MAAM,gBAAgB,GAAG,IAAI,OAAO,EAA8B,CAAC;IAEnE,OAAO;QACL,IAA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AE,QAAQ,CAAC,CAAC;AACrE,CAAC;AAED,MAAM,UAAU,6BAA6B,CAC3C,WAAwB,EACxB,OAAe;IAEf,kCAAkC,CAAC,IAAI,CAAC,WAAW,EAAE,OAAO,CAAC,CAAC;AAChE,CAAC;AAED,MAAM,UAAU,qCAAqC,CACnD,WAAwB,EACxB,QAA2C;IAE3C,OAAO,kCAAkC,CAAC,SAAS,CAAC,WAAW,EAAE,QAAQ,CAAC,CAAC;AAC7E,CAAC;AAED,MAAM,UAAU,yBAAyB,CAAC,WAAwB;IAChE,MAAM,QAAQ,GAAG,8BAA8B,CAAC,GAAG,CAAC,WAAW,CAAC,CAAC;IACjE,IAAI,CAAC,QAAQ,EAAE,CAAC;QACd,OAAO;IACT,CAAC;IAED,KAAK,MAAM,KAAK,IAAI,IAAI,GAAG,CAAC,QAAQ,CAAC,WAAW,CAAC,MAAM,EAAE,CAAC,EAAE,CAAC;QAC3D,KAAK,CAAC,WAAW,EAAE,CAAC;IACtB,CAAC;AACH,CAAC","sourcesContent":["import type { DomainScope } from \"../../data/shared/domainScope\";\nimport {\n registerInternalDocumentRelink,\n relinkDocumentStoreWithCommitSideEffect,\n} from \"./documentStore/internalRelink\";\nimport type {\n DocumentAttachmentUpload,\n DocumentStore,\n DocumentStoreFacade,\n DocumentsRuntime,\n PersistedDocumentListener,\n} from \"./types\";\n\ninterface DocumentStoreRegistry {\n storeKeysByDocumentId: Map<string, string>;\n storeKeysByLocalId: Map<string, string>;\n storesByKey: Map<string, DocumentStoreFacade>;\n}\n\nconst documentStoreRegistriesByScope = new WeakMap<\n DomainScope,\n DocumentStoreRegistry\n>();\ntype PersistedDocumentDeletionListener = (localId: string) => void;\n\n// Per-scope listener plumbing shared by the persisted-document and\n// persisted-document-deletion channels: emit fans out to the scope's\n// listeners, and unsubscribing the last listener drops the scope entry.\nfunction createScopedListenerRegistry<\n Listener extends (...args: never[]) => void,\n>() {\n const listenersByScope = new WeakMap<DomainScope, Set<Listener>>();\n\n return {\n emit: (domainScope: DomainScope, ...args: Parameters<Listener>): void => {\n const listeners = listenersByScope.get(domainScope);\n if (!listeners) {\n return;\n }\n\n for (const listener of listeners) {\n listener(...args);\n }\n },\n subscribe: (domainScope: DomainScope, listener: Listener): (() => void) => {\n const listeners =\n listenersByScope.get(domainScope) ?? new Set<Listener>();\n listeners.add(listener);\n listenersByScope.set(domainScope, listeners);\n\n return () => {\n listeners.delete(listener);\n if (listeners.size === 0) {\n listenersByScope.delete(domainScope);\n }\n };\n },\n };\n}\n\nconst persistedDocumentListeners =\n createScopedListenerRegistry<PersistedDocumentListener>();\nconst persistedDocumentDeletionListeners =\n createScopedListenerRegistry<PersistedDocumentDeletionListener>();\n\nexport function getOrCreateDocumentStoreRegistry(\n domainScope: DomainScope,\n): DocumentStoreRegistry {\n const existingRegistry = documentStoreRegistriesByScope.get(domainScope);\n if (existingRegistry) {\n return existingRegistry;\n }\n\n const nextRegistry: DocumentStoreRegistry = {\n storeKeysByDocumentId: new Map(),\n storeKeysByLocalId: new Map(),\n storesByKey: new Map(),\n };\n documentStoreRegistriesByScope.set(domainScope, nextRegistry);\n return nextRegistry;\n}\n\nexport function resolveDocumentStoreKey(\n registry: DocumentStoreRegistry,\n localId: string,\n documentId: string | null,\n): string {\n return (\n (documentId ? registry.storeKeysByDocumentId.get(documentId) : undefined) ??\n registry.storeKeysByLocalId.get(localId) ??\n localId\n );\n}\n\nexport function registerDocumentStore(\n domainScope: DomainScope,\n localId: string,\n store: DocumentStoreFacade,\n documentId: string | null,\n) {\n const registry = getOrCreateDocumentStoreRegistry(domainScope);\n const storeKey = resolveDocumentStoreKey(registry, localId, documentId);\n registry.storeKeysByLocalId.set(localId, storeKey);\n if (documentId) {\n registry.storeKeysByDocumentId.set(documentId, storeKey);\n }\n registry.storesByKey.set(storeKey, store);\n}\n\nexport function registerDocumentStoreIdentity(\n domainScope: DomainScope,\n localId: string,\n documentId: string | null,\n) {\n if (!documentId) {\n return;\n }\n\n const registry = getOrCreateDocumentStoreRegistry(domainScope);\n const localStoreKey = registry.storeKeysByLocalId.get(localId) ?? localId;\n const documentStoreKey =\n registry.storeKeysByDocumentId.get(documentId) ?? documentId;\n\n registry.storeKeysByLocalId.set(localId, documentStoreKey);\n registry.storeKeysByDocumentId.set(documentId, documentStoreKey);\n\n if (documentStoreKey === localStoreKey) {\n return;\n }\n\n const localStore = registry.storesByKey.get(localStoreKey);\n const documentStore = registry.storesByKey.get(documentStoreKey);\n if (localStore) {\n documentStore?.rebindTo(localStore);\n registry.storesByKey.set(documentStoreKey, localStore);\n }\n registry.storesByKey.delete(localStoreKey);\n}\n\nexport function requestDocumentStoreSync(state: {\n syncLane: {\n requestSync: () => void;\n } | null;\n}) {\n state.syncLane?.requestSync();\n}\n\nexport function requestRegisteredDocumentRemoteSync(\n domainScope: DomainScope,\n localId: string,\n documentId: string | null,\n): boolean {\n const registry = documentStoreRegistriesByScope.get(domainScope);\n if (!registry) {\n return false;\n }\n\n const store = registry.storesByKey.get(\n resolveDocumentStoreKey(registry, localId, documentId),\n );\n if (!store) {\n return false;\n }\n\n // Explicit refresh uses this path to recover missed websocket invalidations\n // without instantiating every unopened document discovered in the container.\n store.requestRemoteSync();\n return true;\n}\n\n/**\n * Discard a registered document's local edits through its store, so the\n * teardown serializes on the store's write machinery instead of racing an\n * in-flight persist that would resurrect the deleted rows. Only registered\n * stores qualify — an unregistered document has no lane submitting its queue,\n * so there is nothing this escape hatch could unstick. No deletion is\n * emitted: the document survives as a re-seeded shell that keeps its listing\n * entry, and the re-pull's own persist announces the restored content.\n *\n * Resolution is strict, unlike the read-side lookups: a destructive action\n * must never run against whichever store won a lookup priority. Both\n * identifiers must map to the SAME registered store — an unknown or stale\n * documentId refuses rather than trusting the localId alone — and the store\n * revalidates the expected documentId against its persisted record inside\n * the teardown's serialized mutation, so even a stale registry mapping\n * cannot discard a different identity's edits.\n */\nexport async function discardRegisteredDocumentLocalState(\n domainScope: DomainScope,\n localId: string,\n documentId: string,\n): Promise<boolean> {\n const registry = documentStoreRegistriesByScope.get(domainScope);\n if (!registry) {\n return false;\n }\n\n const localStoreKey = registry.storeKeysByLocalId.get(localId);\n const documentStoreKey = registry.storeKeysByDocumentId.get(documentId);\n if (\n !localStoreKey ||\n !documentStoreKey ||\n documentStoreKey !== localStoreKey\n ) {\n return false;\n }\n const store = registry.storesByKey.get(localStoreKey);\n if (!store) {\n return false;\n }\n\n return store.discardLocalState(documentId);\n}\n\nexport function createDocumentStoreFacade(\n initialStore: DocumentStore,\n): DocumentStoreFacade {\n let targetStore = initialStore;\n const listeners = new Set<() => void>();\n\n const emitFacade = () => {\n for (const listener of listeners) {\n listener();\n }\n };\n\n let unsubscribeTarget = targetStore.subscribe(() => {\n emitFacade();\n });\n\n const connectTarget = (nextStore: DocumentStore) => {\n if (targetStore === nextStore) {\n return;\n }\n\n unsubscribeTarget();\n targetStore = nextStore;\n unsubscribeTarget = targetStore.subscribe(() => {\n emitFacade();\n });\n emitFacade();\n };\n\n const facade: DocumentStoreFacade = {\n addRow: (fields) => targetStore.addRow(fields),\n assertCanRotateContentKey: () => targetStore.assertCanRotateContentKey(),\n attachFiles: (files: ReadonlyArray<DocumentAttachmentUpload>) =>\n targetStore.attachFiles(files),\n discardLocalState: (expectedDocumentId: string) =>\n targetStore.discardLocalState(expectedDocumentId),\n ensureInitialized: () => targetStore.ensureInitialized(),\n getSnapshot: () => targetStore.getSnapshot(),\n removeAttachment: (slotId: string) => targetStore.removeAttachment(slotId),\n removeRow: (id) => targetStore.removeRow(id),\n replaceAttachment: (slotId: string, file: DocumentAttachmentUpload) =>\n targetStore.replaceAttachment(slotId, file),\n requestRemoteSync: () => targetStore.requestRemoteSync(),\n requestRemoteSyncAndWait: (signal) =>\n targetStore.requestRemoteSyncAndWait(signal),\n requestSync: () => targetStore.requestSync(),\n relink: (input) => targetStore.relink(input),\n rebindTo: (store) => connectTarget(store),\n setStructuredFields: (kind, patch, options) =>\n targetStore.setStructuredFields(kind, patch, options),\n setText: (value: string) => targetStore.setText(value),\n subscribe: (listener: () => void) => {\n listeners.add(listener);\n return () => {\n listeners.delete(listener);\n };\n },\n updateRowFields: (id, patch) => targetStore.updateRowFields(id, patch),\n updateRuntime: (runtime: DocumentsRuntime) =>\n targetStore.updateRuntime(runtime),\n };\n registerInternalDocumentRelink(facade, (input, commitSideEffect) =>\n relinkDocumentStoreWithCommitSideEffect(\n targetStore,\n input,\n commitSideEffect,\n ),\n );\n return facade;\n}\n\nexport function emitPersistedDocument(\n domainScope: DomainScope,\n persistedDocument: Parameters<PersistedDocumentListener>[0],\n placementChanged = false,\n): void {\n persistedDocumentListeners.emit(domainScope, persistedDocument, {\n placementChanged,\n });\n}\n\nexport function subscribeToPersistedDocuments(\n domainScope: DomainScope,\n listener: PersistedDocumentListener,\n): () => void {\n return persistedDocumentListeners.subscribe(domainScope, listener);\n}\n\nexport function emitPersistedDocumentDeletion(\n domainScope: DomainScope,\n localId: string,\n): void {\n persistedDocumentDeletionListeners.emit(domainScope, localId);\n}\n\nexport function subscribeToPersistedDocumentDeletions(\n domainScope: DomainScope,\n listener: PersistedDocumentDeletionListener,\n): () => void {\n return persistedDocumentDeletionListeners.subscribe(domainScope, listener);\n}\n\nexport function requestDomainDocumentSync(domainScope: DomainScope): void {\n const registry = documentStoreRegistriesByScope.get(domainScope);\n if (!registry) {\n return;\n }\n\n for (const store of new Set(registry.storesByKey.values())) {\n store.requestSync();\n }\n}\n"]}
1
+ {"version":3,"file":"registry.js","sourceRoot":"","sources":["registry.ts"],"names":[],"mappings":"AACA,OAAO,EACL,8BAA8B,EAC9B,uCAAuC,GACxC,MAAM,gCAAgC,CAAC;AACxC,OAAO,EAAE,0BAA0B,EAAE,MAAM,wBAAwB,CAAC;AAwBpE,MAAM,8BAA8B,GAAG,IAAI,OAAO,EAG/C,CAAC;AAGJ,mEAAmE;AACnE,qEAAqE;AACrE,wEAAwE;AACxE,SAAS,4BAA4B;IAGnC,MAAM,gBAAgB,GAAG,IAAI,OAAO,EAA8B,CAAC;IAEnE,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AAE,EAAE,CAAC;QAC7C,QAAQ,CAAC,aAAa,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;QACxC,KAAK,CAAC,QAAQ,CAAC,WAAW,EAAE,CAAC,CAAC;IAChC,CAAC;IACD,OAAO,KAAK,CAAC;AACf,CAAC","sourcesContent":["import type { DomainScope } from \"../../data/shared/domainScope\";\nimport {\n registerInternalDocumentRelink,\n relinkDocumentStoreWithCommitSideEffect,\n} from \"./documentStore/internalRelink\";\nimport { createRemovedDocumentStore } from \"./removedDocumentStore\";\nimport type {\n DocumentAttachmentUpload,\n DocumentStore,\n DocumentStoreFacade,\n DocumentsRuntime,\n PersistedDocumentListener,\n} from \"./types\";\n\ninterface DocumentStoreRegistry {\n /** How the last open built each store's backing store, by store key. */\n freshStoreFactories: Map<string, () => DocumentStore>;\n /** Each backing store's way to stop its sync lane, by local id. */\n retireHandlersByLocalId: Map<string, () => void>;\n /**\n * Stores whose document was removed under them, each with whether its\n * removal still holds; see retireDocumentStore.\n */\n retiredStores: Map<string, () => boolean>;\n storeKeysByDocumentId: Map<string, string>;\n storeKeysByLocalId: Map<string, string>;\n storesByKey: Map<string, DocumentStoreFacade>;\n}\n\nconst documentStoreRegistriesByScope = new WeakMap<\n DomainScope,\n DocumentStoreRegistry\n>();\ntype PersistedDocumentDeletionListener = (localId: string) => void;\n\n// Per-scope listener plumbing shared by the persisted-document and\n// persisted-document-deletion channels: emit fans out to the scope's\n// listeners, and unsubscribing the last listener drops the scope entry.\nfunction createScopedListenerRegistry<\n Listener extends (...args: never[]) => void,\n>() {\n const listenersByScope = new WeakMap<DomainScope, Set<Listener>>();\n\n return {\n emit: (domainScope: DomainScope, ...args: Parameters<Listener>): void => {\n const listeners = listenersByScope.get(domainScope);\n if (!listeners) {\n return;\n }\n\n for (const listener of listeners) {\n listener(...args);\n }\n },\n subscribe: (domainScope: DomainScope, listener: Listener): (() => void) => {\n const listeners =\n listenersByScope.get(domainScope) ?? new Set<Listener>();\n listeners.add(listener);\n listenersByScope.set(domainScope, listeners);\n\n return () => {\n listeners.delete(listener);\n if (listeners.size === 0) {\n listenersByScope.delete(domainScope);\n }\n };\n },\n };\n}\n\nconst persistedDocumentListeners =\n createScopedListenerRegistry<PersistedDocumentListener>();\nconst persistedDocumentDeletionListeners =\n createScopedListenerRegistry<PersistedDocumentDeletionListener>();\n\nexport function getOrCreateDocumentStoreRegistry(\n domainScope: DomainScope,\n): DocumentStoreRegistry {\n const existingRegistry = documentStoreRegistriesByScope.get(domainScope);\n if (existingRegistry) {\n return existingRegistry;\n }\n\n const nextRegistry: DocumentStoreRegistry = {\n freshStoreFactories: new Map(),\n retireHandlersByLocalId: new Map(),\n retiredStores: new Map(),\n storeKeysByDocumentId: new Map(),\n storeKeysByLocalId: new Map(),\n storesByKey: new Map(),\n };\n documentStoreRegistriesByScope.set(domainScope, nextRegistry);\n return nextRegistry;\n}\n\nexport function resolveDocumentStoreKey(\n registry: DocumentStoreRegistry,\n localId: string,\n documentId: string | null,\n): string {\n return (\n (documentId ? registry.storeKeysByDocumentId.get(documentId) : undefined) ??\n registry.storeKeysByLocalId.get(localId) ??\n localId\n );\n}\n\nexport function registerDocumentStore(\n domainScope: DomainScope,\n localId: string,\n store: DocumentStoreFacade,\n documentId: string | null,\n) {\n const registry = getOrCreateDocumentStoreRegistry(domainScope);\n const storeKey = resolveDocumentStoreKey(registry, localId, documentId);\n registry.storeKeysByLocalId.set(localId, storeKey);\n if (documentId) {\n registry.storeKeysByDocumentId.set(documentId, storeKey);\n }\n registry.storesByKey.set(storeKey, store);\n}\n\nexport function registerDocumentStoreIdentity(\n domainScope: DomainScope,\n localId: string,\n documentId: string | null,\n) {\n if (!documentId) {\n return;\n }\n\n const registry = getOrCreateDocumentStoreRegistry(domainScope);\n const localStoreKey = registry.storeKeysByLocalId.get(localId) ?? localId;\n const documentStoreKey =\n registry.storeKeysByDocumentId.get(documentId) ?? documentId;\n\n registry.storeKeysByLocalId.set(localId, documentStoreKey);\n registry.storeKeysByDocumentId.set(documentId, documentStoreKey);\n\n if (documentStoreKey === localStoreKey) {\n return;\n }\n\n const localStore = registry.storesByKey.get(localStoreKey);\n const documentStore = registry.storesByKey.get(documentStoreKey);\n if (localStore) {\n documentStore?.rebindTo(localStore);\n registry.storesByKey.set(documentStoreKey, localStore);\n // The store's retirement and how to rebuild it move with it.\n moveKeyedEntry(\n registry.freshStoreFactories,\n localStoreKey,\n documentStoreKey,\n );\n moveKeyedEntry(registry.retiredStores, localStoreKey, documentStoreKey);\n }\n registry.storesByKey.delete(localStoreKey);\n}\n\nfunction moveKeyedEntry<Value>(\n entries: Map<string, Value>,\n fromKey: string,\n toKey: string,\n): void {\n const value = entries.get(fromKey);\n entries.delete(fromKey);\n if (value !== undefined) entries.set(toKey, value);\n}\n\n/**\n * Whether an open store holds local writes for the document that have not\n * reached SQLite yet, which no persisted-write scan can see.\n */\nexport function hasQueuedDocumentStoreWrites(\n domainScope: DomainScope,\n localId: string,\n): boolean {\n const registry = documentStoreRegistriesByScope.get(domainScope);\n const storeKey = registry?.storeKeysByLocalId.get(localId) ?? localId;\n return registry?.storesByKey.get(storeKey)?.hasQueuedWrites() ?? false;\n}\n\nexport function requestDocumentStoreSync(state: {\n syncLane: {\n requestSync: () => void;\n } | null;\n}) {\n state.syncLane?.requestSync();\n}\n\nexport function requestRegisteredDocumentRemoteSync(\n domainScope: DomainScope,\n localId: string,\n documentId: string | null,\n): boolean {\n const registry = documentStoreRegistriesByScope.get(domainScope);\n if (!registry) {\n return false;\n }\n\n const storeKey = resolveDocumentStoreKey(registry, localId, documentId);\n const store = registry.storesByKey.get(storeKey);\n if (!store || registry.retiredStores.has(storeKey)) {\n return false;\n }\n\n // Explicit refresh uses this path to recover missed websocket invalidations\n // without instantiating every unopened document discovered in the container.\n store.requestRemoteSync();\n return true;\n}\n\n/**\n * Discard a registered document's local edits through its store, so the\n * teardown serializes on the store's write machinery instead of racing an\n * in-flight persist that would resurrect the deleted rows. Only registered\n * stores qualify — an unregistered document has no lane submitting its queue,\n * so there is nothing this escape hatch could unstick. No deletion is\n * emitted: the document survives as a re-seeded shell that keeps its listing\n * entry, and the re-pull's own persist announces the restored content.\n *\n * Resolution is strict, unlike the read-side lookups: a destructive action\n * must never run against whichever store won a lookup priority. Both\n * identifiers must map to the SAME registered store — an unknown or stale\n * documentId refuses rather than trusting the localId alone — and the store\n * revalidates the expected documentId against its persisted record inside\n * the teardown's serialized mutation, so even a stale registry mapping\n * cannot discard a different identity's edits.\n */\nexport async function discardRegisteredDocumentLocalState(\n domainScope: DomainScope,\n localId: string,\n documentId: string,\n): Promise<boolean> {\n const registry = documentStoreRegistriesByScope.get(domainScope);\n if (!registry) {\n return false;\n }\n\n const localStoreKey = registry.storeKeysByLocalId.get(localId);\n const documentStoreKey = registry.storeKeysByDocumentId.get(documentId);\n if (\n !localStoreKey ||\n !documentStoreKey ||\n documentStoreKey !== localStoreKey\n ) {\n return false;\n }\n const store = registry.storesByKey.get(localStoreKey);\n if (!store) {\n return false;\n }\n\n return store.discardLocalState(documentId);\n}\n\nexport function createDocumentStoreFacade(\n initialStore: DocumentStore,\n): DocumentStoreFacade {\n let targetStore = initialStore;\n const listeners = new Set<() => void>();\n\n const emitFacade = () => {\n for (const listener of listeners) {\n listener();\n }\n };\n\n let unsubscribeTarget = targetStore.subscribe(() => {\n emitFacade();\n });\n\n const connectTarget = (nextStore: DocumentStore) => {\n if (targetStore === nextStore) {\n return;\n }\n\n unsubscribeTarget();\n targetStore = nextStore;\n unsubscribeTarget = targetStore.subscribe(() => {\n emitFacade();\n });\n emitFacade();\n };\n\n const facade: DocumentStoreFacade = {\n addRow: (fields) => targetStore.addRow(fields),\n assertCanRotateContentKey: () => targetStore.assertCanRotateContentKey(),\n attachFiles: (files: ReadonlyArray<DocumentAttachmentUpload>) =>\n targetStore.attachFiles(files),\n discardLocalState: (expectedDocumentId: string) =>\n targetStore.discardLocalState(expectedDocumentId),\n ensureInitialized: () => targetStore.ensureInitialized(),\n getSnapshot: () => targetStore.getSnapshot(),\n hasQueuedWrites: () => targetStore.hasQueuedWrites(),\n removeAttachment: (slotId: string) => targetStore.removeAttachment(slotId),\n removeRow: (id) => targetStore.removeRow(id),\n replaceAttachment: (slotId: string, file: DocumentAttachmentUpload) =>\n targetStore.replaceAttachment(slotId, file),\n requestRemoteSync: () => targetStore.requestRemoteSync(),\n requestRemoteSyncAndWait: (signal) =>\n targetStore.requestRemoteSyncAndWait(signal),\n requestSync: () => targetStore.requestSync(),\n relink: (input) => targetStore.relink(input),\n rebindTo: (store) => connectTarget(store),\n setStructuredFields: (kind, patch, options) =>\n targetStore.setStructuredFields(kind, patch, options),\n setText: (value: string) => targetStore.setText(value),\n subscribe: (listener: () => void) => {\n listeners.add(listener);\n return () => {\n listeners.delete(listener);\n };\n },\n updateRowFields: (id, patch) => targetStore.updateRowFields(id, patch),\n updateRuntime: (runtime: DocumentsRuntime) =>\n targetStore.updateRuntime(runtime),\n };\n registerInternalDocumentRelink(facade, (input, commitSideEffect) =>\n relinkDocumentStoreWithCommitSideEffect(\n targetStore,\n input,\n commitSideEffect,\n ),\n );\n return facade;\n}\n\nexport function emitPersistedDocument(\n domainScope: DomainScope,\n persistedDocument: Parameters<PersistedDocumentListener>[0],\n placementChanged = false,\n): void {\n persistedDocumentListeners.emit(domainScope, persistedDocument, {\n placementChanged,\n });\n}\n\nexport function subscribeToPersistedDocuments(\n domainScope: DomainScope,\n listener: PersistedDocumentListener,\n): () => void {\n return persistedDocumentListeners.subscribe(domainScope, listener);\n}\n\nexport function emitPersistedDocumentDeletion(\n domainScope: DomainScope,\n localId: string,\n): void {\n persistedDocumentDeletionListeners.emit(domainScope, localId);\n}\n\nexport function subscribeToPersistedDocumentDeletions(\n domainScope: DomainScope,\n listener: PersistedDocumentDeletionListener,\n): () => void {\n return persistedDocumentDeletionListeners.subscribe(domainScope, listener);\n}\n\nexport function requestDomainDocumentSync(domainScope: DomainScope): void {\n const registry = documentStoreRegistriesByScope.get(domainScope);\n if (!registry) {\n return;\n }\n\n for (const [storeKey, store] of registry.storesByKey) {\n if (!registry.retiredStores.has(storeKey)) store.requestSync();\n }\n}\n\n/** Lets the backing store for `localId` stop its sync lane when retired. */\nexport function onDocumentStoreRetired(\n domainScope: DomainScope,\n localId: string,\n stopSyncLane: () => void,\n): void {\n getOrCreateDocumentStoreRegistry(domainScope).retireHandlersByLocalId.set(\n localId,\n stopSyncLane,\n );\n}\n\n/**\n * Retires the store of a document a paused organization's cleanup removed.\n * Its backing store's sync lane stops and the facade an editor may hold\n * switches to a read-only stand-in, so nothing recreates or downloads the\n * document. While `stillRemoved()` holds, reopening returns the stand-in;\n * after that, the next open rebinds the facade to a fresh backing store that\n * loads whatever SQLite holds by then, such as a copy rediscovered on resume.\n */\nexport function retireDocumentStore(\n domainScope: DomainScope,\n localId: string,\n stillRemoved: () => boolean,\n): void {\n const registry = documentStoreRegistriesByScope.get(domainScope);\n const storeKey = registry?.storeKeysByLocalId.get(localId) ?? localId;\n const store = registry?.storesByKey.get(storeKey);\n if (!registry || !store || registry.retiredStores.has(storeKey)) return;\n registry.retireHandlersByLocalId.get(localId)?.();\n store.rebindTo(createRemovedDocumentStore(store.getSnapshot()));\n registry.retiredStores.set(storeKey, stillRemoved);\n}\n\n/**\n * Undoes a retirement whose deletion did not commit: the document survives,\n * so its facade gets a fresh backing store again.\n */\nexport function restoreDocumentStore(\n domainScope: DomainScope,\n localId: string,\n): void {\n const registry = documentStoreRegistriesByScope.get(domainScope);\n const storeKey = registry?.storeKeysByLocalId.get(localId) ?? localId;\n const store = registry?.storesByKey.get(storeKey);\n if (!registry || !store || !registry.retiredStores.delete(storeKey)) return;\n const createFresh = registry.freshStoreFactories.get(storeKey);\n // Without a factory the next open rebinds it instead.\n if (createFresh) store.rebindTo(createFresh());\n else registry.retiredStores.set(storeKey, () => false);\n}\n\n/** The registered store for a key, rebound once its removal has lifted. */\nexport function takeRegisteredDocumentStore(\n registry: DocumentStoreRegistry,\n storeKey: string,\n createFresh: () => DocumentStore,\n): DocumentStoreFacade | undefined {\n registry.freshStoreFactories.set(storeKey, createFresh);\n const store = registry.storesByKey.get(storeKey);\n const stillRemoved = registry.retiredStores.get(storeKey);\n if (store && stillRemoved && !stillRemoved()) {\n registry.retiredStores.delete(storeKey);\n store.rebindTo(createFresh());\n }\n return store;\n}\n"]}
@@ -0,0 +1,38 @@
1
+ const REMOVED_DOCUMENT_MESSAGE = "This document was removed while its organization's sync is paused";
2
+ /**
3
+ * Stands in for a store whose document a paused organization's cleanup
4
+ * removed. It keeps showing the last snapshot, read-only, and does nothing
5
+ * else, so an open editor neither recreates the document nor downloads it
6
+ * while the pause lasts.
7
+ */
8
+ export function createRemovedDocumentStore(lastSnapshot) {
9
+ const snapshot = {
10
+ ...lastSnapshot,
11
+ canAttach: false,
12
+ canWrite: false,
13
+ syncing: false,
14
+ };
15
+ const refuse = () => Promise.reject(new Error(REMOVED_DOCUMENT_MESSAGE));
16
+ return {
17
+ addRow: refuse,
18
+ assertCanRotateContentKey: refuse,
19
+ attachFiles: () => undefined,
20
+ discardLocalState: async () => false,
21
+ ensureInitialized: async () => false,
22
+ getSnapshot: () => snapshot,
23
+ hasQueuedWrites: () => false,
24
+ removeAttachment: () => undefined,
25
+ removeRow: refuse,
26
+ replaceAttachment: refuse,
27
+ requestRemoteSync: () => undefined,
28
+ requestRemoteSyncAndWait: async () => false,
29
+ requestSync: () => undefined,
30
+ relink: async () => null,
31
+ setStructuredFields: refuse,
32
+ setText: refuse,
33
+ subscribe: () => () => undefined,
34
+ updateRowFields: refuse,
35
+ updateRuntime: () => undefined,
36
+ };
37
+ }
38
+ //# sourceMappingURL=removedDocumentStore.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"removedDocumentStore.js","sourceRoot":"","sources":["removedDocumentStore.ts"],"names":[],"mappings":"AAEA,MAAM,wBAAwB,GAC5B,mEAAmE,CAAC;AAEtE;;;;;GAKG;AACH,MAAM,UAAU,0BAA0B,CACxC,YAA8B;IAE9B,MAAM,QAAQ,GAAqB;QACjC,GAAG,YAAY;QACf,SAAS,EAAE,KAAK;QAChB,QAAQ,EAAE,KAAK;QACf,OAAO,EAAE,KAAK;KACf,CAAC;IACF,MAAM,MAAM,GAAG,GAAG,EAAE,CAAC,OAAO,CAAC,MAAM,CAAC,IAAI,KAAK,CAAC,wBAAwB,CAAC,CAAC,CAAC;IACzE,OAAO;QACL,MAAM,EAAE,MAAM;QACd,yBAAyB,EAAE,MAAM;QACjC,WAAW,EAAE,GAAG,EAAE,CAAC,SAAS;QAC5B,iBAAiB,EAAE,KAAK,IAAI,EAAE,CAAC,KAAK;QACpC,iBAAiB,EAAE,KAAK,IAAI,EAAE,CAAC,KAAK;QACpC,WAAW,EAAE,GAAG,EAAE,CAAC,QAAQ;QAC3B,eAAe,EAAE,GAAG,EAAE,CAAC,KAAK;QAC5B,gBAAgB,EAAE,GAAG,EAAE,CAAC,SAAS;QACjC,SAAS,EAAE,MAAM;QACjB,iBAAiB,EAAE,MAAM;QACzB,iBAAiB,EAAE,GAAG,EAAE,CAAC,SAAS;QAClC,wBAAwB,EAAE,KAAK,IAAI,EAAE,CAAC,KAAK;QAC3C,WAAW,EAAE,GAAG,EAAE,CAAC,SAAS;QAC5B,MAAM,EAAE,KAAK,IAAI,EAAE,CAAC,IAAI;QACxB,mBAAmB,EAAE,MAAM;QAC3B,OAAO,EAAE,MAAM;QACf,SAAS,EAAE,GAAG,EAAE,CAAC,GAAG,EAAE,CAAC,SAAS;QAChC,eAAe,EAAE,MAAM;QACvB,aAAa,EAAE,GAAG,EAAE,CAAC,SAAS;KAC/B,CAAC;AACJ,CAAC","sourcesContent":["import type { DocumentSnapshot, DocumentStore } from \"./types\";\n\nconst REMOVED_DOCUMENT_MESSAGE =\n \"This document was removed while its organization's sync is paused\";\n\n/**\n * Stands in for a store whose document a paused organization's cleanup\n * removed. It keeps showing the last snapshot, read-only, and does nothing\n * else, so an open editor neither recreates the document nor downloads it\n * while the pause lasts.\n */\nexport function createRemovedDocumentStore(\n lastSnapshot: DocumentSnapshot,\n): DocumentStore {\n const snapshot: DocumentSnapshot = {\n ...lastSnapshot,\n canAttach: false,\n canWrite: false,\n syncing: false,\n };\n const refuse = () => Promise.reject(new Error(REMOVED_DOCUMENT_MESSAGE));\n return {\n addRow: refuse,\n assertCanRotateContentKey: refuse,\n attachFiles: () => undefined,\n discardLocalState: async () => false,\n ensureInitialized: async () => false,\n getSnapshot: () => snapshot,\n hasQueuedWrites: () => false,\n removeAttachment: () => undefined,\n removeRow: refuse,\n replaceAttachment: refuse,\n requestRemoteSync: () => undefined,\n requestRemoteSyncAndWait: async () => false,\n requestSync: () => undefined,\n relink: async () => null,\n setStructuredFields: refuse,\n setText: refuse,\n subscribe: () => () => undefined,\n updateRowFields: refuse,\n updateRuntime: () => undefined,\n };\n}\n"]}
@@ -73,6 +73,8 @@ export interface DocumentStore {
73
73
  discardLocalState: (expectedDocumentId: string) => Promise<boolean>;
74
74
  ensureInitialized: () => Promise<boolean>;
75
75
  getSnapshot: () => DocumentSnapshot;
76
+ /** Whether local writes are queued that have not reached SQLite yet. */
77
+ hasQueuedWrites: () => boolean;
76
78
  removeAttachment: (slotId: string) => void;
77
79
  removeRow: RemoveDocumentRow;
78
80
  replaceAttachment: (slotId: string, file: DocumentAttachmentUpload) => Promise<void>;