Skip to content

Commit

Permalink
fix: Allow events without parameters
Browse files Browse the repository at this point in the history
Currently `emit` requires two parameters even if the event type is set to `undefined`.
So `emit('foo')` will cause a Typescript error.

This fixes it by removing the second argument if the event type is set to undefined.

Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de>
  • Loading branch information
susnux committed Jun 24, 2024
1 parent 1afc30b commit 8e305ae
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 17 deletions.
6 changes: 5 additions & 1 deletion lib/EventBus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* SPDX-License-Identifier: GPL-3.0-or-later
*/
import type { GenericEvents, NextcloudEvents } from './Event'
import type { IsUndefined } from './types.d.ts'
import { EventHandler } from './EventHandler'

export interface EventBus<E extends GenericEvents = NextcloudEvents> {
Expand Down Expand Up @@ -37,5 +38,8 @@ export interface EventBus<E extends GenericEvents = NextcloudEvents> {
* @param name Name of the event to emit
* @param event Event payload to emit
*/
emit<EventName extends keyof E>(name: EventName, event: E[EventName]): void
emit<EventName extends keyof E>(
name: EventName,
...event: (IsUndefined<E[EventName]> extends true ? [] : [E[EventName]])
): void
}
2 changes: 1 addition & 1 deletion lib/EventHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: GPL-3.0-or-later
*/
import { Event } from './Event'
import type { Event } from './Event'

export interface EventHandler<T extends Event> {
(event: T): void
Expand Down
12 changes: 8 additions & 4 deletions lib/ProxyBus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import valid from 'semver/functions/valid.js'
import major from 'semver/functions/major.js'

import type { GenericEvents, NextcloudEvents } from './Event.js'
import { EventBus } from './EventBus.js'
import { EventHandler } from './EventHandler.js'
import type { EventBus } from './EventBus.js'
import type { EventHandler } from './EventHandler.js'
import type { IsUndefined } from './types.d.ts'

export class ProxyBus<E extends GenericEvents = NextcloudEvents>
implements EventBus<E>
Expand Down Expand Up @@ -48,7 +49,10 @@ export class ProxyBus<E extends GenericEvents = NextcloudEvents>
this.bus.unsubscribe(name, handler)
}

emit<EventName extends keyof E>(name: EventName, event: E[EventName]): void {
this.bus.emit(name, event)
emit<EventName extends keyof E>(
name: EventName,
...event: (IsUndefined<E[EventName]> extends true ? [] : [E[EventName]])
): void {
this.bus.emit(name, ...event)
}
}
17 changes: 11 additions & 6 deletions lib/SimpleBus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
* SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: GPL-3.0-or-later
*/
import { GenericEvents, NextcloudEvents } from './Event.js'
import { EventBus } from './EventBus.js'
import { EventHandler } from './EventHandler.js'
import type { GenericEvents, NextcloudEvents } from './Event.js'
import type { EventBus } from './EventBus.js'
import type { EventHandler } from './EventHandler.js'
import type { IsUndefined } from './types.d.ts'

export class SimpleBus<E extends GenericEvents = NextcloudEvents>
implements EventBus<E>
Expand Down Expand Up @@ -37,10 +38,14 @@ export class SimpleBus<E extends GenericEvents = NextcloudEvents>
)
}

emit<EventName extends keyof E>(name: EventName, event: E[EventName]): void {
;(this.handlers.get(name) || []).forEach((h) => {
emit<EventName extends keyof E>(
name: EventName,
...event: (IsUndefined<E[EventName]> extends true ? [] : [E[EventName]])
): void {
const handlers = this.handlers.get(name) || []
handlers.forEach((h) => {
try {
h(event)
(h as EventHandler<typeof event[0]>)(event[0])
} catch (e) {
console.error('could not invoke event listener', e)
}
Expand Down
12 changes: 7 additions & 5 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
* SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: GPL-3.0-or-later
*/
import { EventBus } from './EventBus'
import { EventHandler } from './EventHandler'
import { NextcloudEvents } from './Event'
import type { EventBus } from './EventBus'
import type { EventHandler } from './EventHandler'
import type { NextcloudEvents } from './Event'
import type { IsUndefined } from './types.d.ts'

import { ProxyBus } from './ProxyBus'
import { SimpleBus } from './SimpleBus'

Expand Down Expand Up @@ -91,7 +93,7 @@ export function unsubscribe<K extends keyof NextcloudEvents>(
*/
export function emit<K extends keyof NextcloudEvents>(
name: K,
event: NextcloudEvents[K],
...event: (IsUndefined<NextcloudEvents[K]> extends true ? [] : [NextcloudEvents[K]])
): void {
getBus().emit(name, event)
getBus().emit(name, ...event)
}
9 changes: 9 additions & 0 deletions lib/types.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: GPL-3.0-or-later
*/

/**
* Helper to check if a type is undefined
*/
export type IsUndefined<T> = [T] extends [undefined] ? true : false

0 comments on commit 8e305ae

Please sign in to comment.