-
Notifications
You must be signed in to change notification settings - Fork 4
π€ Add Scope-Bound Event Registration policy #250
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
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,129 @@ | ||
| # Scope-Bound Event Registration Policy (Recommended) | ||
|
|
||
| This document defines the recommended policy for registering event listeners on | ||
| emitters (Node `EventEmitter`, streams, `EventTarget`) inside Effection code. | ||
|
|
||
| ## Core Principle | ||
|
|
||
| **An event listener's lifetime must depend solely on a scope, never on its event | ||
| firing.** | ||
|
|
||
| ## The Rule | ||
|
|
||
| | Case / Condition | Required behavior | | ||
| | ------------------------------------------------------- | ------------------------------------------------------------------------ | | ||
| | Registering any listener | Use `.on()` / `.addEventListener()` with a **named** handler | | ||
| | `emitter.once(...)` or `{ once: true }` | Never; convert to `.on()` plus scope-bound removal | | ||
| | Removing the listener | `.off()` in the scope's own teardown (`finally` for sync-only, `ensure()` otherwise) | | ||
| | Teardown waits on a value the listener resolves | Deregister in a sync `finally` around that wait, in the same `ensure()` | | ||
|
|
||
| The `once()` *operation* from `@effectionx/node/events` is compliant and | ||
| unaffected: it removes its listener when its scope exits, whether or not the | ||
| event ever fired. This policy is about `EventEmitter.prototype.once` and the | ||
| `{ once: true }` listener option. | ||
|
|
||
| ### Why | ||
|
|
||
| `emitter.once()` deregisters the hook only when the event fires. If the event | ||
| never fires β the process never errors, the stream is destroyed instead of | ||
| ending, the scope is halted first β the listener outlives the scope that | ||
| created it. On emitters shared beyond a single owner (I/O pipes are the | ||
| canonical case: a child's stdio may be inherited by descendants and observed by | ||
| more than one consumer) the leaked listener keeps firing into a dead scope, | ||
| resolving resolvers nobody is waiting on and holding referenced objects alive. | ||
|
|
||
| Symmetric registration and removal on scope entry and exit makes listener | ||
| lifetime deterministic β the same principle | ||
| [Structured Concurrency](./structured-concurrency.md) applies to tasks, applied | ||
| to event hooks. | ||
|
|
||
| ## Examples | ||
|
|
||
| ### Compliant: named handlers, removed in the scope's teardown | ||
|
|
||
| ```typescript | ||
| function useConnection(url: string): Operation<Connection> { | ||
| return resource(function* (provide) { | ||
| let socket = connect(url); | ||
| let onMessage = (message: Message) => inbox.send(message); | ||
| socket.on("message", onMessage); | ||
| try { | ||
| yield* provide(ConnectionHandle(socket)); | ||
| } finally { | ||
| socket.off("message", onMessage); // sync-only: `finally` is fine | ||
| } | ||
| }); | ||
| } | ||
| ``` | ||
|
|
||
| ### Compliant: teardown waits on a listener-resolved value first | ||
|
|
||
| ```typescript | ||
| function useChildProcess(create: () => ChildProcess): Operation<NativeProcess> { | ||
| return resource(function* (provide) { | ||
| let result = withResolvers<Result<ExitStatus>>(); | ||
| let child: ChildProcess | undefined; | ||
|
|
||
| let onClose = (code: number | null) => result.resolve(Ok(ExitStatus(code))); | ||
|
|
||
| yield* ensure(function* () { | ||
| if (child) { | ||
| try { | ||
| yield* result.operation; // needs `onClose` still attached | ||
| } finally { | ||
| child.off("close", onClose); // after the wait, but not dependent | ||
| } // on it completing β sync finally | ||
| } | ||
| }); | ||
|
|
||
| child = create(); | ||
| child.on("close", onClose); | ||
| yield* provide(NativeProcess(child, result.operation)); | ||
| }); | ||
| } | ||
| ``` | ||
|
|
||
| ### Non-Compliant: `once()` ties removal to the event firing | ||
|
|
||
| ```typescript | ||
| child.once("error", (error) => { | ||
| result.resolve(Err(error)); // VIOLATION: if "error" never fires, the | ||
| }); // listener outlives the scope | ||
| ``` | ||
|
|
||
| ### Non-Compliant: registered but never removed | ||
|
|
||
| ```typescript | ||
| stream.on("data", (chunk) => signal.send(chunk)); // VIOLATION: no `.off()` on | ||
| yield* provide(handle); // any teardown path; leaks on | ||
| // emitters shared beyond this scope | ||
| ``` | ||
|
|
||
| ## Verification Checklist | ||
|
|
||
| Before marking a review complete, verify: | ||
|
|
||
| - [ ] No `emitter.once(...)` or `{ once: true }` registration in the diff | ||
| - [ ] Every `.on()` / `.addEventListener()` has a named handler and a matching | ||
| `.off()` / `.removeEventListener()` on a teardown path of the same scope | ||
| - [ ] Removal that follows a `yield*` lives in `ensure()`, not `finally` | ||
| (see [Async Teardown](./async-teardown.md)) | ||
| - [ ] When teardown awaits a listener-resolved value, removal sits in a sync | ||
| `finally` around that wait β ordered after it, but not dependent on it | ||
| completing | ||
|
|
||
| ## Common Mistakes | ||
|
|
||
| | Mistake | Fix | | ||
| | ------------------------------------------------------------ | ------------------------------------------------------------------- | | ||
| | `emitter.once("close", handler)` | `emitter.on("close", handler)` + `.off()` in the scope's teardown | | ||
| | Anonymous inline listener | Name it β `.off()` needs the same function reference | | ||
| | `.off()` in a `finally` while an `ensure()` still awaits the event | Move removal into that `ensure()`, in a sync `finally` around the wait | | ||
| | Skipping removal because "the emitter dies with the process" | Remove anyway; the emitter may be shared beyond this scope | | ||
|
|
||
| ## Related Policies | ||
|
|
||
| - [Async Teardown](./async-teardown.md) - Where removal may live: `finally` only if sync-only | ||
| - [Structured Concurrency](./structured-concurrency.md) - The same lifetime principle, for tasks | ||
| - [Correctness Through Explicit Invariants](./correctness-invariants.md) - The never-fires path must be considered | ||
| - [Policies Index](./index.md) - Add your new policy to the Policy Documents table | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
π Maintainability & Code Quality | π‘ Minor | β‘ Quick win
Keep
.policies/scope-bound-event-registration.mdandAGENTS.mdconsistent.Both documents omit part of the EventTarget and native
onceAPI rules..policies/scope-bound-event-registration.md#L13-L18: Specify.off()for emitters and.removeEventListener()forEventTarget.AGENTS.md#L81-L85: Include{ once: true },addEventListener(), andremoveEventListener().π Affects 2 files
.policies/scope-bound-event-registration.md#L13-L18(this comment)AGENTS.md#L81-L85π€ Prompt for AI Agents