Repository navigation
🐛 Make @effectionx/node once() scope-bound
#252
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,209 @@ | ||
| import { EventEmitter } from "node:events"; | ||
| import { describe, it } from "@effectionx/vitest"; | ||
| import { type Operation, race, scoped, spawn, withResolvers } from "effection"; | ||
| import { expect } from "expect"; | ||
|
|
||
| import { type EventTargetLike, once } from "./events.ts"; | ||
|
|
||
| /** | ||
| * A spawned task does not start until its parent suspends, so anything that | ||
| * has to happen while another task is registering its listener must run from | ||
| * inside a task of its own. | ||
| */ | ||
| function* observe<T>(read: () => T): Operation<T> { | ||
| const task = yield* spawn(function* () { | ||
| return read(); | ||
| }); | ||
| return yield* task; | ||
| } | ||
|
|
||
| /** | ||
| * `EventTarget` has no listener count, so count registrations ourselves. | ||
| */ | ||
| function createEventTarget(): EventTargetLike & { | ||
| listenerCount(): number; | ||
| dispatch(eventName: string): void; | ||
| } { | ||
| const target = new EventTarget(); | ||
| const listeners = new Set<(event: unknown) => void>(); | ||
|
|
||
| return { | ||
| addEventListener(eventName, listener) { | ||
| listeners.add(listener); | ||
| target.addEventListener(eventName, listener as EventListener); | ||
| }, | ||
| removeEventListener(eventName, listener) { | ||
| listeners.delete(listener); | ||
| target.removeEventListener(eventName, listener as EventListener); | ||
| }, | ||
| listenerCount: () => listeners.size, | ||
| dispatch: (eventName) => void target.dispatchEvent(new Event(eventName)), | ||
| }; | ||
| } | ||
|
|
||
| describe("once", () => { | ||
| describe("with an EventEmitter", () => { | ||
| it("registers nothing until it is interpreted", function* () { | ||
| const emitter = new EventEmitter(); | ||
|
|
||
| once(emitter, "done"); | ||
|
|
||
| expect(emitter.listenerCount("done")).toBe(0); | ||
| }); | ||
|
|
||
| it("registers one listener and yields the event arguments", function* () { | ||
| const emitter = new EventEmitter(); | ||
|
|
||
| const task = yield* spawn(function* () { | ||
| return yield* once<[number, string]>(emitter, "exit"); | ||
| }); | ||
|
|
||
| expect(yield* observe(() => emitter.listenerCount("exit"))).toBe(1); | ||
|
|
||
| emitter.emit("exit", 42, "SIGTERM"); | ||
|
|
||
| expect(yield* task).toEqual([42, "SIGTERM"]); | ||
| }); | ||
|
|
||
| it("removes the listener before its owner continues", function* () { | ||
| const emitter = new EventEmitter(); | ||
|
|
||
| const task = yield* spawn(function* () { | ||
| yield* once(emitter, "done"); | ||
| return emitter.listenerCount("done"); | ||
| }); | ||
|
|
||
| yield* observe(() => emitter.emit("done")); | ||
|
|
||
| expect(yield* task).toBe(0); | ||
| }); | ||
|
|
||
| it("removes the listener when the interpreting task is halted", function* () { | ||
| const emitter = new EventEmitter(); | ||
|
|
||
| const task = yield* spawn(function* () { | ||
| yield* once(emitter, "done"); | ||
| }); | ||
|
|
||
| expect(yield* observe(() => emitter.listenerCount("done"))).toBe(1); | ||
|
|
||
| yield* task.halt(); | ||
|
|
||
| expect(emitter.listenerCount("done")).toBe(0); | ||
|
|
||
| emitter.emit("done"); | ||
|
|
||
| expect(emitter.listenerCount("done")).toBe(0); | ||
| }); | ||
|
|
||
| it("removes the listener when its owning scope fails", function* () { | ||
| const emitter = new EventEmitter(); | ||
| const failure = withResolvers<never>(); | ||
| let registered = 0; | ||
| let caught: unknown; | ||
|
|
||
| try { | ||
| yield* scoped(function* () { | ||
| yield* spawn(function* () { | ||
| yield* once(emitter, "done"); | ||
| }); | ||
| yield* spawn(function* () { | ||
| registered = emitter.listenerCount("done"); | ||
| failure.reject(new Error("boom")); | ||
| }); | ||
| yield* failure.operation; | ||
| }); | ||
| } catch (error) { | ||
| caught = error; | ||
| } | ||
|
|
||
| expect((caught as Error).message).toBe("boom"); | ||
| expect(registered).toBe(1); | ||
| expect(emitter.listenerCount("done")).toBe(0); | ||
| }); | ||
|
|
||
| it("ignores an event redelivered before its owner resumes", function* () { | ||
| const emitter = new EventEmitter(); | ||
|
|
||
| const task = yield* spawn(function* () { | ||
| return yield* once<[string]>(emitter, "done"); | ||
| }); | ||
|
|
||
| yield* observe(() => { | ||
| let reentered = false; | ||
| emitter.on("done", () => { | ||
| if (!reentered) { | ||
| reentered = true; | ||
| emitter.emit("done", "second"); | ||
| } | ||
| }); | ||
| emitter.emit("done", "first"); | ||
| }); | ||
|
|
||
| expect(yield* task).toEqual(["first"]); | ||
| }); | ||
|
|
||
| it("deregisters the losing branch of a race", function* () { | ||
| const emitter = new EventEmitter(); | ||
| let racing = 0; | ||
|
|
||
| const value = yield* race([ | ||
| once<[string]>(emitter, "loser"), | ||
| { | ||
| *[Symbol.iterator]() { | ||
| racing = emitter.listenerCount("loser"); | ||
| return "won"; | ||
| }, | ||
| }, | ||
| ]); | ||
|
|
||
| expect(value).toBe("won"); | ||
| expect(racing).toBe(1); | ||
| expect(emitter.listenerCount("loser")).toBe(0); | ||
| }); | ||
| }); | ||
|
|
||
| describe("with an EventTarget", () => { | ||
| it("registers nothing until it is interpreted", function* () { | ||
| const target = createEventTarget(); | ||
|
|
||
| once(target, "done"); | ||
|
|
||
| expect(target.listenerCount()).toBe(0); | ||
| }); | ||
|
|
||
| it("registers one listener and yields the event", function* () { | ||
| const target = createEventTarget(); | ||
|
|
||
| const task = yield* spawn(function* () { | ||
| return yield* once<[Event]>(target, "done"); | ||
| }); | ||
|
|
||
| expect(yield* observe(() => target.listenerCount())).toBe(1); | ||
|
|
||
| target.dispatch("done"); | ||
|
|
||
| const [event] = yield* task; | ||
| expect(event.type).toBe("done"); | ||
| expect(target.listenerCount()).toBe(0); | ||
| }); | ||
|
|
||
| it("removes the listener when the interpreting task is halted", function* () { | ||
| const target = createEventTarget(); | ||
|
|
||
| const task = yield* spawn(function* () { | ||
| yield* once(target, "done"); | ||
| }); | ||
|
|
||
| expect(yield* observe(() => target.listenerCount())).toBe(1); | ||
|
|
||
| yield* task.halt(); | ||
|
|
||
| expect(target.listenerCount()).toBe(0); | ||
|
|
||
| target.dispatch("done"); | ||
|
|
||
| expect(target.listenerCount()).toBe(0); | ||
| }); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.