aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJack Franklin <[email protected]>2020-05-26 22:45:53 +0100
committerGitHub <[email protected]>2020-05-26 17:45:53 -0400
commitbe5237f301b1059ffff9e1e795fd66594644892a (patch)
treec5cae9f5aefeb481dcd88264b2c48c8f248ad42c /src
parentcfd246c8fa9f6e255b63cb98a832b6501cfaa23d (diff)
Migrate to TypeScript and use Map (#99)
Migrate to TypeScript & Microbundle, move to Map for event handler storage
Diffstat (limited to 'src')
-rw-r--r--src/index.js63
-rw-r--r--src/index.ts75
2 files changed, 75 insertions, 63 deletions
diff --git a/src/index.js b/src/index.js
deleted file mode 100644
index d9b36f8..0000000
--- a/src/index.js
+++ /dev/null
@@ -1,63 +0,0 @@
-// @flow
-// An event handler can take an optional event argument
-// and should not return a value
-type EventHandler = (event?: any) => void;
-type WildCardEventHandler = (type: string, event?: any) => void
-
-// An array of all currently registered event handlers for a type
-type EventHandlerList = Array<EventHandler>;
-type WildCardEventHandlerList = Array<WildCardEventHandler>;
-// A map of event types and their corresponding event handlers.
-type EventHandlerMap = {
- '*'?: WildCardEventHandlerList,
- [type: string]: EventHandlerList,
-};
-
-/** Mitt: Tiny (~200b) functional event emitter / pubsub.
- * @name mitt
- * @returns {Mitt}
- */
-export default function mitt(all: EventHandlerMap) {
- all = all || Object.create(null);
-
- return {
- /**
- * Register an event handler for the given type.
- *
- * @param {String} 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: string, handler: EventHandler) {
- (all[type] || (all[type] = [])).push(handler);
- },
-
- /**
- * Remove an event handler for the given type.
- *
- * @param {String} type Type of event to unregister `handler` from, or `"*"`
- * @param {Function} handler Handler function to remove
- * @memberOf mitt
- */
- off(type: string, handler: EventHandler) {
- if (all[type]) {
- all[type].splice(all[type].indexOf(handler) >>> 0, 1);
- }
- },
-
- /**
- * Invoke all handlers for the given type.
- * If present, `"*"` handlers are invoked after type-matched handlers.
- *
- * Note: Manually firing "*" handlers is not supported.
- *
- * @param {String} type The event type to invoke
- * @param {Any} [evt] Any value (object is recommended and powerful), passed to each handler
- * @memberOf mitt
- */
- emit(type: string, evt: any) {
- (all[type] || []).slice().map((handler) => { handler(evt); });
- (all['*'] || []).slice().map((handler) => { handler(type, evt); });
- }
- };
-}
diff --git a/src/index.ts b/src/index.ts
new file mode 100644
index 0000000..f0a2ebb
--- /dev/null
+++ b/src/index.ts
@@ -0,0 +1,75 @@
+type EventType = string | symbol;
+
+// An event handler can take an optional event argument
+// and should not return a value
+type Handler = (event?: any) => void;
+type WildcardHandler= (type: EventType, event?: any) => void
+
+// An array of all currently registered event handlers for a type
+type EventHandlerList = Array<Handler>;
+type WildCardEventHandlerList = Array<WildcardHandler>;
+
+// A map of event types and their corresponding event handlers.
+type EventHandlerMap = Map<EventType, EventHandlerList | WildCardEventHandlerList>;
+
+export interface Emitter {
+ on(type: EventType, handler: Handler): void;
+ on(type: "*", handler: WildcardHandler): void;
+
+ off(type: EventType, handler: Handler): 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 {
+ all = all || new Map();
+
+ return {
+ /**
+ * 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: EventType, handler: Handler) {
+ const handlers = (all.get(type) || []);
+ handlers.push(handler);
+ all.set(type, handlers);
+ },
+
+ /**
+ * Remove an event handler for the given type.
+ *
+ * @param {string|symbol} type Type of event to unregister `handler` from, or `"*"`
+ * @param {Function} handler Handler function to remove
+ * @memberOf mitt
+ */
+ off(type: EventType, handler: Handler) {
+ if (all.has(type)) {
+ all.get(type).splice(all.get(type).indexOf(handler) >>> 0, 1);
+ }
+ },
+
+ /**
+ * Invoke all handlers for the given type.
+ * If present, `"*"` handlers are invoked after type-matched handlers.
+ *
+ * Note: Manually firing "*" handlers is not supported.
+ *
+ * @param {string|symbol} type The event type to invoke
+ * @param {Any} [evt] Any value (object is recommended and powerful), passed to each handler
+ * @memberOf mitt
+ */
+ emit(type: EventType, evt: any) {
+ ((all.get(type) || []) as EventHandlerList).slice().map((handler) => { handler(evt); });
+ ((all.get('*') || []) as WildCardEventHandlerList).slice().map((handler) => { handler(type, evt); });
+ }
+ };
+}