@@ -15,6 +15,7 @@ import type {JsonNode, JsonNodeView} from '../nodes/types';
15
15
import type { Printable } from 'tree-dump/lib/types' ;
16
16
import type { NodeBuilder } from '../../json-crdt-patch' ;
17
17
import type { NodeApi } from './api/nodes' ;
18
+ import { Extension } from '../extensions/Extension' ;
18
19
19
20
export const UNDEFINED = new ConNode ( ORIGIN , undefined ) ;
20
21
@@ -147,17 +148,21 @@ export class Model<N extends JsonNode = JsonNode<any>> implements Printable {
147
148
* session ID generated by {@link Model.sid}.
148
149
* @returns A strictly typed model.
149
150
*/
150
- public static readonly create = < S extends NodeBuilder > (
151
+ public static readonly create = < S extends NodeBuilder , E extends Extension < any , any , any , any , any , any > [ ] > (
151
152
schema ?: S ,
152
153
sidOrClock : clock . ClockVector | number = Model . sid ( ) ,
153
- ) : Model < SchemaToJsonNode < S > > => {
154
+ options ?: { extensions ?: E } ,
155
+ ) : Model < SchemaToJsonNode < S , E > > => {
154
156
const cl =
155
157
typeof sidOrClock === 'number'
156
158
? sidOrClock === SESSION . SERVER
157
159
? new clock . ServerClockVector ( SESSION . SERVER , 1 )
158
160
: new clock . ClockVector ( sidOrClock , 1 )
159
161
: sidOrClock ;
160
- const model = new Model < SchemaToJsonNode < S > > ( cl ) ;
162
+ const model = new Model < SchemaToJsonNode < S , E > > ( cl ) ;
163
+ for ( const extension of options ?. extensions ?? [ ] ) {
164
+ model . ext . register ( extension ) ;
165
+ }
161
166
if ( schema ) model . setSchema ( schema , true ) ;
162
167
return model ;
163
168
} ;
@@ -186,12 +191,16 @@ export class Model<N extends JsonNode = JsonNode<any>> implements Printable {
186
191
* @param sid Session ID to set for the model.
187
192
* @returns An instance of a model.
188
193
*/
189
- public static readonly load = < S extends NodeBuilder > (
194
+ public static readonly load = < S extends NodeBuilder , E extends Extension < any , any , any , any , any , any > [ ] > (
190
195
data : Uint8Array ,
191
196
sid ?: number ,
192
197
schema ?: S ,
193
- ) : Model < SchemaToJsonNode < S > > => {
194
- const model = decoder . decode ( data ) as unknown as Model < SchemaToJsonNode < S > > ;
198
+ options ?: { extensions ?: E } ,
199
+ ) : Model < SchemaToJsonNode < S , E > > => {
200
+ const model = decoder . decode ( data ) as unknown as Model < SchemaToJsonNode < S , E > > ;
201
+ for ( const extension of options ?. extensions ?? [ ] ) {
202
+ model . ext . register ( extension ) ;
203
+ }
195
204
if ( schema ) model . setSchema ( schema , true ) ;
196
205
if ( typeof sid === 'number' ) model . setSid ( sid ) ;
197
206
return model ;
@@ -537,7 +546,7 @@ export class Model<N extends JsonNode = JsonNode<any>> implements Printable {
537
546
* session.
538
547
* @returns Strictly typed model.
539
548
*/
540
- public setSchema < S extends NodeBuilder > ( schema : S , useGlobalSession : boolean = true ) : Model < SchemaToJsonNode < S > > {
549
+ public setSchema < S extends NodeBuilder > ( schema : S , useGlobalSession : boolean = true ) : Model < SchemaToJsonNode < S , [ ] > > {
541
550
const c = this . clock ;
542
551
const isNewDocument = c . time === 1 ;
543
552
if ( isNewDocument ) {
0 commit comments