Subscription: fix it with nostr-machina

This commit is contained in:
Alex Gleason 2023-10-04 16:09:23 -05:00
parent 5847d95121
commit 233f671326
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
2 changed files with 8 additions and 21 deletions

View File

@ -71,5 +71,6 @@ export * as cron from 'https://deno.land/x/deno_cron@v1.0.0/cron.ts';
export { S3Client } from 'https://deno.land/x/s3_lite_client@0.6.1/mod.ts';
export { default as IpfsHash } from 'npm:ipfs-only-hash@^4.0.0';
export { default as uuid62 } from 'npm:uuid62@^1.0.2';
export { Machina } from 'https://gitlab.com/soapbox-pub/nostr-machina/-/raw/08a157d39f2741c9a3a4364cb97db36e71d8c03a/mod.ts';
export type * as TypeFest from 'npm:type-fest@^4.3.0';

View File

@ -1,22 +1,19 @@
import { type Event } from '@/deps.ts';
import { type Event, Machina } from '@/deps.ts';
import { type DittoFilter, matchDittoFilters } from '@/filter.ts';
import type { EventData } from '@/types.ts';
class Subscription<K extends number = number> implements AsyncIterable<Event<K>> {
filters: DittoFilter<K>[];
#next?: (event: Event<K>) => void;
#closed = false;
#machina: Machina<Event<K>>;
constructor(filters: DittoFilter<K>[]) {
this.filters = filters;
this.#machina = new Machina();
}
stream(event: Event<K>): void {
if (this.#next) {
this.#next(event);
this.#next = undefined;
}
this.#machina.push(event);
}
matches(event: Event, data: EventData): boolean {
@ -24,22 +21,11 @@ class Subscription<K extends number = number> implements AsyncIterable<Event<K>>
}
close() {
this.#closed = true;
this.#next?.(undefined!);
this.#machina.close();
}
async *[Symbol.asyncIterator]() {
while (true) {
const event = await new Promise<Event<K>>((resolve) => {
this.#next = resolve;
});
if (this.#closed) {
return;
}
yield event;
}
[Symbol.asyncIterator]() {
return this.#machina.stream();
}
}