@elisym/sdk 0.3.2 → 0.3.3

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/dist/index.cjs CHANGED
@@ -1596,12 +1596,18 @@ var PingService = class _PingService {
1596
1596
  constructor(pool) {
1597
1597
  this.pool = pool;
1598
1598
  this.sessionIdentity = ElisymIdentity.generate();
1599
+ pool.onReset(() => this.clearCache());
1599
1600
  }
1600
1601
  static PING_CACHE_MAX = 1e3;
1601
1602
  sessionIdentity;
1602
1603
  pingCache = /* @__PURE__ */ new Map();
1603
1604
  // pubkey - timestamp of last online result
1604
1605
  pendingPings = /* @__PURE__ */ new Map();
1606
+ /** Drop cached online results. In-flight pings are left alone - they'll
1607
+ * resolve via their own timeouts and remove themselves from `pendingPings`. */
1608
+ clearCache() {
1609
+ this.pingCache.clear();
1610
+ }
1605
1611
  /**
1606
1612
  * Ping an agent via ephemeral Nostr events (kind 20200/20201).
1607
1613
  * Uses a persistent session identity to avoid relay rate-limiting.
@@ -1800,10 +1806,28 @@ var NostrPool = class {
1800
1806
  pool;
1801
1807
  relays;
1802
1808
  activeSubscriptions = /* @__PURE__ */ new Set();
1809
+ resetListeners = /* @__PURE__ */ new Set();
1803
1810
  constructor(relays = RELAYS) {
1804
1811
  this.pool = new nostrTools.SimplePool();
1805
1812
  this.relays = relays;
1806
1813
  }
1814
+ /**
1815
+ * Register a callback to run after `reset()` completes (new SimplePool in place).
1816
+ * Services that cache pool-derived state (e.g. ping results) must clear it here,
1817
+ * otherwise stale values survive the reconnect. Returns an unsubscribe function.
1818
+ *
1819
+ * Contract:
1820
+ * - Listeners are invoked **synchronously** at the end of `reset()`, in
1821
+ * registration order. Do not rely on async work inside a listener having
1822
+ * completed before `reset()` returns.
1823
+ * - Listener exceptions are caught and swallowed so that one faulty listener
1824
+ * cannot prevent the others from running (or abort the reset itself).
1825
+ * If a listener needs to surface errors, it must do so out-of-band.
1826
+ */
1827
+ onReset(listener) {
1828
+ this.resetListeners.add(listener);
1829
+ return () => this.resetListeners.delete(listener);
1830
+ }
1807
1831
  /** Query relays synchronously. Returns `[]` on timeout (no error thrown). */
1808
1832
  async querySync(filter) {
1809
1833
  let timer;
@@ -1811,13 +1835,12 @@ var NostrPool = class {
1811
1835
  query.catch(() => {
1812
1836
  });
1813
1837
  try {
1814
- const result = await Promise.race([
1838
+ return await Promise.race([
1815
1839
  query,
1816
1840
  new Promise((resolve) => {
1817
1841
  timer = setTimeout(() => resolve([]), DEFAULTS.QUERY_TIMEOUT_MS);
1818
1842
  })
1819
1843
  ]);
1820
- return result;
1821
1844
  } finally {
1822
1845
  clearTimeout(timer);
1823
1846
  }
@@ -1999,6 +2022,12 @@ var NostrPool = class {
1999
2022
  } catch {
2000
2023
  }
2001
2024
  this.pool = new nostrTools.SimplePool();
2025
+ for (const listener of this.resetListeners) {
2026
+ try {
2027
+ listener();
2028
+ } catch {
2029
+ }
2030
+ }
2002
2031
  }
2003
2032
  /**
2004
2033
  * Lightweight connectivity probe. Returns true if at least one relay responds.