applesauce-relay 6.0.2 → 6.0.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/pool.d.ts CHANGED
@@ -4,15 +4,18 @@ import { FilterMap, OutboxMap } from "applesauce-core/helpers/relay-selection";
4
4
  import { BehaviorSubject, Observable, Subject } from "rxjs";
5
5
  import { RelayGroup } from "./group.js";
6
6
  import type { NegentropySyncOptions, ReconcileFunction } from "./negentropy.js";
7
- import { Relay, SyncDirection, type RelayOptions } from "./relay.js";
8
- import type { RelayCountResponse, FilterInput, GroupReqOptions, GroupReqMessage, PoolRelayInput, NegentropyReadStore, NegentropySyncStore, PublishResponse, RelayStatus } from "./types.js";
7
+ import { Relay, type RelayOptions, SyncDirection } from "./relay.js";
8
+ import type { FilterInput, GroupReqMessage, GroupReqOptions, NegentropyReadStore, NegentropySyncStore, PoolRelayInput, PublishResponse, RelayCountResponse, RelayStatus } from "./types.js";
9
9
  export declare class RelayPool {
10
10
  options?: RelayOptions | undefined;
11
11
  relays$: BehaviorSubject<Map<string, Relay>>;
12
12
  get relays(): Map<string, Relay>;
13
13
  /** Observable of relay status for all relays in the pool */
14
14
  status$: Observable<Record<string, RelayStatus>>;
15
- /** Whether to ignore relays that are ready=false */
15
+ /**
16
+ * Whether to ignore relays that are ready=false
17
+ * @deprecated use {@link ignoreUnhealthyRelays} in group() or request() input
18
+ */
16
19
  ignoreOffline: boolean;
17
20
  /** A signal when a relay is added */
18
21
  add$: Subject<Relay>;
package/dist/pool.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import { isFilterEqual } from "applesauce-core/helpers/filter";
2
2
  import { createFilterMap } from "applesauce-core/helpers/relay-selection";
3
3
  import { normalizeURL } from "applesauce-core/helpers/url";
4
- import { BehaviorSubject, distinctUntilChanged, isObservable, map, merge, of, scan, shareReplay, startWith, Subject, switchMap, } from "rxjs";
4
+ import { BehaviorSubject, combineLatest, distinctUntilChanged, filter, isObservable, map, merge, of, scan, shareReplay, startWith, Subject, switchMap, take, } from "rxjs";
5
5
  import { RelayGroup } from "./group.js";
6
6
  import { Relay } from "./relay.js";
7
7
  export class RelayPool {
@@ -12,8 +12,11 @@ export class RelayPool {
12
12
  }
13
13
  /** Observable of relay status for all relays in the pool */
14
14
  status$;
15
- /** Whether to ignore relays that are ready=false */
16
- ignoreOffline = true;
15
+ /**
16
+ * Whether to ignore relays that are ready=false
17
+ * @deprecated use {@link ignoreUnhealthyRelays} in group() or request() input
18
+ */
19
+ ignoreOffline = false;
17
20
  /** A signal when a relay is added */
18
21
  add$ = new Subject();
19
22
  /** A signal when a relay is removed */
@@ -55,11 +58,17 @@ export class RelayPool {
55
58
  ? relays.map((url) => this.relay(url))
56
59
  : relays.pipe(map((urls) => urls.map((url) => this.relay(url))));
57
60
  if (ignoreOffline) {
58
- // Filter out the offline relays
59
- if (Array.isArray(input))
60
- input = input.filter((relay) => relay.ready);
61
- else
62
- input = input.pipe(map((relays) => relays.filter((relay) => relay.ready)));
61
+ // Convert input to an observable so it can react to relays becoming ready.
62
+ // Each relay is included once `ready$` first emits true, and stays included
63
+ // afterwards (subsequent ready=false changes are ignored since the request
64
+ // or subscription will have already started).
65
+ const input$ = Array.isArray(input) ? of(input) : input;
66
+ input = input$.pipe(switchMap((relays) => {
67
+ if (relays.length === 0)
68
+ return of([]);
69
+ const signals = relays.map((relay) => relay.ready$.pipe(filter((ready) => ready), take(1), map(() => relay), startWith(null)));
70
+ return combineLatest(signals).pipe(map((arr) => arr.filter((r) => r !== null)));
71
+ }));
63
72
  }
64
73
  return new RelayGroup(input);
65
74
  }
@@ -84,17 +93,15 @@ export class RelayPool {
84
93
  }
85
94
  /** Make a REQ to multiple relays that does not deduplicate events */
86
95
  req(relays, filters, opts) {
87
- // Never filter out offline relays in manual methods
88
- return this.group(relays, false).req(filters, opts);
96
+ return this.group(relays).req(filters, opts);
89
97
  }
90
98
  /** Send an EVENT message to multiple relays */
91
99
  event(relays, event) {
92
- // Never filter out offline relays in manual methods
93
- return this.group(relays, false).event(event);
100
+ return this.group(relays).event(event);
94
101
  }
95
102
  /** Negentropy sync event ids with the relays and an event store */
96
103
  negentropy(relays, store, filter, reconcile, opts) {
97
- return this.group(relays, false).negentropy(store, filter, reconcile, opts);
104
+ return this.group(relays).negentropy(store, filter, reconcile, opts);
98
105
  }
99
106
  /** Publish an event to multiple relays */
100
107
  publish(relays, event, opts) {
@@ -134,10 +141,12 @@ export class RelayPool {
134
141
  }
135
142
  /** Count events on multiple relays */
136
143
  count(relays, filters, id) {
137
- return this.group(relays).count(filters, id);
144
+ // Never filter out offline relays in manual methods
145
+ return this.group(relays, false).count(filters, id);
138
146
  }
139
147
  /** Negentropy sync events with the relays and an event store */
140
148
  sync(relays, store, filter, direction) {
141
- return this.group(relays).sync(store, filter, direction);
149
+ // Never filter out offline relays in manual methods
150
+ return this.group(relays, false).sync(store, filter, direction);
142
151
  }
143
152
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "applesauce-relay",
3
- "version": "6.0.2",
3
+ "version": "6.0.3",
4
4
  "description": "nostr relay communication framework built on rxjs",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",