@motiadev/stream-client-react 0.17.6-beta.188-623819 → 0.17.7-beta.188
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/dist/index.js +50 -138
- package/dist/index.js.map +1 -1
- package/package.json +4 -5
- package/src/use-stream-group.ts +1 -4
- package/tsdown.config.ts +1 -13
package/dist/index.js
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { Stream, Stream as Stream$1, StreamGroupSubscription, StreamItemSubscription } from "@motiadev/stream-client-browser";
|
|
2
|
-
import {
|
|
3
|
-
import React, { useEffect, useRef, useState } from "react";
|
|
2
|
+
import React, { useCallback, useEffect, useMemo, useRef, useState } from "react";
|
|
4
3
|
import { jsx } from "react/jsx-runtime";
|
|
5
4
|
|
|
6
5
|
//#region src/motia-stream-context.ts
|
|
@@ -8,46 +7,18 @@ const MotiaStreamContext = React.createContext({ stream: null });
|
|
|
8
7
|
|
|
9
8
|
//#endregion
|
|
10
9
|
//#region src/motia-stream-provider.tsx
|
|
11
|
-
const MotiaStreamProvider = (
|
|
12
|
-
const $ = c(9);
|
|
13
|
-
const { children, address, protocols } = t0;
|
|
10
|
+
const MotiaStreamProvider = ({ children, address, protocols }) => {
|
|
14
11
|
const [stream, setStream] = useState(null);
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
$[1] = protocols;
|
|
26
|
-
$[2] = t1;
|
|
27
|
-
$[3] = t2;
|
|
28
|
-
} else {
|
|
29
|
-
t1 = $[2];
|
|
30
|
-
t2 = $[3];
|
|
31
|
-
}
|
|
32
|
-
useEffect(t1, t2);
|
|
33
|
-
let t3;
|
|
34
|
-
if ($[4] !== stream) {
|
|
35
|
-
t3 = { stream };
|
|
36
|
-
$[4] = stream;
|
|
37
|
-
$[5] = t3;
|
|
38
|
-
} else t3 = $[5];
|
|
39
|
-
const contextValue = t3;
|
|
40
|
-
let t4;
|
|
41
|
-
if ($[6] !== children || $[7] !== contextValue) {
|
|
42
|
-
t4 = /* @__PURE__ */ jsx(MotiaStreamContext.Provider, {
|
|
43
|
-
value: contextValue,
|
|
44
|
-
children
|
|
45
|
-
});
|
|
46
|
-
$[6] = children;
|
|
47
|
-
$[7] = contextValue;
|
|
48
|
-
$[8] = t4;
|
|
49
|
-
} else t4 = $[8];
|
|
50
|
-
return t4;
|
|
12
|
+
useEffect(() => {
|
|
13
|
+
const streamInstance = new Stream$1(address, { protocols });
|
|
14
|
+
setStream(streamInstance);
|
|
15
|
+
return () => streamInstance.close();
|
|
16
|
+
}, [address, protocols]);
|
|
17
|
+
const contextValue = useMemo(() => ({ stream }), [stream]);
|
|
18
|
+
return /* @__PURE__ */ jsx(MotiaStreamContext.Provider, {
|
|
19
|
+
value: contextValue,
|
|
20
|
+
children
|
|
21
|
+
});
|
|
51
22
|
};
|
|
52
23
|
|
|
53
24
|
//#endregion
|
|
@@ -83,35 +54,17 @@ const useMotiaStream = () => {
|
|
|
83
54
|
* useStreamEventHandler({ event, type: 'on-custom-event', listener: onEventHandled }, [])
|
|
84
55
|
* ```
|
|
85
56
|
*/
|
|
86
|
-
const useStreamEventHandler = (
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
$[0] = event;
|
|
98
|
-
$[1] = listener;
|
|
99
|
-
$[2] = type;
|
|
100
|
-
$[3] = t1;
|
|
101
|
-
} else t1 = $[3];
|
|
102
|
-
let t2;
|
|
103
|
-
if ($[4] !== dependencies || $[5] !== event || $[6] !== type) {
|
|
104
|
-
t2 = [
|
|
105
|
-
event,
|
|
106
|
-
type,
|
|
107
|
-
...dependencies
|
|
108
|
-
];
|
|
109
|
-
$[4] = dependencies;
|
|
110
|
-
$[5] = event;
|
|
111
|
-
$[6] = type;
|
|
112
|
-
$[7] = t2;
|
|
113
|
-
} else t2 = $[7];
|
|
114
|
-
useEffect(t1, t2);
|
|
57
|
+
const useStreamEventHandler = ({ event, type, listener }, dependencies) => {
|
|
58
|
+
useEffect(() => {
|
|
59
|
+
if (event) {
|
|
60
|
+
event.onEvent(type, listener);
|
|
61
|
+
return () => event.offEvent(type, listener);
|
|
62
|
+
}
|
|
63
|
+
}, [
|
|
64
|
+
event,
|
|
65
|
+
type,
|
|
66
|
+
...dependencies
|
|
67
|
+
]);
|
|
115
68
|
};
|
|
116
69
|
|
|
117
70
|
//#endregion
|
|
@@ -141,17 +94,10 @@ const useStreamGroup = (args) => {
|
|
|
141
94
|
const subscriptionRef = useRef(null);
|
|
142
95
|
const { streamName, groupId, sortKey, setData: setDataCallback } = args || {};
|
|
143
96
|
useEffect(() => {
|
|
144
|
-
if (!streamName || !groupId || !stream)
|
|
145
|
-
console.error("useStreamGroup: streamName, groupId or stream is not defined", {
|
|
146
|
-
streamName,
|
|
147
|
-
groupId,
|
|
148
|
-
stream
|
|
149
|
-
});
|
|
150
|
-
return;
|
|
151
|
-
}
|
|
97
|
+
if (!streamName || !groupId || !stream) return;
|
|
152
98
|
subscriptionRef.current = stream.subscribeGroup(streamName, groupId, sortKey);
|
|
153
|
-
subscriptionRef.current.addChangeListener((
|
|
154
|
-
const typedData =
|
|
99
|
+
subscriptionRef.current.addChangeListener((data$1) => {
|
|
100
|
+
const typedData = data$1;
|
|
155
101
|
setData(typedData);
|
|
156
102
|
setDataCallback?.(typedData);
|
|
157
103
|
});
|
|
@@ -192,68 +138,34 @@ const useStreamGroup = (args) => {
|
|
|
192
138
|
* ```
|
|
193
139
|
*/
|
|
194
140
|
const useStreamItem = (args) => {
|
|
195
|
-
const $ = c(12);
|
|
196
141
|
const { stream } = useMotiaStream();
|
|
197
142
|
const [data, setData] = useState();
|
|
198
143
|
const [event, setEvent] = useState(null);
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
setData(
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
} else t1 = $[2];
|
|
213
|
-
const handleChange = t1;
|
|
214
|
-
let t2;
|
|
215
|
-
let t3;
|
|
216
|
-
if ($[3] !== groupId || $[4] !== id || $[5] !== stream || $[6] !== streamName) {
|
|
217
|
-
t2 = () => {
|
|
218
|
-
if (!streamName || !groupId || !id || !stream) return;
|
|
219
|
-
const subscription = stream.subscribeItem(streamName, groupId, id);
|
|
220
|
-
subscription.addChangeListener(handleChange);
|
|
221
|
-
setEvent(subscription);
|
|
222
|
-
return () => {
|
|
223
|
-
setData(void 0);
|
|
224
|
-
setEvent(null);
|
|
225
|
-
subscription.close();
|
|
226
|
-
};
|
|
227
|
-
};
|
|
228
|
-
t3 = [
|
|
229
|
-
stream,
|
|
230
|
-
streamName,
|
|
231
|
-
groupId,
|
|
232
|
-
id,
|
|
233
|
-
handleChange
|
|
234
|
-
];
|
|
235
|
-
$[3] = groupId;
|
|
236
|
-
$[4] = id;
|
|
237
|
-
$[5] = stream;
|
|
238
|
-
$[6] = streamName;
|
|
239
|
-
$[7] = t2;
|
|
240
|
-
$[8] = t3;
|
|
241
|
-
} else {
|
|
242
|
-
t2 = $[7];
|
|
243
|
-
t3 = $[8];
|
|
244
|
-
}
|
|
245
|
-
useEffect(t2, t3);
|
|
246
|
-
let t4;
|
|
247
|
-
if ($[9] !== data || $[10] !== event) {
|
|
248
|
-
t4 = {
|
|
249
|
-
data,
|
|
250
|
-
event
|
|
144
|
+
const { streamName, groupId, id } = args || {};
|
|
145
|
+
const handleChange = useCallback((data$1) => {
|
|
146
|
+
setData(data$1);
|
|
147
|
+
}, []);
|
|
148
|
+
useEffect(() => {
|
|
149
|
+
if (!streamName || !groupId || !id || !stream) return;
|
|
150
|
+
const subscription = stream.subscribeItem(streamName, groupId, id);
|
|
151
|
+
subscription.addChangeListener(handleChange);
|
|
152
|
+
setEvent(subscription);
|
|
153
|
+
return () => {
|
|
154
|
+
setData(void 0);
|
|
155
|
+
setEvent(null);
|
|
156
|
+
subscription.close();
|
|
251
157
|
};
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
158
|
+
}, [
|
|
159
|
+
stream,
|
|
160
|
+
streamName,
|
|
161
|
+
groupId,
|
|
162
|
+
id,
|
|
163
|
+
handleChange
|
|
164
|
+
]);
|
|
165
|
+
return {
|
|
166
|
+
data,
|
|
167
|
+
event
|
|
168
|
+
};
|
|
257
169
|
};
|
|
258
170
|
|
|
259
171
|
//#endregion
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":["Stream","React","MotiaStreamContextType","stream","MotiaStreamContext","createContext","Stream","useEffect","useMemo","useState","MotiaStreamContext","Props","React","PropsWithChildren","address","protocols","MotiaStreamProvider","FC","t0","$","_c","children","stream","setStream","t1","t2","streamInstance","close","t3","contextValue","t4","React","MotiaStreamContext","useMotiaStream","context","useContext","Error","StreamSubscription","DependencyList","useEffect","UseStreamEventHandler","event","type","listener","useStreamEventHandler","t0","dependencies","$","_c","t1","onEvent","offEvent","t2","StreamSubscription","useEffect","useRef","useState","useMotiaStream","StreamGroupArgs","id","streamName","groupId","sortKey","TData","setData","data","useStreamGroup","args","stream","subscriptionRef","setDataCallback","console","error","current","subscribeGroup","addChangeListener","typedData","close","event","StreamSubscription","useCallback","useEffect","useState","useMotiaStream","StreamItemArgs","streamName","groupId","id","useStreamItem","args","$","_c","stream","data","setData","event","setEvent","t0","t1","Symbol","for","data_0","TData","handleChange","t2","t3","subscription","subscribeItem","addChangeListener","undefined","close","t4"],"sources":["../src/motia-stream-context.ts","../src/motia-stream-provider.tsx","../src/use-motia-stream.ts","../src/use-stream-event-handler.ts","../src/use-stream-group.ts","../src/use-stream-item.ts"],"sourcesContent":["import type { Stream } from '@motiadev/stream-client-browser'\nimport React from 'react'\n\ntype MotiaStreamContextType = {\n stream: Stream | null\n}\n\nexport const MotiaStreamContext = React.createContext<MotiaStreamContextType>({\n stream: null,\n})\n","import { Stream } from '@motiadev/stream-client-browser'\nimport { useEffect, useMemo, useState } from 'react'\nimport { MotiaStreamContext } from './motia-stream-context'\n\ntype Props = React.PropsWithChildren<{\n /**\n * The address of the stream server.\n *\n * @example\n * ```tsx\n * <MotiaStreamProvider address=\"ws://localhost:3000\">\n * <App />\n * </MotiaStreamProvider>\n * */\n address: string\n protocols?: string | string[] | undefined\n}>\n\nexport const MotiaStreamProvider: React.FC<Props> = ({ children, address, protocols }) => {\n const [stream, setStream] = useState<Stream | null>(null)\n\n useEffect(() => {\n const streamInstance = new Stream(address, { protocols })\n setStream(streamInstance)\n return () => streamInstance.close()\n }, [address, protocols])\n\n const contextValue = useMemo(() => ({ stream }), [stream])\n\n return <MotiaStreamContext.Provider value={contextValue}>{children}</MotiaStreamContext.Provider>\n}\n","import React from 'react'\nimport { MotiaStreamContext } from './motia-stream-context'\n\n/**\n * A hook to get the stream context.\n *\n * @example\n * ```tsx\n * const { stream } = useMotiaStream()\n * ```\n */\nexport const useMotiaStream = () => {\n const context = React.useContext(MotiaStreamContext)\n\n if (!context) {\n throw new Error('useMotiaStream must be used within a MotiaStreamProvider')\n }\n\n return context\n}\n","import type { StreamSubscription } from '@motiadev/stream-client-browser'\nimport { type DependencyList, useEffect } from 'react'\n\ntype UseStreamEventHandler = {\n event: StreamSubscription | null\n type: string\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n listener: (event: any) => void\n}\n\n/**\n * A hook to handle custom stream events.\n *\n * @example\n * ```tsx\n * const { event } = useStreamItem({ streamName: 'my-stream', id: '123' })\n *\n * const onEventHandled = (event: any) => {\n * // this is going to be called whenever 'on-custom-event' is sent from the server\n * console.log(event)\n * }\n *\n * useStreamEventHandler({ event, type: 'on-custom-event', listener: onEventHandled }, [])\n * ```\n */\nexport const useStreamEventHandler = (\n { event, type, listener }: UseStreamEventHandler,\n dependencies: DependencyList,\n) => {\n useEffect(() => {\n if (event) {\n event.onEvent(type, listener)\n return () => event.offEvent(type, listener)\n }\n }, [event, type, ...dependencies])\n}\n","import type { StreamSubscription } from '@motiadev/stream-client-browser'\nimport { useEffect, useRef, useState } from 'react'\nimport { useMotiaStream } from './use-motia-stream'\n\nexport type StreamGroupArgs<TData extends { id: string }> = {\n streamName: string\n groupId: string\n sortKey?: keyof TData\n setData?: (data: TData[]) => void\n}\n\n/**\n * A hook to get a group of items from a stream.\n *\n * @example\n * ```tsx\n * const { data } = useStreamGroup<{ id:string; name: string }>({\n * streamName: 'my-stream',\n * groupId: '123',\n * })\n *\n * return (\n * <div>\n * {data.map((item) => (\n * <div key={item.id}>{item.name}</div>\n * ))}\n * </div>\n * )\n * ```\n */\nexport const useStreamGroup = <TData extends { id: string }>(args?: StreamGroupArgs<TData>) => {\n const { stream } = useMotiaStream()\n const [data, setData] = useState<TData[]>([])\n const subscriptionRef = useRef<StreamSubscription | null>(null)\n\n const { streamName, groupId, sortKey, setData: setDataCallback } = args || {}\n\n useEffect(() => {\n if (!streamName || !groupId || !stream) {\n console.error('useStreamGroup: streamName, groupId or stream is not defined', { streamName, groupId, stream })\n return\n }\n\n subscriptionRef.current = stream.subscribeGroup(streamName, groupId, sortKey)\n\n subscriptionRef.current.addChangeListener((data) => {\n const typedData = data as TData[]\n setData(typedData)\n setDataCallback?.(typedData)\n })\n\n return () => {\n subscriptionRef.current?.close()\n subscriptionRef.current = null\n setData([])\n }\n }, [stream, streamName, groupId, sortKey, setDataCallback])\n\n return { data, event: subscriptionRef.current }\n}\n","import type { StreamSubscription } from '@motiadev/stream-client-browser'\nimport { useCallback, useEffect, useState } from 'react'\nimport { useMotiaStream } from './use-motia-stream'\n\nexport type StreamItemArgs = {\n streamName: string\n groupId: string\n id?: string\n}\n\n/**\n * A hook to get a single item from a stream.\n *\n * @example\n * ```tsx\n * const { data } = useStreamItem<{ id:string; name: string }>({\n * streamName: 'my-stream',\n * groupId: '123',\n * id: '123',\n * })\n *\n * return (\n * <div>{data?.name}</div>\n * )\n * ```\n */\nexport const useStreamItem = <TData>(args?: StreamItemArgs) => {\n const { stream } = useMotiaStream()\n const [data, setData] = useState<TData | null | undefined>()\n const [event, setEvent] = useState<StreamSubscription | null>(null)\n const { streamName, groupId, id } = args || {}\n\n const handleChange = useCallback((data: unknown) => {\n setData(data as TData)\n }, [])\n\n useEffect(() => {\n if (!streamName || !groupId || !id || !stream) return\n\n const subscription = stream.subscribeItem(streamName, groupId, id)\n\n subscription.addChangeListener(handleChange)\n setEvent(subscription)\n\n return () => {\n setData(undefined)\n setEvent(null)\n subscription.close()\n }\n }, [stream, streamName, groupId, id, handleChange])\n\n return { data, event }\n}\n"],"mappings":";;;;;;AAOA,MAAaI,qBAAqBH,MAAMI,cAAsC,EAC5EF,QAAQ,MACT,CAAC;;;;ACSF,MAAaa,uBAAuCE,OAAA;CAAA,MAAAC,IAAAC,EAAA,EAAA;CAAC,MAAA,EAAAC,UAAAP,SAAAC,cAAAG;CACnD,MAAA,CAAAI,QAAAC,aAA4Bd,SAAwB,KAAK;CAAA,IAAAe;CAAA,IAAAC;AAAA,KAAAN,EAAA,OAAAL,WAAAK,EAAA,OAAAJ,WAAA;AAE/CS,aAAA;GACR,MAAAE,iBAAuB,IAAIpB,SAAOQ,SAAS,EAAAC,WAAa,CAAC;AACzDQ,aAAUG,eAAe;AAAA,gBACZA,eAAcC,OAAQ;;AAClCF,OAAA,CAACX,SAASC,UAAU;AAAAI,IAAA,KAAAL;AAAAK,IAAA,KAAAJ;AAAAI,IAAA,KAAAK;AAAAL,IAAA,KAAAM;QAAA;AAAAD,OAAAL,EAAA;AAAAM,OAAAN,EAAA;;AAJvBZ,WAAUiB,IAIPC,GAAqB;CAAA,IAAAG;AAAA,KAAAT,EAAA,OAAAG,QAAA;AAEYM,OAAA,EAAAN,QAAU;AAAAH,IAAA,KAAAG;AAAAH,IAAA,KA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,IAAAC;AAAA,KAAAf,EAAA,OAAAJ,WAAAI,EAAA,OAAAH,MAAAG,EAAA,OAAAE,UAAAF,EAAA,OAAAL,YAAA;AAEImB,aAAA;AACR,OAAI,CAACnB,cAAD,CAAgBC,WAAhB,CAA4BC,MAA5B,CAAmCK,OAAM;GAE7C,MAAAc,eAAqBd,OAAMe,cAAetB,YAAYC,SAASC,GAAG;AAElEmB,gBAAYE,kBAAmBL,aAAa;AAC5CP,YAASU,aAAa;AAAA,gBAEf;AACLZ,YAAQe,OAAU;AAClBb,aAAS,KAAK;AACdU,iBAAYI,OAAQ;;;AAErBL,OAAA;GAACb;GAAQP;GAAYC;GAASC;GAAIgB;GAAa;AAAAb,IAAA,KAAAJ;AAAAI,IAAA,KAAAH;AAAAG,IAAA,KAAAE;AAAAF,IAAA,KAAAL;AAAAK,IAAA,KAAAc;AAAAd,IAAA,KAAAe;QAAA;AAAAD,OAAAd,EAAA;AAAAe,OAAAf,EAAA;;AAblDT,WAAUuB,IAaPC,GAAgD;CAAA,IAAAM;AAAA,KAAArB,EAAA,OAAAG,QAAAH,EAAA,QAAAK,OAAA;AAE5CgB,OAAA;GAAAlB;GAAAE;GAAe;AAAAL,IAAA,KAAAG;AAAAH,IAAA,MAAAK;AAAAL,IAAA,MAAAqB;OAAAA,MAAArB,EAAA;AAAA,QAAfqB"}
|
|
1
|
+
{"version":3,"file":"index.js","names":["MotiaStreamProvider: React.FC<Props>","Stream","data","data"],"sources":["../src/motia-stream-context.ts","../src/motia-stream-provider.tsx","../src/use-motia-stream.ts","../src/use-stream-event-handler.ts","../src/use-stream-group.ts","../src/use-stream-item.ts"],"sourcesContent":["import type { Stream } from '@motiadev/stream-client-browser'\nimport React from 'react'\n\ntype MotiaStreamContextType = {\n stream: Stream | null\n}\n\nexport const MotiaStreamContext = React.createContext<MotiaStreamContextType>({\n stream: null,\n})\n","import { Stream } from '@motiadev/stream-client-browser'\nimport { useEffect, useMemo, useState } from 'react'\nimport { MotiaStreamContext } from './motia-stream-context'\n\ntype Props = React.PropsWithChildren<{\n /**\n * The address of the stream server.\n *\n * @example\n * ```tsx\n * <MotiaStreamProvider address=\"ws://localhost:3000\">\n * <App />\n * </MotiaStreamProvider>\n * */\n address: string\n protocols?: string | string[] | undefined\n}>\n\nexport const MotiaStreamProvider: React.FC<Props> = ({ children, address, protocols }) => {\n const [stream, setStream] = useState<Stream | null>(null)\n\n useEffect(() => {\n const streamInstance = new Stream(address, { protocols })\n setStream(streamInstance)\n return () => streamInstance.close()\n }, [address, protocols])\n\n const contextValue = useMemo(() => ({ stream }), [stream])\n\n return <MotiaStreamContext.Provider value={contextValue}>{children}</MotiaStreamContext.Provider>\n}\n","import React from 'react'\nimport { MotiaStreamContext } from './motia-stream-context'\n\n/**\n * A hook to get the stream context.\n *\n * @example\n * ```tsx\n * const { stream } = useMotiaStream()\n * ```\n */\nexport const useMotiaStream = () => {\n const context = React.useContext(MotiaStreamContext)\n\n if (!context) {\n throw new Error('useMotiaStream must be used within a MotiaStreamProvider')\n }\n\n return context\n}\n","import type { StreamSubscription } from '@motiadev/stream-client-browser'\nimport { type DependencyList, useEffect } from 'react'\n\ntype UseStreamEventHandler = {\n event: StreamSubscription | null\n type: string\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n listener: (event: any) => void\n}\n\n/**\n * A hook to handle custom stream events.\n *\n * @example\n * ```tsx\n * const { event } = useStreamItem({ streamName: 'my-stream', id: '123' })\n *\n * const onEventHandled = (event: any) => {\n * // this is going to be called whenever 'on-custom-event' is sent from the server\n * console.log(event)\n * }\n *\n * useStreamEventHandler({ event, type: 'on-custom-event', listener: onEventHandled }, [])\n * ```\n */\nexport const useStreamEventHandler = (\n { event, type, listener }: UseStreamEventHandler,\n dependencies: DependencyList,\n) => {\n useEffect(() => {\n if (event) {\n event.onEvent(type, listener)\n return () => event.offEvent(type, listener)\n }\n }, [event, type, ...dependencies])\n}\n","import type { StreamSubscription } from '@motiadev/stream-client-browser'\nimport { useEffect, useRef, useState } from 'react'\nimport { useMotiaStream } from './use-motia-stream'\n\nexport type StreamGroupArgs<TData extends { id: string }> = {\n streamName: string\n groupId: string\n sortKey?: keyof TData\n setData?: (data: TData[]) => void\n}\n\n/**\n * A hook to get a group of items from a stream.\n *\n * @example\n * ```tsx\n * const { data } = useStreamGroup<{ id:string; name: string }>({\n * streamName: 'my-stream',\n * groupId: '123',\n * })\n *\n * return (\n * <div>\n * {data.map((item) => (\n * <div key={item.id}>{item.name}</div>\n * ))}\n * </div>\n * )\n * ```\n */\nexport const useStreamGroup = <TData extends { id: string }>(args?: StreamGroupArgs<TData>) => {\n const { stream } = useMotiaStream()\n const [data, setData] = useState<TData[]>([])\n const subscriptionRef = useRef<StreamSubscription | null>(null)\n\n const { streamName, groupId, sortKey, setData: setDataCallback } = args || {}\n\n useEffect(() => {\n if (!streamName || !groupId || !stream) return\n\n subscriptionRef.current = stream.subscribeGroup(streamName, groupId, sortKey)\n\n subscriptionRef.current.addChangeListener((data) => {\n const typedData = data as TData[]\n setData(typedData)\n setDataCallback?.(typedData)\n })\n\n return () => {\n subscriptionRef.current?.close()\n subscriptionRef.current = null\n setData([])\n }\n }, [stream, streamName, groupId, sortKey, setDataCallback])\n\n return { data, event: subscriptionRef.current }\n}\n","import type { StreamSubscription } from '@motiadev/stream-client-browser'\nimport { useCallback, useEffect, useState } from 'react'\nimport { useMotiaStream } from './use-motia-stream'\n\nexport type StreamItemArgs = {\n streamName: string\n groupId: string\n id?: string\n}\n\n/**\n * A hook to get a single item from a stream.\n *\n * @example\n * ```tsx\n * const { data } = useStreamItem<{ id:string; name: string }>({\n * streamName: 'my-stream',\n * groupId: '123',\n * id: '123',\n * })\n *\n * return (\n * <div>{data?.name}</div>\n * )\n * ```\n */\nexport const useStreamItem = <TData>(args?: StreamItemArgs) => {\n const { stream } = useMotiaStream()\n const [data, setData] = useState<TData | null | undefined>()\n const [event, setEvent] = useState<StreamSubscription | null>(null)\n const { streamName, groupId, id } = args || {}\n\n const handleChange = useCallback((data: unknown) => {\n setData(data as TData)\n }, [])\n\n useEffect(() => {\n if (!streamName || !groupId || !id || !stream) return\n\n const subscription = stream.subscribeItem(streamName, groupId, id)\n\n subscription.addChangeListener(handleChange)\n setEvent(subscription)\n\n return () => {\n setData(undefined)\n setEvent(null)\n subscription.close()\n }\n }, [stream, streamName, groupId, id, handleChange])\n\n return { data, event }\n}\n"],"mappings":";;;;;AAOA,MAAa,qBAAqB,MAAM,cAAsC,EAC5E,QAAQ,MACT,CAAC;;;;ACSF,MAAaA,uBAAwC,EAAE,UAAU,SAAS,gBAAgB;CACxF,MAAM,CAAC,QAAQ,aAAa,SAAwB,KAAK;AAEzD,iBAAgB;EACd,MAAM,iBAAiB,IAAIC,SAAO,SAAS,EAAE,WAAW,CAAC;AACzD,YAAU,eAAe;AACzB,eAAa,eAAe,OAAO;IAClC,CAAC,SAAS,UAAU,CAAC;CAExB,MAAM,eAAe,eAAe,EAAE,QAAQ,GAAG,CAAC,OAAO,CAAC;AAE1D,QAAO,oBAAC,mBAAmB;EAAS,OAAO;EAAe;GAAuC;;;;;;;;;;;;;AClBnG,MAAa,uBAAuB;CAClC,MAAM,UAAU,MAAM,WAAW,mBAAmB;AAEpD,KAAI,CAAC,QACH,OAAM,IAAI,MAAM,2DAA2D;AAG7E,QAAO;;;;;;;;;;;;;;;;;;;;ACOT,MAAa,yBACX,EAAE,OAAO,MAAM,YACf,iBACG;AACH,iBAAgB;AACd,MAAI,OAAO;AACT,SAAM,QAAQ,MAAM,SAAS;AAC7B,gBAAa,MAAM,SAAS,MAAM,SAAS;;IAE5C;EAAC;EAAO;EAAM,GAAG;EAAa,CAAC;;;;;;;;;;;;;;;;;;;;;;;;ACJpC,MAAa,kBAAgD,SAAkC;CAC7F,MAAM,EAAE,WAAW,gBAAgB;CACnC,MAAM,CAAC,MAAM,WAAW,SAAkB,EAAE,CAAC;CAC7C,MAAM,kBAAkB,OAAkC,KAAK;CAE/D,MAAM,EAAE,YAAY,SAAS,SAAS,SAAS,oBAAoB,QAAQ,EAAE;AAE7E,iBAAgB;AACd,MAAI,CAAC,cAAc,CAAC,WAAW,CAAC,OAAQ;AAExC,kBAAgB,UAAU,OAAO,eAAe,YAAY,SAAS,QAAQ;AAE7E,kBAAgB,QAAQ,mBAAmB,WAAS;GAClD,MAAM,YAAYC;AAClB,WAAQ,UAAU;AAClB,qBAAkB,UAAU;IAC5B;AAEF,eAAa;AACX,mBAAgB,SAAS,OAAO;AAChC,mBAAgB,UAAU;AAC1B,WAAQ,EAAE,CAAC;;IAEZ;EAAC;EAAQ;EAAY;EAAS;EAAS;EAAgB,CAAC;AAE3D,QAAO;EAAE;EAAM,OAAO,gBAAgB;EAAS;;;;;;;;;;;;;;;;;;;;;AC7BjD,MAAa,iBAAwB,SAA0B;CAC7D,MAAM,EAAE,WAAW,gBAAgB;CACnC,MAAM,CAAC,MAAM,WAAW,UAAoC;CAC5D,MAAM,CAAC,OAAO,YAAY,SAAoC,KAAK;CACnE,MAAM,EAAE,YAAY,SAAS,OAAO,QAAQ,EAAE;CAE9C,MAAM,eAAe,aAAa,WAAkB;AAClD,UAAQC,OAAc;IACrB,EAAE,CAAC;AAEN,iBAAgB;AACd,MAAI,CAAC,cAAc,CAAC,WAAW,CAAC,MAAM,CAAC,OAAQ;EAE/C,MAAM,eAAe,OAAO,cAAc,YAAY,SAAS,GAAG;AAElE,eAAa,kBAAkB,aAAa;AAC5C,WAAS,aAAa;AAEtB,eAAa;AACX,WAAQ,OAAU;AAClB,YAAS,KAAK;AACd,gBAAa,OAAO;;IAErB;EAAC;EAAQ;EAAY;EAAS;EAAI;EAAa,CAAC;AAEnD,QAAO;EAAE;EAAM;EAAO"}
|
package/package.json
CHANGED
|
@@ -1,21 +1,20 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@motiadev/stream-client-react",
|
|
3
3
|
"description": "Motia Stream Client React Package – Responsible for managing streams of data.",
|
|
4
|
-
"version": "0.17.
|
|
4
|
+
"version": "0.17.7-beta.188",
|
|
5
5
|
"license": "Elastic-2.0",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"main": "./dist/index.js",
|
|
8
8
|
"module": "./dist/index.js",
|
|
9
9
|
"types": "./dist/index.d.ts",
|
|
10
10
|
"peerDependencies": {
|
|
11
|
-
"react": "^19.
|
|
11
|
+
"react": "^19.1.0"
|
|
12
12
|
},
|
|
13
13
|
"dependencies": {
|
|
14
|
-
"@motiadev/stream-client-browser": "0.17.
|
|
14
|
+
"@motiadev/stream-client-browser": "0.17.7-beta.188"
|
|
15
15
|
},
|
|
16
16
|
"devDependencies": {
|
|
17
|
-
"@
|
|
18
|
-
"@types/react": "^19.2.7",
|
|
17
|
+
"@types/react": "^19.0.7",
|
|
19
18
|
"tsdown": "^0.16.6",
|
|
20
19
|
"typescript": "^5.7.2"
|
|
21
20
|
},
|
package/src/use-stream-group.ts
CHANGED
|
@@ -36,10 +36,7 @@ export const useStreamGroup = <TData extends { id: string }>(args?: StreamGroupA
|
|
|
36
36
|
const { streamName, groupId, sortKey, setData: setDataCallback } = args || {}
|
|
37
37
|
|
|
38
38
|
useEffect(() => {
|
|
39
|
-
if (!streamName || !groupId || !stream)
|
|
40
|
-
console.error('useStreamGroup: streamName, groupId or stream is not defined', { streamName, groupId, stream })
|
|
41
|
-
return
|
|
42
|
-
}
|
|
39
|
+
if (!streamName || !groupId || !stream) return
|
|
43
40
|
|
|
44
41
|
subscriptionRef.current = stream.subscribeGroup(streamName, groupId, sortKey)
|
|
45
42
|
|
package/tsdown.config.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import pluginBabel from '@rollup/plugin-babel'
|
|
2
1
|
import { defineConfig } from 'tsdown'
|
|
3
2
|
|
|
4
3
|
export default defineConfig({
|
|
@@ -7,7 +6,7 @@ export default defineConfig({
|
|
|
7
6
|
},
|
|
8
7
|
format: 'esm',
|
|
9
8
|
platform: 'browser',
|
|
10
|
-
external: ['@motiadev/stream-client-browser', /^react($|\/)/, /^react-dom($|\/)
|
|
9
|
+
external: ['@motiadev/stream-client-browser', /^react($|\/)/, /^react-dom($|\/)/],
|
|
11
10
|
dts: {
|
|
12
11
|
build: true,
|
|
13
12
|
},
|
|
@@ -16,15 +15,4 @@ export default defineConfig({
|
|
|
16
15
|
exports: {
|
|
17
16
|
devExports: 'development',
|
|
18
17
|
},
|
|
19
|
-
plugins: [
|
|
20
|
-
pluginBabel({
|
|
21
|
-
babelHelpers: 'bundled',
|
|
22
|
-
parserOpts: {
|
|
23
|
-
sourceType: 'module',
|
|
24
|
-
plugins: ['jsx', 'typescript'],
|
|
25
|
-
},
|
|
26
|
-
plugins: ['babel-plugin-react-compiler'],
|
|
27
|
-
extensions: ['.js', '.jsx', '.ts', '.tsx'],
|
|
28
|
-
}),
|
|
29
|
-
],
|
|
30
18
|
})
|