statebus 7.0.3 → 7.0.4

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/statebus.js CHANGED
@@ -14,8 +14,8 @@
14
14
  && !(typeof key === 'object' && typeof key.key === 'string'))
15
15
  throw ('Error: get(key) called with key = '
16
16
  + JSON.stringify(key))
17
- key = key.key || key // You can pass in an object instead of key
18
- // We should probably disable this in future
17
+ key = key.key || key // You can pass in an object instead of key
18
+ // We should probably disable this in future
19
19
  bogus_check(key)
20
20
 
21
21
  var called_from_reactive_funk = !callback
@@ -413,6 +413,7 @@
413
413
 
414
414
  // Delete the cache entry...?
415
415
  // delete cache[key]
416
+ // delete backup_cache[key]
416
417
  delete gets_out[key]
417
418
  delete to_be_forgotten[key]
418
419
  }, 200)
@@ -503,7 +504,6 @@
503
504
  var funk = funks[k],
504
505
  version = dirty_funks[k]
505
506
 
506
- // console.log(funks[dirty_funks[i]].proxies_for)
507
507
  var p = funk.proxies_for
508
508
  if (p && p.priority) {
509
509
  log('Clean-early:', funk_name(funk))
@@ -525,7 +525,7 @@
525
525
  funk.react()
526
526
  }
527
527
  }
528
- // log('We just cleaned up', dirty_funks.length, 'funks!')
528
+ // console.log('We just cleaned up', dirty_funks.length, 'funks!')
529
529
  }
530
530
 
531
531
  // Let's change this function to go through each key and grab the latest
@@ -537,7 +537,7 @@
537
537
  var result = []
538
538
  var keys = changed_keys.values()
539
539
 
