@@ -23,6 +23,14 @@ import {handleNGFor, SingletonHandle} from '../storageNG/handle.js';
23
23
import { Entity } from '../entity.js' ;
24
24
import { singletonHandle , SingletonInterfaceStore , SingletonEntityStore } from '../storageNG/storage-ng.js' ;
25
25
26
+ async function mapHandleToStore ( arc , recipe , classType , id ) {
27
+ const store = await arc . createStore ( new SingletonType ( classType . type ) , undefined , `test:${ id } ` ) ;
28
+ const storageProxy = new StorageProxy ( 'id' , await store . activate ( ) , new SingletonType ( classType . type ) , store . storageKey . toString ( ) ) ;
29
+ const handle = await handleNGFor ( 'crdt-key' , storageProxy , arc . idGenerator , null , true , true , classType . toString ( ) ) as SingletonHandle < Entity > ;
30
+ recipe . handles [ id ] . mapToStorage ( store ) ;
31
+ return handle ;
32
+ }
33
+
26
34
describe ( 'particle interface loading' , ( ) => {
27
35
28
36
it ( 'loads interfaces into particles' , async ( ) => {
@@ -253,9 +261,9 @@ describe('particle interface loading', () => {
253
261
defineParticle(({Particle}) => {
254
262
var created = false;
255
263
return class extends Particle {
256
- async onCreate() {
264
+ onCreate() {
257
265
this.innerFooHandle = this.handles.get('innerFoo');
258
- await this.innerFooHandle.set(new this.innerFooHandle.entityClass({value: "Created!"}));
266
+ this.innerFooHandle.set(new this.innerFooHandle.entityClass({value: "Created!"}));
259
267
created = true;
260
268
}
261
269
async onHandleSync(handle, model) {
@@ -272,11 +280,7 @@ describe('particle interface loading', () => {
272
280
const storageKey = new VolatileStorageKey ( id , 'unique' ) ;
273
281
const arc = new Arc ( { id, storageKey, loader, context : manifest } ) ;
274
282
const fooClass = Entity . createEntityClass ( manifest . findSchemaByName ( 'Foo' ) , null ) ;
275
-
276
- const fooStore = await arc . createStore ( new SingletonType ( fooClass . type ) , undefined , 'test:0' ) ;
277
- const varStorageProxy = new StorageProxy ( 'id' , await fooStore . activate ( ) , new SingletonType ( fooClass . type ) , fooStore . storageKey . toString ( ) ) ;
278
- const fooHandle = await handleNGFor ( 'crdt-key' , varStorageProxy , arc . idGenerator , null , true , true , 'fooHandle' ) as SingletonHandle < Entity > ;
279
- recipe . handles [ 0 ] . mapToStorage ( fooStore ) ;
283
+ const fooHandle = await mapHandleToStore ( arc , recipe , fooClass , 0 ) ;
280
284
281
285
recipe . normalize ( ) ;
282
286
await arc . instantiate ( recipe ) ;
@@ -293,4 +297,174 @@ describe('particle interface loading', () => {
293
297
const fooHandle2 = await handleNGFor ( 'crdt-key' , varStorageProxy2 , arc2 . idGenerator , null , true , true , 'varHandle' ) as SingletonHandle < Entity > ;
294
298
assert . deepStrictEqual ( await fooHandle2 . fetch ( ) , new fooClass ( { value : 'Not created!' } ) ) ;
295
299
} ) ;
300
+
301
+ it ( 'onReady sees overriden values in onCreate' , async ( ) => {
302
+ const manifest = await Manifest . parse ( `
303
+ schema Foo
304
+ value: Text
305
+
306
+ particle UpdatingParticle in 'updating-particle.js'
307
+ bar: reads writes Foo
308
+ recipe
309
+ h1: use *
310
+ UpdatingParticle
311
+ bar: h1
312
+ ` ) ;
313
+ assert . lengthOf ( manifest . recipes , 1 ) ;
314
+ const recipe = manifest . recipes [ 0 ] ;
315
+ const loader = new Loader ( null , {
316
+ 'updating-particle.js' : `
317
+ 'use strict';
318
+ defineParticle(({Particle}) => {
319
+ var handlesSynced = 0;
320
+ return class extends Particle {
321
+ onCreate() {
322
+ this.barHandle = this.handles.get('bar');
323
+ this.barHandle.set(new this.barHandle.entityClass({value: "Created!"}));
324
+ }
325
+
326
+ async onReady() {
327
+ this.barHandle = this.handles.get('bar');
328
+ this.bar = await this.barHandle.fetch();
329
+
330
+ if(this.bar.value == "Created!") {
331
+ await this.barHandle.set(new this.barHandle.entityClass({value: "Ready!"}))
332
+ } else {
333
+ await this.barHandle.set(new this.barHandle.entityClass({value: "Handle not overriden by onCreate!"}))
334
+ }
335
+ }
336
+ };
337
+ });
338
+ `
339
+ } ) ;
340
+ const id = ArcId . newForTest ( 'test' ) ;
341
+ const storageKey = new VolatileStorageKey ( id , 'unique' ) ;
342
+ const arc = new Arc ( { id, storageKey, loader, context : manifest } ) ;
343
+ const fooClass = Entity . createEntityClass ( manifest . findSchemaByName ( 'Foo' ) , null ) ;
344
+
345
+ const barHandle = await mapHandleToStore ( arc , recipe , fooClass , 0 ) ;
346
+ await barHandle . set ( new fooClass ( { value : 'Set!' } ) ) ;
347
+
348
+ recipe . normalize ( ) ;
349
+ await arc . instantiate ( recipe ) ;
350
+ await arc . idle ;
351
+ assert . deepStrictEqual ( await barHandle . fetch ( ) , new fooClass ( { value : 'Ready!' } ) ) ;
352
+ } ) ;
353
+
354
+ it ( 'onReady runs when handles are first synced' , async ( ) => {
355
+ const manifest = await Manifest . parse ( `
356
+ schema Foo
357
+ value: Text
358
+
359
+ particle UpdatingParticle in 'updating-particle.js'
360
+ innerFoo: reads writes Foo
361
+ bar: reads Foo
362
+ recipe
363
+ h0: use *
364
+ h1: use *
365
+ UpdatingParticle
366
+ innerFoo: h0
367
+ bar: h1
368
+ ` ) ;
369
+ assert . lengthOf ( manifest . recipes , 1 ) ;
370
+ const recipe = manifest . recipes [ 0 ] ;
371
+ const loader = new Loader ( null , {
372
+ 'updating-particle.js' : `
373
+ 'use strict';
374
+ defineParticle(({Particle}) => {
375
+ var handlesSynced = 0;
376
+ return class extends Particle {
377
+ onCreate() {
378
+ this.innerFooHandle = this.handles.get('innerFoo');
379
+ this.innerFooHandle.set(new this.innerFooHandle.entityClass({value: "Created!"}));
380
+ }
381
+ onHandleSync(handle, model) {
382
+ handlesSynced += 1;
383
+ }
384
+ async onReady() {
385
+ this.innerFooHandle = this.handles.get('innerFoo');
386
+ this.foo = await this.innerFooHandle.fetch()
387
+
388
+ this.barHandle = this.handles.get('bar');
389
+ this.bar = await this.barHandle.fetch();
390
+
391
+ var s = "Ready!";
392
+ if(this.foo.value != "Created!") {
393
+ s = s + " onCreate was not called before onReady.";
394
+ }
395
+ if (this.bar.value != "Set!") {
396
+ s = s + " Read only handles not initialised in onReady";
397
+ }
398
+ if (handlesSynced != 2) {
399
+ s = s + " Not all handles were synced before onReady was called.";
400
+ }
401
+
402
+ this.innerFooHandle.set(new this.innerFooHandle.entityClass({value: s}))
403
+ }
404
+ };
405
+ });
406
+ `
407
+ } ) ;
408
+ const id = ArcId . newForTest ( 'test' ) ;
409
+ const storageKey = new VolatileStorageKey ( id , 'unique' ) ;
410
+ const arc = new Arc ( { id, storageKey, loader, context : manifest } ) ;
411
+ const fooClass = Entity . createEntityClass ( manifest . findSchemaByName ( 'Foo' ) , null ) ;
412
+
413
+ const fooHandle = await mapHandleToStore ( arc , recipe , fooClass , 0 ) ;
414
+ const barHandle = await mapHandleToStore ( arc , recipe , fooClass , 1 ) ;
415
+
416
+ await barHandle . set ( new fooClass ( { value : 'Set!' } ) ) ;
417
+
418
+ recipe . normalize ( ) ;
419
+ await arc . instantiate ( recipe ) ;
420
+ await arc . idle ;
421
+ assert . deepStrictEqual ( await fooHandle . fetch ( ) , new fooClass ( { value : 'Ready!' } ) ) ;
422
+ } ) ;
423
+
424
+ it ( 'onReady runs when there are no handles to sync' , async ( ) => {
425
+ const manifest = await Manifest . parse ( `
426
+ schema Foo
427
+ value: Text
428
+ particle UpdatingParticle in 'updating-particle.js'
429
+ innerFoo: writes Foo
430
+ recipe
431
+ h0: use *
432
+ UpdatingParticle
433
+ innerFoo: h0
434
+ ` ) ;
435
+ assert . lengthOf ( manifest . recipes , 1 ) ;
436
+ const recipe = manifest . recipes [ 0 ] ;
437
+ const loader = new Loader ( null , {
438
+ 'updating-particle.js' : `
439
+ 'use strict';
440
+ defineParticle(({Particle}) => {
441
+ var created = false;
442
+ return class extends Particle {
443
+ onCreate() {
444
+ created = true;
445
+ }
446
+ onReady(handle, model) {
447
+ this.innerFooHandle = this.handles.get('innerFoo');
448
+ if (created) {
449
+ this.innerFooHandle.set(new this.innerFooHandle.entityClass({value: "Created!"}));
450
+ } else {
451
+ this.innerFooHandle.set(new this.innerFooHandle.entityClass({value: "Not created!"}));
452
+ }
453
+ }
454
+ };
455
+ });
456
+ `
457
+ } ) ;
458
+ const id = ArcId . newForTest ( 'test' ) ;
459
+ const storageKey = new VolatileStorageKey ( id , 'unique' ) ;
460
+ const arc = new Arc ( { id, storageKey, loader, context : manifest } ) ;
461
+ const fooClass = Entity . createEntityClass ( manifest . findSchemaByName ( 'Foo' ) , null ) ;
462
+
463
+ const fooHandle = await mapHandleToStore ( arc , recipe , fooClass , 0 ) ;
464
+
465
+ recipe . normalize ( ) ;
466
+ await arc . instantiate ( recipe ) ;
467
+ await arc . idle ;
468
+ assert . deepStrictEqual ( await fooHandle . fetch ( ) , new fooClass ( { value : 'Created!' } ) ) ;
469
+ } ) ;
296
470
} ) ;
0 commit comments