@siggn/react 0.1.5 → 0.2.1
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/README.md +38 -4
- package/dist/hooks.d.ts +30 -13
- package/dist/hooks.d.ts.map +1 -1
- package/dist/index.cjs.js +1 -1
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.es.js +26 -22
- package/dist/index.es.js.map +1 -1
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -166,6 +166,24 @@ function LocalComponent() {
|
|
|
166
166
|
}
|
|
167
167
|
```
|
|
168
168
|
|
|
169
|
+
### Using Middleware
|
|
170
|
+
|
|
171
|
+
You can use the `useMiddleware` hook to register middleware for a `Siggn` instance. The middleware intercepts messages before they are delivered to subscribers and is automatically unregistered when the component unmounts.
|
|
172
|
+
|
|
173
|
+
```tsx
|
|
174
|
+
import { useMiddleware } from '@siggn/react';
|
|
175
|
+
import { siggn } from '../siggn';
|
|
176
|
+
|
|
177
|
+
function LoggerComponent() {
|
|
178
|
+
useMiddleware(siggn, (msg, next) => {
|
|
179
|
+
console.log('Middleware:', msg);
|
|
180
|
+
next();
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
return null;
|
|
184
|
+
}
|
|
185
|
+
```
|
|
186
|
+
|
|
169
187
|
## API
|
|
170
188
|
|
|
171
189
|
### `useSiggn<T>()`
|
|
@@ -176,12 +194,21 @@ Creates and returns a `Siggn` instance that persists for the lifetime of the com
|
|
|
176
194
|
|
|
177
195
|
Returns a `Siggn<T>` instance.
|
|
178
196
|
|
|
179
|
-
### `useSubscribe(options,
|
|
197
|
+
### `useSubscribe(options, type, callback, deps)`
|
|
198
|
+
|
|
199
|
+
Subscribes to a single message type and automatically unsubscribes when the component unmounts.
|
|
200
|
+
|
|
201
|
+
- `options`: A `Siggn` instance or an object `{ instance: Siggn<T>; id?: string; }`.
|
|
202
|
+
- `type`: The message type to subscribe to.
|
|
203
|
+
- `callback`: The function to call when the message is received.
|
|
204
|
+
- `deps` (optional): A dependency array to control when the subscription is re-created.
|
|
205
|
+
|
|
206
|
+
### `useSubscribeMany(options, setup, deps)`
|
|
180
207
|
|
|
181
|
-
Subscribes to
|
|
208
|
+
Subscribes to multiple message types and automatically unsubscribes when the component unmounts.
|
|
182
209
|
|
|
183
210
|
- `options`: A `Siggn` instance or an object `{ instance: Siggn<T>; id?: string; }`.
|
|
184
|
-
- `setup`: A function that receives a `subscribe` helper to define subscriptions
|
|
211
|
+
- `setup`: A function that receives a `subscribe` helper to define subscriptions.
|
|
185
212
|
- `deps` (optional): A dependency array to control when the subscriptions are re-created.
|
|
186
213
|
|
|
187
214
|
### `useSubscribeAll(options, callback, deps)`
|
|
@@ -189,9 +216,16 @@ Subscribes to messages and automatically unsubscribes when the component unmount
|
|
|
189
216
|
Subscribes to all messages and automatically unsubscribes when the component unmounts.
|
|
190
217
|
|
|
191
218
|
- `options`: A `Siggn` instance or an object `{ instance: Siggn<T>; id?: string; }`.
|
|
192
|
-
- `callback`: A function that will be called with every message.
|
|
193
219
|
- `deps` (optional): A dependency array to control when the subscriptions are re-created.
|
|
194
220
|
|
|
221
|
+
### `useMiddleware(instance, middleware, deps)`
|
|
222
|
+
|
|
223
|
+
Registers a middleware and automatically unregisters it when the component unmounts.
|
|
224
|
+
|
|
225
|
+
- `instance`: A `Siggn` instance.
|
|
226
|
+
- `middleware`: The middleware function.
|
|
227
|
+
- `deps` (optional): A dependency array to control when the middleware is re-registered.
|
|
228
|
+
|
|
195
229
|
## License
|
|
196
230
|
|
|
197
231
|
This project is licensed under the [MIT License](LICENSE).
|
package/dist/hooks.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { Msg, Siggn } from '@siggn/core';
|
|
1
|
+
import { Middleware, Msg, Siggn } from '@siggn/core';
|
|
2
|
+
import { SubscriptionOptions } from './types';
|
|
2
3
|
import { DependencyList } from 'react';
|
|
3
4
|
/**
|
|
4
5
|
* Creates and returns a `Siggn` instance that persists for the lifetime of the component.
|
|
@@ -41,10 +42,7 @@ export declare function useSiggn<T extends Msg>(): Siggn<T>;
|
|
|
41
42
|
* }
|
|
42
43
|
* ```
|
|
43
44
|
*/
|
|
44
|
-
export declare function useSubscribeMany<M extends Msg>(options:
|
|
45
|
-
instance: Siggn<M>;
|
|
46
|
-
id?: string;
|
|
47
|
-
}, setup: (subscribe: <T extends M['type']>(type: T, callback: (msg: Extract<M, {
|
|
45
|
+
export declare function useSubscribeMany<M extends Msg>(options: SubscriptionOptions<M>, setup: (subscribe: <T extends M['type']>(type: T, callback: (msg: Extract<M, {
|
|
48
46
|
type: T;
|
|
49
47
|
}>) => void) => void) => void, deps?: DependencyList): void;
|
|
50
48
|
/**
|
|
@@ -67,10 +65,7 @@ export declare function useSubscribeMany<M extends Msg>(options: Siggn<M> | {
|
|
|
67
65
|
* }
|
|
68
66
|
* ```
|
|
69
67
|
*/
|
|
70
|
-
export declare function useSubscribe<M extends Msg, T extends
|
|
71
|
-
instance: Siggn<M>;
|
|
72
|
-
id?: string;
|
|
73
|
-
}, type: T, callback: (msg: Extract<M, {
|
|
68
|
+
export declare function useSubscribe<M extends Msg, T extends M['type']>(options: SubscriptionOptions<M>, type: T, callback: (msg: Extract<M, {
|
|
74
69
|
type: T;
|
|
75
70
|
}>) => void, deps?: DependencyList): void;
|
|
76
71
|
/**
|
|
@@ -96,8 +91,30 @@ export declare function useSubscribe<M extends Msg, T extends string>(options: S
|
|
|
96
91
|
* }
|
|
97
92
|
* ```
|
|
98
93
|
*/
|
|
99
|
-
export declare function useSubscribeAll<
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
94
|
+
export declare function useSubscribeAll<M extends Msg>(options: SubscriptionOptions<M>, callback: (msg: M) => void, deps?: DependencyList): void;
|
|
95
|
+
/**
|
|
96
|
+
* Adds a middleware to a `Siggn` instance that persists for the lifetime of the component.
|
|
97
|
+
* Automatically removes the middleware when the component unmounts.
|
|
98
|
+
*
|
|
99
|
+
* @template M A union of all possible message types.
|
|
100
|
+
* @param options A `Siggn` instance or an object with the instance.
|
|
101
|
+
* @param middleware The middleware function to add.
|
|
102
|
+
* @param deps An optional dependency array to control when the middleware is re-added.
|
|
103
|
+
* @category Middleware
|
|
104
|
+
* @since 0.0.6
|
|
105
|
+
* @example
|
|
106
|
+
*
|
|
107
|
+
* ```tsx
|
|
108
|
+
* import { siggn } from './siggn';
|
|
109
|
+
*
|
|
110
|
+
* function LoggerComponent() {
|
|
111
|
+
* useMiddleware(siggn, async (msg, next) => {
|
|
112
|
+
* console.log('Middleware:', msg);
|
|
113
|
+
* next();
|
|
114
|
+
* }, []);
|
|
115
|
+
* // ...
|
|
116
|
+
* }
|
|
117
|
+
* ```
|
|
118
|
+
*/
|
|
119
|
+
export declare function useMiddleware<M extends Msg>(instance: Siggn<M>, middleware: Middleware<M>, deps?: React.DependencyList): void;
|
|
103
120
|
//# sourceMappingURL=hooks.d.ts.map
|
package/dist/hooks.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"hooks.d.ts","sourceRoot":"","sources":["../src/hooks.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,GAAG,EAAE,KAAK,EAAE,MAAM,aAAa,CAAC;
|
|
1
|
+
{"version":3,"file":"hooks.d.ts","sourceRoot":"","sources":["../src/hooks.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,UAAU,EAAE,KAAK,GAAG,EAAE,KAAK,EAAE,MAAM,aAAa,CAAC;AAC/D,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,SAAS,CAAC;AAEnD,OAAO,EAAgC,KAAK,cAAc,EAAE,MAAM,OAAO,CAAC;AAE1E;;;;;;;;;;;;;;;;GAgBG;AACH,wBAAgB,QAAQ,CAAC,CAAC,SAAS,GAAG,KAAK,KAAK,CAAC,CAAC,CAAC,CAGlD;AAED;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,wBAAgB,gBAAgB,CAAC,CAAC,SAAS,GAA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ol.toStringTag,{value:"Module"});const i=require("@siggn/core"),n=require("react");function b(){const[e]=n.useState(new i.Siggn);return e}function a(e,s,r=[]){const u=n.useMemo(()=>e instanceof i.Siggn?e:e.instance,[e]),c=n.useMemo(()=>u.makeId("id"in e?e.id:void 0),[u]);n.useEffect(()=>(u.subscribeMany(c,s),()=>{u.unsubscribe(c)}),[u,c,...r])}function d(e,s,r,u=[]){const c=n.useMemo(()=>e instanceof i.Siggn?e:e.instance,[e]),t=n.useMemo(()=>c.makeId("id"in e?e.id:void 0),[c]);n.useEffect(()=>(c.subscribe(t,s,r),()=>{c.unsubscribe(t)}),[c,t,...u])}function f(e,s,r=[]){const u=n.useMemo(()=>e instanceof i.Siggn?e:e.instance,[e]),c=n.useMemo(()=>u.makeId("id"in e?e.id:void 0),[u]);n.useEffect(()=>(u.subscribeAll(c,s),()=>{u.unsubscribeGlobal(c)}),[u,c,...r])}function l(e,s,r=[]){n.useEffect(()=>e.use(s),[e,s,...r])}exports.useMiddleware=l;exports.useSiggn=b;exports.useSubscribe=d;exports.useSubscribeAll=f;exports.useSubscribeMany=a;Object.keys(i).forEach(e=>{e!=="default"&&!Object.prototype.hasOwnProperty.call(exports,e)&&Object.defineProperty(exports,e,{enumerable:!0,get:()=>i[e]})});
|
|
2
2
|
//# sourceMappingURL=index.cjs.js.map
|
package/dist/index.cjs.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs.js","sources":["../src/hooks.ts"],"sourcesContent":["import { type Msg, Siggn } from '@siggn/core';\n\nimport { useEffect, useMemo, useState, type DependencyList } from 'react';\n\n/**\n * Creates and returns a `Siggn` instance that persists for the lifetime of the component.\n * This is useful for creating a message bus scoped to a component and its children.\n *\n * @template T A union of all possible message types for the new instance.\n * @returns A `Siggn<T>` instance.\n * @category Lifecycle\n * @since 0.0.1\n * @example\n * \n```tsx\n * function MyComponent() {\n * const localSiggn = useSiggn<{ type: 'local-event' }>();\n * // ...\n * }\n * ```\n */\nexport function useSiggn<T extends Msg>(): Siggn<T> {\n const [siggn] = useState(new Siggn<T>());\n return siggn;\n}\n\n/**\n * Subscribes to multiple messages and automatically unsubscribes when the component unmounts.\n *\n * @template T A union of all possible message types.\n * @param options A `Siggn` instance or an object with the instance and an optional subscriber ID.\n * @param setup A function that receives a `subscribe` helper to define subscriptions.\n * @param deps An optional dependency array to control when the subscriptions are re-created.\n * @category Subscription\n * @since 0.0.1\n * @example\n * \n```tsx\n * import { siggn } from './siggn'; // Your shared instance\n *\n * function MyComponent() {\n * useSubscribeMany(siggn, (subscribe) => {\n * subscribe('user-created', (msg) => console.log(msg.name));\n * subscribe('user-updated', (msg) => console.log(msg.name));\n * });\n * // ...\n * }\n * ```\n */\nexport function useSubscribeMany<M extends Msg>(\n options:
|
|
1
|
+
{"version":3,"file":"index.cjs.js","sources":["../src/hooks.ts"],"sourcesContent":["import { type Middleware, type Msg, Siggn } from '@siggn/core';\nimport type { SubscriptionOptions } from './types';\n\nimport { useEffect, useMemo, useState, type DependencyList } from 'react';\n\n/**\n * Creates and returns a `Siggn` instance that persists for the lifetime of the component.\n * This is useful for creating a message bus scoped to a component and its children.\n *\n * @template T A union of all possible message types for the new instance.\n * @returns A `Siggn<T>` instance.\n * @category Lifecycle\n * @since 0.0.1\n * @example\n * \n```tsx\n * function MyComponent() {\n * const localSiggn = useSiggn<{ type: 'local-event' }>();\n * // ...\n * }\n * ```\n */\nexport function useSiggn<T extends Msg>(): Siggn<T> {\n const [siggn] = useState(new Siggn<T>());\n return siggn;\n}\n\n/**\n * Subscribes to multiple messages and automatically unsubscribes when the component unmounts.\n *\n * @template T A union of all possible message types.\n * @param options A `Siggn` instance or an object with the instance and an optional subscriber ID.\n * @param setup A function that receives a `subscribe` helper to define subscriptions.\n * @param deps An optional dependency array to control when the subscriptions are re-created.\n * @category Subscription\n * @since 0.0.1\n * @example\n * \n```tsx\n * import { siggn } from './siggn'; // Your shared instance\n *\n * function MyComponent() {\n * useSubscribeMany(siggn, (subscribe) => {\n * subscribe('user-created', (msg) => console.log(msg.name));\n * subscribe('user-updated', (msg) => console.log(msg.name));\n * });\n * // ...\n * }\n * ```\n */\nexport function useSubscribeMany<M extends Msg>(\n options: SubscriptionOptions<M>,\n setup: (\n subscribe: <T extends M['type']>(\n type: T,\n callback: (msg: Extract<M, { type: T }>) => void,\n ) => void,\n ) => void,\n deps: DependencyList = [],\n) {\n const instance = useMemo(\n () => (options instanceof Siggn ? options : options.instance),\n [options],\n );\n const id = useMemo(() => instance.makeId('id' in options ? options.id : undefined), [instance]);\n\n useEffect(() => {\n instance.subscribeMany(id, setup);\n\n return () => {\n instance.unsubscribe(id);\n };\n }, [instance, id, ...deps]);\n}\n\n/**\n * Subscribes to a single message and automatically unsubscribe when the component unmounts.\n *\n * @template T A union of all possible message types.\n * @param options A `Siggn` instance or an object with the instance and an optional subscriber ID.\n * @param setup A function that receives a `subscribe` helper to define subscriptions.\n * @param deps An optional dependency array to control when the subscriptions are re-created.\n * @category Subscription\n * @since 0.0.1\n * @example\n * \n```tsx\n * import { siggn } from './siggn'; // Your shared instance\n *\n * function MyComponent() {\n * useSubscribe(siggn, 'user-created', (msg) => console.log(msg.name));\n * // ...\n * }\n * ```\n */\nexport function useSubscribe<M extends Msg, T extends M['type']>(\n options: SubscriptionOptions<M>,\n type: T,\n callback: (msg: Extract<M, { type: T }>) => void,\n deps: DependencyList = [],\n) {\n const instance = useMemo(\n () => (options instanceof Siggn ? options : options.instance),\n [options],\n );\n const id = useMemo(() => instance.makeId('id' in options ? options.id : undefined), [instance]);\n\n useEffect(() => {\n instance.subscribe(id, type, callback);\n\n return () => {\n instance.unsubscribe(id);\n };\n }, [instance, id, ...deps]);\n}\n\n/**\n * Subscribes to all messages on a `Siggn` instance and automatically unsubscribes\n * when the component unmounts.\n *\n * @template T A union of all possible message types.\n * @param options A `Siggn` instance or an object with the instance and an optional subscriber ID.\n * @param callback The function to call for any message.\n * @param deps An optional dependency array to control when the subscription is re-created.\n * @category Subscription\n * @since 0.0.1\n * @example\n * \n```tsx\n * import { siggn } from './siggn';\n *\n * function LoggerComponent() {\n * useSubscribeAll(siggn, (msg) => {\n * console.log(`[LOG]: ${msg.type}`);\n * }, []);\n * // ...\n * }\n * ```\n */\nexport function useSubscribeAll<M extends Msg>(\n options: SubscriptionOptions<M>,\n callback: (msg: M) => void,\n deps: DependencyList = [],\n) {\n const instance = useMemo(\n () => (options instanceof Siggn ? options : options.instance),\n [options],\n );\n const id = useMemo(() => instance.makeId('id' in options ? options.id : undefined), [instance]);\n\n useEffect(() => {\n instance.subscribeAll(id, callback);\n\n return () => {\n instance.unsubscribeGlobal(id);\n };\n }, [instance, id, ...deps]);\n}\n\n/**\n * Adds a middleware to a `Siggn` instance that persists for the lifetime of the component.\n * Automatically removes the middleware when the component unmounts.\n *\n * @template M A union of all possible message types.\n * @param options A `Siggn` instance or an object with the instance.\n * @param middleware The middleware function to add.\n * @param deps An optional dependency array to control when the middleware is re-added.\n * @category Middleware\n * @since 0.0.6\n * @example\n *\n * ```tsx\n * import { siggn } from './siggn';\n *\n * function LoggerComponent() {\n * useMiddleware(siggn, async (msg, next) => {\n * console.log('Middleware:', msg);\n * next();\n * }, []);\n * // ...\n * }\n * ```\n */\nexport function useMiddleware<M extends Msg>(\n instance: Siggn<M>,\n middleware: Middleware<M>,\n deps: React.DependencyList = [],\n) {\n useEffect(() => {\n const cleanup = instance.use(middleware);\n return cleanup;\n }, [instance, middleware, ...deps]);\n}\n"],"names":["useSiggn","siggn","useState","Siggn","useSubscribeMany","options","setup","deps","instance","useMemo","id","useEffect","useSubscribe","type","callback","useSubscribeAll","useMiddleware","middleware"],"mappings":"kIAsBO,SAASA,GAAoC,CAClD,KAAM,CAACC,CAAK,EAAIC,WAAS,IAAIC,EAAAA,KAAU,EACvC,OAAOF,CACT,CAyBO,SAASG,EACdC,EACAC,EAMAC,EAAuB,CAAA,EACvB,CACA,MAAMC,EAAWC,EAAAA,QACf,IAAOJ,aAAmBF,EAAAA,MAAQE,EA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,CAACT,EAAUS,EAAY,GAAGV,CAAI,CAAC,CACpC"}
|
package/dist/index.es.js
CHANGED
|
@@ -1,41 +1,45 @@
|
|
|
1
1
|
import { Siggn as s } from "@siggn/core";
|
|
2
2
|
export * from "@siggn/core";
|
|
3
|
-
import { useState as
|
|
4
|
-
function
|
|
5
|
-
const [e] =
|
|
3
|
+
import { useState as a, useMemo as r, useEffect as b } from "react";
|
|
4
|
+
function l() {
|
|
5
|
+
const [e] = a(new s());
|
|
6
6
|
return e;
|
|
7
7
|
}
|
|
8
|
-
function
|
|
9
|
-
const n =
|
|
8
|
+
function m(e, u, i = []) {
|
|
9
|
+
const n = r(
|
|
10
10
|
() => e instanceof s ? e : e.instance,
|
|
11
11
|
[e]
|
|
12
|
-
), c =
|
|
13
|
-
|
|
12
|
+
), c = r(() => n.makeId("id" in e ? e.id : void 0), [n]);
|
|
13
|
+
b(() => (n.subscribeMany(c, u), () => {
|
|
14
14
|
n.unsubscribe(c);
|
|
15
|
-
}), [n, c, ...
|
|
15
|
+
}), [n, c, ...i]);
|
|
16
16
|
}
|
|
17
|
-
function
|
|
18
|
-
const c =
|
|
17
|
+
function g(e, u, i, n = []) {
|
|
18
|
+
const c = r(
|
|
19
19
|
() => e instanceof s ? e : e.instance,
|
|
20
20
|
[e]
|
|
21
|
-
),
|
|
22
|
-
|
|
23
|
-
c.unsubscribe(
|
|
24
|
-
}), [c,
|
|
21
|
+
), t = r(() => c.makeId("id" in e ? e.id : void 0), [c]);
|
|
22
|
+
b(() => (c.subscribe(t, u, i), () => {
|
|
23
|
+
c.unsubscribe(t);
|
|
24
|
+
}), [c, t, ...n]);
|
|
25
25
|
}
|
|
26
|
-
function S(e, u,
|
|
27
|
-
const n =
|
|
26
|
+
function S(e, u, i = []) {
|
|
27
|
+
const n = r(
|
|
28
28
|
() => e instanceof s ? e : e.instance,
|
|
29
29
|
[e]
|
|
30
|
-
), c =
|
|
31
|
-
|
|
30
|
+
), c = r(() => n.makeId("id" in e ? e.id : void 0), [n]);
|
|
31
|
+
b(() => (n.subscribeAll(c, u), () => {
|
|
32
32
|
n.unsubscribeGlobal(c);
|
|
33
|
-
}), [n, c, ...
|
|
33
|
+
}), [n, c, ...i]);
|
|
34
|
+
}
|
|
35
|
+
function M(e, u, i = []) {
|
|
36
|
+
b(() => e.use(u), [e, u, ...i]);
|
|
34
37
|
}
|
|
35
38
|
export {
|
|
36
|
-
|
|
37
|
-
l as
|
|
39
|
+
M as useMiddleware,
|
|
40
|
+
l as useSiggn,
|
|
41
|
+
g as useSubscribe,
|
|
38
42
|
S as useSubscribeAll,
|
|
39
|
-
|
|
43
|
+
m as useSubscribeMany
|
|
40
44
|
};
|
|
41
45
|
//# sourceMappingURL=index.es.js.map
|
package/dist/index.es.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.es.js","sources":["../src/hooks.ts"],"sourcesContent":["import { type Msg, Siggn } from '@siggn/core';\n\nimport { useEffect, useMemo, useState, type DependencyList } from 'react';\n\n/**\n * Creates and returns a `Siggn` instance that persists for the lifetime of the component.\n * This is useful for creating a message bus scoped to a component and its children.\n *\n * @template T A union of all possible message types for the new instance.\n * @returns A `Siggn<T>` instance.\n * @category Lifecycle\n * @since 0.0.1\n * @example\n * \n```tsx\n * function MyComponent() {\n * const localSiggn = useSiggn<{ type: 'local-event' }>();\n * // ...\n * }\n * ```\n */\nexport function useSiggn<T extends Msg>(): Siggn<T> {\n const [siggn] = useState(new Siggn<T>());\n return siggn;\n}\n\n/**\n * Subscribes to multiple messages and automatically unsubscribes when the component unmounts.\n *\n * @template T A union of all possible message types.\n * @param options A `Siggn` instance or an object with the instance and an optional subscriber ID.\n * @param setup A function that receives a `subscribe` helper to define subscriptions.\n * @param deps An optional dependency array to control when the subscriptions are re-created.\n * @category Subscription\n * @since 0.0.1\n * @example\n * \n```tsx\n * import { siggn } from './siggn'; // Your shared instance\n *\n * function MyComponent() {\n * useSubscribeMany(siggn, (subscribe) => {\n * subscribe('user-created', (msg) => console.log(msg.name));\n * subscribe('user-updated', (msg) => console.log(msg.name));\n * });\n * // ...\n * }\n * ```\n */\nexport function useSubscribeMany<M extends Msg>(\n options:
|
|
1
|
+
{"version":3,"file":"index.es.js","sources":["../src/hooks.ts"],"sourcesContent":["import { type Middleware, type Msg, Siggn } from '@siggn/core';\nimport type { SubscriptionOptions } from './types';\n\nimport { useEffect, useMemo, useState, type DependencyList } from 'react';\n\n/**\n * Creates and returns a `Siggn` instance that persists for the lifetime of the component.\n * This is useful for creating a message bus scoped to a component and its children.\n *\n * @template T A union of all possible message types for the new instance.\n * @returns A `Siggn<T>` instance.\n * @category Lifecycle\n * @since 0.0.1\n * @example\n * \n```tsx\n * function MyComponent() {\n * const localSiggn = useSiggn<{ type: 'local-event' }>();\n * // ...\n * }\n * ```\n */\nexport function useSiggn<T extends Msg>(): Siggn<T> {\n const [siggn] = useState(new Siggn<T>());\n return siggn;\n}\n\n/**\n * Subscribes to multiple messages and automatically unsubscribes when the component unmounts.\n *\n * @template T A union of all possible message types.\n * @param options A `Siggn` instance or an object with the instance and an optional subscriber ID.\n * @param setup A function that receives a `subscribe` helper to define subscriptions.\n * @param deps An optional dependency array to control when the subscriptions are re-created.\n * @category Subscription\n * @since 0.0.1\n * @example\n * \n```tsx\n * import { siggn } from './siggn'; // Your shared instance\n *\n * function MyComponent() {\n * useSubscribeMany(siggn, (subscribe) => {\n * subscribe('user-created', (msg) => console.log(msg.name));\n * subscribe('user-updated', (msg) => console.log(msg.name));\n * });\n * // ...\n * }\n * ```\n */\nexport function useSubscribeMany<M extends Msg>(\n options: SubscriptionOptions<M>,\n setup: (\n subscribe: <T extends M['type']>(\n type: T,\n callback: (msg: Extract<M, { type: T }>) => void,\n ) => void,\n ) => void,\n deps: DependencyList = [],\n) {\n const instance = useMemo(\n () => (options instanceof Siggn ? options : options.instance),\n [options],\n );\n const id = useMemo(() => instance.makeId('id' in options ? options.id : undefined), [instance]);\n\n useEffect(() => {\n instance.subscribeMany(id, setup);\n\n return () => {\n instance.unsubscribe(id);\n };\n }, [instance, id, ...deps]);\n}\n\n/**\n * Subscribes to a single message and automatically unsubscribe when the component unmounts.\n *\n * @template T A union of all possible message types.\n * @param options A `Siggn` instance or an object with the instance and an optional subscriber ID.\n * @param setup A function that receives a `subscribe` helper to define subscriptions.\n * @param deps An optional dependency array to control when the subscriptions are re-created.\n * @category Subscription\n * @since 0.0.1\n * @example\n * \n```tsx\n * import { siggn } from './siggn'; // Your shared instance\n *\n * function MyComponent() {\n * useSubscribe(siggn, 'user-created', (msg) => console.log(msg.name));\n * // ...\n * }\n * ```\n */\nexport function useSubscribe<M extends Msg, T extends M['type']>(\n options: SubscriptionOptions<M>,\n type: T,\n callback: (msg: Extract<M, { type: T }>) => void,\n deps: DependencyList = [],\n) {\n const instance = useMemo(\n () => (options instanceof Siggn ? options : options.instance),\n [options],\n );\n const id = useMemo(() => instance.makeId('id' in options ? options.id : undefined), [instance]);\n\n useEffect(() => {\n instance.subscribe(id, type, callback);\n\n return () => {\n instance.unsubscribe(id);\n };\n }, [instance, id, ...deps]);\n}\n\n/**\n * Subscribes to all messages on a `Siggn` instance and automatically unsubscribes\n * when the component unmounts.\n *\n * @template T A union of all possible message types.\n * @param options A `Siggn` instance or an object with the instance and an optional subscriber ID.\n * @param callback The function to call for any message.\n * @param deps An optional dependency array to control when the subscription is re-created.\n * @category Subscription\n * @since 0.0.1\n * @example\n * \n```tsx\n * import { siggn } from './siggn';\n *\n * function LoggerComponent() {\n * useSubscribeAll(siggn, (msg) => {\n * console.log(`[LOG]: ${msg.type}`);\n * }, []);\n * // ...\n * }\n * ```\n */\nexport function useSubscribeAll<M extends Msg>(\n options: SubscriptionOptions<M>,\n callback: (msg: M) => void,\n deps: DependencyList = [],\n) {\n const instance = useMemo(\n () => (options instanceof Siggn ? options : options.instance),\n [options],\n );\n const id = useMemo(() => instance.makeId('id' in options ? options.id : undefined), [instance]);\n\n useEffect(() => {\n instance.subscribeAll(id, callback);\n\n return () => {\n instance.unsubscribeGlobal(id);\n };\n }, [instance, id, ...deps]);\n}\n\n/**\n * Adds a middleware to a `Siggn` instance that persists for the lifetime of the component.\n * Automatically removes the middleware when the component unmounts.\n *\n * @template M A union of all possible message types.\n * @param options A `Siggn` instance or an object with the instance.\n * @param middleware The middleware function to add.\n * @param deps An optional dependency array to control when the middleware is re-added.\n * @category Middleware\n * @since 0.0.6\n * @example\n *\n * ```tsx\n * import { siggn } from './siggn';\n *\n * function LoggerComponent() {\n * useMiddleware(siggn, async (msg, next) => {\n * console.log('Middleware:', msg);\n * next();\n * }, []);\n * // ...\n * }\n * ```\n */\nexport function useMiddleware<M extends Msg>(\n instance: Siggn<M>,\n middleware: Middleware<M>,\n deps: React.DependencyList = [],\n) {\n useEffect(() => {\n const cleanup = instance.use(middleware);\n return cleanup;\n }, [instance, middleware, ...deps]);\n}\n"],"names":["useSiggn","siggn","useState","Siggn","useSubscribeMany","options","setup","deps","instance","useMemo","id","useEffect","useSubscribe","type","callback","useSubscribeAll","useMiddleware","middleware"],"mappings":";;;AAsBO,SAASA,IAAoC;AAClD,QAAM,CAACC,CAAK,IAAIC,EAAS,IAAIC,GAAU;AACvC,SAAOF;AACT;AAyBO,SAASG,EACdC,GACAC,GAMAC,IAAuB,CAAA,GACvB;AACA,QAAMC,IAAWC;AAAA,IACf,MAAOJ,aAAmBF,IAAQE,IAAUA,EAAQ;AAAA,IACpD,CAACA,CAAO;AAAA,EAAA,GAEJK,IAAKD,EAAQ,MAAMD,EAAS,OAAO,QAAQH,IAAUA,EAAQ,KAAK,MAAS,GAAG,CAACG,CAAQ,CAAC;AAE9F,EAAAG,EAAU,OACRH,EAAS,cAAcE,GAAIJ,CAAK,GAEzB,MAAM;AACX,IAAAE,EAAS,YAAYE,CAAE;AAAA,EACzB,IACC,CAACF,GAAUE,GAAI,GAAGH,CAAI,CAAC;AAC5B;AAsBO,SAASK,EACdP,GACAQ,GACAC,GACAP,IAAuB,CAAA,GACvB;AACA,QAAMC,IAAWC;AAAA,IACf,MAAOJ,aAAmBF,IAAQE,IAAUA,EAAQ;AAAA,IACpD,CAACA,CAAO;AAAA,EAAA,GAEJK,IAAKD,EAAQ,MAAMD,EAAS,OAAO,QAAQH,IAAUA,EAAQ,KAAK,MAAS,GAAG,CAACG,CAAQ,CAAC;AAE9F,EAAAG,EAAU,OACRH,EAAS,UAAUE,GAAIG,GAAMC,CAAQ,GAE9B,MAAM;AACX,IAAAN,EAAS,YAAYE,CAAE;AAAA,EACzB,IACC,CAACF,GAAUE,GAAI,GAAGH,CAAI,CAAC;AAC5B;AAyBO,SAASQ,EACdV,GACAS,GACAP,IAAuB,CAAA,GACvB;AACA,QAAMC,IAAWC;AAAA,IACf,MAAOJ,aAAmBF,IAAQE,IAAUA,EAAQ;AAAA,IACpD,CAACA,CAAO;AAAA,EAAA,GAEJK,IAAKD,EAAQ,MAAMD,EAAS,OAAO,QAAQH,IAAUA,EAAQ,KAAK,MAAS,GAAG,CAACG,CAAQ,CAAC;AAE9F,EAAAG,EAAU,OACRH,EAAS,aAAaE,GAAII,CAAQ,GAE3B,MAAM;AACX,IAAAN,EAAS,kBAAkBE,CAAE;AAAA,EAC/B,IACC,CAACF,GAAUE,GAAI,GAAGH,CAAI,CAAC;AAC5B;AA0BO,SAASS,EACdR,GACAS,GACAV,IAA6B,CAAA,GAC7B;AACA,EAAAI,EAAU,MACQH,EAAS,IAAIS,CAAU,GAEtC,CAACT,GAAUS,GAAY,GAAGV,CAAI,CAAC;AACpC;"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@siggn/react",
|
|
3
|
-
"version": "0.1
|
|
3
|
+
"version": "0.2.1",
|
|
4
4
|
"description": "A lightweight type safe message bus system for React",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"pub/sub",
|
|
@@ -58,7 +58,7 @@
|
|
|
58
58
|
"react": ">=18 <20"
|
|
59
59
|
},
|
|
60
60
|
"dependencies": {
|
|
61
|
-
"@siggn/core": "0.
|
|
61
|
+
"@siggn/core": "0.2.0"
|
|
62
62
|
},
|
|
63
63
|
"scripts": {
|
|
64
64
|
"dev": "vite build --watch",
|