@motiadev/stream-client-react 0.15.6-beta.174 → 0.16.0-beta.175
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.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +13 -13
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
- package/src/use-stream-group.ts +14 -14
package/dist/index.d.ts
CHANGED
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","names":[],"sources":["../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":[],"mappings":";;;;;KAIK,KAAA,GAAQ,KAAA,CAAM;;;;;;AAcnB;;;;ECPa,OAAA,EAAA,MAAA;;;cDOA,qBAAqB,KAAA,CAAM,GAAG;;;;;;;;;AAA3C;;cCPa;UAQZ,gCAAA,CAAA,MAAA;AARD,CAAA;;;KCRK,qBAAA;SACI;;EFAJ,QAAK,EAAA,CAAA,KAAA,EAAA,GAAG,EAAA,GAAM,IAAA;AAcnB,CAAA;;;;ACPA;;;;ACVsD;AAwBtD;;;;;;;cAAa;;;;GACgB,qCACb;;;KCvBJ;;;;EHAP,OAAA,EAAK,MAAA;EAcG,OAAA,CAAA,EAAA,MGXK,KHWL;;;;
|
|
1
|
+
{"version":3,"file":"index.d.ts","names":[],"sources":["../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":[],"mappings":";;;;;KAIK,KAAA,GAAQ,KAAA,CAAM;;;;;;AAcnB;;;;ECPa,OAAA,EAAA,MAAA;;;cDOA,qBAAqB,KAAA,CAAM,GAAG;;;;;;;;;AAA3C;;cCPa;UAQZ,gCAAA,CAAA,MAAA;AARD,CAAA;;;KCRK,qBAAA;SACI;;EFAJ,QAAK,EAAA,CAAA,KAAA,EAAA,GAAG,EAAA,GAAM,IAAA;AAcnB,CAAA;;;;ACPA;;;;ACVsD;AAwBtD;;;;;;;cAAa;;;;GACgB,qCACb;;;KCvBJ;;;;EHAP,OAAA,EAAK,MAAA;EAcG,OAAA,CAAA,EAAA,MGXK,KHWL;mBGVM;;;AFGnB;;;;ACVsD;AAwBtD;;;;;;;;;;ACrBA;AA0BA;;AAAoE,cAAvD,cAAuD,EAAA,CAAA,cAAA;;UAAA,gBAAgB;;;;;;KC1BxE,cAAA;;;;;AJcZ;;;;ACPA;;;;ACVsD;AAwBtD;;;;;;;cECa,8BAA+B;;;ADtB5C,CAAA"}
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Stream, Stream as Stream$1, StreamGroupSubscription, StreamItemSubscription } from "@motiadev/stream-client-browser";
|
|
2
|
-
import React, { useCallback, useEffect, useMemo, useState } from "react";
|
|
2
|
+
import React, { useCallback, useEffect, useMemo, useRef, useState } from "react";
|
|
3
3
|
import { jsx } from "react/jsx-runtime";
|
|
4
4
|
|
|
5
5
|
//#region src/motia-stream-context.ts
|
|
@@ -91,31 +91,31 @@ const useStreamEventHandler = ({ event, type, listener }, dependencies) => {
|
|
|
91
91
|
const useStreamGroup = (args) => {
|
|
92
92
|
const { stream } = useMotiaStream();
|
|
93
93
|
const [data, setData] = useState([]);
|
|
94
|
-
const
|
|
95
|
-
const { streamName, groupId, sortKey } = args || {};
|
|
96
|
-
const handleChange = useCallback((data$1) => {
|
|
97
|
-
setData(data$1);
|
|
98
|
-
}, []);
|
|
94
|
+
const subscriptionRef = useRef(null);
|
|
95
|
+
const { streamName, groupId, sortKey, setData: setDataCallback } = args || {};
|
|
99
96
|
useEffect(() => {
|
|
100
97
|
if (!streamName || !groupId || !stream) return;
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
98
|
+
subscriptionRef.current = stream.subscribeGroup(streamName, groupId, sortKey);
|
|
99
|
+
subscriptionRef.current.addChangeListener((data$1) => {
|
|
100
|
+
const typedData = data$1;
|
|
101
|
+
setData(typedData);
|
|
102
|
+
setDataCallback?.(typedData);
|
|
103
|
+
});
|
|
104
104
|
return () => {
|
|
105
|
+
subscriptionRef.current?.close();
|
|
106
|
+
subscriptionRef.current = null;
|
|
105
107
|
setData([]);
|
|
106
|
-
setEvent(null);
|
|
107
|
-
subscription.close();
|
|
108
108
|
};
|
|
109
109
|
}, [
|
|
110
110
|
stream,
|
|
111
111
|
streamName,
|
|
112
112
|
groupId,
|
|
113
113
|
sortKey,
|
|
114
|
-
|
|
114
|
+
setDataCallback
|
|
115
115
|
]);
|
|
116
116
|
return {
|
|
117
117
|
data,
|
|
118
|
-
event
|
|
118
|
+
event: subscriptionRef.current
|
|
119
119
|
};
|
|
120
120
|
};
|
|
121
121
|
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
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 {
|
|
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,S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description": "Motia Stream Client React Package – Responsible for managing streams of data.",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.16.0-beta.175",
|
|
5
5
|
"license": "Elastic-2.0",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"main": "./dist/index.js",
|
|
@@ -11,7 +11,7 @@
|
|
|
11
11
|
"react": "^19.1.0"
|
|
12
12
|
},
|
|
13
13
|
"dependencies": {
|
|
14
|
-
"@motiadev/stream-client-browser": "0.
|
|
14
|
+
"@motiadev/stream-client-browser": "0.16.0-beta.175"
|
|
15
15
|
},
|
|
16
16
|
"devDependencies": {
|
|
17
17
|
"@types/react": "^19.0.7",
|
package/src/use-stream-group.ts
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
import type { StreamSubscription } from '@motiadev/stream-client-browser'
|
|
2
|
-
import {
|
|
2
|
+
import { useEffect, useRef, useState } from 'react'
|
|
3
3
|
import { useMotiaStream } from './use-motia-stream'
|
|
4
4
|
|
|
5
5
|
export type StreamGroupArgs<TData extends { id: string }> = {
|
|
6
6
|
streamName: string
|
|
7
7
|
groupId: string
|
|
8
8
|
sortKey?: keyof TData
|
|
9
|
+
setData?: (data: TData[]) => void
|
|
9
10
|
}
|
|
10
11
|
|
|
11
12
|
/**
|
|
@@ -30,28 +31,27 @@ export type StreamGroupArgs<TData extends { id: string }> = {
|
|
|
30
31
|
export const useStreamGroup = <TData extends { id: string }>(args?: StreamGroupArgs<TData>) => {
|
|
31
32
|
const { stream } = useMotiaStream()
|
|
32
33
|
const [data, setData] = useState<TData[]>([])
|
|
33
|
-
const
|
|
34
|
+
const subscriptionRef = useRef<StreamSubscription | null>(null)
|
|
34
35
|
|
|
35
|
-
const { streamName, groupId, sortKey } = args || {}
|
|
36
|
-
|
|
37
|
-
const handleChange = useCallback((data: unknown) => {
|
|
38
|
-
setData(data as TData[])
|
|
39
|
-
}, [])
|
|
36
|
+
const { streamName, groupId, sortKey, setData: setDataCallback } = args || {}
|
|
40
37
|
|
|
41
38
|
useEffect(() => {
|
|
42
39
|
if (!streamName || !groupId || !stream) return
|
|
43
40
|
|
|
44
|
-
|
|
41
|
+
subscriptionRef.current = stream.subscribeGroup(streamName, groupId, sortKey)
|
|
45
42
|
|
|
46
|
-
|
|
47
|
-
|
|
43
|
+
subscriptionRef.current.addChangeListener((data) => {
|
|
44
|
+
const typedData = data as TData[]
|
|
45
|
+
setData(typedData)
|
|
46
|
+
setDataCallback?.(typedData)
|
|
47
|
+
})
|
|
48
48
|
|
|
49
49
|
return () => {
|
|
50
|
+
subscriptionRef.current?.close()
|
|
51
|
+
subscriptionRef.current = null
|
|
50
52
|
setData([])
|
|
51
|
-
setEvent(null)
|
|
52
|
-
subscription.close()
|
|
53
53
|
}
|
|
54
|
-
}, [stream, streamName, groupId, sortKey,
|
|
54
|
+
}, [stream, streamName, groupId, sortKey, setDataCallback])
|
|
55
55
|
|
|
56
|
-
return { data, event }
|
|
56
|
+
return { data, event: subscriptionRef.current }
|
|
57
57
|
}
|