init commit

This commit is contained in:
Carlos
2026-02-21 10:33:18 +01:00
parent c863a943ed
commit 9d955bf338
9512 changed files with 2015317 additions and 1305 deletions

View File

@@ -0,0 +1,22 @@
export type EventType = string | symbol;
export type Handler<T = any> = (event?: T) => void;
type WildcardHandler = (type: EventType, event?: any) => void;
type EventHandlerList = Array<Handler>;
type WildCardEventHandlerList = Array<WildcardHandler>;
type EventHandlerMap = Map<EventType, EventHandlerList | WildCardEventHandlerList>;
export interface Emitter {
all: EventHandlerMap;
on<T = any>(type: EventType, handler: Handler<T>): void;
on(type: '*', handler: WildcardHandler): void;
off<T = any>(type: EventType, handler: Handler<T>): void;
off(type: '*', handler: WildcardHandler): void;
emit<T = any>(type: EventType, event?: T): void;
emit(type: '*', event?: any): void;
}
/**
* Mitt: Tiny (~200b) functional event emitter / pubsub.
* @name mitt
* @returns {Mitt}
*/
export default function mitt(all?: EventHandlerMap): Emitter;
export {};

View File

@@ -0,0 +1,49 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = mitt;
/**
* Mitt: Tiny (~200b) functional event emitter / pubsub.
* @name mitt
* @returns {Mitt}
*/
function mitt(all) {
all = all || new Map();
return {
/**
* A Map of event names to registered handler functions.
*/
all,
/**
* Register an event handler for the given type.
* @param {string|symbol} type Type of event to listen for, or `"*"` for all events
* @param {Function} handler Function to call in response to given event
* @memberOf mitt
*/
on: (type, handler) => {
const handlers = all === null || all === void 0 ? void 0 : all.get(type);
const added = handlers === null || handlers === void 0 ? void 0 : handlers.push(handler);
if (!added) {
all === null || all === void 0 ? void 0 : all.set(type, [handler]);
}
},
off: (type, handler) => {
const handlers = all === null || all === void 0 ? void 0 : all.get(type);
if (handlers) {
// eslint-disable-next-line no-bitwise
handlers.splice(handlers.indexOf(handler) >>> 0, 1);
}
},
emit: (type, evt) => {
((all === null || all === void 0 ? void 0 : all.get(type)) || [])
.slice()
.forEach((handler) => {
handler(evt);
});
((all === null || all === void 0 ? void 0 : all.get('*')) || [])
.slice()
.forEach((handler) => {
handler(type, evt);
});
},
};
}