sen-ether-client 0.1.5 → 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 +77 -22
  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
  }
@@ -364,12 +371,7 @@ export class Sen extends EventEmitter {
364
371
  throw new Error('no SEN ether processes discovered');
365
372
  }
366
373
  this.targets = targets;
367
- for (const target of targets) {
368
- const sessionName = targetSessionName(target);
369
- if (sessionName && !this.targetsBySession.has(sessionName)) {
370
- this.targetsBySession.set(sessionName, target);
371
- }
372
- }
374
+ this.#rememberTargets(targets, { replace: false });
373
375
  this.emit('connect', {
374
376
  sessions: [...this.targetsBySession.keys()],
375
377
  targets
@@ -602,8 +604,7 @@ export class Sen extends EventEmitter {
602
604
  }
603
605
 
604
606
  const sessionName = this.target?.session?.name ?? this.client.processInfo.sessionName;
605
- return [...this.remoteBuses]
606
- .sort()
607
+ return knownBusNames(this)
607
608
  .map(busName => options.qualified ? queryBusName(sessionName, busName) : busName);
608
609
  }
609
610
 
@@ -621,24 +622,68 @@ export class Sen extends EventEmitter {
621
622
 
622
623
  if (this.client) {
623
624
  const sessionName = this.target?.session?.name ?? this.client.processInfo.sessionName;
624
- return (await waitForSessionBuses(this, settleMs))
625
- .map(busName => busSummary(sessionName, busName))
626
- .filter(Boolean)
627
- .sort((a, b) => a.qualified.localeCompare(b.qualified));
628
- }
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
+ };
629
632
 
630
- if (!this.targets.length && !this.sessions.size) {
631
- const targets = await this.#discoverTargets(config);
632
- if (!targets.length) {
633
- throw new Error('no SEN ether processes discovered');
633
+ for (const busName of await waitForSessionBuses(this, settleMs)) {
634
+ addBus(busName);
634
635
  }
635
- this.targets = targets;
636
- for (const target of targets) {
637
- const sessionName = targetSessionName(target);
638
- if (sessionName && !this.targetsBySession.has(sessionName)) {
639
- 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);
640
664
  }
641
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');
642
687
  }
643
688
 
644
689
  const summaries = new Map();
@@ -782,6 +827,16 @@ export class Sen extends EventEmitter {
782
827
  return target;
783
828
  }
784
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
+
785
840
  async #reconnectTarget(options) {
786
841
  if (options.tcpHub || !options.target) {
787
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.5",
3
+ "version": "0.1.6",
4
4
  "description": "Pure JavaScript SEN client for existing kernels over ether",
5
5
  "senCompatibility": {
6
6
  "kernelProtocolVersion": 9,