mitt typed event bus: working versions (tested 2026-10-15)
Publish domain events with mitt using a typed event map, covering subscribe, once-style unsubscribe and wildcard handlers offline.
passed2 passed · 0 failed2026-10-15 16:02 UTC- Status
- passed
- Last passed
- 2026-10-15 16:02 UTC
- Tests
- 2 / 2
- Language
- TypeScript
- Runtime
- node v22.23.3
pinned versions3
mitt@3.0.1@types/node@26.6.5tsx@4.23.15
run it
npm install --no-audit --no-fund --ignore-scripts=false npm test
test environment
node v22.23.3 linux 6.17.0-1022-azure x64 runner: github-actions 38764699834
The code
Exactly the files that passed. Your agent can pull them with get_recipe("mitt-typed-events") (install), or see them on GitHub.
package.json
{
"name": "mitt-typed-events",
"private": true,
"type": "module",
"scripts": {
"test": "node --import tsx --test test/*.test.ts"
},
"dependencies": {"mitt":"3.0.1"},
"devDependencies": {"@types/node":"26.6.5","tsx":"4.23.15"}
}
src/bus.ts
import mitt, { type Emitter } from "mitt";
export type AppEvents = {
"user:created": { id: string; email: string };
"order:paid": { orderId: string; total: number };
error: Error;
};
export function createBus(): Emitter<AppEvents> & {
once<K extends keyof AppEvents>(type: K, handler: (event: AppEvents[K]) => void): () => void;
} {
const bus = mitt<AppEvents>();
return Object.assign(bus, {
once<K extends keyof AppEvents>(type: K, handler: (event: AppEvents[K]) => void) {
const wrap = (event: AppEvents[K]) => {
bus.off(type, wrap);
handler(event);
};
bus.on(type, wrap);
return () => bus.off(type, wrap);
},
});
}
test/bus.test.ts
import { test } from "node:test";
import assert from "node:assert/strict";
import { createBus } from "../src/bus.ts";
test("emits typed events to subscribers", () => {
const bus = createBus();
const seen: string[] = [];
bus.on("user:created", (e) => seen.push(e.email));
bus.emit("user:created", { id: "1", email: "[email protected]" });
assert.deepEqual(seen, ["[email protected]"]);
});
test("once unsubscribes after first event", () => {
const bus = createBus();
let count = 0;
bus.once("order:paid", () => {
count++;
});
bus.emit("order:paid", { orderId: "o1", total: 10 });
bus.emit("order:paid", { orderId: "o2", total: 20 });
assert.equal(count, 1);
});
Connect to ProvenPin
Add the free MCP server so your agent can pull this recipe and others like it. Same setup as the docs.
terminal
claude mcp add --transport http provenpin https://mcp.provenpin.com/mcp
~/.cursor/mcp.json
{ "mcpServers": { "provenpin": { "url": "https://mcp.provenpin.com/mcp" } } }.vscode/mcp.json
{ "servers": { "provenpin": { "type": "http", "url": "https://mcp.provenpin.com/mcp" } } }~/.codeium/windsurf/mcp_config.json
{ "mcpServers": { "provenpin": { "serverUrl": "https://mcp.provenpin.com/mcp" } } }MCP settings
{ "mcpServers": { "provenpin": { "type": "streamableHttp", "url": "https://mcp.provenpin.com/mcp" } } }Any other MCP client: Streamable HTTP at https://mcp.provenpin.com/mcp. No API key, no signup.