540
- //log(bus+' Cleaning up!', keys, 'keys, and', getters.length, 'getters')
540
+ // console.log(bus+' Finding rerunnable funcs for', keys, 'keys, and', Object.keys(dirty_getters).length, 'dirty_getters')
541
541
  for (var i=0; i<keys.length; i++) { // Collect all keys
542
542
  // if (to_be_forgotten[keys[i]])
543
543
  // // Ignore changes to keys that have been forgotten, but not
@@ -549,7 +549,7 @@
549
549
  if (f.react) {
550
550
  // Skip if it's already up to date
551
551
  var v = f.getted_keys[JSON.stringify([this.id, keys[i]])]
552
- //log('re-run:', keys[i], f.statebus_id, f.getted_keys)
552
+ // console.log('re-run:', keys[i], f.statebus_id, f.getted_keys)
553
553
  if (v && v.indexOf(versions[keys[i]]) !== -1) {
554
554
  log('skipping', funk_name(f), 'already at version', versions[keys[i]], 'proof:', v)
555
555
  continue
@@ -580,7 +580,7 @@
580
580
  changed_keys.clear()
581
581
  dirty_getters = {}
582
582
 
583
- //log('found', result.length, 'funks to re run')
583
+ // console.log('found', result.length, 'funks to re run')
584
584
 
585
585
  return result
586
586
  }
@@ -1258,8 +1258,8 @@
1258
1258
  return o
1259
1259
 
1260
1260
  // We recursively descend through {key: ...} links
1261
- if (typeof o === 'object' && 'key' in o) {
1262
- var new_base = bus.get(o.key)
1261
+ if (typeof o === 'object' && 'link' in o) {
1262
+ var new_base = bus.get(o.link)
1263
1263
  return item_proxy(new_base, new_base.val)
1264
1264
  }
1265
1265
 
@@ -1409,9 +1409,13 @@
1409
1409
  function rem_prefix (key) {
1410
1410
  return has_prefix.test(key) ? key.substr(preprefix.length) : key }
1411
1411
  function add_prefixes (obj) {
1412
- return bus.translate_keys(bus.clone(obj), add_prefix) }
1412
+ var keyed = bus.translate_keys(bus.clone(obj), add_prefix)
1413
+ return bus.translate_links(bus.clone(keyed), add_prefix)
1414
+ }
1413
1415
  function rem_prefixes (obj) {
1414
- return bus.translate_keys(bus.clone(obj), rem_prefix) }
1416
+ var keyed = bus.translate_keys(bus.clone(obj), rem_prefix)
1417
+ return bus.translate_links(bus.clone(keyed), rem_prefix)
1418
+ }
1415
1419
 
1416
1420
  bus(prefix).to_set = function (obj, t) {
1417
1421
  bus.set.fire(obj)
@@ -1446,9 +1450,9 @@
1446
1450
  var i = []
1447
1451
  function intro (o) {i.push(JSON.stringify({set: o}))}
1448
1452
  if (creds.clientid)
1449
- intro({key: 'current_user', client: creds.clientid})
1453
+ intro({key: 'current_user', val: {client: creds.clientid}})
1450
1454
  if (creds.name && creds.pass)
1451
- intro({key: 'current_user', login_as: {name: creds.name, pass: creds.pass}})
1455
+ intro({key: 'current_user', val: {login_as: {name: creds.name, pass: creds.pass}}})
1452
1456
  // Todo: make this kinda thing work:
1453
1457
  if (creds.private_key && creds.public_key) {
1454
1458
  // Send public_key... start waiting for a
@@ -1540,6 +1544,14 @@
1540
1544
  return {send: send, sock: sock, close: function () {done = true; sock.close()}}
1541
1545
  }
1542
1546
 
1547
+ bus.client_creds = function client_creds (server_url) {
1548
+ // This default implementation just creates a different random id each time
1549
+ // we connect. Override this if you want a client ID that persists.
1550
+ return {clientid: (Math.random().toString(36).substring(2)
1551
+ + Math.random().toString(36).substring(2)
1552
+ + Math.random().toString(36).substring(2))}
1553
+ }
1554
+
1543
1555
  function net_automount () {
1544
1556
  var bus = this
1545
1557
  var old_route = bus.route
@@ -1586,6 +1598,30 @@
1586
1598
  return k.replace(/(_$)/, '')
1587
1599
  }
1588
1600
 
1601
+ // ************************************************
1602
+ // Translating the URLs under links of state
1603
+ function translate_links (obj, f) {
1604
+ // Recurse through each element in arrays
1605
+ if (Array.isArray(obj))
1606
+ for (var i=0; i < obj.length; i++)
1607
+ translate_links(obj[i], f)
1608
+
1609
+ // Recurse through each property on objects
1610
+ else if (typeof obj === 'object')
1611
+ for (var k in obj) {
1612
+ if (k === 'link')
1613
+ if (typeof obj[k] == 'string')
1614
+ obj[k] = f(obj[k])
1615
+ else if (Array.isArray(obj[k]))
1616
+ for (var i=0; i < obj[k].length; i++) {
1617
+ if (typeof obj[k][i] === 'string')
1618
+ obj[k][i] = f(obj[k][i])
1619
+ }
1620
+ translate_links(obj[k], f)
1621
+ }
1622
+ return obj
1623
+ }
1624
+
1589
1625
 
1590
1626
  // ************************************************
1591
1627
  // Translating fields of objects
@@ -1863,13 +1899,13 @@
1863
1899
  if (a_array !== b_array) return ' = ' + JSON.stringify(b)
1864
1900
  if (a_array) {
1865
1901
  if (a.length === b.length-1
1866
- && !deep_equals(a[a.length], b[b.length])) {
1867
- return '.push(' +JSON.stringify(b[b.length]) + ')'
1902
+ && deep_equals(a[a.length-1], b[b.length-2])) {
1903
+ return '.push(' +JSON.stringify(b[b.length-1]) + ')'
1868
1904
  }
1869
1905
  for (var i=0; i < a.length; i++) {
1870
1906
  var tmp = sorta_diff (a[i], b[i])
1871
1907
  if (tmp)
1872
- return '['+i+'] = '+tmp
1908
+ return '['+i+']'+tmp
1873
1909
  }
1874
1910
  return null
1875
1911
  }
@@ -2070,7 +2106,7 @@
2070
2106
  if (!(key in bogus_keys))
2071
2107
  return
2072
2108
 
2073
- var msg = "Sorry, statebus.js currently prohibits use of the key \""+key+"\", and in fact all of these keys: " + Object.keys(bogus_keys).join(', ') + ". This is because Javascript is kinda lame, and even empty objects like \"{}\" have the \""+key+"\" field defined on them. Try typing this in your Javascript console: \"({}).constructor\" -- it returns a function instead of undefined! We could work around it by meticulously replacing every \"obj[key]\" with \"obj.hasOwnProperty(key) && obj[key]\" in the statebus code, but that will make the source more difficult to understand. So please contact us if this use-case is important for you, and we'll consider doing it. We're hoping that, for now, our users don't need to use these keys."
2109
+ var msg = "Sorry, statebus.js currently prohibits use of the key \""+key+"\", and in fact all of these keys: " + Object.keys(bogus_keys).join(', ') + ". This is because Javascript is kinda lame, and even empty objects like \"{}\" have the \""+key+"\" field defined on them. Try typing this in your Javascript console: \"({}).constructor\" -- it returns a function instead of undefined! Mike needs to work around it by replacing every \"obj[key]\" with \"obj.hasOwnProperty(key) && obj[key]\" in the statebus code, or switching to a Map() object. Please contact him and let him know where this is impacting you, so he can bump the priority on it."
2074
2110
  console.error(msg)
2075
2111
  throw 'Invalid key'
2076
2112
  }
@@ -2083,7 +2119,7 @@
2083
2119
  'funk_key funk_name funks key_id key_name id',
2084
2120
  'pending_gets gets_in gets_out loading_keys loading once',
2085
2121
  'global_funk busses rerunnable_funks',
2086
- 'escape_keys unescape_keys translate_keys apply_patch',
2122
+ 'escape_keys unescape_keys translate_keys translate_links apply_patch',
2087
2123
  'keyed_2_proxied proxied_2_keyed translate_fields',
2088
2124
  'ws_mount net_automount message_method',
2089
2125
  'parse Set One_To_Many clone extend deep_map deep_equals prune validate sorta_diff log deps symbols'
@@ -2110,7 +2146,7 @@
2110
2146
  bus.libs = {}
2111
2147
 
2112
2148
  if (nodejs)
2113
- require('./servers').import_server(bus, options)
2149
+ require('./servers').import_server(bus, make_bus, options)
2114
2150
 
2115
2151
  bus.render_when_loading = true
2116
2152
  return bus