statebus 6.1.22 → 6.1.25

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.
Files changed (3) hide show
  1. package/client.js +56 -8
  2. package/package.json +1 -1
  3. package/statebus.js +13 -7
package/client.js CHANGED
@@ -63,6 +63,7 @@
63
63
  var has_prefix = new RegExp('^' + preprefix)
64
64
  var is_absolute = /^https?:\/\//
65
65
  var subscriptions = {}
66
+ var put_counter = 0
66
67
  //if (url[url.length-1]=='/') url = url.substr(0,url.length-1)
67
68
  function add_prefix (key) {
68
69
  return is_absolute.test(key) ? key : preprefix + key }
@@ -73,21 +74,66 @@
73
74
  function rem_prefixes (obj) {
74
75
  return bus.translate_keys(bus.clone(obj), rem_prefix) }
75
76
 
77
+
78
+ var puts = new Map()
79
+ function enqueue_put (url, body) {
80
+ var id = put_counter++
81
+ puts.set(id, {url: url, body: body, id: id})
82
+ send_put(id)
83
+ }
84
+ function send_put (id) {
85
+ try {
86
+ puts.get(id).status = 'sending'
87
+ braid_fetch(
88
+ puts.get(id).url,
89
+ {
90
+ method: 'put',
91
+ headers: {
92
+ 'content-type': 'application/json',
93
+ 'put-order': id,
94
+ },
95
+ body: puts.get(id).body
96
+ }
97
+ ).then(function (res) {
98
+ if (res.status !== 200)
99
+ console.error('Server gave error on PUT:',
100
+ e, 'for', puts.get(id).body)
101
+ else
102
+ console.log('Successful put!', puts.get(id).body)
103
+ puts.delete(id)
104
+ }).catch(function (e) {
105
+ console.error('Error on PUT, waiting...', puts.get(id).url)
106
+ puts.get(id).status = 'waiting'
107
+ // console.error('Error on PUT:', e, 'for', puts.get(id).body)
108
+ // puts.delete(id)
109
+ })
110
+ } catch (e) {
111
+ console.error('Error on PUT, waiting...', puts.get(id).url)
112
+ puts.get(id).status = 'waiting'
113
+ }
114
+ }
115
+ function retry_put (id) {
116
+ setTimeout(function () {send_put(id)}, 1000)
117
+ }
118
+ function send_all_puts () {
119
+ puts.keys().forEach(function (id) {
120
+ if (puts.get(id).status === 'waiting') {
121
+ console.log('Sending waiting put', id)
122
+ send_put(id)
123
+ }
124
+ })
125
+ }
126
+
76
127
  bus(prefix).to_save = function (obj, t) {
77
128
  bus.save.fire(obj)
78
129
 
130
+ enqueue_put(url + rem_prefix(obj.key),
131
+ JSON.stringify(obj.val))
79
132
  // var x = {save: obj}
80
133
  // if (t.version) x.version = t.version
81
134
  // if (t.parents) x.parents = t.parents
82
135
  // if (t.patch) x.patch = t.patch
83
136
  // if (t.patch) x.save = rem_prefix(x.save.key)
84
-
85
- braid_fetch(url + rem_prefix(obj.key),
86
- {
87
- method: 'put',
88
- 'content-type': 'application/json',
89
- body: JSON.stringify(obj.val)
90
- })
91
137
  }
92
138
 
93
139
  bus(prefix).to_fetch = function (key, t) {
@@ -140,6 +186,7 @@
140
186
  'color: blue')
141
187
  reconnect_attempts = 0
142
188
  subscriptions[key].status = 'connected'
189
+ send_all_puts()
143
190
  }
144
191
 
145
192
  // Return the update
@@ -644,7 +691,8 @@
644
691
  for (var k in this.props)
645
692
  if (this.props.hasOwnProperty(k))
646
693
  new_props[k] = this.props[k]
647
- if (this.state.value) new_props.value = this.state.value
694
+ if (this.state.hasOwnProperty('value'))
695
+ new_props.value = this.state.value
648
696
  new_props.onChange = this.onChange
649
697
  return element(new_props)
650
698
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "statebus",
3
- "version": "6.1.22",
3
+ "version": "6.1.25",
4
4
  "description": "Synchronize state.",
5
5
  "main": "statebus.js",
6
6
  "scripts": {
package/statebus.js CHANGED
@@ -430,9 +430,11 @@
430
430
  statelog(key, yellow, 'v', 'Deleting ' + key)
431
431
  // Call the to_delete handlers
432
432
  var handlers_called = bus.route(key, 'to_delete', key)
433
- if (handlers_called === 0)
433
+ if (handlers_called === 0) {
434
434
  // And go ahead and delete if there aren't any!
435
435
  delete cache[key]
436
+ delete backup_cache[key]
437
+ }
436
438
 
437
439
  // Call the on_delete handlers
438
440
  bus.route(key, 'on_delete', cache[key] || {key: key}, t)
@@ -828,9 +830,10 @@
828
830
  // except through an explicit .revise() function.
829
831
  if (o) t.version = new_version()
830
832
 
831
- if (method === 'to_delete')
833
+ if (method === 'to_delete') {
832
834
  delete bus.cache[key]
833
- else if (method === 'to_save') {
835
+ delete bus.backup_cache[key]
836
+ } else if (method === 'to_save') {
834
837
  bus.save.fire(o || arg, t)
835
838
  bus.route(key, 'on_set_sync', o || arg, t)
836
839
  } else {
@@ -1742,7 +1745,10 @@
1742
1745
  client_fetched_keys.add(key) }
1743
1746
  bus(prefix).to_forget = function (key) { send({forget: key}),
1744
1747
  client_fetched_keys.delete(key) }
1745
- bus(prefix).to_delete = function (key) { send({'delete': key}) }
1748
+ bus(prefix).to_delete = function (key, t) {
1749
+ t.done()
1750
+ send({'delete': key})
1751
+ }
1746
1752
 
1747
1753
  function connect () {
1748
1754
  nlog('[ ] trying to open ' + url)
@@ -2179,13 +2185,13 @@
2179
2185
  if (a_array !== b_array) return ' = ' + JSON.stringify(b)
2180
2186
  if (a_array) {
2181
2187
  if (a.length === b.length-1
2182
- && !deep_equals(a[a.length], b[b.length])) {
2183
- return '.push(' +JSON.stringify(b[b.length]) + ')'
2188
+ && deep_equals(a[a.length-1], b[b.length-2])) {
2189
+ return '.push(' +JSON.stringify(b[b.length-1]) + ')'
2184
2190
  }
2185
2191
  for (var i=0; i < a.length; i++) {
2186
2192
  var tmp = sorta_diff (a[i], b[i])
2187
2193
  if (tmp)
2188
- return '['+i+'] = '+tmp
2194
+ return '['+i+']'+tmp
2189
2195
  }
2190
2196
  return null
2191
2197
  }