{"version":3,"file":"entity-sync.mjs","names":[],"sources":["../src/entity-sync.ts"],"sourcesContent":["import warning from '@wordpress/warning';\n\n/**\n * The entity sync interface.\n *\n * core-data knows nothing about how collaborative editing works. It offers\n * one registration point (registerEntitySyncManager) where a sync manager\n * can plug into the entity lifecycle. The registered manager receives\n * lifecycle events about records as they load, edits as they happen,\n * saves before and after they go out, and  unloads. Everything else\n * (documents, transports, presence, conflict handling, undo semantics) lives\n * inside of the manager.\n *\n * This is a private API (see `private-apis.ts`). Only one manager can be\n * registered at a time.\n */\n\nexport type EntitySyncRecord = Record< string, unknown >;\n\nexport type EntitySyncRecordId = string | number;\n\n/**\n * Describes the intent behind an edit passed to `update`.\n *\n * In Gutenberg, block changes flow through two callbacks: `onInput` for\n * transient, in-progress changes (typing each character), which use\n * `isCached: true`, and `onChange` for completed changes (formatting,\n * block insertions), which use `isCached: false`. `undoIgnore: true` means\n * the change must not affect undo history at all (for example selection-only\n * changes).\n */\nexport interface EntitySyncEditOptions {\n\tisCached: boolean;\n\tundoIgnore: boolean;\n}\n\n/**\n * What a manager can ask core-data to do for one loaded record.\n */\nexport interface EntitySyncRecordHandlers {\n\t/**\n\t * Applies edits that originate from the manager (remote changes, undo)\n\t * to the entity record without recording them in the undo history.\n\t */\n\teditRecord: (\n\t\tedits: EntitySyncRecord,\n\t\toptions?: { undoIgnore?: boolean }\n\t) => void;\n\n\t/** The current record with its unsaved edits applied. */\n\tgetEditedRecord: () => Promise< EntitySyncRecord >;\n\n\t/** Refetches the record from the REST API. */\n\trefetchRecord: () => Promise< void >;\n\n\t/**\n\t * Tells core-data that a local change to the record opened a new undo\n\t * level in the manager's undo history. core-data records a placeholder\n\t * for it in its own undo manager, so the level keeps its place among\n\t * the edits core-data records itself, and delegates it back to\n\t * `undoHistory` when it is undone or redone.\n\t */\n\tonUndoLevelOpened: () => void;\n}\n\n/**\n * What a manager can ask core-data to do for a loaded collection.\n */\nexport interface EntitySyncCollectionHandlers {\n\t/** Refetches the whole collection from the REST API. */\n\trefetchRecords: () => Promise< void >;\n}\n\nexport interface EntitySyncBeforeSaveContext {\n\t/**\n\t * The record as last received from the server, or undefined when it\n\t * is not in the store.\n\t */\n\tpersistedRecord?: EntitySyncRecord;\n\tisAutosave: boolean;\n}\n\nexport interface EntitySyncAfterSaveContext {\n\t/** The raw save response. */\n\tsavedRecord: EntitySyncRecord;\n\n\t/**\n\t * The record as it was before the save, or undefined for a new record\n\t * or one that was not in the store.\n\t */\n\tpersistedRecord?: EntitySyncRecord;\n\n\t/** The edits that were sent. */\n\tedits: EntitySyncRecord;\n}\n\n/**\n * The undo history a sync manager keeps for the records it syncs, one per\n * record. core-data's own undo manager stays in charge: it holds one\n * placeholder per level reported through `onUndoLevelOpened`, naming the\n * record that opened it, in order with the edits it records itself. It calls\n * back here for that record when a placeholder is the level to move.\n */\nexport interface EntitySyncUndoHistory {\n\t/**\n\t * Undoes the most recent level of a record. Returns false when there is\n\t * none left, for example because the record was unloaded.\n\t */\n\tundo: (\n\t\tkind: string,\n\t\tname: string,\n\t\trecordId: EntitySyncRecordId\n\t) => boolean;\n\n\t/** Redoes the most recently undone level of a record. */\n\tredo: (\n\t\tkind: string,\n\t\tname: string,\n\t\trecordId: EntitySyncRecordId\n\t) => boolean;\n\n\t/**\n\t * Closes the current level of every record so the next change opens a\n\t * new one. core-data calls it when it records an edit itself, so a later\n\t * synced change cannot merge into a level that is no longer the most\n\t * recent one.\n\t *\n\t * Some managers wait a tick before they apply a local change. Managers\n\t * must apply every waiting change when this is called. core-data calls\n\t * this right before an undo or redo to ensure its history will also\n\t * include deferred changes in the operation.\n\t */\n\tstopCapturing: () => void;\n\n\t/** Drops the redo levels of every record. core-data calls it when another edit ends the redo history. */\n\tclearRedo: () => void;\n}\n\nexport interface EntitySyncManager {\n\t/**\n\t * Optional check that runs before a record is loaded. When this returns\n\t * false for a record, core-data does not call `load` for it and skips\n\t * the transient reads it would otherwise do for a synced record.\n\t *\n\t * Only `load` is gated. `loadCollection`, `update`, `beforeSave`,\n\t * `afterSave`, and `unload` are still called for every record, so a\n\t * manager that syncs only some entities must check for itself inside\n\t * those members.\n\t */\n\tshouldSync?: (\n\t\tkind: string,\n\t\tname: string,\n\t\trecordId: EntitySyncRecordId\n\t) => boolean;\n\n\t/**\n\t * Called once when a single record is fetched by numeric id, right after\n\t * it arrives from the REST API. Records fetched with a query do not\n\t * trigger it. The record already has its transient properties filled\n\t * in, for example the parsed `blocks` of a post.\n\t *\n\t * This is where a manager starts syncing the record. The handlers let it\n\t * write back into the store later: e.g. `editRecord` applies edits that\n\t * came from somewhere else, `refetchRecord` reloads the record from the\n\t * server.\n\t */\n\tload: (\n\t\tkind: string,\n\t\tname: string,\n\t\trecordId: EntitySyncRecordId,\n\t\trecord: EntitySyncRecord,\n\t\thandlers: EntitySyncRecordHandlers\n\t) => void | Promise< void >;\n\n\t/**\n\t * Called when a whole collection is fetched (`per_page: -1`).\n\t */\n\tloadCollection?: (\n\t\tkind: string,\n\t\tname: string,\n\t\thandlers: EntitySyncCollectionHandlers\n\t) => void | Promise< void >;\n\n\t/**\n\t * Called for every `editEntityRecord`, BEFORE the edit reaches the\n\t * store. The edits already have merged fields applied\n\t * (see `mergedEdits` on the entity config).\n\t *\n\t * Known ordering: a manager that calls `editRecord` synchronously from\n\t * here has its edit overwritten by the store update that follows.\n\t * Dispatch such edits from a later task.\n\t */\n\tupdate: (\n\t\tkind: string,\n\t\tname: string,\n\t\trecordId: EntitySyncRecordId,\n\t\tedits: EntitySyncRecord,\n\t\toptions: EntitySyncEditOptions\n\t) => void;\n\n\t/**\n\t * Called right before a save request goes out for an existing record,\n\t * with the edits about to be saved.\n\t *\n\t * This is the place to add sync-related data to the request. Any\n\t * object returned here is merged over the edits, so the manager can\n\t * add fields or override existing ones.\n\t */\n\tbeforeSave?: (\n\t\tkind: string,\n\t\tname: string,\n\t\trecordId: EntitySyncRecordId,\n\t\tedits: EntitySyncRecord,\n\t\tcontext: EntitySyncBeforeSaveContext\n\t) => EntitySyncRecord | void | Promise< EntitySyncRecord | void >;\n\n\t/**\n\t * Called after a regular (non-autosave) save succeeded and the response\n\t * was received into the store. `recordId` is the id the save was\n\t * called with, so it is undefined for a record that was just created.\n\t * Read the new id from `context.savedRecord` in that case.\n\t */\n\tafterSave?: (\n\t\tkind: string,\n\t\tname: string,\n\t\trecordId: EntitySyncRecordId | undefined,\n\t\tcontext: EntitySyncAfterSaveContext\n\t) => void;\n\n\t/**\n\t * Called after `deleteEntityRecord` removed a record from the store,\n\t * once the delete request succeeded. The manager should stop syncing\n\t * that record and release anything it holds for it.\n\t */\n\tunload: (\n\t\tkind: string,\n\t\tname: string,\n\t\trecordId: EntitySyncRecordId\n\t) => void;\n\n\t/**\n\t * Called when collaboration support is switched off. The manager should\n\t * stop syncing every record it holds and release its resources.\n\t */\n\tunloadAll: () => void;\n\n\t/**\n\t * Whether the manager has the record loaded: `load` accepted it and it\n\t * was not unloaded since. core-data then leaves the record's\n\t * undo history to the manager instead of recording its edits.\n\t */\n\tisLoaded?: (\n\t\tkind: string,\n\t\tname: string,\n\t\trecordId: EntitySyncRecordId\n\t) => boolean;\n\n\t/**\n\t * The manager's undo history for the records it syncs. See\n\t * `EntitySyncUndoHistory` for how core-data keeps it in order with its\n\t * own undo manager.\n\t */\n\treadonly undoHistory?: EntitySyncUndoHistory;\n}\n\nlet registeredManager: EntitySyncManager | undefined;\n\n/**\n * Registers the entity sync manager. Only one can be active. The first\n * registration wins: a later call with a different manager is ignored and\n * warns in development. To swap managers, unregister the current one first.\n *\n * @param manager The manager to register.\n * @return A function that unregisters this manager. When the registration\n *         was ignored because another manager is active, it does nothing.\n */\nexport function registerEntitySyncManager(\n\tmanager: EntitySyncManager\n): () => void {\n\tif ( registeredManager && registeredManager !== manager ) {\n\t\twarning(\n\t\t\t'registerEntitySyncManager: an entity sync manager is already registered. The new manager was ignored.'\n\t\t);\n\n\t\treturn () => {};\n\t}\n\n\tregisteredManager = manager;\n\n\treturn () => {\n\t\tif ( registeredManager === manager ) {\n\t\t\tregisteredManager = undefined;\n\t\t}\n\t};\n}\n\n/**\n * Returns the registered entity sync manager, if any.\n *\n * @return The manager, or undefined when none is registered.\n */\nexport function getEntitySyncManager(): EntitySyncManager | undefined {\n\treturn registeredManager;\n}\n"],"mappings":";;AAyQA,IAAI;;;;;;;;;;AAWJ,SAAgB,0BACf,SACa;CACb,IAAK,qBAAqB,sBAAsB,SAAU;EACzD,QACC,uGACD;EAEA,aAAa,CAAC;CACf;CAEA,oBAAoB;CAEpB,aAAa;EACZ,IAAK,sBAAsB,SAC1B,oBAAoB,KAAA;CAEtB;AACD;;;;;;AAOA,SAAgB,uBAAsD;CACrE,OAAO;AACR"}