sen-ether-client 0.1.4 → 0.1.6

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 (2) hide show
  1. package/lib/sen.js +81 -29
  2. package/package.json +1 -1
package/lib/sen.js CHANGED
@@ -132,6 +132,13 @@ function busSummary(sessionName, busName) {
132
132
  };
133
133
  }
134
134
 
135
+ function knownBusNames(sen) {
136
+ return [...new Set([
137
+ ...sen.remoteBuses,
138
+ ...sen.buses.keys()
139
+ ])].sort();
140
+ }
141
+
135
142
  function selectorDescription(selector) {
136
143
  return typeof selector === 'function' ? '<predicate>' : String(selector);
137
144
  }
@@ -159,17 +166,14 @@ function stateRequestKey(interestId, objectId) {
159
166
  }
160
167
 
161
168
  async function waitForSessionBuses(session, timeoutMs) {
162
- let buses = session.listBuses();
163
- if (buses.length || timeoutMs <= 0) {
164
- return buses;
169
+ if (timeoutMs <= 0) {
170
+ return session.listBuses();
165
171
  }
166
-
167
172
  const deadline = Date.now() + timeoutMs;
168
- while (!buses.length && Date.now() < deadline) {
173
+ while (Date.now() < deadline) {
169
174
  await wait(Math.min(50, Math.max(1, deadline - Date.now())));
170
- buses = session.listBuses();
171
175
  }
172
- return buses;
176
+ return session.listBuses();
173
177
  }
174
178
 
175
179
  class ChangeBatcher {
@@ -367,12 +371,7 @@ export class Sen extends EventEmitter {
367
371
  throw new Error('no SEN ether processes discovered');
368
372
  }
369
373
  this.targets = targets;
370
- for (const target of targets) {
371
- const sessionName = targetSessionName(target);
372
- if (sessionName && !this.targetsBySession.has(sessionName)) {
373
- this.targetsBySession.set(sessionName, target);
374
- }
375
- }
374
+ this.#rememberTargets(targets, { replace: false });
376
375
  this.emit('connect', {
377
376
  sessions: [...this.targetsBySession.keys()],
378
377
  targets
@@ -605,8 +604,7 @@ export class Sen extends EventEmitter {
605
604
  }
606
605
 
607
606
  const sessionName = this.target?.session?.name ?? this.client.processInfo.sessionName;
608
- return [...this.remoteBuses]
609
- .sort()
607
+ return knownBusNames(this)
610
608
  .map(busName => options.qualified ? queryBusName(sessionName, busName) : busName);
611
609
  }
612
610
 
@@ -624,24 +622,68 @@ export class Sen extends EventEmitter {
624
622
 
625
623
  if (this.client) {
626
624
  const sessionName = this.target?.session?.name ?? this.client.processInfo.sessionName;
627
- return (await waitForSessionBuses(this, settleMs))
628
- .map(busName => busSummary(sessionName, busName))
629
- .filter(Boolean)
630
- .sort((a, b) => a.qualified.localeCompare(b.qualified));
631
- }
625
+ const summaries = new Map();
626
+ const addBus = busName => {
627
+ const summary = busSummary(sessionName, busName);
628
+ if (summary) {
629
+ summaries.set(summary.qualified, summary);
630
+ }
631
+ };
632
632
 
633
- if (!this.targets.length && !this.sessions.size) {
634
- const targets = await this.#discoverTargets(config);
635
- if (!targets.length) {
636
- throw new Error('no SEN ether processes discovered');
633
+ for (const busName of await waitForSessionBuses(this, settleMs)) {
634
+ addBus(busName);
637
635
  }
638
- this.targets = targets;
639
- for (const target of targets) {
640
- const sessionName = targetSessionName(target);
641
- if (sessionName && !this.targetsBySession.has(sessionName)) {
642
- this.targetsBySession.set(sessionName, target);
636
+
637
+ if (!summaries.size || config.refreshTargets === true) {
638
+ try {
639
+ const target = await this.#discoverTarget({ ...config, session: sessionName });
640
+ if (target) {
641
+ const session = new Sen({
642
+ ...config,
643
+ session: sessionName,
644
+ reconnect: false
645
+ });
646
+ session.on('warning', error => this.emit('warning', error));
647
+ session.on('error', error => this.emit('warning', error));
648
+ try {
649
+ await session.connect({
650
+ ...config,
651
+ session: sessionName,
652
+ target,
653
+ reconnect: false
654
+ });
655
+ for (const busName of await waitForSessionBuses(session, settleMs)) {
656
+ addBus(busName);
657
+ }
658
+ } finally {
659
+ await session.close().catch(error => this.emit('warning', error));
660
+ }
661
+ }
662
+ } catch (error) {
663
+ this.emit('warning', error);
643
664
  }
644
665
  }
666
+
667
+ return [...summaries.values()].sort((a, b) => a.qualified.localeCompare(b.qualified));
668
+ }
669
+
670
+ let discoveredTargets = [];
671
+ if (!this.targets.length || this.sessions.size || config.refreshTargets === true) {
672
+ try {
673
+ discoveredTargets = await this.#discoverTargets(config);
674
+ } catch (error) {
675
+ if (!this.targets.length && !this.sessions.size) {
676
+ throw error;
677
+ }
678
+ this.emit('warning', error);
679
+ }
680
+ }
681
+
682
+ if (discoveredTargets.length) {
683
+ this.targets = discoveredTargets;
684
+ this.#rememberTargets(discoveredTargets, { replace: true });
685
+ } else if (!this.targets.length && !this.sessions.size) {
686
+ throw new Error('no SEN ether processes discovered');
645
687
  }
646
688
 
647
689
  const summaries = new Map();
@@ -785,6 +827,16 @@ export class Sen extends EventEmitter {
785
827
  return target;
786
828
  }
787
829
 
830
+ #rememberTargets(targets, options = {}) {
831
+ const replace = options.replace !== false;
832
+ for (const target of targets) {
833
+ const sessionName = targetSessionName(target);
834
+ if (sessionName && (replace || !this.targetsBySession.has(sessionName))) {
835
+ this.targetsBySession.set(sessionName, target);
836
+ }
837
+ }
838
+ }
839
+
788
840
  async #reconnectTarget(options) {
789
841
  if (options.tcpHub || !options.target) {
790
842
  return await this.#discoverTarget(options);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sen-ether-client",
3
- "version": "0.1.4",
3
+ "version": "0.1.6",
4
4
  "description": "Pure JavaScript SEN client for existing kernels over ether",
5
5
  "senCompatibility": {
6
6
  "kernelProtocolVersion": 9,