react-event-channel-z 1.0.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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Delpi.Kye
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,165 @@
1
+ # ๐Ÿ“ก react-event-channel-z
2
+
3
+ [![NPM](https://img.shields.io/npm/v/react-event-channel-z.svg)](https://www.npmjs.com/package/react-event-channel-z)\
4
+ ![Downloads](https://img.shields.io/npm/dt/react-event-channel-z.svg)
5
+
6
+ [LIVE EXAMPLE](https://codesandbox.io/p/sandbox/6w6wtk)
7
+
8
+ ---
9
+
10
+ **react-event-channel-z** is a lightweight, fully typed, React-aware event channel built on top of `eventbus-z`.
11
+
12
+ > Build scalable UI communication without prop drilling or global state
13
+ > overkill.
14
+
15
+ ---
16
+
17
+ ## ๐Ÿ“– Why react-event-channel-z?
18
+
19
+ - โœ… Fully typed EventMap
20
+ - โš›๏ธ Optional startTransition support (React 18+)
21
+ - ๐Ÿ›ก Safe subscriptions with useChannel
22
+ - ๐Ÿงผ No any, no unsafe casts
23
+ - ๐Ÿ“ฆ Lightweight and focused
24
+ - ๐Ÿ”— Built on top of eventbus-z
25
+
26
+ ---
27
+
28
+ ## ๐Ÿ“ฆ Installation
29
+
30
+ ``` bash
31
+ npm install react-event-channel-z
32
+ ```
33
+
34
+ ---
35
+
36
+ ## ๐Ÿš€ Quick Start
37
+
38
+ ### 1๏ธโƒฃ Define your EventMap
39
+
40
+ ``` ts
41
+ type AppEvents = {
42
+ toast: [message: string, type?: "success" | "error"];
43
+ modal: [open: boolean];
44
+ };
45
+ ```
46
+
47
+ ---
48
+
49
+ ### 2๏ธโƒฃ Create a Channel
50
+
51
+ ``` ts
52
+ import { createChannel } from "react-event-channel-z";
53
+
54
+ export const appChannel = createChannel<AppEvents>();
55
+ ```
56
+
57
+ ---
58
+
59
+ ### 3๏ธโƒฃ Emit Events
60
+
61
+ ``` ts
62
+ appChannel.emit("toast", "Saved successfully", "success");
63
+
64
+ appChannel.emit("modal", true, {
65
+ transition: true // uses React.startTransition if available
66
+ });
67
+ ```
68
+
69
+ ---
70
+
71
+ ### 4๏ธโƒฃ Listen inside React Components
72
+
73
+ ``` ts
74
+ import { useChannel } from "react-event-channel-z";
75
+
76
+ useChannel(appChannel, "toast", (message, type) => {
77
+ console.log(message, type);
78
+ });
79
+ ```
80
+
81
+ ---
82
+
83
+ ## โšก React 18 Transition Support
84
+
85
+ If `transition: true` is passed in `emit` options, the event will be
86
+ wrapped with:
87
+
88
+ ``` ts
89
+ React.startTransition(() => { ... });
90
+ ```
91
+
92
+ Fallback is automatically applied for React \<18.
93
+
94
+ ---
95
+
96
+ ## ๐Ÿง  Type Safety
97
+
98
+ - Full inference from `EventMap`
99
+ - Strongly typed `emit`
100
+ - Strongly typed `useChannel`
101
+ - No unsafe casts
102
+ - No `any`
103
+
104
+ ---
105
+
106
+ ## ๐ŸŽฏ Use Cases
107
+
108
+ - Toast systems
109
+ - Modal controllers
110
+ - Global notifications
111
+ - Micro-frontend communication
112
+ - Plugin architecture
113
+ - Dashboard cross-widgets sync
114
+
115
+ ---
116
+
117
+ ## ๐Ÿ“ฆ Bundle Philosophy
118
+
119
+ - No global singletons
120
+ - No Context nesting
121
+ - No reducers
122
+ - No external state managers
123
+ - Just events --- fully typed
124
+
125
+ ---
126
+
127
+ ## ๐Ÿ”Ž Comparison with eventbus-z
128
+
129
+ | Criteria | eventbus-z | react-event-channel-z |
130
+ |---------------------------|------------|-----------------------|
131
+ | Framework agnostic | โœ… | โŒ (React only) |
132
+ | React hook (`useChannel`) | โŒ | โœ… |
133
+ | `startTransition` support | โŒ | โœ… |
134
+ | StrictMode-safe | โŒ | โœ… |
135
+
136
+ ---
137
+
138
+ ## ๐Ÿงฉ Whatโ€™s the difference?
139
+ #### ๐Ÿ“ฆ eventbus-z
140
+
141
+ - Framework-agnostic
142
+ - Pure TypeScript event bus
143
+ - Zero React awareness
144
+ - Works in Node, Vue, Svelte, vanilla JS, etc.
145
+ - Best when you need a core pub/sub engine.
146
+
147
+ #### โš›๏ธ react-event-channel-z
148
+
149
+ - Built specifically for React 18+
150
+ - Wraps eventbus-z
151
+ - Adds:
152
+ - useChannel hook
153
+ - React startTransition integration
154
+ - StrictMode-safe subscription lifecycle
155
+ - React-friendly typing patterns
156
+
157
+ Best when you're building React applications and want event-based communication that behaves correctly with concurrent rendering.
158
+
159
+ > Think of react-event-channel-z as a React-native layer on top of eventbus-z.
160
+
161
+ ---
162
+
163
+ ## ๐Ÿ“„ License
164
+
165
+ MIT
@@ -0,0 +1,8 @@
1
+ import type { EventMapType, EmitOptions } from "./types";
2
+ export type ChannelType<E extends EventMapType> = {
3
+ emit<K extends keyof E>(name: K, ...args: E[K]): void;
4
+ emit<K extends keyof E>(name: K, ...args: [...E[K], EmitOptions]): void;
5
+ on<K extends keyof E>(name: K, handler: (...args: E[K]) => void): void;
6
+ off<K extends keyof E>(name: K, handler: (...args: E[K]) => void): void;
7
+ };
8
+ export declare function createChannel<E extends EventMapType>(): ChannelType<E>;
@@ -0,0 +1,4 @@
1
+ export type EventMapType = Record<string, any[]>;
2
+ export type EmitOptions = {
3
+ transition?: boolean;
4
+ };
@@ -0,0 +1,3 @@
1
+ import type { EventMapType } from "./types";
2
+ import type { ChannelType } from "./createChannel";
3
+ export declare function useChannel<E extends EventMapType, K extends keyof E>(channel: ChannelType<E>, name: K, handler: (...args: E[K]) => void): void;
@@ -0,0 +1,2 @@
1
+ "use strict";var t=require("react"),n=require("eventbus-z");exports.createChannel=function(){const e=n.createTypedEventBus(),o={emit(t,n){e.$emit(t,...n)},on(t,n){e.$on(t,n)},off(t,n){e.$off(t,n)}},r="function"==typeof t.startTransition?t.startTransition:t=>t();return{emit:function(t,...n){const e=n[n.length-1],i="object"==typeof e&&null!==e&&"transition"in e,f=i?n.slice(0,-1):n;i&&e.transition?r(()=>{o.emit(t,f)}):o.emit(t,f)},on:o.on,off:o.off}},exports.useChannel=function(n,e,o){const r=t.useRef(o);r.current=o,t.useEffect(()=>{function t(...t){r.current(...t)}return n.on(e,t),()=>{n.off(e,t)}},[n,e])};
2
+ //# sourceMappingURL=index.cjs.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.cjs.js","sources":["../src/channel/createChannel.ts","../src/channel/useChannel.ts"],"sourcesContent":["import React from \"react\";\nimport { createTypedEventBus } from \"eventbus-z\";\nimport type { EventMapType, EmitOptions } from \"./types\";\n\nexport type ChannelType<E extends EventMapType> = {\n emit<K extends keyof E>(\n name: K,\n ...args: E[K]\n ): void;\n\n emit<K extends keyof E>(\n name: K,\n ...args: [...E[K], EmitOptions]\n ): void;\n\n on<K extends keyof E>(\n name: K,\n handler: (...args: E[K]) => void\n ): void;\n\n off<K extends keyof E>(\n name: K,\n handler: (...args: E[K]) => void\n ): void;\n};\n\nexport function createChannel<E extends EventMapType>(): ChannelType<E> {\n const rawBus = createTypedEventBus<E>();\n\n // Fully typed internal adapter\n const bus = {\n emit<K extends keyof E>(name: K, args: E[K]) {\n rawBus.$emit(name as string, ...args);\n },\n on<K extends keyof E>(\n name: K,\n handler: (...args: E[K]) => void\n ) {\n rawBus.$on(name as string, handler);\n },\n off<K extends keyof E>(\n name: K,\n handler: (...args: E[K]) => void\n ) {\n rawBus.$off(name as string, handler);\n }\n };\n\n const startTransition: (cb: () => void) => void =\n typeof React.startTransition === \"function\"\n ? React.startTransition\n : (cb) => cb();\n\n // โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€\n // overload signatures\n // โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€\n\n function emit<K extends keyof E>(\n name: K,\n ...args: E[K]\n ): void;\n\n function emit<K extends keyof E>(\n name: K,\n ...args: [...E[K], EmitOptions]\n ): void;\n\n function emit<K extends keyof E>(\n name: K,\n ...args: unknown[]\n ) {\n const last = args[args.length - 1];\n\n const hasOptions =\n typeof last === \"object\" &&\n last !== null &&\n \"transition\" in last;\n\n const eventArgs = (hasOptions\n ? args.slice(0, -1)\n : args) as E[K];\n\n if (hasOptions && (last as EmitOptions).transition) {\n startTransition(() => {\n bus.emit(name, eventArgs);\n });\n } else {\n bus.emit(name, eventArgs);\n }\n }\n\n return {\n emit,\n on: bus.on,\n off: bus.off\n };\n}\n","import { useEffect, useRef } from \"react\";\nimport type { EventMapType } from \"./types\";\nimport type { ChannelType } from \"./createChannel\";\n\nexport function useChannel<\n E extends EventMapType,\n K extends keyof E\n>(\n channel: ChannelType<E>,\n name: K,\n handler: (...args: E[K]) => void\n) {\n const handlerRef = useRef(handler);\n\n handlerRef.current = handler;\n\n useEffect(() => {\n function stableWrapper(...args: E[K]) {\n handlerRef.current(...args);\n }\n\n channel.on(name, stableWrapper);\n\n return () => {\n channel.off(name, stableWrapper);\n };\n }, [channel, name]);\n}\n"],"names":["rawBus","createTypedEventBus","bus","emit","name","args","$emit","on","handler","$on","off","$off","startTransition","React","cb","last","length","hasOptions","eventArgs","slice","transition","channel","handlerRef","useRef","current","useEffect","stableWrapper"],"mappings":"6FA2BE,MAAMA,EAASC,EAAAA,sBAGTC,EAAM,CACV,IAAAC,CAAwBC,EAASC,GAC/BL,EAAOM,MAAMF,KAAmBC,EAClC,EACA,EAAAE,CACEH,EACAI,GAEAR,EAAOS,IAAIL,EAAgBI,EAC7B,EACA,GAAAE,CACEN,EACAI,GAEAR,EAAOW,KAAKP,EAAgBI,EAC9B,GAGII,EAC6B,mBAA1BC,EAAMD,gBACTC,EAAMD,gBACLE,GAAOA,IAwCd,MAAO,CACLX,KAzBF,SACEC,KACGC,GAEH,MAAMU,EAAOV,EAAKA,EAAKW,OAAS,GAE1BC,EACY,iBAATF,GACE,OAATA,GACA,eAAgBA,EAEZG,EAAaD,EACfZ,EAAKc,MAAM,GAAG,GACdd,EAEAY,GAAeF,EAAqBK,WACtCR,EAAgB,KACdV,EAAIC,KAAKC,EAAMc,KAGjBhB,EAAIC,KAAKC,EAAMc,EAEnB,EAIEX,GAAIL,EAAIK,GACRG,IAAKR,EAAIQ,IAEb,8BCxFEW,EACAjB,EACAI,GAEA,MAAMc,EAAaC,EAAAA,OAAOf,GAE1Bc,EAAWE,QAAUhB,EAErBiB,EAAAA,UAAU,KACR,SAASC,KAAiBrB,GACxBiB,EAAWE,WAAWnB,EACxB,CAIA,OAFAgB,EAAQd,GAAGH,EAAMsB,GAEV,KACLL,EAAQX,IAAIN,EAAMsB,KAEnB,CAACL,EAASjB,GACf"}
@@ -0,0 +1,3 @@
1
+ export * from "./channel/createChannel";
2
+ export * from "./channel/useChannel";
3
+ export * from "./channel/types";
@@ -0,0 +1,2 @@
1
+ import t,{useRef as n,useEffect as o}from"react";import{createTypedEventBus as i}from"eventbus-z";function r(){const n=i(),o={emit(t,o){n.$emit(t,...o)},on(t,o){n.$on(t,o)},off(t,o){n.$off(t,o)}},r="function"==typeof t.startTransition?t.startTransition:t=>t();return{emit:function(t,...n){const i=n[n.length-1],f="object"==typeof i&&null!==i&&"transition"in i,e=f?n.slice(0,-1):n;f&&i.transition?r(()=>{o.emit(t,e)}):o.emit(t,e)},on:o.on,off:o.off}}function f(t,i,r){const f=n(r);f.current=r,o(()=>{function n(...t){f.current(...t)}return t.on(i,n),()=>{t.off(i,n)}},[t,i])}export{r as createChannel,f as useChannel};
2
+ //# sourceMappingURL=index.esm.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.esm.js","sources":["../src/channel/createChannel.ts","../src/channel/useChannel.ts"],"sourcesContent":["import React from \"react\";\nimport { createTypedEventBus } from \"eventbus-z\";\nimport type { EventMapType, EmitOptions } from \"./types\";\n\nexport type ChannelType<E extends EventMapType> = {\n emit<K extends keyof E>(\n name: K,\n ...args: E[K]\n ): void;\n\n emit<K extends keyof E>(\n name: K,\n ...args: [...E[K], EmitOptions]\n ): void;\n\n on<K extends keyof E>(\n name: K,\n handler: (...args: E[K]) => void\n ): void;\n\n off<K extends keyof E>(\n name: K,\n handler: (...args: E[K]) => void\n ): void;\n};\n\nexport function createChannel<E extends EventMapType>(): ChannelType<E> {\n const rawBus = createTypedEventBus<E>();\n\n // Fully typed internal adapter\n const bus = {\n emit<K extends keyof E>(name: K, args: E[K]) {\n rawBus.$emit(name as string, ...args);\n },\n on<K extends keyof E>(\n name: K,\n handler: (...args: E[K]) => void\n ) {\n rawBus.$on(name as string, handler);\n },\n off<K extends keyof E>(\n name: K,\n handler: (...args: E[K]) => void\n ) {\n rawBus.$off(name as string, handler);\n }\n };\n\n const startTransition: (cb: () => void) => void =\n typeof React.startTransition === \"function\"\n ? React.startTransition\n : (cb) => cb();\n\n // โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€\n // overload signatures\n // โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€\n\n function emit<K extends keyof E>(\n name: K,\n ...args: E[K]\n ): void;\n\n function emit<K extends keyof E>(\n name: K,\n ...args: [...E[K], EmitOptions]\n ): void;\n\n function emit<K extends keyof E>(\n name: K,\n ...args: unknown[]\n ) {\n const last = args[args.length - 1];\n\n const hasOptions =\n typeof last === \"object\" &&\n last !== null &&\n \"transition\" in last;\n\n const eventArgs = (hasOptions\n ? args.slice(0, -1)\n : args) as E[K];\n\n if (hasOptions && (last as EmitOptions).transition) {\n startTransition(() => {\n bus.emit(name, eventArgs);\n });\n } else {\n bus.emit(name, eventArgs);\n }\n }\n\n return {\n emit,\n on: bus.on,\n off: bus.off\n };\n}\n","import { useEffect, useRef } from \"react\";\nimport type { EventMapType } from \"./types\";\nimport type { ChannelType } from \"./createChannel\";\n\nexport function useChannel<\n E extends EventMapType,\n K extends keyof E\n>(\n channel: ChannelType<E>,\n name: K,\n handler: (...args: E[K]) => void\n) {\n const handlerRef = useRef(handler);\n\n handlerRef.current = handler;\n\n useEffect(() => {\n function stableWrapper(...args: E[K]) {\n handlerRef.current(...args);\n }\n\n channel.on(name, stableWrapper);\n\n return () => {\n channel.off(name, stableWrapper);\n };\n }, [channel, name]);\n}\n"],"names":["createChannel","rawBus","createTypedEventBus","bus","emit","name","args","$emit","on","handler","$on","off","$off","startTransition","React","cb","last","length","hasOptions","eventArgs","slice","transition","useChannel","channel","handlerRef","useRef","current","useEffect","stableWrapper"],"mappings":"2GA0BgBA,IACd,MAAMC,EAASC,IAGTC,EAAM,CACV,IAAAC,CAAwBC,EAASC,GAC/BL,EAAOM,MAAMF,KAAmBC,EAClC,EACA,EAAAE,CACEH,EACAI,GAEAR,EAAOS,IAAIL,EAAgBI,EAC7B,EACA,GAAAE,CACEN,EACAI,GAEAR,EAAOW,KAAKP,EAAgBI,EAC9B,GAGII,EAC6B,mBAA1BC,EAAMD,gBACTC,EAAMD,gBACLE,GAAOA,IAwCd,MAAO,CACLX,KAzBF,SACEC,KACGC,GAEH,MAAMU,EAAOV,EAAKA,EAAKW,OAAS,GAE1BC,EACY,iBAATF,GACE,OAATA,GACA,eAAgBA,EAEZG,EAAaD,EACfZ,EAAKc,MAAM,GAAG,GACdd,EAEAY,GAAeF,EAAqBK,WACtCR,EAAgB,KACdV,EAAIC,KAAKC,EAAMc,KAGjBhB,EAAIC,KAAKC,EAAMc,EAEnB,EAIEX,GAAIL,EAAIK,GACRG,IAAKR,EAAIQ,IAEb,UC5FgBW,EAIdC,EACAlB,EACAI,GAEA,MAAMe,EAAaC,EAAOhB,GAE1Be,EAAWE,QAAUjB,EAErBkB,EAAU,KACR,SAASC,KAAiBtB,GACxBkB,EAAWE,WAAWpB,EACxB,CAIA,OAFAiB,EAAQf,GAAGH,EAAMuB,GAEV,KACLL,EAAQZ,IAAIN,EAAMuB,KAEnB,CAACL,EAASlB,GACf"}
package/package.json ADDED
@@ -0,0 +1,72 @@
1
+ {
2
+ "name": "react-event-channel-z",
3
+ "version": "1.0.0",
4
+ "description": "Fully typed React-aware event channel built on top of eventbus-z.",
5
+ "author": "Delpi.Kye",
6
+ "license": "MIT",
7
+ "type": "module",
8
+ "sideEffects": false,
9
+
10
+ "main": "./build/index.cjs.js",
11
+ "module": "./build/index.esm.js",
12
+ "types": "./build/index.d.ts",
13
+ "exports": {
14
+ ".": {
15
+ "types": "./build/index.d.ts",
16
+ "import": "./build/index.esm.js",
17
+ "require": "./build/index.cjs.js"
18
+ }
19
+ },
20
+ "files": [
21
+ "build"
22
+ ],
23
+
24
+ "scripts": {
25
+ "clean": "rimraf build",
26
+ "build": "rollup -c",
27
+ "cb": "npm run clean && npm run build",
28
+ "prepublishOnly": "npm run build"
29
+ },
30
+
31
+ "repository": {
32
+ "type": "git",
33
+ "url": "https://github.com/delpikye-v/react-event-channel.git"
34
+ },
35
+ "homepage": "https://github.com/delpikye-v/react-event-channel#readme",
36
+ "bugs": {
37
+ "url": "https://github.com/delpikye-v/react-event-channel/issues"
38
+ },
39
+
40
+ "keywords": [
41
+ "react",
42
+ "typescript",
43
+ "event-bus",
44
+ "event-channel",
45
+ "pubsub",
46
+ "react-patterns",
47
+ "micro-frontend",
48
+ "react-architecture"
49
+ ],
50
+
51
+ "peerDependencies": {
52
+ "react": "^18.0.0 || ^19.0.0"
53
+ },
54
+ "dependencies": {
55
+ "eventbus-z": "^2.3.0"
56
+ },
57
+ "devDependencies": {
58
+ "@rollup/plugin-commonjs": "^25.0.0",
59
+ "@rollup/plugin-node-resolve": "^15.0.0",
60
+ "@rollup/plugin-terser": "^0.4.4",
61
+ "@rollup/plugin-typescript": "^12.3.0",
62
+ "@types/react": "^18.0.0",
63
+ "rimraf": "^5.0.0",
64
+ "rollup": "^4.0.0",
65
+ "rollup-plugin-peer-deps-external": "^2.2.4",
66
+ "tslib": "^2.6.0",
67
+ "typescript": "^5.3.0"
68
+ },
69
+ "engines": {
70
+ "node": ">=16"
71
+ }
72
+ }