@knowlearning/agents 0.4.3 → 0.4.5
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/agents/generic.js +22 -8
- package/package.json +1 -1
package/agents/generic.js
CHANGED
|
@@ -29,6 +29,7 @@ export default function Agent({ host, token, WebSocket, protocol='ws', uuid, fet
|
|
|
29
29
|
const states = {}
|
|
30
30
|
const responses = {}
|
|
31
31
|
const watchers = {}
|
|
32
|
+
const keyToSubscriptionId = {}
|
|
32
33
|
const lastInteractionUpdateForWatchedScope = {}
|
|
33
34
|
const messageQueue = []
|
|
34
35
|
let resolveEnvironment
|
|
@@ -48,6 +49,10 @@ export default function Agent({ host, token, WebSocket, protocol='ws', uuid, fet
|
|
|
48
49
|
log('INITIALIZING AGENT CONNECTION')
|
|
49
50
|
initWS()
|
|
50
51
|
|
|
52
|
+
function subscription(key) {
|
|
53
|
+
return subscriptions[keyToSubscriptionId[key]]
|
|
54
|
+
}
|
|
55
|
+
|
|
51
56
|
function log() {
|
|
52
57
|
if (mode === 'debug') console.log(...arguments)
|
|
53
58
|
}
|
|
@@ -155,11 +160,13 @@ export default function Agent({ host, token, WebSocket, protocol='ws', uuid, fet
|
|
|
155
160
|
}
|
|
156
161
|
else {
|
|
157
162
|
const key = `${message.domain}/${message.user}/${message.scope}`
|
|
163
|
+
const sub = subscription(key)
|
|
158
164
|
if (watchers[key]) {
|
|
159
|
-
|
|
165
|
+
// TODO: debug case where sub is not defined
|
|
166
|
+
if (sub && sub.ii + 1 !== message.ii) {
|
|
160
167
|
// TODO: this seems to be an error that happens with decent regularity (an answer with a given si was skipped/failed)
|
|
161
168
|
// we should be wary of out-of-order ii being passed down (maybe need to wait for older ones???)
|
|
162
|
-
console.warn('UNEXPECTED UPDATE INTERACTION INDEX!!!!!!!!!!! last index in session',
|
|
169
|
+
console.warn('UNEXPECTED UPDATE INTERACTION INDEX!!!!!!!!!!! last index in session', sub, ' passed index ', message.ii)
|
|
163
170
|
}
|
|
164
171
|
states[key] = await states[key] || {}
|
|
165
172
|
|
|
@@ -168,7 +175,7 @@ export default function Agent({ host, token, WebSocket, protocol='ws', uuid, fet
|
|
|
168
175
|
|
|
169
176
|
applyPatch(states[key], standardJSONPatch(message.patch.slice(lastResetPatchIndex + 1)))
|
|
170
177
|
watchers[key].forEach(fn => fn({ ...message, state: states[key] }))
|
|
171
|
-
|
|
178
|
+
if (sub) sub.ii = message.ii
|
|
172
179
|
}
|
|
173
180
|
}
|
|
174
181
|
}
|
|
@@ -206,18 +213,25 @@ export default function Agent({ host, token, WebSocket, protocol='ws', uuid, fet
|
|
|
206
213
|
const k = `${d || domain}/${u || user}/${scope}`
|
|
207
214
|
resolveKey(k)
|
|
208
215
|
|
|
209
|
-
if (
|
|
216
|
+
if (!keyToSubscriptionId[k]) {
|
|
217
|
+
const id = uuid()
|
|
218
|
+
|
|
219
|
+
keyToSubscriptionId[k] = id
|
|
210
220
|
watchers[k] = []
|
|
211
221
|
states[k] = new Promise(async resolve => {
|
|
212
|
-
subscriptions[
|
|
222
|
+
subscriptions[id] = { scope, ii: null, session }
|
|
223
|
+
|
|
224
|
+
if (d !== domain) subscriptions[id].domain = d
|
|
225
|
+
if (u !== user) subscriptions[id].user = u
|
|
226
|
+
|
|
213
227
|
const { ii, state } = await lastMessageResponse()
|
|
214
|
-
subscriptions[
|
|
228
|
+
subscriptions[id].ii = ii
|
|
215
229
|
resolve(state)
|
|
216
230
|
if (ii === -1) {
|
|
217
231
|
// -1 indicates the result is a computed scope, so
|
|
218
232
|
// ii does not apply (we clear out the subscription to not cache value)
|
|
219
233
|
delete states[k]
|
|
220
|
-
delete subscriptions[
|
|
234
|
+
delete subscriptions[id]
|
|
221
235
|
}
|
|
222
236
|
})
|
|
223
237
|
}
|
|
@@ -247,7 +261,7 @@ export default function Agent({ host, token, WebSocket, protocol='ws', uuid, fet
|
|
|
247
261
|
const k = await key
|
|
248
262
|
removeWatcher(k, watchFn)
|
|
249
263
|
if (watchers[k].length === 0) {
|
|
250
|
-
delete subscriptions[k]
|
|
264
|
+
delete subscriptions[keyToSubscriptionId[k]]
|
|
251
265
|
delete watchers[k]
|
|
252
266
|
}
|
|
253
267
|
}
|