import type { ChangesetResendReason, CollaborationStatus, ConflictReason, IRemoteRevertInfo, IUnitCacheData } from '@univerjs-pro/collaboration-client'; import type { IEventBase } from '@univerjs/core/facade'; /** * Fired once when a unit enters `CollaborationStatus.CONFLICT`, after its offline cache has been cleared. Carries the * reason and the local edits the server has not acknowledged. */ export interface ICollaborationConflictEventParams extends IEventBase { /** The unit ID that entered the conflict status */ unitId: string; /** * - `transform_failed`: a remote changeset could not be transformed against the local edits * - `apply_failed`: a remote changeset was transformed but one of its mutations failed to execute * - `changeset_rejected`: the server rejected the local changeset * - `permission_rejected`: the server rejected the local changeset for lack of permission * - `remote_revert`: another collaborator reverted the unit to a historical revision */ reason: ConflictReason; /** The error message, or the reason the server gave when it rejected the changeset */ message?: string; /** The revert that caused the conflict, when `reason` is `remote_revert` */ revert?: IRemoteRevertInfo; /** * The local edits the server has not acknowledged, in the format of the offline cache, or `null` when there * are none. Not available after this event. `awaitingChangeset` has been sent; whether the server applied it is * unknown. */ localChanges: IUnitCacheData | null; /** Whether the offline cache of the unit was cleared. When `false`, a page reload replays the cached edits. */ cacheCleared: boolean; } /** * Event triggered when collaboration status changes for a unit */ export interface ICollaborationStatusChangedEventParams extends IEventBase { /** * The unit ID whose collaboration status changed */ unitId: string; /** * The new collaboration status. When it is `CONFLICT`, the `CollaborationConflict` event follows with the reason * and the unacknowledged local edits. */ status: CollaborationStatus; } /** * Event triggered every time a changeset is sent to the server again because it was not acknowledged. * The first send of a changeset does not trigger it. */ export interface ICollaborationChangesetResendEventParams extends IEventBase { /** The unit ID whose changeset was resent */ unitId: string; /** The request ID of the changeset within the current editing session */ reqId: number; /** * Why the changeset was resent: * - `ack_timeout`: no acknowledgement arrived in time * - `should_retry`: the server asked for the changeset to be resent * - `reconnect`: the connection was restored or the changeset was restored from the local cache * - `license_editing_connections_exceeded`: the server asked for a resend; its License editing connections were all in use */ reason: ChangesetResendReason; /** How many times the changeset has been resent, counting this one */ attempt: number; /** Milliseconds since the changeset was first sent */ elapsedMs: number; /** Local mutations the server has not acknowledged yet, including those not sent yet */ unsyncedMutationCount: number; /** Milliseconds until the next resend if the server stays silent */ nextDelayMs: number; } /** * Collaboration event name mixin interface * @ignore */ export interface IFCollaborationEventNameMixin { /** * Event triggered when collaboration status changes * @see {@link ICollaborationStatusChangedEventParams} * @example * ```typescript * univerAPI.addEvent(univerAPI.Event.CollaborationStatusChanged, (event) => { * const { unitId, status } = event; * console.log(`Unit ${unitId} status changed to:`, status); * * if (status === univerAPI.Enum.CollaborationStatus.SYNCED) { * console.log('All changes are synced!'); * } * }); * ``` */ readonly CollaborationStatusChanged: 'CollaborationStatusChanged'; /** * Event triggered once per conflict, after the unit's offline cache was cleared: the reason and the local edits * the server has not acknowledged * @see {@link ICollaborationConflictEventParams} * @example * ```typescript * univerAPI.addEvent(univerAPI.Event.CollaborationConflict, (event) => { * const { unitId, reason, localChanges, cacheCleared } = event; * if (localChanges) saveSomewhere(unitId, localChanges); * if (reason === 'permission_rejected') return showNoPermission(unitId); * if (reason === 'license_unit_type_rejected') return showLicenseLimit(unitId, event.message); * if (cacheCleared) window.location.reload(); * }); * ``` */ readonly CollaborationConflict: 'CollaborationConflict'; /** * Event triggered every time a changeset is resent because the server did not acknowledge it * @see {@link ICollaborationChangesetResendEventParams} * @example * ```typescript * univerAPI.addEvent(univerAPI.Event.CollaborationChangesetResend, (event) => { * const { unitId, reason, attempt, unsyncedMutationCount, elapsedMs } = event; * if (reason === 'ack_timeout' && attempt >= 2) { * console.warn(`${unsyncedMutationCount} unsaved mutations of ${unitId} for ${Math.round(elapsedMs / 1000)}s`); * } * }); * ``` */ readonly CollaborationChangesetResend: 'CollaborationChangesetResend'; } /** * Collaboration event parameter configuration * @ignore */ export interface ICollaborationEventParamConfig { CollaborationStatusChanged: ICollaborationStatusChangedEventParams; CollaborationConflict: ICollaborationConflictEventParams; CollaborationChangesetResend: ICollaborationChangesetResendEventParams; } declare module '@univerjs/core/facade' { interface FEventName extends IFCollaborationEventNameMixin { } interface IEventParamConfig extends ICollaborationEventParamConfig { } }