Comparing version 0.7.0 to 0.8.0
@@ -7,2 +7,8 @@ export type Event<Input, Output> = { | ||
type NonUndefined<T> = T extends undefined | void ? never : T; | ||
interface ListenerOptions { | ||
spreadReturn: boolean; | ||
} | ||
type IsSpreadOptions = ListenerOptions & { | ||
spreadReturn: true; | ||
}; | ||
/** | ||
@@ -14,8 +20,11 @@ * emmi - a small event emitter that enables many workflows | ||
export declare function emmi<EMap extends EventMap>(): { | ||
on: <Key extends keyof EMap>(key: Key, listener: (args: EMap[Key]["input"]) => EMap[Key]["output"]) => void; | ||
emit: <Key_1 extends keyof EMap>(key: Key_1, data: EMap[Key_1]["input"]) => NonUndefined<EMap[Key_1]["output"]>[]; | ||
onReply: <Key_2 extends keyof EMap>(key: Key_2, listener: (input: EMap[Key_2]["input"], output: NonUndefined<EMap[Key_2]["output"]>[]) => void) => void; | ||
off: <Key_3 extends keyof EMap>(key: Key_3, listener?: ((args: EMap[Key_3]["input"]) => EMap[Key_3]["output"]) | undefined) => void; | ||
offReply: <Key_4 extends keyof EMap>(key: Key_4, listener?: ((input: EMap[Key_4]["input"], output: NonUndefined<EMap[Key_4]["output"]>[]) => void) | undefined) => void; | ||
on: { | ||
<Key extends keyof EMap>(key: Key, listener: (args: EMap[Key]["input"]) => EMap[Key]["output"][], options: IsSpreadOptions): void; | ||
<Key_1 extends keyof EMap>(key: Key_1, listener: (args: EMap[Key_1]["input"]) => EMap[Key_1]["output"], options?: ListenerOptions): void; | ||
}; | ||
emit: <Key_2 extends keyof EMap>(key: Key_2, data: EMap[Key_2]["input"]) => NonUndefined<EMap[Key_2]["output"]>[]; | ||
onReply: <Key_3 extends keyof EMap>(key: Key_3, listener: (input: EMap[Key_3]["input"], output: NonUndefined<EMap[Key_3]["output"]>[]) => void) => void; | ||
off: <Key_4 extends keyof EMap>(key: Key_4, listener?: ((args: EMap[Key_4]["input"]) => EMap[Key_4]["output"]) | undefined) => void; | ||
offReply: <Key_5 extends keyof EMap>(key: Key_5, listener?: ((input: EMap[Key_5]["input"], output: NonUndefined<EMap[Key_5]["output"]>[]) => void) | undefined) => void; | ||
}; | ||
export {}; |
function emmi() { | ||
const listeners = /* @__PURE__ */ new Map(); | ||
const optionsMap = /* @__PURE__ */ new Map(); | ||
const replyListeners = /* @__PURE__ */ new Map(); | ||
function on(key, listener) { | ||
function on(key, listener, options) { | ||
options && optionsMap.set(listener, options); | ||
const handlers = listeners.get(key); | ||
@@ -26,4 +28,9 @@ if (handlers) { | ||
const fn = l[i]; | ||
const { spreadReturn = false } = optionsMap.get(fn) || {}; | ||
const res = fn(data); | ||
isDefined(res) && replies.push(res); | ||
if (shouldSpreadResult(spreadReturn)) { | ||
replies.push(...getDefinedArr(res)); | ||
} else if (isDefined(res)) { | ||
replies.push(res); | ||
} | ||
} | ||
@@ -71,4 +78,10 @@ const rl = replyListeners.get(key) || []; | ||
} | ||
function getDefinedArr(t) { | ||
return t.filter(isDefined); | ||
} | ||
function shouldSpreadResult(shouldSpreadResult2, t) { | ||
return shouldSpreadResult2; | ||
} | ||
export { | ||
emmi | ||
}; |
{ | ||
"name": "emmi", | ||
"version": "0.7.0", | ||
"version": "0.8.0", | ||
"type": "module", | ||
@@ -5,0 +5,0 @@ "main": "./dist/index.cjs", |
Sorry, the diff of this file is not supported yet
11309
337