use-typed-event 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +516 -0
- package/dist/chunk-ZRE57KHP.js +133 -0
- package/dist/index.cjs +159 -0
- package/dist/index.d.cts +80 -0
- package/dist/index.d.ts +80 -0
- package/dist/index.js +8 -0
- package/dist/react.cjs +234 -0
- package/dist/react.d.cts +50 -0
- package/dist/react.d.ts +50 -0
- package/dist/react.js +82 -0
- package/package.json +100 -0
package/dist/react.js
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createEmitter,
|
|
3
|
+
createSelectors,
|
|
4
|
+
keyOf
|
|
5
|
+
} from "./chunk-ZRE57KHP.js";
|
|
6
|
+
|
|
7
|
+
// src/react/use-event.ts
|
|
8
|
+
import { useEffect, useRef } from "react";
|
|
9
|
+
var makeUseEvent = (emitter) => {
|
|
10
|
+
const useEvent = (name, handler) => {
|
|
11
|
+
const key = keyOf(name);
|
|
12
|
+
const handlerRef = useRef(handler);
|
|
13
|
+
useEffect(() => {
|
|
14
|
+
handlerRef.current = handler;
|
|
15
|
+
});
|
|
16
|
+
useEffect(() => emitter.on(key, (payload) => handlerRef.current(payload)), [key]);
|
|
17
|
+
};
|
|
18
|
+
return useEvent;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
// src/react/use-event-state.ts
|
|
22
|
+
import { useCallback, useRef as useRef2, useSyncExternalStore } from "react";
|
|
23
|
+
var makeUseEventState = (emitter, last) => {
|
|
24
|
+
function useEventState(name, initialValue) {
|
|
25
|
+
const initialRef = useRef2(initialValue);
|
|
26
|
+
const key = keyOf(name);
|
|
27
|
+
const subscribe = useCallback(
|
|
28
|
+
(onStoreChange) => emitter.on(key, (payload) => {
|
|
29
|
+
last.set(key, payload);
|
|
30
|
+
onStoreChange();
|
|
31
|
+
}),
|
|
32
|
+
[key]
|
|
33
|
+
);
|
|
34
|
+
const getSnapshot = () => last.has(key) ? last.get(key) : initialRef.current;
|
|
35
|
+
const getServerSnapshot = () => initialRef.current;
|
|
36
|
+
const value = useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot);
|
|
37
|
+
const setValue = useCallback(
|
|
38
|
+
(...args) => {
|
|
39
|
+
emitter.emit(key, ...args);
|
|
40
|
+
},
|
|
41
|
+
[key]
|
|
42
|
+
);
|
|
43
|
+
return [value, setValue];
|
|
44
|
+
}
|
|
45
|
+
return useEventState;
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
// src/react.ts
|
|
49
|
+
var setUpHooks = (emitter) => {
|
|
50
|
+
const last = /* @__PURE__ */ new Map();
|
|
51
|
+
const record = (name, payload) => {
|
|
52
|
+
last.set(name, payload);
|
|
53
|
+
};
|
|
54
|
+
const installRecorder = () => {
|
|
55
|
+
emitter.onAny(record);
|
|
56
|
+
};
|
|
57
|
+
installRecorder();
|
|
58
|
+
const hooks = {
|
|
59
|
+
useEvent: makeUseEvent(emitter),
|
|
60
|
+
useEventState: makeUseEventState(emitter, last)
|
|
61
|
+
};
|
|
62
|
+
return { hooks, last, installRecorder };
|
|
63
|
+
};
|
|
64
|
+
var createEventHooks = (emitter) => setUpHooks(emitter).hooks;
|
|
65
|
+
var createEventBus = () => {
|
|
66
|
+
const emitter = createEmitter();
|
|
67
|
+
const bus = { ...emitter };
|
|
68
|
+
const { hooks, last, installRecorder } = setUpHooks(bus);
|
|
69
|
+
const clear = () => {
|
|
70
|
+
emitter.clear();
|
|
71
|
+
last.clear();
|
|
72
|
+
installRecorder();
|
|
73
|
+
};
|
|
74
|
+
const events = createSelectors();
|
|
75
|
+
return Object.assign(bus, hooks, { clear, events });
|
|
76
|
+
};
|
|
77
|
+
export {
|
|
78
|
+
createEmitter,
|
|
79
|
+
createEventBus,
|
|
80
|
+
createEventHooks,
|
|
81
|
+
createSelectors
|
|
82
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "use-typed-event",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Minimal TS-native typed event emitter with first-class React hooks. Typed wildcard, void events, waitFor, zero dependencies.",
|
|
5
|
+
"author": "Dominik Rycharski (https://github.com/doryski)",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/Doryski/use-typed-event.git"
|
|
9
|
+
},
|
|
10
|
+
"bugs": {
|
|
11
|
+
"url": "https://github.com/Doryski/use-typed-event/issues"
|
|
12
|
+
},
|
|
13
|
+
"homepage": "https://github.com/Doryski/use-typed-event#readme",
|
|
14
|
+
"keywords": [
|
|
15
|
+
"event",
|
|
16
|
+
"emitter",
|
|
17
|
+
"event-emitter",
|
|
18
|
+
"event-bus",
|
|
19
|
+
"typed",
|
|
20
|
+
"typescript",
|
|
21
|
+
"react",
|
|
22
|
+
"hooks",
|
|
23
|
+
"pubsub",
|
|
24
|
+
"wildcard",
|
|
25
|
+
"mitt"
|
|
26
|
+
],
|
|
27
|
+
"type": "module",
|
|
28
|
+
"license": "MIT",
|
|
29
|
+
"engines": {
|
|
30
|
+
"node": ">=20"
|
|
31
|
+
},
|
|
32
|
+
"main": "./dist/index.cjs",
|
|
33
|
+
"module": "./dist/index.js",
|
|
34
|
+
"types": "./dist/index.d.ts",
|
|
35
|
+
"exports": {
|
|
36
|
+
".": {
|
|
37
|
+
"import": {
|
|
38
|
+
"types": "./dist/index.d.ts",
|
|
39
|
+
"default": "./dist/index.js"
|
|
40
|
+
},
|
|
41
|
+
"require": {
|
|
42
|
+
"types": "./dist/index.d.cts",
|
|
43
|
+
"default": "./dist/index.cjs"
|
|
44
|
+
}
|
|
45
|
+
},
|
|
46
|
+
"./react": {
|
|
47
|
+
"import": {
|
|
48
|
+
"types": "./dist/react.d.ts",
|
|
49
|
+
"default": "./dist/react.js"
|
|
50
|
+
},
|
|
51
|
+
"require": {
|
|
52
|
+
"types": "./dist/react.d.cts",
|
|
53
|
+
"default": "./dist/react.cjs"
|
|
54
|
+
}
|
|
55
|
+
},
|
|
56
|
+
"./package.json": "./package.json"
|
|
57
|
+
},
|
|
58
|
+
"sideEffects": false,
|
|
59
|
+
"files": [
|
|
60
|
+
"dist"
|
|
61
|
+
],
|
|
62
|
+
"scripts": {
|
|
63
|
+
"bench": "tsx benchmarks/emit.ts",
|
|
64
|
+
"build": "tsup src/index.ts src/react.ts --format esm,cjs --dts --clean",
|
|
65
|
+
"test": "vitest run --maxWorkers=1 --bail=5",
|
|
66
|
+
"type-check": "tsc --noEmit",
|
|
67
|
+
"lint": "eslint .",
|
|
68
|
+
"lint:fix": "eslint . --fix",
|
|
69
|
+
"prepublishOnly": "pnpm build",
|
|
70
|
+
"release": "doryski-release"
|
|
71
|
+
},
|
|
72
|
+
"peerDependencies": {
|
|
73
|
+
"react": "^18.0.0 || ^19.0.0"
|
|
74
|
+
},
|
|
75
|
+
"peerDependenciesMeta": {
|
|
76
|
+
"react": {
|
|
77
|
+
"optional": true
|
|
78
|
+
}
|
|
79
|
+
},
|
|
80
|
+
"devDependencies": {
|
|
81
|
+
"@doryski/release": "^1.0.0",
|
|
82
|
+
"@eslint/js": "^10.0.1",
|
|
83
|
+
"@testing-library/react": "^16.3.2",
|
|
84
|
+
"@types/node": "^26.0.1",
|
|
85
|
+
"@types/react": "^19.2.17",
|
|
86
|
+
"@types/react-dom": "^19.2.3",
|
|
87
|
+
"commander": "^14.0.3",
|
|
88
|
+
"eslint": "^10.5.0",
|
|
89
|
+
"eslint-plugin-react-hooks": "^7.1.1",
|
|
90
|
+
"happy-dom": "^20.10.6",
|
|
91
|
+
"react": "^19.2.7",
|
|
92
|
+
"react-dom": "^19.2.7",
|
|
93
|
+
"tinybench": "^6.0.2",
|
|
94
|
+
"tsup": "^8.5.1",
|
|
95
|
+
"tsx": "^4.22.4",
|
|
96
|
+
"typescript": "^6.0.3",
|
|
97
|
+
"typescript-eslint": "^8.62.0",
|
|
98
|
+
"vitest": "^4.1.9"
|
|
99
|
+
}
|
|
100
|
+
}
|