Skip to content

Commit

Permalink
feat(api): add generics for Event & INotify
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Jul 25, 2023
1 parent be70868 commit 7702882
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions packages/api/src/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ import type { IID } from "./id.js";
*/
export type Listener = Fn<Event, void>;

export interface Event extends IID<PropertyKey> {
/**
* Event type used in combination with {@link INotify}.
*/
export interface Event<T extends PropertyKey = PropertyKey> extends IID<T> {
target?: any;
canceled?: boolean;
value?: any;
Expand All @@ -15,10 +18,12 @@ export interface Event extends IID<PropertyKey> {
/**
* Interface to provide event emitter functionality. Also see
* {@link INotifyMixin} decorator mixin.
*
* The type param `T` can be used to constrain the event type/id.
*/
export interface INotify {
addListener(id: string, fn: Listener, scope?: any): boolean;
removeListener(id: string, fn: Listener, scope?: any): boolean;
export interface INotify<T extends string = string> {
addListener(id: T, fn: Listener, scope?: any): boolean;
removeListener(id: T, fn: Listener, scope?: any): boolean;
/**
* Broadcasts all registered listeners for given event type (in order
* registration) and returns true if any of them have been successfully
Expand All @@ -32,5 +37,5 @@ export interface INotify {
*
* @param event
*/
notify(event: Event): boolean;
notify(event: Event<T>): boolean;
}

0 comments on commit 7702882

Please sign in to comment.