applesauce-core 0.0.0-next-20241126195214 → 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.
- package/dist/event-store/database.js +5 -11
- package/dist/event-store/event-store.d.ts +1 -1
- package/dist/event-store/event-store.js +3 -3
- package/dist/helpers/filter.js +1 -1
- package/dist/queries/simple.js +5 -2
- package/dist/query-store/index.d.ts +3 -1
- package/dist/query-store/index.js +12 -5
- package/package.json +1 -1
|
@@ -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
|
|
208
|
+
if (start)
|
|
211
209
|
untilIndex = start[0];
|
|
212
210
|
const end = since
|
|
213
211
|
? binarySearch(this.created_at, (mid) => {
|
|
214
|
-
|
|
215
|
-
return 1;
|
|
216
|
-
return since - mid.created_at;
|
|
212
|
+
return mid.created_at - since;
|
|
217
213
|
})
|
|
218
214
|
: undefined;
|
|
219
|
-
if (end
|
|
215
|
+
if (end)
|
|
220
216
|
sinceIndex = end[0];
|
|
221
|
-
|
|
222
|
-
|
|
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
|
-
|
|
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 */
|
|
@@ -6,7 +6,7 @@ import { Database } from "./database.js";
|
|
|
6
6
|
import { getEventUID, getReplaceableUID, getTagValue, isReplaceable } from "../helpers/event.js";
|
|
7
7
|
import { matchFilters } from "../helpers/filter.js";
|
|
8
8
|
import { addSeenRelay } from "../helpers/relays.js";
|
|
9
|
-
import { getDeleteIds } from "../helpers/delete.js";
|
|
9
|
+
import { getDeleteCoordinates, getDeleteIds } from "../helpers/delete.js";
|
|
10
10
|
export class EventStore {
|
|
11
11
|
database;
|
|
12
12
|
/** Whether to keep old versions of replaceable events */
|
|
@@ -51,7 +51,7 @@ export class EventStore {
|
|
|
51
51
|
if (event)
|
|
52
52
|
this.database.deleteEvent(event);
|
|
53
53
|
}
|
|
54
|
-
const coords =
|
|
54
|
+
const coords = getDeleteCoordinates(deleteEvent);
|
|
55
55
|
for (const coord of coords) {
|
|
56
56
|
this.deletedCoords.set(coord, Math.max(this.deletedCoords.get(coord) ?? 0, deleteEvent.created_at));
|
|
57
57
|
// remove deleted events in the database
|
|
@@ -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
|
-
|
|
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 */
|
package/dist/helpers/filter.js
CHANGED
|
@@ -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) =>
|
|
23
|
+
if (values.some((v) => tags.has(tagName + ":" + v)) === false)
|
|
24
24
|
return false;
|
|
25
25
|
}
|
|
26
26
|
}
|
package/dist/queries/simple.js
CHANGED
|
@@ -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(
|
|
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<
|
|
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
|
|
15
|
-
const key = `${queryConstructor.name}|${
|
|
16
|
-
|
|
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.
|
|
25
|
+
this.observables.set(query, observable);
|
|
19
26
|
return observable;
|
|
20
27
|
}
|
|
21
|
-
return this.
|
|
28
|
+
return this.observables.get(query);
|
|
22
29
|
};
|
|
23
30
|
}
|
|
24
31
|
/** Returns a single event */
|