@@ -17,15 +17,6 @@ import type {NodeApi} from './api/nodes';
1717
1818export const UNDEFINED = new ConNode ( ORIGIN , undefined ) ;
1919
20- export const enum ModelChangeType {
21- /** When operations are applied through `.applyPatch()` directly. */
22- REMOTE = 0 ,
23- /** When local operations are applied through the `ModelApi`. */
24- LOCAL = 1 ,
25- /** When model is reset using the `.reset()` method. */
26- RESET = 2 ,
27- }
28-
2920/**
3021 * In instance of Model class represents the underlying data structure,
3122 * i.e. model, of the JSON CRDT document.
@@ -140,15 +131,6 @@ export class Model<N extends JsonNode = JsonNode> implements Printable {
140131 */
141132 public tick : number = 0 ;
142133
143- /**
144- * Callback called after every `applyPatch` call.
145- *
146- * When using the `.api` API, this property is set automatically by
147- * the {@link ModelApi} class. In that case use the `mode.api.evens.on('change')`
148- * to subscribe to changes.
149- */
150- public onchange : undefined | ( ( type : ModelChangeType ) => void ) = undefined ;
151-
152134 /**
153135 * Applies a batch of patches to the document.
154136 *
@@ -159,16 +141,27 @@ export class Model<N extends JsonNode = JsonNode> implements Printable {
159141 for ( let i = 0 ; i < length ; i ++ ) this . applyPatch ( patches [ i ] ) ;
160142 }
161143
144+ /**
145+ * Callback called before every `applyPatch` call.
146+ */
147+ public onbeforepatch ?: ( patch : Patch ) => void = undefined ;
148+
149+ /**
150+ * Callback called after every `applyPatch` call.
151+ */
152+ public onpatch ?: ( patch : Patch ) => void = undefined ;
153+
162154 /**
163155 * Applies a single patch to the document. All mutations to the model must go
164156 * through this method.
165157 */
166158 public applyPatch ( patch : Patch ) {
159+ this . onbeforepatch ?.( patch ) ;
167160 const ops = patch . ops ;
168161 const { length} = ops ;
169162 for ( let i = 0 ; i < length ; i ++ ) this . applyOperation ( ops [ i ] ) ;
170163 this . tick ++ ;
171- this . onchange ?.( ModelChangeType . REMOTE ) ;
164+ this . onpatch ?.( patch ) ;
172165 }
173166
174167 /**
@@ -180,6 +173,7 @@ export class Model<N extends JsonNode = JsonNode> implements Printable {
180173 *
181174 * @param op Any JSON CRDT Patch operation
182175 * @ignore
176+ * @internal
183177 */
184178 public applyOperation ( op : JsonCrdtPatchOperation ) : void {
185179 this . clock . observe ( op . id , op . span ( ) ) ;
@@ -293,7 +287,7 @@ export class Model<N extends JsonNode = JsonNode> implements Printable {
293287 const node = this . index . get ( value ) ;
294288 if ( ! node ) return ;
295289 const api = node . api ;
296- if ( api ) ( api as NodeApi ) . events . onDelete ( ) ;
290+ if ( api ) ( api as NodeApi ) . events . handleDelete ( ) ;
297291 node . children ( ( child ) => this . deleteNodeTree ( child . id ) ) ;
298292 this . index . del ( value ) ;
299293 }
@@ -322,18 +316,40 @@ export class Model<N extends JsonNode = JsonNode> implements Printable {
322316 return this . fork ( this . clock . sid ) ;
323317 }
324318
319+ /**
320+ * Callback called before model isi reset using the `.reset()` method.
321+ */
322+ public onbeforereset ?: ( ) => void = undefined ;
323+
324+ /**
325+ * Callback called after model has been reset using the `.reset()` method.
326+ */
327+ public onreset ?: ( ) => void = undefined ;
328+
325329 /**
326330 * Resets the model to equivalent state of another model.
327331 */
328332 public reset ( to : Model < N > ) : void {
333+ this . onbeforereset ?.( ) ;
334+ const index = this . index ;
329335 this . index = new AvlMap < clock . ITimestampStruct , JsonNode > ( clock . compare ) ;
330336 const blob = to . toBinary ( ) ;
331337 decoder . decode ( blob , < any > this ) ;
332338 this . clock = to . clock . clone ( ) ;
333339 this . ext = to . ext . clone ( ) ;
334- const api = this . _api ;
335- if ( api ) api . flush ( ) ;
336- this . onchange ?.( ModelChangeType . RESET ) ;
340+ this . _api ?. flush ( ) ;
341+ index . forEach ( ( { v : node } ) => {
342+ const api = node . api as NodeApi | undefined ;
343+ if ( ! api ) return ;
344+ const newNode = this . index . get ( node . id ) ;
345+ if ( ! newNode ) {
346+ api . events . handleDelete ( ) ;
347+ return ;
348+ }
349+ api . node = newNode ;
350+ newNode . api = api ;
351+ } ) ;
352+ this . onreset ?.( ) ;
337353 }
338354
339355 /**
0 commit comments