@tiny-event-bus/react 0.10.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 +220 -0
- package/dist/cjs/create-bus-context.d.ts +12 -0
- package/dist/cjs/create-bus-context.d.ts.map +1 -0
- package/dist/cjs/create-bus-context.js +64 -0
- package/dist/cjs/create-bus-context.js.map +1 -0
- package/dist/cjs/index.d.ts +5 -0
- package/dist/cjs/index.d.ts.map +1 -0
- package/dist/cjs/index.js +12 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/cjs/use-any-event.d.ts +3 -0
- package/dist/cjs/use-any-event.d.ts.map +1 -0
- package/dist/cjs/use-any-event.js +16 -0
- package/dist/cjs/use-any-event.js.map +1 -0
- package/dist/cjs/use-event-bus.d.ts +8 -0
- package/dist/cjs/use-event-bus.d.ts.map +1 -0
- package/dist/cjs/use-event-bus.js +22 -0
- package/dist/cjs/use-event-bus.js.map +1 -0
- package/dist/cjs/use-event.d.ts +3 -0
- package/dist/cjs/use-event.d.ts.map +1 -0
- package/dist/cjs/use-event.js +16 -0
- package/dist/cjs/use-event.js.map +1 -0
- package/dist/esm/create-bus-context.d.ts +12 -0
- package/dist/esm/create-bus-context.d.ts.map +1 -0
- package/dist/esm/create-bus-context.js +28 -0
- package/dist/esm/create-bus-context.js.map +1 -0
- package/dist/esm/index.d.ts +5 -0
- package/dist/esm/index.d.ts.map +1 -0
- package/dist/esm/index.js +5 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/use-any-event.d.ts +3 -0
- package/dist/esm/use-any-event.d.ts.map +1 -0
- package/dist/esm/use-any-event.js +13 -0
- package/dist/esm/use-any-event.js.map +1 -0
- package/dist/esm/use-event-bus.d.ts +8 -0
- package/dist/esm/use-event-bus.d.ts.map +1 -0
- package/dist/esm/use-event-bus.js +19 -0
- package/dist/esm/use-event-bus.js.map +1 -0
- package/dist/esm/use-event.d.ts +3 -0
- package/dist/esm/use-event.d.ts.map +1 -0
- package/dist/esm/use-event.js +13 -0
- package/dist/esm/use-event.js.map +1 -0
- package/package.json +68 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Vineet Desai
|
|
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,220 @@
|
|
|
1
|
+
# @tiny-event-bus/react
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/@tiny-event-bus/react)
|
|
4
|
+
|
|
5
|
+
React hooks plugin for `@tiny-event-bus/core`. ~915 B gzipped.
|
|
6
|
+
|
|
7
|
+
Requires React >=17 and `@tiny-event-bus/core` as peer dependencies.
|
|
8
|
+
|
|
9
|
+
## Install
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
npm install @tiny-event-bus/core @tiny-event-bus/react
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Quick Start
|
|
16
|
+
|
|
17
|
+
```tsx
|
|
18
|
+
import { createEventBus } from '@tiny-event-bus/core';
|
|
19
|
+
import { useEvent, useEventBus } from '@tiny-event-bus/react';
|
|
20
|
+
|
|
21
|
+
type AppEvents = {
|
|
22
|
+
'toast:show': { message: string; severity: 'info' | 'error' };
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
const bus = createEventBus<AppEvents>();
|
|
26
|
+
|
|
27
|
+
function ToastListener() {
|
|
28
|
+
useEvent(
|
|
29
|
+
'toast:show',
|
|
30
|
+
(data) => {
|
|
31
|
+
console.log(data.message); // fully typed, auto-cleanup on unmount
|
|
32
|
+
},
|
|
33
|
+
bus,
|
|
34
|
+
);
|
|
35
|
+
return null;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function EmitButton() {
|
|
39
|
+
const { emit } = useEventBus(bus);
|
|
40
|
+
return (
|
|
41
|
+
<button
|
|
42
|
+
onClick={() => emit('toast:show', { message: 'Hi!', severity: 'info' })}
|
|
43
|
+
>
|
|
44
|
+
Show Toast
|
|
45
|
+
</button>
|
|
46
|
+
);
|
|
47
|
+
}
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
## API
|
|
51
|
+
|
|
52
|
+
| Hook | Description |
|
|
53
|
+
| ------------------------------- | ------------------------------------------------------------------------------------------------------------------------------ |
|
|
54
|
+
| `useEvent(event, handler, bus)` | Subscribe with auto-cleanup on unmount. Uses `useRef` internally so handler updates never cause re-subscription. |
|
|
55
|
+
| `useEventBus(bus)` | Returns `BusMethods<B>` — all function-valued properties of the bus with stable refs via `useMemo`. Works with any bus object. |
|
|
56
|
+
| `useAnyEvent(handler, bus)` | Subscribe to all events with auto-cleanup. Uses `useRef` for handler stability. |
|
|
57
|
+
| `createBusContext<T>()` | Factory — returns `{ Provider, useEvent, useEventBus, useAnyEvent }`. Hooks read bus from context (no bus arg needed). |
|
|
58
|
+
|
|
59
|
+
### `useEvent` — subscribe to a single event
|
|
60
|
+
|
|
61
|
+
```tsx
|
|
62
|
+
function ToastListener() {
|
|
63
|
+
useEvent(
|
|
64
|
+
'toast:show',
|
|
65
|
+
(data) => {
|
|
66
|
+
showToast(data.message);
|
|
67
|
+
},
|
|
68
|
+
bus,
|
|
69
|
+
);
|
|
70
|
+
return null;
|
|
71
|
+
}
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
Handler is stored in a `useRef` — updating the handler function doesn't cause re-subscription. Cleanup runs automatically on unmount.
|
|
75
|
+
|
|
76
|
+
### `useEventBus` — emit, subscribe, and clear with stable refs
|
|
77
|
+
|
|
78
|
+
```tsx
|
|
79
|
+
function ActionBar() {
|
|
80
|
+
const { emit, on, once, clear } = useEventBus(bus);
|
|
81
|
+
|
|
82
|
+
return (
|
|
83
|
+
<button
|
|
84
|
+
onClick={() => emit('toast:show', { message: 'Hi!', severity: 'info' })}
|
|
85
|
+
>
|
|
86
|
+
Toast
|
|
87
|
+
</button>
|
|
88
|
+
);
|
|
89
|
+
}
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
Returns `BusMethods<B>` — a mapped type that exposes every function-valued property on the bus object. This means it works with plain buses _and_ decorated buses like `ReplayBus`:
|
|
93
|
+
|
|
94
|
+
```tsx
|
|
95
|
+
import { withReplay } from '@tiny-event-bus/replay';
|
|
96
|
+
|
|
97
|
+
const replayBus = withReplay(createEventBus<AppEvents>());
|
|
98
|
+
|
|
99
|
+
function Component() {
|
|
100
|
+
// getHistory and clearHistory are also available
|
|
101
|
+
const { emit, getHistory, clearHistory } = useEventBus(replayBus);
|
|
102
|
+
}
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
`clear()` removes all listeners. `clear(event)` removes only that event's listeners.
|
|
106
|
+
|
|
107
|
+
### `useAnyEvent` — catch-all hook
|
|
108
|
+
|
|
109
|
+
```tsx
|
|
110
|
+
function AnalyticsLogger() {
|
|
111
|
+
useAnyEvent((event, data) => {
|
|
112
|
+
analytics.track(String(event), data);
|
|
113
|
+
}, bus);
|
|
114
|
+
return null;
|
|
115
|
+
}
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
### `createBusContext` — scoped context (no prop drilling)
|
|
119
|
+
|
|
120
|
+
```tsx
|
|
121
|
+
import { createEventBus } from '@tiny-event-bus/core';
|
|
122
|
+
import { createBusContext } from '@tiny-event-bus/react';
|
|
123
|
+
|
|
124
|
+
type ChatEvents = { 'message:new': { text: string } };
|
|
125
|
+
|
|
126
|
+
const { Provider, useEvent, useEventBus, useAnyEvent } =
|
|
127
|
+
createBusContext<ChatEvents>();
|
|
128
|
+
|
|
129
|
+
// Provide a bus instance at the top
|
|
130
|
+
function ChatRoot() {
|
|
131
|
+
const bus = createEventBus<ChatEvents>();
|
|
132
|
+
return (
|
|
133
|
+
<Provider bus={bus}>
|
|
134
|
+
<ChatMessages />
|
|
135
|
+
</Provider>
|
|
136
|
+
);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
// Hooks read bus from context — no bus arg needed
|
|
140
|
+
function ChatMessages() {
|
|
141
|
+
useEvent('message:new', (data) => console.log(data.text));
|
|
142
|
+
return null;
|
|
143
|
+
}
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
Throws if hooks are used outside a `<Provider>`. Each `createBusContext()` call produces an isolated context — multiple scopes can coexist.
|
|
147
|
+
|
|
148
|
+
## Using with `@tiny-event-bus/replay`
|
|
149
|
+
|
|
150
|
+
All hooks work seamlessly with `withReplay` decorated buses. Install the replay plugin alongside:
|
|
151
|
+
|
|
152
|
+
```bash
|
|
153
|
+
npm install @tiny-event-bus/replay
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
Wrap your bus with `withReplay` and pass it to hooks as usual:
|
|
157
|
+
|
|
158
|
+
```tsx
|
|
159
|
+
import { createEventBus } from '@tiny-event-bus/core';
|
|
160
|
+
import { withReplay } from '@tiny-event-bus/replay';
|
|
161
|
+
import { createBusContext } from '@tiny-event-bus/react';
|
|
162
|
+
|
|
163
|
+
type ActivityEvents = {
|
|
164
|
+
'activity:log': { action: string; timestamp: number };
|
|
165
|
+
};
|
|
166
|
+
|
|
167
|
+
const activityBus = withReplay(createEventBus<ActivityEvents>(), {
|
|
168
|
+
maxSize: 50,
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
const { Provider, useEvent, useEventBus, useAnyEvent } =
|
|
172
|
+
createBusContext<ActivityEvents>();
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
Late-mounting components can replay buffered events on mount:
|
|
176
|
+
|
|
177
|
+
```tsx
|
|
178
|
+
function ActivityFeed() {
|
|
179
|
+
const { getHistory } = useEventBus(activityBus);
|
|
180
|
+
const [entries, setEntries] = useState<string[]>([]);
|
|
181
|
+
|
|
182
|
+
useEffect(() => {
|
|
183
|
+
// Seed from replay buffer
|
|
184
|
+
setEntries(getHistory().map((e) => e.data.action));
|
|
185
|
+
}, [getHistory]);
|
|
186
|
+
|
|
187
|
+
// Live updates going forward
|
|
188
|
+
useEvent(
|
|
189
|
+
'activity:log',
|
|
190
|
+
(data) => {
|
|
191
|
+
setEntries((prev) => [...prev, data.action]);
|
|
192
|
+
},
|
|
193
|
+
activityBus,
|
|
194
|
+
);
|
|
195
|
+
|
|
196
|
+
return (
|
|
197
|
+
<ul>
|
|
198
|
+
{entries.map((e, i) => (
|
|
199
|
+
<li key={i}>{e}</li>
|
|
200
|
+
))}
|
|
201
|
+
</ul>
|
|
202
|
+
);
|
|
203
|
+
}
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
`useEventBus` automatically discovers `getHistory` and `clearHistory` from the replay bus — no extra configuration needed.
|
|
207
|
+
|
|
208
|
+
## When to Use (vs React State)
|
|
209
|
+
|
|
210
|
+
| Use event bus | Use React state |
|
|
211
|
+
| ------------------------------------------------------ | ----------------------------- |
|
|
212
|
+
| Fire-and-forget signals (toasts, analytics, shortcuts) | UI data that drives rendering |
|
|
213
|
+
| Cross-module notifications | Component-local data |
|
|
214
|
+
| No re-renders needed | Must trigger re-render |
|
|
215
|
+
|
|
216
|
+
**Rule of thumb**: if a component needs to _render_ data, use React state. If something needs to _react to a signal_, use the event bus.
|
|
217
|
+
|
|
218
|
+
## License
|
|
219
|
+
|
|
220
|
+
MIT
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import type { EventMap, EventKey, EventHandler, AnyEventHandler, IEventBus } from '@tiny-event-bus/core';
|
|
3
|
+
export declare function createBusContext<T extends EventMap>(): {
|
|
4
|
+
Provider: ({ bus, children, }: {
|
|
5
|
+
bus: IEventBus<T>;
|
|
6
|
+
children: React.ReactNode;
|
|
7
|
+
}) => React.FunctionComponentElement<React.ProviderProps<IEventBus<T> | null>>;
|
|
8
|
+
useEvent: <K extends EventKey<T>>(event: K, handler: EventHandler<T[K]>) => void;
|
|
9
|
+
useEventBus: () => import("./use-event-bus.js").BusMethods<IEventBus<T>>;
|
|
10
|
+
useAnyEvent: (handler: AnyEventHandler<T>) => void;
|
|
11
|
+
};
|
|
12
|
+
//# sourceMappingURL=create-bus-context.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"create-bus-context.d.ts","sourceRoot":"","sources":["../../src/create-bus-context.ts"],"names":[],"mappings":"AAAA,OAAO,KAAoC,MAAM,OAAO,CAAC;AACzD,OAAO,KAAK,EACV,QAAQ,EACR,QAAQ,EACR,YAAY,EACZ,eAAe,EACf,SAAS,EACV,MAAM,sBAAsB,CAAC;AAK9B,wBAAgB,gBAAgB,CAAC,CAAC,SAAS,QAAQ;mCAc9C;QACD,GAAG,EAAE,SAAS,CAAC,CAAC,CAAC,CAAC;QAClB,QAAQ,EAAE,KAAK,CAAC,SAAS,CAAC;KAC3B;eAIiB,CAAC,SAAS,QAAQ,CAAC,CAAC,CAAC,SAC9B,CAAC,WACC,YAAY,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;;2BASC,eAAe,CAAC,CAAC,CAAC;EAKjD"}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.createBusContext = createBusContext;
|
|
37
|
+
const react_1 = __importStar(require("react"));
|
|
38
|
+
const use_event_js_1 = require("./use-event.js");
|
|
39
|
+
const use_event_bus_js_1 = require("./use-event-bus.js");
|
|
40
|
+
const use_any_event_js_1 = require("./use-any-event.js");
|
|
41
|
+
function createBusContext() {
|
|
42
|
+
const Ctx = (0, react_1.createContext)(null);
|
|
43
|
+
function useBus() {
|
|
44
|
+
const bus = (0, react_1.useContext)(Ctx);
|
|
45
|
+
if (!bus) {
|
|
46
|
+
throw new Error('useBus must be used within a <Provider>');
|
|
47
|
+
}
|
|
48
|
+
return bus;
|
|
49
|
+
}
|
|
50
|
+
function Provider({ bus, children, }) {
|
|
51
|
+
return react_1.default.createElement(Ctx.Provider, { value: bus }, children);
|
|
52
|
+
}
|
|
53
|
+
function useEvent(event, handler) {
|
|
54
|
+
return (0, use_event_js_1.useEvent)(event, handler, useBus());
|
|
55
|
+
}
|
|
56
|
+
function useEventBus() {
|
|
57
|
+
return (0, use_event_bus_js_1.useEventBus)(useBus());
|
|
58
|
+
}
|
|
59
|
+
function useAnyEvent(handler) {
|
|
60
|
+
return (0, use_any_event_js_1.useAnyEvent)(handler, useBus());
|
|
61
|
+
}
|
|
62
|
+
return { Provider, useEvent, useEventBus, useAnyEvent };
|
|
63
|
+
}
|
|
64
|
+
//# sourceMappingURL=create-bus-context.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"create-bus-context.js","sourceRoot":"","sources":["../../src/create-bus-context.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAYA,4CAqCC;AAjDD,+CAAyD;AAQzD,iDAA0D;AAC1D,yDAAoE;AACpE,yDAAoE;AAEpE,SAAgB,gBAAgB;IAC9B,MAAM,GAAG,GAAG,IAAA,qBAAa,EAAsB,IAAI,CAAC,CAAC;IAErD,SAAS,MAAM;QACb,MAAM,GAAG,GAAG,IAAA,kBAAU,EAAC,GAAG,CAAC,CAAC;QAC5B,IAAI,CAAC,GAAG,EAAE,CAAC;YACT,MAAM,IAAI,KAAK,CAAC,yCAAyC,CAAC,CAAC;QAC7D,CAAC;QACD,OAAO,GAAG,CAAC;IACb,CAAC;IAED,SAAS,QAAQ,CAAC,EAChB,GAAG,EACH,QAAQ,GAIT;QACC,OAAO,eAAK,CAAC,aAAa,CAAC,GAAG,CAAC,QAAQ,EAAE,EAAE,KAAK,EAAE,GAAG,EAAE,EAAE,QAAQ,CAAC,CAAC;IACrE,CAAC;IAED,SAAS,QAAQ,CACf,KAAQ,EACR,OAA2B;QAE3B,OAAO,IAAA,uBAAY,EAAC,KAAK,EAAE,OAAO,EAAE,MAAM,EAAE,CAAC,CAAC;IAChD,CAAC;IAED,SAAS,WAAW;QAClB,OAAO,IAAA,8BAAe,EAAC,MAAM,EAAE,CAAC,CAAC;IACnC,CAAC;IAED,SAAS,WAAW,CAAC,OAA2B;QAC9C,OAAO,IAAA,8BAAe,EAAC,OAAO,EAAE,MAAM,EAAE,CAAC,CAAC;IAC5C,CAAC;IAED,OAAO,EAAE,QAAQ,EAAE,QAAQ,EAAE,WAAW,EAAE,WAAW,EAAE,CAAC;AAC1D,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAC1C,OAAO,EAAE,WAAW,EAAE,KAAK,UAAU,EAAE,MAAM,oBAAoB,CAAC;AAClE,OAAO,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAC;AACjD,OAAO,EAAE,gBAAgB,EAAE,MAAM,yBAAyB,CAAC"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createBusContext = exports.useAnyEvent = exports.useEventBus = exports.useEvent = void 0;
|
|
4
|
+
var use_event_js_1 = require("./use-event.js");
|
|
5
|
+
Object.defineProperty(exports, "useEvent", { enumerable: true, get: function () { return use_event_js_1.useEvent; } });
|
|
6
|
+
var use_event_bus_js_1 = require("./use-event-bus.js");
|
|
7
|
+
Object.defineProperty(exports, "useEventBus", { enumerable: true, get: function () { return use_event_bus_js_1.useEventBus; } });
|
|
8
|
+
var use_any_event_js_1 = require("./use-any-event.js");
|
|
9
|
+
Object.defineProperty(exports, "useAnyEvent", { enumerable: true, get: function () { return use_any_event_js_1.useAnyEvent; } });
|
|
10
|
+
var create_bus_context_js_1 = require("./create-bus-context.js");
|
|
11
|
+
Object.defineProperty(exports, "createBusContext", { enumerable: true, get: function () { return create_bus_context_js_1.createBusContext; } });
|
|
12
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;AAAA,+CAA0C;AAAjC,wGAAA,QAAQ,OAAA;AACjB,uDAAkE;AAAzD,+GAAA,WAAW,OAAA;AACpB,uDAAiD;AAAxC,+GAAA,WAAW,OAAA;AACpB,iEAA2D;AAAlD,yHAAA,gBAAgB,OAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"use-any-event.d.ts","sourceRoot":"","sources":["../../src/use-any-event.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,QAAQ,EAER,eAAe,EACf,SAAS,EACV,MAAM,sBAAsB,CAAC;AAE9B,wBAAgB,WAAW,CAAC,CAAC,SAAS,QAAQ,EAC5C,OAAO,EAAE,eAAe,CAAC,CAAC,CAAC,EAC3B,GAAG,EAAE,SAAS,CAAC,CAAC,CAAC,GAChB,IAAI,CAWN"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useAnyEvent = useAnyEvent;
|
|
4
|
+
const react_1 = require("react");
|
|
5
|
+
function useAnyEvent(handler, bus) {
|
|
6
|
+
const handlerRef = (0, react_1.useRef)(handler);
|
|
7
|
+
// Assign during render to avoid stale closure between render and effect.
|
|
8
|
+
// eslint-disable-next-line react-hooks/refs
|
|
9
|
+
handlerRef.current = handler;
|
|
10
|
+
(0, react_1.useEffect)(() => {
|
|
11
|
+
return bus.onAny(((event, data) => {
|
|
12
|
+
handlerRef.current(event, data);
|
|
13
|
+
}));
|
|
14
|
+
}, [bus]);
|
|
15
|
+
}
|
|
16
|
+
//# sourceMappingURL=use-any-event.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"use-any-event.js","sourceRoot":"","sources":["../../src/use-any-event.ts"],"names":[],"mappings":";;AAQA,kCAcC;AAtBD,iCAA0C;AAQ1C,SAAgB,WAAW,CACzB,OAA2B,EAC3B,GAAiB;IAEjB,MAAM,UAAU,GAAG,IAAA,cAAM,EAAqB,OAAO,CAAC,CAAC;IACvD,yEAAyE;IACzE,4CAA4C;IAC5C,UAAU,CAAC,OAAO,GAAG,OAAO,CAAC;IAE7B,IAAA,iBAAS,EAAC,GAAG,EAAE;QACb,OAAO,GAAG,CAAC,KAAK,CAAC,CAAC,CAAC,KAAkB,EAAE,IAAoB,EAAE,EAAE;YAC7D,UAAU,CAAC,OAAO,CAAC,KAAK,EAAE,IAAI,CAAC,CAAC;QAClC,CAAC,CAAuB,CAAC,CAAC;IAC5B,CAAC,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC;AACZ,CAAC"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { IEventBus } from '@tiny-event-bus/core';
|
|
2
|
+
type AnyFunction = (...args: any[]) => any;
|
|
3
|
+
export type BusMethods<B> = {
|
|
4
|
+
[K in keyof B as B[K] extends AnyFunction ? K : never]: B[K];
|
|
5
|
+
};
|
|
6
|
+
export declare function useEventBus<B extends IEventBus<any>>(bus: B): BusMethods<B>;
|
|
7
|
+
export {};
|
|
8
|
+
//# sourceMappingURL=use-event-bus.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"use-event-bus.d.ts","sourceRoot":"","sources":["../../src/use-event-bus.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,sBAAsB,CAAC;AAGtD,KAAK,WAAW,GAAG,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,GAAG,CAAC;AAE3C,MAAM,MAAM,UAAU,CAAC,CAAC,IAAI;KACzB,CAAC,IAAI,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,SAAS,WAAW,GAAG,CAAC,GAAG,KAAK,GAAG,CAAC,CAAC,CAAC,CAAC;CAC7D,CAAC;AAMF,wBAAgB,WAAW,CAAC,CAAC,SAAS,SAAS,CAAC,GAAG,CAAC,EAAE,GAAG,EAAE,CAAC,GAAG,UAAU,CAAC,CAAC,CAAC,CAe3E"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useEventBus = useEventBus;
|
|
4
|
+
const react_1 = require("react");
|
|
5
|
+
// IEventBus<any> — `any` is intentional: IEventBus<T> is invariant in T, so no
|
|
6
|
+
// concrete IEventBus<MyEvents> is assignable to IEventBus<EventMap>. Using `any`
|
|
7
|
+
// sidesteps invariance while BusMethods<B> still preserves full static typing.
|
|
8
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
9
|
+
function useEventBus(bus) {
|
|
10
|
+
return (0, react_1.useMemo)(() => {
|
|
11
|
+
const source = bus;
|
|
12
|
+
const methods = {};
|
|
13
|
+
for (const key of Object.keys(source)) {
|
|
14
|
+
const fn = source[key];
|
|
15
|
+
if (typeof fn === 'function') {
|
|
16
|
+
methods[key] = (...args) => fn.apply(bus, args);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
return methods;
|
|
20
|
+
}, [bus]);
|
|
21
|
+
}
|
|
22
|
+
//# sourceMappingURL=use-event-bus.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"use-event-bus.js","sourceRoot":"","sources":["../../src/use-event-bus.ts"],"names":[],"mappings":";;AAcA,kCAeC;AA7BD,iCAAgC;AAUhC,+EAA+E;AAC/E,iFAAiF;AACjF,+EAA+E;AAC/E,8DAA8D;AAC9D,SAAgB,WAAW,CAA2B,GAAM;IAC1D,OAAO,IAAA,eAAO,EAAC,GAAG,EAAE;QAClB,MAAM,MAAM,GAAG,GAA8B,CAAC;QAC9C,MAAM,OAAO,GAA4B,EAAE,CAAC;QAE5C,KAAK,MAAM,GAAG,IAAI,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,EAAE,CAAC;YACtC,MAAM,EAAE,GAAG,MAAM,CAAC,GAAG,CAAC,CAAC;YACvB,IAAI,OAAO,EAAE,KAAK,UAAU,EAAE,CAAC;gBAC7B,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,IAAe,EAAE,EAAE,CACnC,EAAkB,CAAC,KAAK,CAAC,GAAG,EAAE,IAAI,CAAC,CAAC;YACzC,CAAC;QACH,CAAC;QAED,OAAO,OAAwB,CAAC;IAClC,CAAC,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC;AACZ,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"use-event.d.ts","sourceRoot":"","sources":["../../src/use-event.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,QAAQ,EACR,QAAQ,EACR,YAAY,EACZ,SAAS,EACV,MAAM,sBAAsB,CAAC;AAE9B,wBAAgB,QAAQ,CAAC,CAAC,SAAS,QAAQ,EAAE,CAAC,SAAS,QAAQ,CAAC,CAAC,CAAC,EAChE,KAAK,EAAE,CAAC,EACR,OAAO,EAAE,YAAY,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,EAC3B,GAAG,EAAE,SAAS,CAAC,CAAC,CAAC,GAChB,IAAI,CAWN"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.useEvent = useEvent;
|
|
4
|
+
const react_1 = require("react");
|
|
5
|
+
function useEvent(event, handler, bus) {
|
|
6
|
+
const handlerRef = (0, react_1.useRef)(handler);
|
|
7
|
+
// Assign during render to avoid stale closure between render and effect.
|
|
8
|
+
// eslint-disable-next-line react-hooks/refs
|
|
9
|
+
handlerRef.current = handler;
|
|
10
|
+
(0, react_1.useEffect)(() => {
|
|
11
|
+
return bus.on(event, ((data) => {
|
|
12
|
+
handlerRef.current(data);
|
|
13
|
+
}));
|
|
14
|
+
}, [bus, event]);
|
|
15
|
+
}
|
|
16
|
+
//# sourceMappingURL=use-event.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"use-event.js","sourceRoot":"","sources":["../../src/use-event.ts"],"names":[],"mappings":";;AAQA,4BAeC;AAvBD,iCAA0C;AAQ1C,SAAgB,QAAQ,CACtB,KAAQ,EACR,OAA2B,EAC3B,GAAiB;IAEjB,MAAM,UAAU,GAAG,IAAA,cAAM,EAAqB,OAAO,CAAC,CAAC;IACvD,yEAAyE;IACzE,4CAA4C;IAC5C,UAAU,CAAC,OAAO,GAAG,OAAO,CAAC;IAE7B,IAAA,iBAAS,EAAC,GAAG,EAAE;QACb,OAAO,GAAG,CAAC,EAAE,CAAC,KAAK,EAAE,CAAC,CAAC,IAAU,EAAE,EAAE;YACnC,UAAU,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;QAC3B,CAAC,CAAuB,CAAC,CAAC;IAC5B,CAAC,EAAE,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC,CAAC;AACnB,CAAC"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import type { EventMap, EventKey, EventHandler, AnyEventHandler, IEventBus } from '@tiny-event-bus/core';
|
|
3
|
+
export declare function createBusContext<T extends EventMap>(): {
|
|
4
|
+
Provider: ({ bus, children, }: {
|
|
5
|
+
bus: IEventBus<T>;
|
|
6
|
+
children: React.ReactNode;
|
|
7
|
+
}) => React.FunctionComponentElement<React.ProviderProps<IEventBus<T> | null>>;
|
|
8
|
+
useEvent: <K extends EventKey<T>>(event: K, handler: EventHandler<T[K]>) => void;
|
|
9
|
+
useEventBus: () => import("./use-event-bus.js").BusMethods<IEventBus<T>>;
|
|
10
|
+
useAnyEvent: (handler: AnyEventHandler<T>) => void;
|
|
11
|
+
};
|
|
12
|
+
//# sourceMappingURL=create-bus-context.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"create-bus-context.d.ts","sourceRoot":"","sources":["../../src/create-bus-context.ts"],"names":[],"mappings":"AAAA,OAAO,KAAoC,MAAM,OAAO,CAAC;AACzD,OAAO,KAAK,EACV,QAAQ,EACR,QAAQ,EACR,YAAY,EACZ,eAAe,EACf,SAAS,EACV,MAAM,sBAAsB,CAAC;AAK9B,wBAAgB,gBAAgB,CAAC,CAAC,SAAS,QAAQ;mCAc9C;QACD,GAAG,EAAE,SAAS,CAAC,CAAC,CAAC,CAAC;QAClB,QAAQ,EAAE,KAAK,CAAC,SAAS,CAAC;KAC3B;eAIiB,CAAC,SAAS,QAAQ,CAAC,CAAC,CAAC,SAC9B,CAAC,WACC,YAAY,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;;2BASC,eAAe,CAAC,CAAC,CAAC;EAKjD"}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import React, { createContext, useContext } from 'react';
|
|
2
|
+
import { useEvent as useEventBase } from './use-event.js';
|
|
3
|
+
import { useEventBus as useEventBusBase } from './use-event-bus.js';
|
|
4
|
+
import { useAnyEvent as useAnyEventBase } from './use-any-event.js';
|
|
5
|
+
export function createBusContext() {
|
|
6
|
+
const Ctx = createContext(null);
|
|
7
|
+
function useBus() {
|
|
8
|
+
const bus = useContext(Ctx);
|
|
9
|
+
if (!bus) {
|
|
10
|
+
throw new Error('useBus must be used within a <Provider>');
|
|
11
|
+
}
|
|
12
|
+
return bus;
|
|
13
|
+
}
|
|
14
|
+
function Provider({ bus, children, }) {
|
|
15
|
+
return React.createElement(Ctx.Provider, { value: bus }, children);
|
|
16
|
+
}
|
|
17
|
+
function useEvent(event, handler) {
|
|
18
|
+
return useEventBase(event, handler, useBus());
|
|
19
|
+
}
|
|
20
|
+
function useEventBus() {
|
|
21
|
+
return useEventBusBase(useBus());
|
|
22
|
+
}
|
|
23
|
+
function useAnyEvent(handler) {
|
|
24
|
+
return useAnyEventBase(handler, useBus());
|
|
25
|
+
}
|
|
26
|
+
return { Provider, useEvent, useEventBus, useAnyEvent };
|
|
27
|
+
}
|
|
28
|
+
//# sourceMappingURL=create-bus-context.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"create-bus-context.js","sourceRoot":"","sources":["../../src/create-bus-context.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,EAAE,aAAa,EAAE,UAAU,EAAE,MAAM,OAAO,CAAC;AAQzD,OAAO,EAAE,QAAQ,IAAI,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAC1D,OAAO,EAAE,WAAW,IAAI,eAAe,EAAE,MAAM,oBAAoB,CAAC;AACpE,OAAO,EAAE,WAAW,IAAI,eAAe,EAAE,MAAM,oBAAoB,CAAC;AAEpE,MAAM,UAAU,gBAAgB;IAC9B,MAAM,GAAG,GAAG,aAAa,CAAsB,IAAI,CAAC,CAAC;IAErD,SAAS,MAAM;QACb,MAAM,GAAG,GAAG,UAAU,CAAC,GAAG,CAAC,CAAC;QAC5B,IAAI,CAAC,GAAG,EAAE,CAAC;YACT,MAAM,IAAI,KAAK,CAAC,yCAAyC,CAAC,CAAC;QAC7D,CAAC;QACD,OAAO,GAAG,CAAC;IACb,CAAC;IAED,SAAS,QAAQ,CAAC,EAChB,GAAG,EACH,QAAQ,GAIT;QACC,OAAO,KAAK,CAAC,aAAa,CAAC,GAAG,CAAC,QAAQ,EAAE,EAAE,KAAK,EAAE,GAAG,EAAE,EAAE,QAAQ,CAAC,CAAC;IACrE,CAAC;IAED,SAAS,QAAQ,CACf,KAAQ,EACR,OAA2B;QAE3B,OAAO,YAAY,CAAC,KAAK,EAAE,OAAO,EAAE,MAAM,EAAE,CAAC,CAAC;IAChD,CAAC;IAED,SAAS,WAAW;QAClB,OAAO,eAAe,CAAC,MAAM,EAAE,CAAC,CAAC;IACnC,CAAC;IAED,SAAS,WAAW,CAAC,OAA2B;QAC9C,OAAO,eAAe,CAAC,OAAO,EAAE,MAAM,EAAE,CAAC,CAAC;IAC5C,CAAC;IAED,OAAO,EAAE,QAAQ,EAAE,QAAQ,EAAE,WAAW,EAAE,WAAW,EAAE,CAAC;AAC1D,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAC1C,OAAO,EAAE,WAAW,EAAE,KAAK,UAAU,EAAE,MAAM,oBAAoB,CAAC;AAClE,OAAO,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAC;AACjD,OAAO,EAAE,gBAAgB,EAAE,MAAM,yBAAyB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAC1C,OAAO,EAAE,WAAW,EAAmB,MAAM,oBAAoB,CAAC;AAClE,OAAO,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAC;AACjD,OAAO,EAAE,gBAAgB,EAAE,MAAM,yBAAyB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"use-any-event.d.ts","sourceRoot":"","sources":["../../src/use-any-event.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,QAAQ,EAER,eAAe,EACf,SAAS,EACV,MAAM,sBAAsB,CAAC;AAE9B,wBAAgB,WAAW,CAAC,CAAC,SAAS,QAAQ,EAC5C,OAAO,EAAE,eAAe,CAAC,CAAC,CAAC,EAC3B,GAAG,EAAE,SAAS,CAAC,CAAC,CAAC,GAChB,IAAI,CAWN"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { useEffect, useRef } from 'react';
|
|
2
|
+
export function useAnyEvent(handler, bus) {
|
|
3
|
+
const handlerRef = useRef(handler);
|
|
4
|
+
// Assign during render to avoid stale closure between render and effect.
|
|
5
|
+
// eslint-disable-next-line react-hooks/refs
|
|
6
|
+
handlerRef.current = handler;
|
|
7
|
+
useEffect(() => {
|
|
8
|
+
return bus.onAny(((event, data) => {
|
|
9
|
+
handlerRef.current(event, data);
|
|
10
|
+
}));
|
|
11
|
+
}, [bus]);
|
|
12
|
+
}
|
|
13
|
+
//# sourceMappingURL=use-any-event.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"use-any-event.js","sourceRoot":"","sources":["../../src/use-any-event.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,EAAE,MAAM,OAAO,CAAC;AAQ1C,MAAM,UAAU,WAAW,CACzB,OAA2B,EAC3B,GAAiB;IAEjB,MAAM,UAAU,GAAG,MAAM,CAAqB,OAAO,CAAC,CAAC;IACvD,yEAAyE;IACzE,4CAA4C;IAC5C,UAAU,CAAC,OAAO,GAAG,OAAO,CAAC;IAE7B,SAAS,CAAC,GAAG,EAAE;QACb,OAAO,GAAG,CAAC,KAAK,CAAC,CAAC,CAAC,KAAkB,EAAE,IAAoB,EAAE,EAAE;YAC7D,UAAU,CAAC,OAAO,CAAC,KAAK,EAAE,IAAI,CAAC,CAAC;QAClC,CAAC,CAAuB,CAAC,CAAC;IAC5B,CAAC,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC;AACZ,CAAC"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { IEventBus } from '@tiny-event-bus/core';
|
|
2
|
+
type AnyFunction = (...args: any[]) => any;
|
|
3
|
+
export type BusMethods<B> = {
|
|
4
|
+
[K in keyof B as B[K] extends AnyFunction ? K : never]: B[K];
|
|
5
|
+
};
|
|
6
|
+
export declare function useEventBus<B extends IEventBus<any>>(bus: B): BusMethods<B>;
|
|
7
|
+
export {};
|
|
8
|
+
//# sourceMappingURL=use-event-bus.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"use-event-bus.d.ts","sourceRoot":"","sources":["../../src/use-event-bus.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,sBAAsB,CAAC;AAGtD,KAAK,WAAW,GAAG,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,GAAG,CAAC;AAE3C,MAAM,MAAM,UAAU,CAAC,CAAC,IAAI;KACzB,CAAC,IAAI,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,SAAS,WAAW,GAAG,CAAC,GAAG,KAAK,GAAG,CAAC,CAAC,CAAC,CAAC;CAC7D,CAAC;AAMF,wBAAgB,WAAW,CAAC,CAAC,SAAS,SAAS,CAAC,GAAG,CAAC,EAAE,GAAG,EAAE,CAAC,GAAG,UAAU,CAAC,CAAC,CAAC,CAe3E"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { useMemo } from 'react';
|
|
2
|
+
// IEventBus<any> — `any` is intentional: IEventBus<T> is invariant in T, so no
|
|
3
|
+
// concrete IEventBus<MyEvents> is assignable to IEventBus<EventMap>. Using `any`
|
|
4
|
+
// sidesteps invariance while BusMethods<B> still preserves full static typing.
|
|
5
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
6
|
+
export function useEventBus(bus) {
|
|
7
|
+
return useMemo(() => {
|
|
8
|
+
const source = bus;
|
|
9
|
+
const methods = {};
|
|
10
|
+
for (const key of Object.keys(source)) {
|
|
11
|
+
const fn = source[key];
|
|
12
|
+
if (typeof fn === 'function') {
|
|
13
|
+
methods[key] = (...args) => fn.apply(bus, args);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
return methods;
|
|
17
|
+
}, [bus]);
|
|
18
|
+
}
|
|
19
|
+
//# sourceMappingURL=use-event-bus.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"use-event-bus.js","sourceRoot":"","sources":["../../src/use-event-bus.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,OAAO,CAAC;AAUhC,+EAA+E;AAC/E,iFAAiF;AACjF,+EAA+E;AAC/E,8DAA8D;AAC9D,MAAM,UAAU,WAAW,CAA2B,GAAM;IAC1D,OAAO,OAAO,CAAC,GAAG,EAAE;QAClB,MAAM,MAAM,GAAG,GAA8B,CAAC;QAC9C,MAAM,OAAO,GAA4B,EAAE,CAAC;QAE5C,KAAK,MAAM,GAAG,IAAI,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,EAAE,CAAC;YACtC,MAAM,EAAE,GAAG,MAAM,CAAC,GAAG,CAAC,CAAC;YACvB,IAAI,OAAO,EAAE,KAAK,UAAU,EAAE,CAAC;gBAC7B,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,IAAe,EAAE,EAAE,CACnC,EAAkB,CAAC,KAAK,CAAC,GAAG,EAAE,IAAI,CAAC,CAAC;YACzC,CAAC;QACH,CAAC;QAED,OAAO,OAAwB,CAAC;IAClC,CAAC,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC;AACZ,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"use-event.d.ts","sourceRoot":"","sources":["../../src/use-event.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,QAAQ,EACR,QAAQ,EACR,YAAY,EACZ,SAAS,EACV,MAAM,sBAAsB,CAAC;AAE9B,wBAAgB,QAAQ,CAAC,CAAC,SAAS,QAAQ,EAAE,CAAC,SAAS,QAAQ,CAAC,CAAC,CAAC,EAChE,KAAK,EAAE,CAAC,EACR,OAAO,EAAE,YAAY,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,EAC3B,GAAG,EAAE,SAAS,CAAC,CAAC,CAAC,GAChB,IAAI,CAWN"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { useEffect, useRef } from 'react';
|
|
2
|
+
export function useEvent(event, handler, bus) {
|
|
3
|
+
const handlerRef = useRef(handler);
|
|
4
|
+
// Assign during render to avoid stale closure between render and effect.
|
|
5
|
+
// eslint-disable-next-line react-hooks/refs
|
|
6
|
+
handlerRef.current = handler;
|
|
7
|
+
useEffect(() => {
|
|
8
|
+
return bus.on(event, ((data) => {
|
|
9
|
+
handlerRef.current(data);
|
|
10
|
+
}));
|
|
11
|
+
}, [bus, event]);
|
|
12
|
+
}
|
|
13
|
+
//# sourceMappingURL=use-event.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"use-event.js","sourceRoot":"","sources":["../../src/use-event.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,EAAE,MAAM,OAAO,CAAC;AAQ1C,MAAM,UAAU,QAAQ,CACtB,KAAQ,EACR,OAA2B,EAC3B,GAAiB;IAEjB,MAAM,UAAU,GAAG,MAAM,CAAqB,OAAO,CAAC,CAAC;IACvD,yEAAyE;IACzE,4CAA4C;IAC5C,UAAU,CAAC,OAAO,GAAG,OAAO,CAAC;IAE7B,SAAS,CAAC,GAAG,EAAE;QACb,OAAO,GAAG,CAAC,EAAE,CAAC,KAAK,EAAE,CAAC,CAAC,IAAU,EAAE,EAAE;YACnC,UAAU,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;QAC3B,CAAC,CAAuB,CAAC,CAAC;IAC5B,CAAC,EAAE,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC,CAAC;AACnB,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@tiny-event-bus/react",
|
|
3
|
+
"version": "0.10.0",
|
|
4
|
+
"description": "React hooks plugin for tiny-event-bus",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "https://github.com/vineet2907/tiny-event-bus.git",
|
|
9
|
+
"directory": "packages/react"
|
|
10
|
+
},
|
|
11
|
+
"homepage": "https://github.com/vineet2907/tiny-event-bus/tree/main/packages/react#readme",
|
|
12
|
+
"bugs": {
|
|
13
|
+
"url": "https://github.com/vineet2907/tiny-event-bus/issues"
|
|
14
|
+
},
|
|
15
|
+
"publishConfig": {
|
|
16
|
+
"access": "public"
|
|
17
|
+
},
|
|
18
|
+
"type": "module",
|
|
19
|
+
"main": "./dist/cjs/index.js",
|
|
20
|
+
"module": "./dist/esm/index.js",
|
|
21
|
+
"types": "./dist/esm/index.d.ts",
|
|
22
|
+
"exports": {
|
|
23
|
+
".": {
|
|
24
|
+
"import": {
|
|
25
|
+
"types": "./dist/esm/index.d.ts",
|
|
26
|
+
"default": "./dist/esm/index.js"
|
|
27
|
+
},
|
|
28
|
+
"require": {
|
|
29
|
+
"types": "./dist/cjs/index.d.ts",
|
|
30
|
+
"default": "./dist/cjs/index.js"
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
},
|
|
34
|
+
"files": [
|
|
35
|
+
"dist"
|
|
36
|
+
],
|
|
37
|
+
"engines": {
|
|
38
|
+
"node": ">=20"
|
|
39
|
+
},
|
|
40
|
+
"peerDependencies": {
|
|
41
|
+
"react": ">=17",
|
|
42
|
+
"@tiny-event-bus/core": "^0.10.0"
|
|
43
|
+
},
|
|
44
|
+
"devDependencies": {
|
|
45
|
+
"@testing-library/react": "^16.3.0",
|
|
46
|
+
"@types/react": "^19.0.0",
|
|
47
|
+
"jsdom": "^29.0.0",
|
|
48
|
+
"react": "^19.0.0",
|
|
49
|
+
"react-dom": "^19.0.0",
|
|
50
|
+
"@tiny-event-bus/core": "0.10.0"
|
|
51
|
+
},
|
|
52
|
+
"keywords": [
|
|
53
|
+
"event-bus",
|
|
54
|
+
"react",
|
|
55
|
+
"hooks",
|
|
56
|
+
"typescript",
|
|
57
|
+
"plugin"
|
|
58
|
+
],
|
|
59
|
+
"scripts": {
|
|
60
|
+
"build:esm": "tsc -p tsconfig.json",
|
|
61
|
+
"build:cjs": "tsc -p tsconfig.cjs.json",
|
|
62
|
+
"build": "rm -rf dist && pnpm run build:esm && pnpm run build:cjs",
|
|
63
|
+
"test": "vitest run",
|
|
64
|
+
"test:coverage": "vitest run --coverage",
|
|
65
|
+
"test:watch": "vitest --watch",
|
|
66
|
+
"typecheck": "tsc --noEmit"
|
|
67
|
+
}
|
|
68
|
+
}
|