applesauce-core 5.0.3 → 5.1.0
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/README.md
CHANGED
|
@@ -16,8 +16,8 @@ Applesauce is a collection of utilities for building reactive nostr applications
|
|
|
16
16
|
|
|
17
17
|
For detailed documentation and guides, visit:
|
|
18
18
|
|
|
19
|
-
- [Getting Started](https://
|
|
20
|
-
- [API Reference](https://
|
|
19
|
+
- [Getting Started](https://applesauce.build/introduction/getting-started)
|
|
20
|
+
- [API Reference](https://applesauce.build/typedoc/)
|
|
21
21
|
|
|
22
22
|
## Example
|
|
23
23
|
|
|
@@ -1,7 +1,10 @@
|
|
|
1
|
-
import { NostrEvent } from "../helpers/event.js";
|
|
2
1
|
import { MonoTypeOperatorFunction } from "rxjs";
|
|
3
2
|
import { IAsyncEventStoreActions, IEventStoreActions } from "../event-store/interface.js";
|
|
4
|
-
|
|
5
|
-
|
|
3
|
+
import type { NostrEvent } from "../helpers/event.js";
|
|
4
|
+
/**
|
|
5
|
+
* Saves all events to an event store and filters out invalid events
|
|
6
|
+
* If a string is passed in, it will be passed through
|
|
7
|
+
*/
|
|
8
|
+
export declare function mapEventsToStore<T extends NostrEvent | string>(store: IEventStoreActions | IAsyncEventStoreActions, removeDuplicates?: boolean): MonoTypeOperatorFunction<T>;
|
|
6
9
|
/** Alias for {@link mapEventsToStore} */
|
|
7
|
-
export declare
|
|
10
|
+
export declare function filterDuplicateEvents<T extends NostrEvent | string>(store: IEventStoreActions | IAsyncEventStoreActions): MonoTypeOperatorFunction<T>;
|
|
@@ -1,23 +1,36 @@
|
|
|
1
|
-
import { catchError, distinct, filter, from, identity, mergeMap, of } from "rxjs";
|
|
2
|
-
/**
|
|
1
|
+
import { catchError, distinct, EMPTY, filter, from, identity, mergeMap, mergeWith, of, share, } from "rxjs";
|
|
2
|
+
/**
|
|
3
|
+
* Saves all events to an event store and filters out invalid events
|
|
4
|
+
* If a string is passed in, it will be passed through
|
|
5
|
+
*/
|
|
3
6
|
export function mapEventsToStore(store, removeDuplicates = true) {
|
|
4
|
-
return (source) =>
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
7
|
+
return (source) => {
|
|
8
|
+
const shared = source.pipe(share());
|
|
9
|
+
return shared.pipe(
|
|
10
|
+
// Map all events to the store
|
|
11
|
+
// NOTE: mergeMap is used here because we want to return the single instance of the event so that distinct() can be used later
|
|
12
|
+
mergeMap((event) => {
|
|
13
|
+
// Ignore strings
|
|
14
|
+
if (typeof event === "string")
|
|
15
|
+
return EMPTY;
|
|
16
|
+
const r = store.add(event);
|
|
17
|
+
// Unwrap the promise from the async store
|
|
18
|
+
if (r instanceof Promise)
|
|
19
|
+
return from(r);
|
|
20
|
+
else
|
|
21
|
+
return of(r);
|
|
22
|
+
}),
|
|
23
|
+
// Ignore errors when inserting events into the store
|
|
24
|
+
catchError(() => of(null)),
|
|
25
|
+
// Ignore invalid events
|
|
26
|
+
filter((e) => e !== null),
|
|
27
|
+
// Remove duplicates if requested
|
|
28
|
+
removeDuplicates ? distinct() : identity,
|
|
29
|
+
// Merge the strings back in if there are any
|
|
30
|
+
mergeWith(shared.pipe(filter((e) => typeof e === "string"))));
|
|
31
|
+
};
|
|
21
32
|
}
|
|
22
33
|
/** Alias for {@link mapEventsToStore} */
|
|
23
|
-
export
|
|
34
|
+
export function filterDuplicateEvents(store) {
|
|
35
|
+
return mapEventsToStore(store, true);
|
|
36
|
+
}
|
|
@@ -2,6 +2,7 @@ import { OperatorFunction } from "rxjs";
|
|
|
2
2
|
import { NostrEvent } from "../helpers/event.js";
|
|
3
3
|
/**
|
|
4
4
|
* Accumulate events into an ordered timeline
|
|
5
|
+
* @note If a string is passed in, it will be ignored and the timeline will not be modified
|
|
5
6
|
* @note This does not remove duplicate events
|
|
6
7
|
*/
|
|
7
|
-
export declare function mapEventsToTimeline(): OperatorFunction<
|
|
8
|
+
export declare function mapEventsToTimeline<T extends NostrEvent | string>(): OperatorFunction<T, NostrEvent[]>;
|
|
@@ -3,10 +3,16 @@ import { insertEventIntoDescendingList } from "../helpers/event.js";
|
|
|
3
3
|
import { withImmediateValueOrDefault } from "./with-immediate-value.js";
|
|
4
4
|
/**
|
|
5
5
|
* Accumulate events into an ordered timeline
|
|
6
|
+
* @note If a string is passed in, it will be ignored and the timeline will not be modified
|
|
6
7
|
* @note This does not remove duplicate events
|
|
7
8
|
*/
|
|
8
9
|
export function mapEventsToTimeline() {
|
|
9
|
-
return pipe(scan((timeline, event) =>
|
|
10
|
+
return pipe(scan((timeline, event) => {
|
|
11
|
+
if (typeof event === "string")
|
|
12
|
+
return timeline;
|
|
13
|
+
else
|
|
14
|
+
return insertEventIntoDescendingList(timeline, event);
|
|
15
|
+
}, []),
|
|
10
16
|
// Emit an empty array first. This is to prevent empty observables completing without a value (EMPTY)
|
|
11
17
|
withImmediateValueOrDefault([]));
|
|
12
18
|
}
|