-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Allow to fully type events by extending the
NextcloudEvents
i…
…nterface Signed-off-by: Ferdinand Thiessen <[email protected]>
- Loading branch information
Showing
7 changed files
with
104 additions
and
26 deletions.
There are no files selected for viewing
This file contains 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 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 |
---|---|---|
@@ -1 +1,31 @@ | ||
export type Event = object | number | string | boolean | null | undefined | ||
|
||
/** | ||
* Generic events mapping, fallback if no explicit types events are defined | ||
* @see NextcloudEvents | ||
*/ | ||
export type GenericEvents = Record<string | symbol, Event> | ||
|
||
/** | ||
* Nextcloud EventBus events | ||
* This can be extended to allow typing of events like: | ||
* @example | ||
* ```ts | ||
* // event-bus.d.ts | ||
* // Extend the Nextcloud events interface for your custom event | ||
* declare module '@nextcloud/event-bus' { | ||
* export interface NextcloudEvents { | ||
* // mapping of 'event name' => 'event type' | ||
* 'my-event': { foo: number, bar: boolean } | ||
* } | ||
* } | ||
* | ||
* // your-code.ts | ||
* import { emit } from '@nextcloud/event-bus' | ||
* // Here the type of 'params' is infered automatically | ||
* emit('my-event', (params) => { console.debug(params.foo, params.bar) }) | ||
* ``` | ||
*/ | ||
export interface NextcloudEvents { | ||
[eventName: string | symbol]: Event | ||
} |
This file contains 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 |
---|---|---|
@@ -1,14 +1,33 @@ | ||
import { Event } from "./Event"; | ||
import type { GenericEvents, NextcloudEvents } from "./Event"; | ||
import { EventHandler } from "./EventHandler"; | ||
|
||
export interface EventBus { | ||
export interface EventBus<E extends GenericEvents = NextcloudEvents> { | ||
|
||
/** | ||
* Get the version of this event bus instance | ||
* This is used for compatibility checking | ||
*/ | ||
getVersion(): string; | ||
|
||
subscribe(name: string, handler: EventHandler): void; | ||
/** | ||
* Subscribe the event bus | ||
* @param name Name of the event to subscribe | ||
* @param handler Handler ivoken when receiving the event | ||
*/ | ||
subscribe<EventName extends keyof E>(name: EventName, handler: EventHandler<E[EventName]>): void; | ||
|
||
unsubscribe(name: string, handler: EventHandler): void; | ||
/** | ||
* Unsubscribe a handler on one event from the event bus | ||
* @param name Name of the event to unsubscribe | ||
* @param handler Handler to unsubscribe | ||
*/ | ||
unsubscribe<EventName extends keyof E>(name: EventName, handler: EventHandler<E[EventName]>): void; | ||
|
||
emit(name: string, event: Event): void; | ||
/** | ||
* Emit an event on the event bus | ||
* @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; | ||
|
||
} |
This file contains 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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import { Event } from "./Event"; | ||
|
||
export interface EventHandler { | ||
(event: Event): void | ||
export interface EventHandler<T extends Event> { | ||
(event: T): void | ||
} |
This file contains 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 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 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