@hydroperx/event
Advanced tools
+32
-9
@@ -1,10 +0,33 @@ | ||
| export type TypedEventTarget<EventMap extends object> = { | ||
| new (): IntermediateEventTarget<EventMap>; | ||
| /** | ||
| * Used to declare known compile-time events | ||
| * for a class that extends `EventTarget` or | ||
| * implements `IEventTarget`. | ||
| */ | ||
| export declare const EventRecord: unique symbol; | ||
| export declare class EventTarget implements IEventTarget { | ||
| [EventRecord]: {}; | ||
| private m_eventTarget; | ||
| on<K extends keyof this[typeof EventRecord]>(type: K, listener: (e: this[typeof EventRecord][K] extends Event ? this[typeof EventRecord][K] : never) => void | undefined, options?: boolean | OnEventOptions): void; | ||
| on(type: string, listener: (e: Event) => void | undefined, options?: boolean | OnEventOptions): void; | ||
| off<K extends keyof this[typeof EventRecord]>(type: K, listener: (e: this[typeof EventRecord][K] extends Event ? this[typeof EventRecord][K] : never) => void | undefined, options?: boolean | OffEventOptions): void; | ||
| off(type: string, listener: (e: Event) => void | undefined, options?: boolean | OffEventOptions): void; | ||
| emit<K extends string & keyof this[typeof EventRecord], C extends (new (type: K) => EventResult), EventResult extends this[typeof EventRecord][K]>(type: K, constructor: C): boolean; | ||
| emit<K extends string & keyof this[typeof EventRecord], C extends (new (type: K, options: O) => EventResult), O, EventResult extends this[typeof EventRecord][K]>(type: K, constructor: C, options: O): boolean; | ||
| emit(event: Event): boolean; | ||
| } | ||
| export interface IEventTarget { | ||
| [EventRecord]: {}; | ||
| on<K extends keyof this[typeof EventRecord]>(type: K, listener: (e: this[typeof EventRecord][K] extends Event ? this[typeof EventRecord][K] : never) => void | undefined, options?: boolean | OnEventOptions): void; | ||
| on(type: string, listener: (e: Event) => void | undefined, options?: boolean | OnEventOptions): void; | ||
| off<K extends keyof this[typeof EventRecord]>(type: K, listener: (e: this[typeof EventRecord][K] extends Event ? this[typeof EventRecord][K] : never) => void | undefined, options?: boolean | OffEventOptions): void; | ||
| off(type: string, listener: (e: Event) => void | undefined, options?: boolean | OffEventOptions): void; | ||
| emit<K extends string & keyof this[typeof EventRecord], C extends (new (type: K) => EventResult), EventResult extends this[typeof EventRecord][K]>(type: K, constructor: C): boolean; | ||
| emit<K extends string & keyof this[typeof EventRecord], C extends (new (type: K, options: O) => EventResult), O, EventResult extends this[typeof EventRecord][K]>(type: K, constructor: C, options: O): boolean; | ||
| emit(event: Event): boolean; | ||
| } | ||
| export type OnEventOptions = { | ||
| capture?: boolean; | ||
| }; | ||
| interface IntermediateEventTarget<EventMap> extends EventTarget { | ||
| addEventListener<K extends keyof EventMap>(type: K, callback: (event: EventMap[K] extends Event ? EventMap[K] : never) => EventMap[K] extends Event ? void : never, options?: boolean | AddEventListenerOptions): void; | ||
| addEventListener(type: string, callback: EventListenerOrEventListenerObject | null, options?: EventListenerOptions | boolean): void; | ||
| removeEventListener<K extends keyof EventMap>(type: K, callback: (event: EventMap[K] extends Event ? EventMap[K] : never) => EventMap[K] extends Event ? void : never, options?: boolean | EventListenerOptions): void; | ||
| removeEventListener(type: string, callback: EventListenerOrEventListenerObject | null, options?: EventListenerOptions | boolean): void; | ||
| } | ||
| export {}; | ||
| export type OffEventOptions = { | ||
| capture?: boolean; | ||
| }; |
+35
-1
@@ -1,1 +0,35 @@ | ||
| export {}; | ||
| /** | ||
| * Used to declare known compile-time events | ||
| * for a class that extends `EventTarget` or | ||
| * implements `IEventTarget`. | ||
| */ | ||
| export const EventRecord = Symbol("EventRecord"); | ||
| // | ||
| export class EventTarget { | ||
| // | ||
| m_eventTarget = new globalThis.EventTarget(); | ||
| on(type, listener, options) { | ||
| const new_options = typeof options == "object" | ||
| ? { capture: options.capture } | ||
| : options; | ||
| this.m_eventTarget.addEventListener(type, listener, new_options); | ||
| } | ||
| off(type, listener, options) { | ||
| const new_options = typeof options == "object" | ||
| ? { capture: options.capture } | ||
| : options; | ||
| this.m_eventTarget.removeEventListener(type, listener, new_options); | ||
| } | ||
| emit(arg1, arg2, arg3) { | ||
| let event = null; | ||
| if (arg1 instanceof Event) { | ||
| event = arg1; | ||
| } | ||
| else { | ||
| event = typeof arg3 !== "undefined" ? | ||
| new arg1(arg2, arg3) : | ||
| new arg1(arg2); | ||
| } | ||
| return this.m_eventTarget.dispatchEvent(event); | ||
| } | ||
| } |
+7
-4
| { | ||
| "name": "@hydroperx/event", | ||
| "version": "1.0.3", | ||
| "description": "Provide types of dispatched events in event targets.", | ||
| "version": "2.0.0", | ||
| "description": "Statically typed event facilities.", | ||
| "scripts": { | ||
@@ -16,6 +16,9 @@ "build": "tsc", | ||
| "files": ["/dist"], | ||
| "repository": "https://github.com/hydroperx/event", | ||
| "repository": "https://github.com/hydroperx/event.js", | ||
| "license": "Apache-2.0", | ||
| "author": "Hydroper <HydroperFox@gmail.com>", | ||
| "keywords": ["event"] | ||
| "keywords": ["event"], | ||
| "devDependencies": { | ||
| "typescript": "~5.6.2" | ||
| } | ||
| } |
+22
-8
| # Event | ||
| Easily define event types for an `EventTarget`. Credits to [this article](https://dev.to/marcogrcr/type-safe-eventtarget-subclasses-in-typescript-1nkf). | ||
| ## Getting started | ||
@@ -10,10 +8,26 @@ | ||
| ```ts | ||
| import { TypedEventTarget } from "@hydroperx/event"; | ||
| import { EventRecord, EventTarget } from "@hydroperx/event"; | ||
| class MediaPlayer extends (EventTarget as TypedEventTarget<{ | ||
| play: CustomEvent<MediaPlayerEvent>; | ||
| stop: CustomEvent<MediaPlayerEvent>; | ||
| }>) {} | ||
| // media player | ||
| class MediaPlayer extends EventTarget { | ||
| // declare events | ||
| declare [EventRecord]: { | ||
| play: MediaPlayerEvent, | ||
| stop: MediaPlayerEvent, | ||
| }; | ||
| } | ||
| ``` | ||
| > Note that event types must implement the `Event` interface, thus in the previous program `CustomEvent` is used; if there was no data, it could have just been `Event` itself. | ||
| Extending `MediaPlayer` with more events: | ||
| ```ts | ||
| // media player | ||
| class MoreSpecializedPlayer extends MediaPlayer { | ||
| // declare events | ||
| declare [EventRecord]: MediaPlayer[typeof EventRecord] & { | ||
| // more events... | ||
| }; | ||
| } | ||
| ``` | ||
| > Note that event types must implement the `Event` interface. |
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
4871
139.36%68
518.18%32
77.78%1
Infinity%