Skip to content

Commit

Permalink
feat: add subscribe and dispatch
Browse files Browse the repository at this point in the history
  • Loading branch information
Yukiniro committed Oct 16, 2022
1 parent dd2339e commit a42f1a1
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 18 deletions.
4 changes: 1 addition & 3 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
dist
__test__
node_modules
website
node_modules
6 changes: 0 additions & 6 deletions __test__/hello.test.js

This file was deleted.

41 changes: 41 additions & 0 deletions __test__/hello.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { describe } from "vitest";
import { test, expect } from "vitest";
import miis from "../src";

describe("basic useage", () => {
test("subscribe", async () => {
await new Promise((resolve) => {
miis.subscribe("a", resolve);
miis.dispatch("a");
});
});

test("unsubscribe", async () => {
await new Promise((resolve, reject) => {
const unsubscribe = miis.subscribe("b", reject);
unsubscribe();
miis.dispatch("b");
setTimeout(resolve, 10);
});
});
});

describe("arguments", () => {
test("single", async () => {
const result = await new Promise((resolve) => {
miis.subscribe("b", resolve);
miis.dispatch("b", 1);
});
expect(result).toBe(1);
});

test("multi", async () => {
const result = await new Promise((resolve) => {
miis.subscribe("b", (...args) => {
resolve(Array.from(args));
});
miis.dispatch("b", 2, 3, 4);
});
expect(result).toEqual([2, 3, 4]);
});
});
16 changes: 10 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
{
"name": "miis",
"version": "0.0.1",
"description": "Simple Start Template",
"description": "Tiny functional event subscriber and dispatcher.",
"main": "index.ts",
"scripts": {
"build": "rollup -c rollup.config.js",
"lint": "npx eslint src/**",
"prettier": "npx prettier src/** --write",
"prettier": "npx prettier src/** --write && npx prettier __test__/** --write",
"test": "npx vitest",
"test:run": "npx vitest run",
"test:coverrage": "npx vitest --coverage",
"publish": "npm run build && npm publish"
},
"keywords": [
"vite",
"vitest",
"docusaurus",
"template"
"event",
"emitter",
"subscribe",
"dispatch",
"linstener"
],
"author": "Yukiniro",
"license": "MIT",
Expand Down Expand Up @@ -44,5 +45,8 @@
"tslib": "^2.3.1",
"typescript": "^4.6.4",
"vitest": "^0.12.2"
},
"dependencies": {
"bittydash": "^0.1.1"
}
}
8 changes: 8 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

51 changes: 48 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,48 @@
export const sayHello = function () {
return "Hello World";
}
import { remove } from "bittydash";

type Listener = (...args: any[]) => void;
type EventName = string | symbol;
type Options = {
once: boolean;
};
type Subscriber = {
listener: Listener;
once: boolean;
};

const handlerMap = new Map();

function subscribe(
key: EventName,
listener: Listener,
options?: Options
): () => void {
if (!handlerMap.has(key)) {
handlerMap.set(key, []);
}
const list = handlerMap.get(key);
const { once } = options || {};
const item = { listener, once };
list.push(item);
return () => {
remove(list, item);
};
}

function dispatch(key: EventName, ...args: any[]) {
const list = handlerMap.get(key) || [];
const removeList = [];
list.forEach((item: Subscriber) => {
const { listener, once } = item;
listener(...args);
if (once) {
removeList.push(item);
}
});
removeList.forEach((item: Subscriber) => remove(list, item));
}

export default {
subscribe,
dispatch,
};

0 comments on commit a42f1a1

Please sign in to comment.