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