applesauce-core 0.0.0-next-20241126184016 → 0.0.0-next-20241126192236
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.
|
@@ -28,6 +28,12 @@ export declare class EventStore {
|
|
|
28
28
|
events(ids: string[]): Observable<Map<string, NostrEvent>>;
|
|
29
29
|
/** Creates an observable with the latest version of a replaceable event */
|
|
30
30
|
replaceable(kind: number, pubkey: string, d?: string): Observable<NostrEvent | undefined>;
|
|
31
|
+
/** Creates an observable with the latest versions of replaceable events */
|
|
32
|
+
replaceableSet(pointers: {
|
|
33
|
+
kind: number;
|
|
34
|
+
pubkey: string;
|
|
35
|
+
identifier?: string;
|
|
36
|
+
}[]): Observable<Map<string, NostrEvent>>;
|
|
31
37
|
/** Creates an observable that streams all events that match the filter */
|
|
32
38
|
stream(filters: Filter[]): Observable<NostrEvent>;
|
|
33
39
|
/** Creates an observable that updates with an array of sorted events */
|
|
@@ -225,6 +225,63 @@ export class EventStore {
|
|
|
225
225
|
};
|
|
226
226
|
});
|
|
227
227
|
}
|
|
228
|
+
/** Creates an observable with the latest versions of replaceable events */
|
|
229
|
+
replaceableSet(pointers) {
|
|
230
|
+
return new Observable((observer) => {
|
|
231
|
+
const coords = pointers.map((p) => getReplaceableUID(p.kind, p.pubkey, p.identifier));
|
|
232
|
+
const events = new Map();
|
|
233
|
+
const handleEvent = (event) => {
|
|
234
|
+
const uid = getEventUID(event);
|
|
235
|
+
const current = events.get(uid);
|
|
236
|
+
if (current) {
|
|
237
|
+
if (event.created_at > current.created_at) {
|
|
238
|
+
this.database.removeClaim(current, observer);
|
|
239
|
+
}
|
|
240
|
+
else
|
|
241
|
+
return;
|
|
242
|
+
}
|
|
243
|
+
events.set(uid, event);
|
|
244
|
+
this.database.claimEvent(event, observer);
|
|
245
|
+
};
|
|
246
|
+
// get latest version
|
|
247
|
+
for (const pointer of pointers) {
|
|
248
|
+
const events = this.database.getReplaceable(pointer.kind, pointer.pubkey, pointer.identifier);
|
|
249
|
+
if (events)
|
|
250
|
+
for (const event of events)
|
|
251
|
+
handleEvent(event);
|
|
252
|
+
}
|
|
253
|
+
// subscribe to future events
|
|
254
|
+
const inserted = this.database.inserted.subscribe((event) => {
|
|
255
|
+
if (isReplaceable(event.kind) && coords.includes(getEventUID(event))) {
|
|
256
|
+
handleEvent(event);
|
|
257
|
+
observer.next(events);
|
|
258
|
+
}
|
|
259
|
+
});
|
|
260
|
+
// subscribe to updated events
|
|
261
|
+
const updated = this.database.updated.subscribe((event) => {
|
|
262
|
+
if (isReplaceable(event.kind) && coords.includes(getEventUID(event))) {
|
|
263
|
+
observer.next(events);
|
|
264
|
+
}
|
|
265
|
+
});
|
|
266
|
+
// subscribe to deleted events
|
|
267
|
+
const deleted = this.database.deleted.subscribe((event) => {
|
|
268
|
+
const uid = getEventUID(event);
|
|
269
|
+
if (events.has(uid)) {
|
|
270
|
+
events.delete(uid);
|
|
271
|
+
this.database.removeClaim(event, observer);
|
|
272
|
+
observer.next(events);
|
|
273
|
+
}
|
|
274
|
+
});
|
|
275
|
+
return () => {
|
|
276
|
+
inserted.unsubscribe();
|
|
277
|
+
deleted.unsubscribe();
|
|
278
|
+
updated.unsubscribe();
|
|
279
|
+
for (const [_id, event] of events) {
|
|
280
|
+
this.database.removeClaim(event, observer);
|
|
281
|
+
}
|
|
282
|
+
};
|
|
283
|
+
});
|
|
284
|
+
}
|
|
228
285
|
/** Creates an observable that streams all events that match the filter */
|
|
229
286
|
stream(filters) {
|
|
230
287
|
return new Observable((observer) => {
|
package/dist/queries/simple.js
CHANGED
|
@@ -30,9 +30,8 @@ export function TimelineQuery(filters, keepOldVersions) {
|
|
|
30
30
|
}
|
|
31
31
|
/** Creates a Query that returns a directory of events by their UID */
|
|
32
32
|
export function ReplaceableSetQuery(pointers) {
|
|
33
|
-
const cords = pointers.map((pointer) => getReplaceableUID(pointer.kind, pointer.pubkey, pointer.identifier));
|
|
34
33
|
return {
|
|
35
34
|
key: hash_sum(pointers),
|
|
36
|
-
run: (events) => events.
|
|
35
|
+
run: (events) => events.replaceableSet(pointers),
|
|
37
36
|
};
|
|
38
37
|
}
|