Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/okikio/native. An initiative which aims to make it easy to create complex, light-weight, and perfor okikio.github.io/native
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472import { Manager, methodCall } from "@okikio/manager/src/index.ts";
/** * Represents a listener callback function */export type TypeListenerCallback = ((...args: any) => void);
/** Represents a new event listener consisting of properties like: callback, scope, name */export interface IListener { readonly callback: TypeListenerCallback; readonly scope?: object | null; readonly name: string;}
/** * Creates a new listener based on {@link IListener | IListener} */export const newListener = ({ callback = () => { }, scope = null, name = "event",}: IListener): IListener => ({ callback, scope, name });
/** Represents a new event */export class Event extends Manager<number, IListener> { /** The name of the event */ public name: string; constructor(name: string = "event") { super(); this.name = name; }}
/** * Helper function to check if a value is an object. * Determines whether the value is a pure object (not array, not function, etc.) * * @param value - The value to check. * @returns True if the value is an object, false otherwise. * * @example * // Check if a value is an object * const isObj = isObject({ key: 'value' }); // true * * @example * // Check if a value is not an object * const isNotObj = isObject('string'); // false */export function isObject(value: any): boolean { return value !== null && typeof value === 'object' && !Array.isArray(value);}
/** * The types of values `EventEmitter.prototype.on(...), EventEmitter.prototype.once(...), and EventEmitter.prototype.off(...)` accept */export type TypeEventInput = (string & {}) | object | Array<(string & {})>;
/** * An Event Emitter * */export class EventEmitter extends Manager<string, Event> { constructor() { super(); }
/** Gets event, if event doesn't exist create a new one */ public getEvent(name: string): Event | undefined { let event = this.get(name); if (!(event instanceof Event)) { this.set(name, new Event(name)); return this.get(name); }
return event; }
/** Creates a listener and adds it to an event */ public newListener( name: string, callback: TypeListenerCallback, scope?: object | null ): Event { const event = this.getEvent(name); if (!event) throw new Error(`Can't add listener to event "${name}", as event "${name}" does not exist.`); event.add(newListener({ name, callback, scope })); return event; }
/** Adds a listener to a given event */ public on( events: TypeEventInput, callback?: TypeListenerCallback | object, scope?: object ): EventEmitter { // If there is no event break if ( events === undefined || events === null ) return this;
// Create a new event every space if (typeof events === "string") events = events.trim().split(/\s+/);
let _name: string; let _callback: TypeListenerCallback; let isObj = isObject(events);
let _scope: object | undefined = isObj ? callback : scope; if (!isObj) _callback = (callback as TypeListenerCallback);
// Loop through the list of events Object.keys(events).forEach(key => { // Select the name of the event from the list // Remember events can be {String | Object | Array<string>}
// Check If events is an Object (JSON like Object, and not an Array) const evts = events as Record<PropertyKey, string | TypeListenerCallback>; _name = isObj ? key : evts[key] as string; if (isObj) _callback = evts[key] as TypeListenerCallback;
this.newListener(_name, _callback, _scope); }, this); return this; }
/** Removes a listener from an event */ public removeListener( name: string, callback?: TypeListenerCallback, scope?: object | null ): Event { let event = this.get(name); if (!callback) throw new Error("Callback is required to remove a listener"); if (!event) throw new Error(`Can't remove listener from event "${name}", as event "${name}" does not exist.`); if (event instanceof Event) { let listener = newListener({ name, callback, scope }); event.forEach((value?: IListener, i?: number) => { if ( value && typeof i === "number" && value.callback === listener.callback && value.scope === listener.scope ) return event.remove(i); }); }
return event; }
/** Remove a listener from a given event, or just completely remove an event */ public off( events: TypeEventInput, callback?: TypeListenerCallback | object, scope?: object ): EventEmitter { // If there is no event break if ( events === undefined || events === null ) return this;
// Create a new event every space if (typeof events === "string") events = events.trim().split(/\s+/);
let _name: string; let _callback: TypeListenerCallback; let isObj = isObject(events);
let _scope: object | undefined = isObj ? (callback as object) : scope; if (!isObj) _callback = (callback as TypeListenerCallback);
// Loop through the list of events Object.keys(events).forEach(key => { // Select the name of the event from the list // Remember events can be {String | Object | Array<any>}
// Check If events is an Object (JSON like Object, and not an Array) const evts = events as Record<PropertyKey, string | TypeListenerCallback>; _name = isObj ? key : evts[key] as string; if (isObj) _callback = evts[key] as TypeListenerCallback;
if (typeof _callback === "function") { this.removeListener(_name, _callback, _scope); } else this.remove(_name); }, this); return this; }
/** * Adds a one time event listener for an event */ public once( events: TypeEventInput, callback?: TypeListenerCallback | object, scope?: object ): EventEmitter { // If there is no event break if ( events === undefined || events === null ) return this;
// Create a new event every space if (typeof events == "string") events = events.trim().split(/\s+/);
let isObj = isObject(events);
// Loop through the list of events Object.keys(events).forEach(key => { // Select the name of the event from the list // Remember events can be {String | Object | Array<string>} // Check If events is an Object (JSON like Object, and not an Array) const evts = events as Record<PropertyKey, string | TypeListenerCallback>; let _name: string = isObj ? key : (evts[key] as string); let _callback: TypeListenerCallback = isObj ? (evts[key] as TypeListenerCallback) : (callback as TypeListenerCallback); let _scope: object | undefined = isObj ? callback : scope; let onceFn: TypeListenerCallback = (...args) => { _callback.apply(_scope, args); this.removeListener(_name, onceFn, _scope); };
this.newListener(_name, onceFn, _scope); }, this); return this; }
/** Call all listeners within an event */ public emit( events: string | Array<any>, ...args: any ): EventEmitter { // If there is no event break if ( events === undefined || events === null ) return this;
// Create a new event every space if (typeof events == "string") events = events.trim().split(/\s+/);
// Loop through the list of events events.forEach((event: string) => { let _event: Event | undefined = this.get(event); if (_event instanceof Event) { _event.forEach((listener?: IListener) => { if (!listener) return; let { callback, scope } = listener; callback.apply(scope, args); }); } }, this); return this; }
/** Clears events and event listeners */ public clear() { methodCall(this, "clear"); super.clear(); return this; }}
/** * Adds event listeners to an EventTarget. * * @param {EventTarget} target - The target to attach event listeners to. * @param {(string | Array<string> | Record<PropertyKey, string | EventListenerOrEventListenerObject | null>)} events - The events to listen for. Can be a string, an array of strings, or an object with event names as keys. * @param {EventListenerOrEventListenerObject | null | boolean | AddEventListenerOptions} [callback] - The callback function or options for the event listener. * @param {boolean | AddEventListenerOptions} [opts] - Additional options for the event listener. * @returns {EventTarget | null} The target if successful, or null if there was an error. * * @example * // Single event with a callback * on(document.body, 'click', (e) => console.log('Body clicked!', e)); * * @example * // Multiple events with a single callback * on(document.body, ['click', 'mouseover'], (e) => console.log('Event triggered!', e)); * * @example * // Event object with different callbacks * on(document.body, { * click: (e) => console.log('Body clicked!', e), * mouseover: (e) => console.log('Mouse over body!', e) * }); * * @example * // Event handler object with a handle method * on(document.body, 'click', { handle: (e) => console.log('Handled click event!', e) }); * * @example * // Event with options (once: true) * on(document.body, 'click', (e) => console.log('Body clicked once!', e), { once: true }); * * @example * // Event with options (capture: true) * on(document.body, 'click', (e) => console.log('Body clicked with capture!', e), { capture: true }); * * @example * // Event with options (passive: true) * on(document.body, 'click', (e) => console.log('Body clicked with passive option!', e), { passive: true }); */export function on( target: EventTarget, events: (string & {}) | Array<(string & {})> | Record<PropertyKey, string | EventListenerOrEventListenerObject | null>, callback?: EventListenerOrEventListenerObject | null | boolean | AddEventListenerOptions, opts?: boolean | AddEventListenerOptions): EventTarget | null { // If there is no event or target, break if (target === undefined || target === null) return null; if (events === undefined || events === null) return target;
// Create a new event every space if (typeof events === "string") events = events.trim().split(/\s+/);
let _name: string; let _callback: EventListenerOrEventListenerObject; let isObj = isObject(events);
let _opts = isObj ? (callback as boolean | AddEventListenerOptions) : opts; if (!isObj) _callback = (callback as EventListenerOrEventListenerObject);
// Loop through the list of events Object.keys(events).forEach(key => { // Select the name of the event from the list // Remember events can be {String | Object | Array<string>} const evts = events as Record<PropertyKey, string | EventListenerOrEventListenerObject>; _name = isObj ? key : evts[key] as string; if (isObj) _callback = evts[key] as EventListenerOrEventListenerObject;
target.addEventListener(_name, _callback, _opts); }, target); return target;}
/** * Removes event listeners from an EventTarget. * * @param {EventTarget} target - The target to remove event listeners from. * @param {(string | Array<string> | Record<PropertyKey, string | EventListenerOrEventListenerObject | null>)} events - The events to stop listening for. Can be a string, an array of strings, or an object with event names as keys. * @param {EventListenerOrEventListenerObject | null | boolean | EventListenerOptions} [callback] - The callback function or options for the event listener. * @param {boolean | EventListenerOptions} [opts] - Additional options for the event listener. * @returns {EventTarget | null} The target if successful, or null if there was an error. * * @example * // Single event with a callback * off(document.body, 'click', handleClick); * * @example * // Multiple events with a single callback * off(document.body, ['click', 'mouseover'], handleEvent); * * @example * // Event object with different callbacks * off(document.body, { * click: handleClick, * mouseover: handleMouseOver * }); * * @example * // Event with options (once: true) * off(document.body, 'click', handleClick, { once: true }); */export function off( target: EventTarget, events: (string & {}) | Array<(string & {})> | Record<PropertyKey, string | EventListenerOrEventListenerObject | null>, callback?: EventListenerOrEventListenerObject | null | boolean | EventListenerOptions, opts?: boolean | EventListenerOptions): EventTarget | null { // If there is no event or target, break if (target === undefined || target === null) return null; if (events === undefined || events === null) return target;
// Create a new event every space if (typeof events === "string") events = events.trim().split(/\s+/);
let _name: string; let _callback: EventListenerOrEventListenerObject; let isObj = isObject(events);
let _opts = isObj ? (callback as boolean | EventListenerOptions) : opts; if (!isObj) _callback = (callback as EventListenerOrEventListenerObject);
// Loop through the list of events Object.keys(events).forEach(key => { // Select the name of the event from the list // Remember events can be {String | Object | Array<string>} const evts = events as Record<PropertyKey, string | EventListenerOrEventListenerObject>; _name = isObj ? key : evts[key] as string; if (isObj) _callback = evts[key] as EventListenerOrEventListenerObject;
target.removeEventListener(_name, _callback, _opts); }, target); return target;}
/** * Dispatches events on an EventTarget. * * @param {EventTarget} target - The target to dispatch events on. * @param {(globalThis.Event | string | Array<(globalThis.Event | string) & {})>} events - The events to dispatch. Can be an Event object, a string, or an array of strings or Event objects. * @returns {EventTarget | null} The target if successful, or null if there was an error. * * @example * // Single event as string * emit(document.body, 'customEvent'); * * @example * // Single event as Event object * const event = new Event('customEvent'); * emit(document.body, event); * * @example * // Multiple events as strings * emit(document.body, ['customEvent1', 'customEvent2']); * * @example * // Multiple events as Event objects * const event1 = new Event('customEvent1'); * const event2 = new Event('customEvent2'); * emit(document.body, [event1, event2]); */export function emit( target: EventTarget, events: globalThis.Event | (string & {}) | Array<(globalThis.Event | string & {})>): EventTarget | null { // If there is no event or target, break if (target === undefined || target === null) return null; if (events === undefined || events === null) return target;
// Create a new event every space if (typeof events === "string") events = events.trim().split(/\s+/); if (events instanceof globalThis.Event) events = [events];
// Loop through the list of events events.forEach(event => { // Select the name of the event from the list // Remember events can be {String | Array<string>} const _event = event instanceof globalThis.Event ? event : new globalThis.Event(event); target.dispatchEvent(_event); }, target); return target;}
/** * Converts a list of event names to CustomEvent objects. * * @param {(string | Array<string>)} events - The events to convert. Can be a string or an array of strings. * @param {T} detail - The detail to include in the CustomEvent. * @returns {Array<CustomEvent<T>>} An array of CustomEvent objects. * * @example * // Single event with detail * const customEvents = toCustomEvents('customEvent', { key: 'value' }); * customEvents.forEach(event => document.body.dispatchEvent(event)); * * @example * // Multiple events with detail * const customEvents = toCustomEvents(['customEvent1', 'customEvent2'], { key: 'value' }); * customEvents.forEach(event => document.body.dispatchEvent(event)); */export function toCustomEvents<T = any>(events: (string & {}) | Array<(string & {})>, detail: T) { // Create a new event every space if (typeof events === "string") events = events.trim().split(/\s+/);
return events.map((evt) => { return new CustomEvent(evt, { detail }); });}
export default EventEmitter;