Skip to content

Commit

Permalink
simplify implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
noomorph committed May 17, 2024
1 parent 74e8057 commit d3269f0
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 55 deletions.
19 changes: 10 additions & 9 deletions src/emitters/SerialAsyncEmitter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { arraysEqual } from '../utils';
import type { AsyncEmitter } from './AsyncEmitter';
import { logError } from './logError';
import { ReadonlyEmitterBase } from './ReadonlyEmitterBase';
Expand All @@ -8,8 +7,8 @@ export class SerialAsyncEmitter<EventMap>
extends ReadonlyEmitterBase<EventMap>
implements AsyncEmitter<EventMap>
{
#tasks: Promise<void>[] = [];
#idle?: Promise<void>;
readonly #tasks: Promise<void>[] = [];

emit<K extends keyof EventMap>(eventType: K, event: EventMap[K]): Promise<void> {
this.#tasks.push(this.#doEmit(eventType, event));
Expand All @@ -18,13 +17,15 @@ export class SerialAsyncEmitter<EventMap>
}

async #waitForIdle() {
let $promises: Promise<void>[] = [];
while (!arraysEqual($promises, this.#tasks)) {
$promises = [...this.#tasks];
await Promise.all($promises);
}

this.#tasks.splice(0, this.#tasks.length);
do {
const $promises = new Set(this.#tasks);
await Promise.all(this.#tasks);
for (let index = this.#tasks.length - 1; index >= 0; index--) {
if ($promises.has(this.#tasks[index])) {
this.#tasks.splice(index, 1);
}
}
} while (this.#tasks.length > 0);
this.#idle = undefined;
}

Expand Down
33 changes: 0 additions & 33 deletions src/utils/arraysEqual.test.ts

This file was deleted.

12 changes: 0 additions & 12 deletions src/utils/arraysEqual.ts

This file was deleted.

1 change: 0 additions & 1 deletion src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export * from './arraysEqual';
export * from './assertions';
export * from './getHierarchy';
export * from './iterateSorted';
Expand Down

0 comments on commit d3269f0

Please sign in to comment.