-
Notifications
You must be signed in to change notification settings - Fork 134
fix: keep the GlobalBus emit override assignable to EventEmitter #1166
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -11,12 +11,24 @@ export type GlobalEvent = { | |||||
| class GlobalBusEmitter extends EventEmitter<{ | ||||||
| event: [GlobalEvent] | ||||||
| }> { | ||||||
| override emit(eventName: "event", event: GlobalEvent): boolean { | ||||||
| if (event.payload && typeof event.payload === "object" && !("id" in event.payload)) { | ||||||
| // altimate_change start — upstream_fix: keep the override assignable to the base. | ||||||
| // `EventEmitter<T>` declares `emit` across several overloads, one of which is | ||||||
| // `(eventName: string | symbol, ...args: any[])`. An override has to be | ||||||
| // assignable to all of them, and a lone `(eventName: "event", event: | ||||||
| // GlobalEvent)` is not — newer `@types/node` rejects it with "Type 'any[]' is | ||||||
| // not assignable to type '[event: GlobalEvent]'", which fails `bun typecheck` | ||||||
| // and so blocks `git push` on unmodified code. The public overload keeps call | ||||||
| // sites typed; the implementation signature is what satisfies the base. | ||||||
| override emit(eventName: "event", event: GlobalEvent): boolean | ||||||
| override emit(eventName: string | symbol, ...args: any[]): boolean | ||||||
| override emit(eventName: string | symbol, ...args: any[]): boolean { | ||||||
| const event = args[0] as GlobalEvent | undefined | ||||||
| if (eventName === "event" && event?.payload && typeof event.payload === "object" && !("id" in event.payload)) { | ||||||
| event.payload.id = event.payload.syncEvent?.id ?? Identifier.create("evt", "ascending") | ||||||
| } | ||||||
| return super.emit(eventName, event) | ||||||
| return super.emit(eventName as "event", ...(args as [GlobalEvent])) | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P2: When callers pass extra arguments through the wide signature, this forwards them to Prompt for AI agents
Suggested change
|
||||||
| } | ||||||
| // altimate_change end | ||||||
| } | ||||||
|
|
||||||
| export const GlobalBus = new GlobalBusEmitter() | ||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| // altimate_change start — upstream_fix: pin the override's assignability to the base. | ||
| import { describe, expect, test } from "bun:test" | ||
| import { GlobalBus, type GlobalEvent } from "@/bus/global" | ||
|
|
||
| describe("GlobalBusEmitter.emit", () => { | ||
| // The regression this guards is a COMPILE error, not a runtime one: a lone | ||
| // `(eventName: "event", event: GlobalEvent)` override is not assignable to | ||
| // the base `EventEmitter<T>` overload set, which newer `@types/node` rejects | ||
| // with "Type 'any[]' is not assignable to type '[event: GlobalEvent]'". That | ||
| // failed `bun typecheck`, and so blocked `git push` via the pre-push hook, on | ||
| // code nobody had touched. This assignment only compiles while the override | ||
| // keeps the wide implementation signature, so `tsgo` fails if it is narrowed | ||
| // again — the test body below merely keeps the reference alive. | ||
| test("stays assignable to the base EventEmitter signature", () => { | ||
| const wide: (eventName: string | symbol, ...args: any[]) => boolean = GlobalBus.emit.bind(GlobalBus) | ||
| expect(typeof wide).toBe("function") | ||
| }) | ||
|
|
||
| test("stamps an id onto a payload that has none", () => { | ||
| const seen: GlobalEvent[] = [] | ||
| const on = (event: GlobalEvent) => void seen.push(event) | ||
| GlobalBus.on("event", on) | ||
| try { | ||
| GlobalBus.emit("event", { payload: { kind: "test" } }) | ||
| expect(seen).toHaveLength(1) | ||
| expect(typeof seen[0]!.payload.id).toBe("string") | ||
| expect(seen[0]!.payload.id).toStartWith("evt") | ||
| } finally { | ||
| GlobalBus.off("event", on) | ||
|
Comment on lines
+20
to
+29
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win Isolate each listener from concurrent Each test subscribes to the shared Create the event object before subscribing. Record an event only when As per coding guidelines, tests using shared state must provide teardown and isolation safe for parallel Also applies to: 34-41, 46-53 🤖 Prompt for AI AgentsSource: Coding guidelines |
||
| } | ||
| }) | ||
|
|
||
| test("leaves an existing id alone", () => { | ||
| const seen: GlobalEvent[] = [] | ||
| const on = (event: GlobalEvent) => void seen.push(event) | ||
| GlobalBus.on("event", on) | ||
| try { | ||
| GlobalBus.emit("event", { payload: { id: "evt_already_set" } }) | ||
| expect(seen[0]!.payload.id).toBe("evt_already_set") | ||
| } finally { | ||
| GlobalBus.off("event", on) | ||
| } | ||
| }) | ||
|
|
||
| test("prefers the syncEvent id when the payload has none", () => { | ||
| const seen: GlobalEvent[] = [] | ||
| const on = (event: GlobalEvent) => void seen.push(event) | ||
| GlobalBus.on("event", on) | ||
| try { | ||
| GlobalBus.emit("event", { payload: { syncEvent: { id: "evt_from_sync" } } }) | ||
| expect(seen[0]!.payload.id).toBe("evt_from_sync") | ||
| } finally { | ||
| GlobalBus.off("event", on) | ||
| } | ||
| }) | ||
| }) | ||
| // altimate_change end | ||
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.
SUGGESTION: The
eventName as "event"andargs as [GlobalEvent]casts are redundant — the wide base overloademit(eventName: string | symbol, ...args: any[])already acceptseventNameandargsas-is, so the call simplifies to a plain spread.Reply with
@kilocode-bot fix itto have Kilo Code address this issue.