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 { 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 IpfsHash } from 'npm:ipfs-only-hash@^4.0.0';
export { default as uuid62 } from 'npm:uuid62@^1.0.2'; 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'; 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 DittoFilter, matchDittoFilters } from '@/filter.ts';
import type { EventData } from '@/types.ts'; import type { EventData } from '@/types.ts';
class Subscription<K extends number = number> implements AsyncIterable<Event<K>> { class Subscription<K extends number = number> implements AsyncIterable<Event<K>> {
filters: DittoFilter<K>[]; filters: DittoFilter<K>[];
#next?: (event: Event<K>) => void; #machina: Machina<Event<K>>;
#closed = false;
constructor(filters: DittoFilter<K>[]) { constructor(filters: DittoFilter<K>[]) {
this.filters = filters; this.filters = filters;
this.#machina = new Machina();
} }
stream(event: Event<K>): void { stream(event: Event<K>): void {
if (this.#next) { this.#machina.push(event);
this.#next(event);
this.#next = undefined;
}
} }
matches(event: Event, data: EventData): boolean { matches(event: Event, data: EventData): boolean {
@ -24,22 +21,11 @@ class Subscription<K extends number = number> implements AsyncIterable<Event<K>>
} }
close() { close() {
this.#closed = true; this.#machina.close();
this.#next?.(undefined!);
} }
async *[Symbol.asyncIterator]() { [Symbol.asyncIterator]() {
while (true) { return this.#machina.stream();
const event = await new Promise<Event<K>>((resolve) => {
this.#next = resolve;
});
if (this.#closed) {
return;
}
yield event;
}
} }
} }