applesauce-core 0.0.0-next-20241126222927 → 0.0.0-next-20241127180935

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.
@@ -202,27 +202,21 @@ export class Database {
202
202
  let sinceIndex = this.created_at.length - 1;
203
203
  let start = until
204
204
  ? binarySearch(this.created_at, (mid) => {
205
- if (mid.created_at === until)
206
- return -1;
207
205
  return mid.created_at - until;
208
206
  })
209
207
  : undefined;
210
- if (start && start[1])
208
+ if (start)
211
209
  untilIndex = start[0];
212
210
  const end = since
213
211
  ? binarySearch(this.created_at, (mid) => {
214
- if (mid.created_at === since)
215
- return 1;
216
- return since - mid.created_at;
212
+ return mid.created_at - since;
217
213
  })
218
214
  : undefined;
219
- if (end && end[1])
215
+ if (end)
220
216
  sinceIndex = end[0];
221
- const events = new Set();
222
- for (let i = untilIndex; i <= sinceIndex; i++) {
223
- events.add(this.created_at[i]);
217
+ for (let i = untilIndex; i < sinceIndex; i++) {
218
+ yield this.created_at[i];
224
219
  }
225
- return events;
226
220
  }
227
221
  *iterateIds(ids) {
228
222
  for (const id of ids) {
@@ -21,7 +21,7 @@ export declare class EventStore {
21
21
  /** Gets the latest version of a replaceable event */
22
22
  getReplaceable(kind: number, pubkey: string, d?: string): NostrEvent | undefined;
23
23
  /** Returns all versions of a replaceable event */
24
- getAllReplaceable(kind: number, pubkey: string, d?: string): NostrEvent[] | undefined;
24
+ getReplaceableHistory(kind: number, pubkey: string, d?: string): NostrEvent[] | undefined;
25
25
  /** Creates an observable that updates a single event */
26
26
  event(id: string): Observable<NostrEvent | undefined>;
27
27
  /** Creates an observable that subscribes to multiple events */
@@ -91,7 +91,7 @@ export class EventStore {
91
91
  return this.database.getReplaceable(kind, pubkey, d)?.[0];
92
92
  }
93
93
  /** Returns all versions of a replaceable event */
94
- getAllReplaceable(kind, pubkey, d) {
94
+ getReplaceableHistory(kind, pubkey, d) {
95
95
  return this.database.getReplaceable(kind, pubkey, d);
96
96
  }
97
97
  /** Creates an observable that updates a single event */
@@ -20,7 +20,7 @@ export function matchFilter(filter, event) {
20
20
  let values = filter[`#${tagName}`];
21
21
  if (values) {
22
22
  const tags = getIndexableTags(event);
23
- if (values.some((v) => !tags.has(tagName + ":" + v)))
23
+ if (values.some((v) => tags.has(tagName + ":" + v)) === false)
24
24
  return false;
25
25
  }
26
26
  }
@@ -1,5 +1,5 @@
1
- import { getReplaceableUID } from "../helpers/event.js";
2
1
  import hash_sum from "hash-sum";
2
+ import { getReplaceableUID } from "../helpers/event.js";
3
3
  /** Creates a Query that returns a single event or undefined */
4
4
  export function SingleEventQuery(id) {
5
5
  return {
@@ -23,9 +23,10 @@ export function ReplaceableQuery(kind, pubkey, d) {
23
23
  }
24
24
  /** Creates a Query that returns an array of sorted events matching the filters */
25
25
  export function TimelineQuery(filters, keepOldVersions) {
26
+ filters = Array.isArray(filters) ? filters : [filters];
26
27
  return {
27
28
  key: hash_sum(filters) + (keepOldVersions ? "-history" : ""),
28
- run: (events) => events.timeline(Array.isArray(filters) ? filters : [filters], keepOldVersions),
29
+ run: (events) => events.timeline(filters, keepOldVersions),
29
30
  };
30
31
  }
31
32
  /** Creates a Query that returns a directory of events by their UID */
@@ -35,3 +36,5 @@ export function ReplaceableSetQuery(pointers) {
35
36
  run: (events) => events.replaceableSet(pointers),
36
37
  };
37
38
  }
39
+ // @ts-expect-error
40
+ window.hash_sum = hash_sum;
@@ -6,6 +6,7 @@ import * as Queries from "../queries/index.js";
6
6
  import { AddressPointer, EventPointer } from "nostr-tools/nip19";
7
7
  export type Query<T extends unknown> = {
8
8
  key: string;
9
+ args?: Array<any>;
9
10
  run: (events: EventStore, store: QueryStore) => Observable<T>;
10
11
  };
11
12
  export type QueryConstructor<T extends unknown, Args extends Array<any>> = (...args: Args) => Query<T>;
@@ -13,7 +14,8 @@ export declare class QueryStore {
13
14
  static Queries: typeof Queries;
14
15
  store: EventStore;
15
16
  constructor(store: EventStore);
16
- queries: LRU<Observable<any> | BehaviorSubject<any>>;
17
+ queries: LRU<Query<any>>;
18
+ observables: WeakMap<Query<any>, Observable<any> | BehaviorSubject<any>>;
17
19
  /** Creates a cached query */
18
20
  runQuery<T extends unknown, Args extends Array<any>>(queryConstructor: (...args: Args) => {
19
21
  key: string;
@@ -8,17 +8,24 @@ export class QueryStore {
8
8
  this.store = store;
9
9
  }
10
10
  queries = new LRU();
11
+ observables = new WeakMap();
11
12
  /** Creates a cached query */
12
13
  runQuery(queryConstructor) {
13
14
  return (...args) => {
14
- const query = queryConstructor(...args);
15
- const key = `${queryConstructor.name}|${query.key}`;
16
- if (!this.queries.has(key)) {
15
+ const tempQuery = queryConstructor(...args);
16
+ const key = `${queryConstructor.name}|${tempQuery.key}`;
17
+ let query = this.queries.get(key);
18
+ if (!query) {
19
+ query = tempQuery;
20
+ this.queries.set(key, tempQuery);
21
+ }
22
+ if (!this.observables.has(query)) {
23
+ query.args = args;
17
24
  const observable = query.run(this.store, this).pipe(shareLatestValue());
18
- this.queries.set(key, observable);
25
+ this.observables.set(query, observable);
19
26
  return observable;
20
27
  }
21
- return this.queries.get(key);
28
+ return this.observables.get(query);
22
29
  };
23
30
  }
24
31
  /** Returns a single event */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "applesauce-core",
3
- "version": "0.0.0-next-20241126222927",
3
+ "version": "0.0.0-next-20241127180935",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",