{"version":3,"file":"deferred-updates.mjs","names":[],"sources":["../src/deferred-updates.ts"],"sourcesContent":["import type {\n\tObjectData,\n\tObjectID,\n\tObjectType,\n\tSyncManagerUpdateOptions,\n} from './types';\n\n/**\n * A queue of items that are applied later, in the order they were added.\n */\nexport interface DeferredUpdateQueue {\n\t/**\n\t * Add an item to the queue. It is applied on the next tick of the event\n\t * loop, or sooner if `flush` is called first.\n\t *\n\t * @param item The item to apply later.\n\t */\n\tdefer: ( item: DeferredUpdate ) => void;\n\n\t/**\n\t * Apply all queued items now, in the order they were added.\n\t */\n\tflush: () => void;\n}\n\n/**\n * Create a queue for local updates that are kept off the typing hot path\n * (#79964).\n *\n * The queue is shared by all documents, because the order of updates to\n * different documents decides the order of their undo levels. The items are\n * held in a list, and not closed over in their timeouts, so that a read of a\n * document (a snapshot, persistence, undo) can flush them first.\n *\n * @param apply Applies one item.\n */\nexport function createDeferredUpdateQueue(\n\tapply: ( item: DeferredUpdate ) => void\n): DeferredUpdateQueue {\n\tconst queue: DeferredUpdate[] = [];\n\tlet isFlushing = false;\n\n\tfunction flush(): void {\n\t\t// Applying an item can ask for a flush itself (closing an undo level\n\t\t// flushes first). The items already queued keep their order by\n\t\t// letting the outer flush finish them.\n\t\tif ( isFlushing ) {\n\t\t\treturn;\n\t\t}\n\n\t\tisFlushing = true;\n\n\t\ttry {\n\t\t\twhile ( queue.length > 0 ) {\n\t\t\t\tconst item = queue.shift();\n\n\t\t\t\tif ( item ) {\n\t\t\t\t\tapply( item );\n\t\t\t\t}\n\t\t\t}\n\t\t} finally {\n\t\t\tisFlushing = false;\n\t\t}\n\t}\n\n\treturn {\n\t\tdefer( item ) {\n\t\t\tqueue.push( item );\n\t\t\tsetTimeout( flush, 0 );\n\t\t},\n\t\tflush,\n\t};\n}\n\n/**\n * A local update that waits in the queue.\n */\nexport interface DeferredUpdate {\n\tchanges: Partial< ObjectData >;\n\tobjectId: ObjectID | null;\n\tobjectType: ObjectType;\n\toptions: SyncManagerUpdateOptions;\n\torigin: string;\n}\n"],"mappings":";;;;;;;;;;;;AAoCA,SAAgB,0BACf,OACsB;CACtB,MAAM,QAA0B,CAAC;CACjC,IAAI,aAAa;CAEjB,SAAS,QAAc;EAItB,IAAK,YACJ;EAGD,aAAa;EAEb,IAAI;GACH,OAAQ,MAAM,SAAS,GAAI;IAC1B,MAAM,OAAO,MAAM,MAAM;IAEzB,IAAK,MACJ,MAAO,IAAK;GAEd;EACD,UAAU;GACT,aAAa;EACd;CACD;CAEA,OAAO;EACN,MAAO,MAAO;GACb,MAAM,KAAM,IAAK;GACjB,WAAY,OAAO,CAAE;EACtB;EACA;CACD;AACD"}