statebus 7.1.1 → 7.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/extras/tests.js CHANGED
@@ -319,61 +319,61 @@ test(function proxy_link (done) {
319
319
  done()
320
320
  })
321
321
 
322
- test(function proxy_links_through1 (done) {
323
- var bus = require('../statebus')()
324
-
325
- // First try to dereference through three links
322
+ // Disable the version of proxy_links_through where the user does not have to
323
+ // explicitly traverse links with ._()
324
+ if (true)
325
+ test(function proxy_links_through2 (done) {
326
+ var bus = require('../statebus')()
326
327
 
327
- bus.state.a = [bus.link('b')]
328
- bus.state.b = bus.link('c')
329
- bus.state.c = 'see'
330
-
331
- log('state.c is', bus.state.c)
332
- log('state.b is', bus.state.b, 'from cache', bus.cache.b)
333
- log('state.a is', bus.state.a)
334
- log('state.a[0] is', bus.state.a[0])
335
- log('state.a[0] is', bus.state.a[0])
336
- assert(bus.state.a[0] === 'see')
328
+ // First try to dereference through three links
337
329
 
338
- // Add a little more wrapping and try some more
330
+ bus.state.a = [bus.link('b')]
331
+ bus.state.b = bus.link('c')
332
+ bus.state.c = 'see'
339
333
 
340
- bus.state.nested = [99, {a: bus.link('a')}]
341
- log('nested is', bus.state.nested)
342
- log('nested[1].a[0] is', bus.state.nested[1].a[0])
343
- assert(bus.state.nested[1].a[0] === 'see')
334
+ log('state.c is', bus.state.c)
335
+ log('state.b is', bus.state.b, 'from cache', bus.cache.b)
336
+ log('state.a is', bus.state.a)
337
+ log('state.a[0] is', bus.state.a[0])
338
+ log('state.a[0]._() is', bus.state.a[0]._())
339
+ log('state.a[0]._()._() is', bus.state.a[0]._()._())
340
+ assert(bus.state.a[0]._()._() === 'see')
344
341
 
345
- // Now try to go raw at the top-level
342
+ // Add a little more wrapping and try some more
346
343
 
347
- log('Raw is', bus.state[bus.symbols.raw].nested)
348
- log('Raw is', bus.raw(bus.state).nested)
349
- assert(bus.deep_equals(
350
- bus.state[bus.symbols.raw].nested,
351
- bus.raw(bus.state).nested
352
- ))
344
+ bus.state.nested = [99, {a: bus.link('a')}]
345
+ log('nested is', bus.state.nested)
346
+ log('nested[1].a._()[0]._()._() is', bus.state.nested[1].a._()[0]._()._())
347
+ assert(bus.state.nested[1].a._()[0]._()._() === 'see')
353
348
 
354
- log('Gonna deep_quals between:',
355
- bus.raw(bus.state).nested,
356
- [ 99, { a: { link: 'a' } } ])
349
+ // Now try to go raw at the top-level
357
350
 
358
- assert(bus.deep_equals(
359
- bus.raw(bus.state).nested,
360
- [ 99, { a: { link: 'a' } } ]
361
- ))
351
+ log('Raw is', bus.state[bus.symbols.raw].nested)
352
+ log('Raw is', bus.raw(bus.state).nested)
353
+ assert(bus.deep_equals(
354
+ bus.state[bus.symbols.raw].nested,
355
+ bus.raw(bus.state).nested
356
+ ))
362
357
 
363
- // Now try to go raw within
358
+ log('Gonna deep_quals between:',
359
+ bus.raw(bus.state).nested,
360
+ [ 99, { a: { link: 'a' } } ])
364
361
 
365
- log('Raw inner is', bus.raw(bus.state.nested[1]))
362
+ assert(bus.deep_equals(
363
+ bus.raw(bus.state).nested,
364
+ [ 99, { a: { link: 'a' } } ]
365
+ ))
366
366
 
367
- log('Bad raw call is', bus.raw(bus.state.nested[0]))
367
+ // Now try to go raw within
368
368
 
369
- done()
370
- })
369
+ log('Raw inner is', bus.raw(bus.state.nested[1]))
371
370
 
371
+ log('Bad raw call is', bus.raw(bus.state.nested[0]))
372
372
 
373
- // Disable the version of proxy_links_through where the user has to explicitly
374
- // traverse links with ()
375
- if (false)
376
- test(function proxy_links_through2 (done) {
373
+ done()
374
+ })
375
+ else
376
+ test(function proxy_links_through1 (done) {
377
377
  var bus = require('../statebus')()
378
378
 
379
379
  // First try to dereference through three links
@@ -386,16 +386,15 @@ if (false)
386
386
  log('state.b is', bus.state.b, 'from cache', bus.cache.b)
387
387
  log('state.a is', bus.state.a)
388
388
  log('state.a[0] is', bus.state.a[0])
389
- log('state.a[0]() is', bus.state.a[0]())
390
- log('state.a[0]()() is', bus.state.a[0]()())
391
- assert(bus.state.a[0]()() === 'see')
389
+ log('state.a[0] is', bus.state.a[0])
390
+ assert(bus.state.a[0] === 'see')
392
391
 
393
392
  // Add a little more wrapping and try some more
394
393
 
395
394
  bus.state.nested = [99, {a: bus.link('a')}]
396
395
  log('nested is', bus.state.nested)
397
- log('nested[1].a()[0]()() is', bus.state.nested[1].a()[0]()())
398
- assert(bus.state.nested[1].a()[0]()() === 'see')
396
+ log('nested[1].a[0] is', bus.state.nested[1].a[0])
397
+ assert(bus.state.nested[1].a[0] === 'see')
399
398
 
400
399
  // Now try to go raw at the top-level
401
400
 
@@ -406,9 +405,9 @@ if (false)
406
405
  bus.raw(bus.state).nested
407
406
  ))
408
407
 
409
- console.log('Gonna deep_quals between:',
410
- bus.raw(bus.state).nested,
411
- [ 99, { a: { link: 'a' } } ])
408
+ log('Gonna deep_quals between:',
409
+ bus.raw(bus.state).nested,
410
+ [ 99, { a: { link: 'a' } } ])
412
411
 
413
412
  assert(bus.deep_equals(
414
413
  bus.raw(bus.state).nested,
@@ -425,6 +424,27 @@ if (false)
425
424
  })
426
425
 
427
426
 
427
+ test(function copy_link (done) {
428
+ var bus = require('../statebus')(), state = bus.state
429
+
430
+ state.a = [bus.link('b')]
431
+ state.b = 3
432
+ state.copy_of_a = state.a
433
+ state.copy_of_a0 = state.a[0]
434
+
435
+ console.log(bus.cache.a)
436
+ console.log(bus.cache.b)
437
+ console.log(bus.cache.copy_of_a)
438
+ console.log(bus.cache.copy_of_a0)
439
+
440
+ assert(bus.cache.a.val[0].link === 'b')
441
+ assert(bus.cache.b.val === 3)
442
+ assert(bus.cache.copy_of_a.val[0].link === 'b')
443
+ assert(bus.cache.copy_of_a0.val.link === 'b')
444
+
445
+ done()
446
+ })
447
+
428
448
 
429
449
  test(function translate_fields (done) {
430
450
  // Translate Statebus -> Proxy format
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "statebus",
3
- "version": "7.1.1",
3
+ "version": "7.1.2",
4
4
  "description": "Synchronize state.",
5
5
  "main": "statebus.js",
6
6
  "scripts": {
package/statebus.js CHANGED
@@ -1497,9 +1497,9 @@
1497
1497
 
1498
1498
  // We recursively descend through {key: ...} links
1499
1499
  if (typeof o === 'object' && 'link' in o) {
1500
- var new_base = bus.get(o.link)
1501
- return json_proxy(new_base, '', new_base.val)
1502
- // return link(o.link)
1500
+ // var new_base = bus.get(o.link)
1501
+ // return json_proxy(new_base, '', new_base.val)
1502
+ return link(o.link)
1503
1503
  }
1504
1504
 
1505
1505
 
@@ -1597,7 +1597,11 @@
1597
1597
  // - We auto-dereference the key, with another get()
1598
1598
  // - So the user actually sees the value of the resource on the other side of the link
1599
1599
  function link (url) {
1600
- return {[symbols.link]: url}
1600
+ return {
1601
+ link: url,
1602
+ [symbols.link]: true,
1603
+ _: (args, o) => (o = get(url), json_proxy(o, '', o.val))
1604
+ }
1601
1605
  }
1602
1606
 
1603
1607
  // // The proxy object for links. Disabled for now.
@@ -2098,7 +2102,7 @@
2098
2102
  var escape_json_to_bus = (obj) => {
2099
2103
  obj = translate_fields(obj, escape_field_json_to_bus)
2100
2104
  return deep_map(obj, o => (typeof o === 'object' && symbols.link in o
2101
- ? {link: o[symbols.link]}
2105
+ ? {link: o.link}
2102
2106
  : o))
2103
2107
  }
2104
2108
  var unescape_bus_to_json = (obj) =>