ditto/src/subscription.ts

33 lines
742 B
TypeScript
Raw Normal View History

import { type Event, Machina } from '@/deps.ts';
2023-08-25 18:38:21 +00:00
import { type DittoFilter, matchDittoFilters } from '@/filter.ts';
2023-08-25 18:38:21 +00:00
import type { EventData } from '@/types.ts';
class Subscription<K extends number = number> implements AsyncIterable<Event<K>> {
filters: DittoFilter<K>[];
#machina: Machina<Event<K>>;
constructor(filters: DittoFilter<K>[]) {
this.filters = filters;
this.#machina = new Machina();
}
stream(event: Event<K>): void {
this.#machina.push(event);
}
matches(event: Event, data: EventData): boolean {
return matchDittoFilters(this.filters, event, data);
}
close() {
this.#machina.close();
}
[Symbol.asyncIterator]() {
return this.#machina.stream();
}
}
export { Subscription };