Comparing version 0.0.1 to 0.1.0
@@ -21,4 +21,16 @@ function emmi() { | ||
function emit(key, data) { | ||
const replies = (listeners.get(key) || []).map((fn) => fn(data)); | ||
(replyListeners.get(key) || []).forEach((fn) => fn(replies)); | ||
let replies = []; | ||
const l = listeners.get(key) || []; | ||
let i = -1; | ||
while (l.length > ++i) { | ||
const fn = l[i]; | ||
const res = fn(data); | ||
res !== void 0 && replies.push(res); | ||
} | ||
const rl = replyListeners.get(key) || []; | ||
i = -1; | ||
while (rl.length > ++i) { | ||
const fn = rl[i]; | ||
fn(replies); | ||
} | ||
return replies; | ||
@@ -43,3 +55,4 @@ } | ||
if (handlers) { | ||
handlers.splice(handlers.indexOf(listener) >>> 0, 1); | ||
const index = handlers.indexOf(listener); | ||
index !== -1 && handlers.splice(index, 1); | ||
} | ||
@@ -46,0 +59,0 @@ } |
{ | ||
"name": "emmi", | ||
"version": "0.0.1", | ||
"version": "0.1.0", | ||
"type": "module", | ||
"main": "./dist/index.cjs", | ||
"module": "./dist/index.js", | ||
@@ -9,3 +10,4 @@ "types": "./dist/index.d.ts", | ||
".": { | ||
"import": "./dist/index.js" | ||
"import": "./dist/index.js", | ||
"require": "./dist/index.cjs" | ||
} | ||
@@ -19,6 +21,8 @@ }, | ||
"build": "vite build", | ||
"test": "vitest" | ||
"test": "vitest", | ||
"prepublishOnly": "vitest run && npm run build" | ||
}, | ||
"devDependencies": { | ||
"@types/node": "^20.4.8", | ||
"@vitest/coverage-v8": "^0.34.1", | ||
"prettier": "^3.0.1", | ||
@@ -25,0 +29,0 @@ "typescript": "^5.0.2", |
@@ -1,3 +0,10 @@ | ||
# mitti | ||
# emmi | ||
Inspired by [mitt](https://github.com/developit/mitt/tree/main), but with a few modifications. | ||
> WIP | ||
Inspired by [mitt](https://github.com/developit/mitt/tree/main), but with a few modifications. Main change is that events are typed with an `input` and `output` field. | ||
This enables: | ||
- `emit` returns an `Output[]` type when emitting an event. | ||
- You can listen to event replies with `onReply` |
4995
8
152
11
7