vrpc 3.8.0 → 3.8.1

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vrpc",
3
- "version": "3.8.0",
3
+ "version": "3.8.1",
4
4
  "description": "Variadic Remote Procedure Calls",
5
5
  "main": "index.js",
6
6
  "directories": {
@@ -321,7 +321,7 @@ class VrpcClient extends EventEmitter {
321
321
  if (removed.length !== 0) {
322
322
  this.emit('instanceGone', removed, { domain, agent, className })
323
323
  removed.forEach(lostInstance =>
324
- this._clearCachedSubscriptions({ lostInstance })
324
+ this._clearCachedSubscriptions({ lostInstance, agent, className })
325
325
  )
326
326
  }
327
327
  if (added.length !== 0) {
@@ -438,7 +438,14 @@ class VrpcClient extends EventEmitter {
438
438
  v: VRPC_PROTOCOL_VERSION
439
439
  }
440
440
  const proxy = await this._getProxy(agent, className, json)
441
- if (instance && cacheProxy) this._proxies[instance] = proxy
441
+ if (instance && cacheProxy) {
442
+ this._proxies[`${agent}/${className}/${instance}`] = {
443
+ agent,
444
+ className,
445
+ instance,
446
+ proxy
447
+ }
448
+ }
442
449
  return proxy
443
450
  }
444
451
 
@@ -460,7 +467,8 @@ class VrpcClient extends EventEmitter {
460
467
  if (!this._isConnected()) {
461
468
  throw new Error('Client is not connected')
462
469
  }
463
- if (this._proxies[instance]) return this._proxies[instance]
470
+ const cached = this._cachedProxy(instance, options)
471
+ if (cached) return cached
464
472
  const { agent, className } = await this._getInstanceData(instance, options)
465
473
  return this._createProxy(agent, className, instance)
466
474
  }
@@ -492,7 +500,7 @@ class VrpcClient extends EventEmitter {
492
500
  }
493
501
  const topic = `${this._domain}/${agent}/${className}/__static__/__delete__`
494
502
  this._mqttPublish(topic, JSON.stringify(json))
495
- if (this._proxies[instance]) delete this._proxies[instance]
503
+ delete this._proxies[`${agent}/${className}/${instance}`]
496
504
  return this._handleAgentAnswer(json, agent)
497
505
  }
498
506
 
@@ -1374,11 +1382,19 @@ class VrpcClient extends EventEmitter {
1374
1382
  return foundId
1375
1383
  }
1376
1384
 
1377
- _clearCachedSubscriptions ({ lostAgent, lostInstance }) {
1385
+ _clearCachedSubscriptions ({ lostAgent, lostInstance, agent, className }) {
1378
1386
  const obsoleteTopics = Object.keys(this._cachedSubscriptions).filter(
1379
1387
  topic => {
1380
1388
  const sub = this._cachedSubscriptions[topic]
1381
- return sub.agent === lostAgent || sub.instance === lostInstance
1389
+ if (lostAgent !== undefined) return sub.agent === lostAgent
1390
+ // Instance ids are unique per agent only: a twin of the same id on
1391
+ // another agent (a deployed app next to its builder) keeps its
1392
+ // subscriptions when this one goes
1393
+ return (
1394
+ sub.instance === lostInstance &&
1395
+ sub.agent === agent &&
1396
+ sub.className === className
1397
+ )
1382
1398
  }
1383
1399
  )
1384
1400
 
@@ -1432,12 +1448,25 @@ class VrpcClient extends EventEmitter {
1432
1448
  return instanceData
1433
1449
  }
1434
1450
 
1451
+ _cachedProxy (instance, { agent, className } = {}) {
1452
+ // Instance ids are unique per agent only: a cached proxy is served
1453
+ // solely when it is the one match for the given agent and class - twins
1454
+ // on several agents are resolved through the topology instead
1455
+ const hits = Object.values(this._proxies).filter(
1456
+ x =>
1457
+ x.instance === instance &&
1458
+ (!agent || x.agent === agent) &&
1459
+ (!className || x.className === className)
1460
+ )
1461
+ return hits.length === 1 ? hits[0].proxy : null
1462
+ }
1463
+
1435
1464
  _getInstanceFromCache (instance, options = {}) {
1436
1465
  // when no agent is specified, but an explicit class default exists give
1437
1466
  // such instances priority in being found
1438
1467
  if (!options.agent && this._agent !== '*') {
1439
1468
  const { classes, status } = this._agents[this._agent] || {}
1440
- if (!classes || status === 'offline') {
1469
+ if (classes && status !== 'offline') {
1441
1470
  for (const className in classes) {
1442
1471
  if (options.className && className !== options.className) continue
1443
1472
  const { instances } = classes[className]