import type { PdfMeasure } from '../dto/Measure'; import type { PageScaleResult } from '../mutation/PageScaleResult'; import type { PdfRotation } from '../geometry/primitives'; import type { AnnotationRef } from '../identity/AnnotationRef'; import type { OutlineItemRef } from '../identity/OutlineItemRef'; import type { PageRef } from '../identity/PageRef'; import type { AnnotationCreateResult, AnnotationDeleteResult, AnnotationReorderResult, AnnotationUpdateResult, } from '../mutation/AnnotationMutationResults'; import type { AttachmentCreateResult, AttachmentDeleteResult, AttachmentRestoreResult, } from '../mutation/AttachmentMutationResults'; import type { FormFieldCreateResult, FormFieldDeleteResult, FormFieldUpdateResult, FormMutationMeta, FormRepairResult, FormSetValueResult, FormWidgetDeleteResult, FormWidgetLinkResult, FormWidgetRestoreResult, FormWidgetsReorderResult, FormCalculationsReorderResult, FormWidgetUpdateResult, } from '../mutation/FormMutationResults'; import type { AnnotationListMutationMeta } from '../mutation/AnnotationListMutationMeta'; import type { Annotation } from '../annotation/kinds'; import type { WidgetAnnotation } from '../annotation/kinds/widget'; import type { FormField } from '../forms/field'; import type { CustomMetadataUpdateResult } from '../mutation/CustomMetadataUpdateResult'; import type { MetadataUpdateResult } from '../mutation/MetadataUpdateResult'; import type { OutlineCreateResult, OutlineDeleteResult, OutlineMoveResult, OutlineRestoreResult, OutlineUpdateResult, } from '../mutation/OutlineMutationResults'; import type { AnnotationFlattenResult } from '../mutation/AnnotationFlattenResult'; import type { PageDeleteResult } from '../mutation/PageDeleteResult'; import type { PageFlattenResult, PageFlattenUsage } from '../mutation/PageFlattenResult'; import type { RedactionApplyResult } from '../mutation/RedactionApplyResult'; import type { PageInsertResult } from '../mutation/PageInsertResult'; import type { PageReorderResult } from '../mutation/PageReorderResult'; import type { PageRestoreResult } from '../mutation/PageRestoreResult'; import type { PageNameResult } from '../mutation/PageNameResult'; import type { PageRotateResult } from '../mutation/PageRotateResult'; import type { FormFieldRef } from '../identity/FormFieldRef'; import type { BaseVersionInfo, SignatureCompleteResult, SignaturePrepared, ValidationInfoAddResult, } from '../signature/types'; import type { AttachmentRef } from '../dto/Attachment'; /** * Provenance of a `DocumentEvent` — whose hand caused the mutation, never * which transport delivered it (transport is invisible by design). * * `kind: 'local'` means made through this open document (this handle), not * "this user". Every open is its own session: the same user in two tabs, or * an app that opens one cloud document twice, is two sessions, and a * mutation made through one arrives in the other as `'remote'` (with the * same `subject`). Rule of thumb for consumers: "is this my action" → check * `kind` / `sessionId` (undo stacks, optimism reconciliation); "is this my * user" → check `subject` (attribution). */ export interface EventOrigin { /** 'local' = made through this handle; 'remote' = another session. */ kind: 'local' | 'remote'; /** Identifies the session (the open document handle) that made the mutation. */ sessionId: string; /** Authenticated subject of the originator (cloud); `null` locally. */ subject: string | null; /** When the mutation was made, as an ISO 8601 string: the server's time for remote, the client's for local. */ at: string; /** Server audit-log row id — monotonic per document, the resume cursor. * `null` until the mutation has a server identity (local engines; cloud * own-mutation events before the server echoes the id). */ serverSequence: number | null; /** * Set when this fact is one of several committed together, such as the * annotations of one import: they share `id`, and `index` counts from 0 to * `count - 1` in the order they were emitted. */ transaction?: { id: string; index: number; count: number }; /** Set on the events of an undo: the `opId` of the change it undid. */ undoOf?: string; } /** * The one event stream both engines speak — the model is the contract, the * transport differs (local: in-process after the worker confirms; cloud: * in-process for your own mutations, SSE for everyone else's). * * Invariants (locked — the collaboration design rests on these): * * - exactly once: every mutation that touches your document appears in * your stream exactly once. The handle that performs a mutation emits * the event itself at confirmation time; the remote channel exists to * tell everyone else (own echoes are dropped by `sessionId`). A change * that commits several facts at once, such as an import, emits one * event per fact, back to back, sharing `origin.transaction`. * - ground truth only: events fire after the mutation is confirmed — * never optimistically. Optimism is a plugin concern. * - results ride verbatim: each event embeds the mutation result the * caller received, unmodified — which (cloud) is byte-identical to the * audit-log payload. A handler sees the same fact whether it performed * the mutation, watched it locally, or received it over the wire. * - published before settlement: the event for a session's own mutation * reaches subscribers before the mutation's promise settles, so a caller * awaiting the mutation already sees every state derived from the event. * * Handlers updating UI/document state should be origin-agnostic ("a page * was removed → update the registry"); `origin` is metadata for the few * provenance-aware features (undo, attribution toasts, camera etiquette). */ export type DocumentEvent = | ({ type: 'pages.scaleSet'; origin: EventOrigin; /** The pages, and the scale each has now. */ pages: PageRef[]; measure: PdfMeasure | null; } & PageScaleResult) | ({ type: 'annotations.created'; page: PageRef; origin: EventOrigin; } & AnnotationCreateResult) | ({ type: 'annotations.updated'; page: PageRef; origin: EventOrigin; } & AnnotationUpdateResult) | ({ type: 'annotations.deleted'; page: PageRef; origin: EventOrigin; /** * What was deleted: the annotation and what went with it (replies, * grouped parts, review states, popups), the annotation first; see * `deletedAnnotationsOf`. */ deleted: AnnotationRef[]; } & AnnotationDeleteResult) | ({ /** A stacking-order change: the page's annotations in their new order. */ type: 'annotations.reordered'; page: PageRef; origin: EventOrigin; } & AnnotationReorderResult) | { /** * An undo brought deleted annotations back: the same objects, under * the refs they had, with their owner, replies and popup. A listener * that keeps things by ref (comments, links) attaches them again. */ type: 'annotations.restored'; page: PageRef; origin: EventOrigin; /** What came back, in `/Annots` order: the annotation first, then what went with it. */ annotations: Annotation[]; meta: AnnotationListMutationMeta; } | ({ type: 'annotations.flattened'; origin: EventOrigin; } & AnnotationFlattenResult) | ({ type: 'pages.reordered'; origin: EventOrigin } & PageReorderResult) | ({ type: 'pages.rotated'; pages: PageRef[]; rotation: PdfRotation; origin: EventOrigin; } & PageRotateResult) | ({ type: 'pages.deleted'; /** The retired pages — not derivable from the surviving `layout`. */ pages: PageRef[]; origin: EventOrigin; } & PageDeleteResult) | ({ type: 'pages.inserted'; origin: EventOrigin } & PageInsertResult) /** An undo brought deleted pages back, or a redo brought inserted pages back. */ | ({ type: 'pages.restored'; origin: EventOrigin } & PageRestoreResult) | ({ type: 'pages.named'; /** The decoded key that was registered, renamed, or removed. */ name: string; /** The page it now points at; `null` when the registration was removed. */ page: PageRef | null; origin: EventOrigin; } & PageNameResult) | ({ type: 'attachments.created'; origin: EventOrigin } & AttachmentCreateResult) | ({ type: 'attachments.deleted'; origin: EventOrigin; /** What was deleted (see `deletedAttachmentOf`). */ deleted: AttachmentRef | null; } & AttachmentDeleteResult) | { /** * The fonts that can be named in the document changed: a write added or * removed one, or (on the local engine) the engine's fonts changed. Read * `doc.fonts.list()` again. */ type: 'fonts.changed'; origin: EventOrigin; } /** An undo brought a deleted file back, under the key it had. */ | ({ type: 'attachments.restored'; origin: EventOrigin } & AttachmentRestoreResult) | ({ type: 'metadata.updated'; origin: EventOrigin } & MetadataUpdateResult) | ({ type: 'metadata.customUpdated'; origin: EventOrigin } & CustomMetadataUpdateResult) | ({ type: 'outline.created'; origin: EventOrigin } & OutlineCreateResult) | ({ type: 'outline.updated'; origin: EventOrigin } & OutlineUpdateResult) | ({ type: 'outline.moved'; origin: EventOrigin } & OutlineMoveResult) | ({ type: 'outline.deleted'; origin: EventOrigin; /** What was deleted: the item first, then the items under it (see `deletedOutlineItemsOf`). */ deleted: OutlineItemRef[]; } & OutlineDeleteResult) | ({ /** * An undo brought deleted outline items back: the same items, under the * refs they had, at the place the item had among its parent's children. */ type: 'outline.restored'; origin: EventOrigin; } & OutlineRestoreResult) | ({ type: 'forms.valueSet'; origin: EventOrigin } & FormSetValueResult) | ({ type: 'forms.repaired'; origin: EventOrigin } & FormRepairResult) | ({ type: 'forms.created'; origin: EventOrigin } & FormFieldCreateResult) | ({ type: 'forms.updated'; origin: EventOrigin } & FormFieldUpdateResult) | ({ type: 'forms.deleted'; origin: EventOrigin; /** The field that went (see `deletedFieldOf`). */ deleted: FormFieldRef | null; } & FormFieldDeleteResult) | { /** * An undo brought a deleted field back: the same field and widgets, * under the refs they had, at their places, with its value. */ type: 'forms.restored'; origin: EventOrigin; field: FormField; widgets: WidgetAnnotation[]; meta: FormMutationMeta; } | ({ type: 'forms.widgetAdded'; origin: EventOrigin } & FormWidgetLinkResult) | ({ type: 'forms.widgetRemoved'; origin: EventOrigin } & FormWidgetLinkResult) | ({ type: 'forms.widgetDeleted'; origin: EventOrigin } & FormWidgetDeleteResult) | ({ /** An undo brought a deleted widget back: onto its page at its place, into its field. */ type: 'forms.widgetRestored'; origin: EventOrigin; } & FormWidgetRestoreResult) | ({ type: 'forms.widgetUpdated'; origin: EventOrigin } & FormWidgetUpdateResult) | ({ /** A stacking-order change: the page's widgets in their new order. */ type: 'forms.widgetsReordered'; origin: EventOrigin; } & FormWidgetsReorderResult) | ({ /** A calculation-order change: the form's whole new order. */ type: 'forms.calculationsReordered'; origin: EventOrigin; } & FormCalculationsReorderResult) | ({ type: 'pages.flattened'; pages: PageRef[]; usage: PageFlattenUsage; origin: EventOrigin; } & PageFlattenResult) | ({ type: 'redaction.applied'; origin: EventOrigin; } & RedactionApplyResult) | ({ /** A signing candidate was parked: the document is read-only until it completes or is cancelled. */ type: 'signatures.prepared'; field: FormFieldRef; origin: EventOrigin; } & SignaturePrepared) | ({ /** The sealed bytes are installed; `version` is what they became. */ type: 'signatures.completed'; signingId: string; origin: EventOrigin; } & SignatureCompleteResult) | { type: 'signatures.cancelled'; signingId: string; origin: EventOrigin; } | ({ /** * Validation data was stored in the document. A verdict that depends * on it must be read again. */ type: 'signatures.validationInfoAdded'; origin: EventOrigin; } & ValidationInfoAddResult) | { /** * The session moved to a new saved version (a completed signature, * here or in another session). Byte-level facts — revisions, * coverage, digests, verdicts — must be re-read. */ type: 'document.versioned'; version: BaseVersionInfo; origin: EventOrigin; } | { /** * Cloud only: the live event stream can't be followed change by * change from here. `'backlog-overflow'`: it fell too far behind to * replay. `'unknown-change'`: a change of a kind this version * doesn't know arrived, from a newer server. Either way, state * derived from earlier events or snapshots may be stale, and the * missed mutations will never arrive as events. Consumers must * re-read the snapshots they keep fresh from this stream * (`doc.annotations.list()`, `doc.forms.list()`, `doc.pages.list()`, …). * * Carries no `EventOrigin`: it is a transport notice, not a * document mutation — the exactly-once / results-ride-verbatim * invariants above apply to mutation events only. The local engine * never emits it. */ type: 'stream.desynced'; reason: 'backlog-overflow' | 'unknown-change'; /** When the stream fell behind, as an ISO 8601 string. */ at: string; }; export type DocumentEventType = DocumentEvent['type']; /** The event of one type, such as `DocumentEventOf<'annotations.created'>`. */ export type DocumentEventOf = Extract; type DistributiveOmit = T extends unknown ? Omit : never; /** A `DocumentEvent` before provenance is stamped — what mutation paths * hand to their engine's publisher, which adds `origin`. Transport * notices (`stream.desynced`) are excluded: they are published directly * by the remote channel, never through a mutation publisher. */ export type DocumentEventInit = DistributiveOmit< Exclude, 'origin' >;