@@ -39,6 +39,9 @@ export class ModelApi<N extends JsonNode = JsonNode> implements SyncStore<JsonNo
3939 public readonly onBeforeLocalChange = new FanOut < number > ( ) ;
4040 /** Emitted after local changes through `model.api` are applied. */
4141 public readonly onLocalChange = new FanOut < number > ( ) ;
42+ /** Emitted after local changes through `model.api` are applied. Same as
43+ * `.onLocalChange`, but this event buffered withing a microtask. */
44+ public readonly onLocalChanges = new MicrotaskBufferFanOut < number > ( this . onLocalChange ) ;
4245 /** Emitted when the model changes. Combines `onReset`, `onPatch` and `onLocalChange`. */
4346 public readonly onChange = new MergeFanOut < number | Patch | void > ( [ this . onReset , this . onPatch , this . onLocalChange ] ) ;
4447 /** Emitted when the model changes. Same as `.onChange`, but this event is emitted once per microtask. */
@@ -249,6 +252,7 @@ export class ModelApi<N extends JsonNode = JsonNode> implements SyncStore<JsonNo
249252 * Flushes the builder and returns a patch.
250253 *
251254 * @returns A JSON CRDT patch.
255+ * @todo Make this return undefined if there are no operations in the builder.
252256 */
253257 public flush ( ) : Patch {
254258 const patch = this . builder . flush ( ) ;
@@ -257,6 +261,17 @@ export class ModelApi<N extends JsonNode = JsonNode> implements SyncStore<JsonNo
257261 return patch ;
258262 }
259263
264+ /** Emitted before a transaction is started. */
265+ public readonly onBeforeTransaction = new FanOut < void > ( ) ;
266+ /** Emitted after transaction completes. */
267+ public readonly onTransaction = new FanOut < void > ( ) ;
268+
269+ public transaction ( callback : ( ) => void ) {
270+ this . onBeforeTransaction . emit ( ) ;
271+ callback ( ) ;
272+ this . onTransaction . emit ( ) ;
273+ }
274+
260275 // ---------------------------------------------------------------- SyncStore
261276
262277 public readonly subscribe = ( callback : ( ) => void ) => this . onChanges . listen ( ( ) => callback ( ) ) ;
0 commit comments