Initial commit with Advoware proxy

This commit is contained in:
root
2025-10-19 14:57:07 +00:00
commit 273aa8b549
45771 changed files with 5534555 additions and 0 deletions

View File

@@ -0,0 +1,21 @@
MIT License
Copyright (c) 2025 Motia
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

View File

@@ -0,0 +1,214 @@
# @motiadev/stream-client-react
Motia Stream Client React Package Responsible for managing streams of data in React applications.
For more information about Motia Streams, please refer to the [Motia Streams documentation](https://motia.dev/docs).
## Overview
`@motiadev/stream-client-react` provides a set of React utilities and hooks for integrating Motia's real-time streaming capabilities into your React applications. It enables you to subscribe to individual stream items, groups, and handle real-time events with idiomatic React patterns.
---
## Installation
```bash
npm install @motiadev/stream-client-react
```
## Exports
- **Stream, StreamItemSubscription, StreamGroupSubscription**
(Re-exported from `@motiadev/stream-client-browser`)
- **MotiaStreamProvider**
React provider for initializing and supplying the stream context.
- **useMotiaStream**
Hook to access the current stream instance from context.
- **useStreamItem**
Hook to subscribe to a single stream item.
- **useStreamGroup**
Hook to subscribe to a group of stream items.
- **useStreamEventHandler**
Hook to attach event listeners to stream subscriptions.
## Usage
### 1. MotiaStreamProvider
Wrap your application (or a subtree) with `MotiaStreamProvider` to initialize the stream and provide it via context.
```tsx
import { MotiaStreamProvider } from '@motiadev/stream-client-react'
const App = () => {
return (
<MotiaStreamProvider address="wss://your-stream-server">
<App />
</MotiaStreamProvider>
)
}
```
**Props:**
- `address` (string): The WebSocket address of your Motia stream server.
---
### 2. useMotiaStream
Access the current stream instance anywhere within the provider.
```tsx
import { useMotiaStream } from '@motiadev/stream-client-react'
const { stream } = useMotiaStream()
```
---
### 3. useStreamItem
Subscribe to a single item in a stream and receive real-time updates.
```tsx
import { useStreamItem } from '@motiadev/stream-client-react'
const { data, event } = useStreamItem<{ name: string }>({
/**
* The stream name from motia Server
*/
streamName: 'users',
/**
* The id of the item to subscribe to
*/
id: 'user-123',
})
```
- `data`: The current value of the item (typed).
- `event`: The subscription object to subscribe to custom events. Check `useStreamEventHandler` for more information.
---
### 4. useStreamGroup
Subscribe to a group of items in a stream.
```tsx
import { useStreamGroup } from '@motiadev/stream-client-react'
const { data, event } = useStreamGroup<{ name: string }>({
/**
* The stream name from motia Server
*/
streamName: 'users',
/**
* The group id to subscribe to
*/
groupId: 'admins',
})
```
- `data`: Array of current group items.
- `event`: The group subscription object.
---
### 5. useStreamEventHandler
Attach custom event listeners to a stream subscription.
```tsx
import { useStreamEventHandler } from '@motiadev/stream-client-react'
useStreamEventHandler(
{
event, // from useStreamItem or useStreamGroup
type: 'custom-event',
listener: (eventData) => {
// handle event
},
},
[
/* dependencies */
],
)
```
---
## API Reference
### MotiaStreamProvider
- **Props:**
- `address: string` WebSocket address for the stream server.
- `children: React.ReactNode`
### useMotiaStream
- Returns `{ stream }` from context.
### useStreamItem<TData>
- **Args:** `{ streamName: string, id: string }`
- **Returns:** `{ data: TData | null, event: StreamSubscription | null }`
### useStreamGroup<TData>
- **Args:** `{ streamName: string, groupId: string }`
- **Returns:** `{ data: TData[], event: StreamSubscription | null }`
### useStreamEventHandler
- **Args:**
- `{ event, type, listener }`
- `dependencies` (array): React dependency list
---
## Example
```tsx
import { MotiaStreamProvider, useStreamItem, useStreamEventHandler } from '@motiadev/stream-client-react'
function UserComponent({ userId }) {
const { data, event } = useStreamItem<{ name: string }>({
streamName: 'users',
id: userId,
})
useStreamEventHandler(
{
event,
type: 'user-updated',
listener: (e) => alert('User updated!'),
},
[event],
)
if (!data) return <div>Loading...</div>
return <div>{data.name}</div>
}
export default function App() {
return (
<MotiaStreamProvider address="wss://your-stream-server">
<UserComponent userId="user-123" />
</MotiaStreamProvider>
)
}
```
---
## Notes
- All hooks must be used within a `MotiaStreamProvider`.
- The library is designed to work seamlessly with Motia's event-driven architecture.
- For advanced stream management, refer to the [@motiadev/stream-client-browser](https://www.npmjs.com/package/@motiadev/stream-client-browser) documentation.
## License
MIT

View File

@@ -0,0 +1,8 @@
export { Stream } from '@motiadev/stream-client-browser';
export { StreamItemSubscription } from '@motiadev/stream-client-browser';
export { StreamGroupSubscription } from '@motiadev/stream-client-browser';
export { MotiaStreamProvider } from './src/motia-stream-provider';
export { useMotiaStream } from './src/use-motia-stream';
export { useStreamItem, type StreamItemArgs } from './src/use-stream-item';
export { useStreamGroup, type StreamGroupArgs } from './src/use-stream-group';
export { useStreamEventHandler } from './src/use-stream-event-handler';

View File

@@ -0,0 +1,8 @@
export { Stream } from '@motiadev/stream-client-browser';
export { StreamItemSubscription } from '@motiadev/stream-client-browser';
export { StreamGroupSubscription } from '@motiadev/stream-client-browser';
export { MotiaStreamProvider } from './src/motia-stream-provider';
export { useMotiaStream } from './src/use-motia-stream';
export { useStreamItem } from './src/use-stream-item';
export { useStreamGroup } from './src/use-stream-group';
export { useStreamEventHandler } from './src/use-stream-event-handler';

View File

@@ -0,0 +1,7 @@
import React from 'react';
import { Stream } from '@motiadev/stream-client-browser';
type MotiaStreamContextType = {
stream: Stream | null;
};
export declare const MotiaStreamContext: React.Context<MotiaStreamContextType>;
export {};

View File

@@ -0,0 +1,4 @@
import React from 'react';
export const MotiaStreamContext = React.createContext({
stream: null,
});

View File

@@ -0,0 +1,15 @@
import React from 'react';
type Props = React.PropsWithChildren<{
/**
* The address of the stream server.
*
* @example
* ```tsx
* <MotiaStreamProvider address="ws://localhost:3000">
* <App />
* </MotiaStreamProvider>
*/
address: string;
}>;
export declare const MotiaStreamProvider: React.FC<Props>;
export {};

View File

@@ -0,0 +1,13 @@
import { jsx as _jsx } from "react/jsx-runtime";
import { Stream } from '@motiadev/stream-client-browser';
import { useEffect, useState } from 'react';
import { MotiaStreamContext } from './motia-stream-context';
export const MotiaStreamProvider = ({ children, address }) => {
const [stream, setStream] = useState(null);
useEffect(() => {
const stream = new Stream(address);
setStream(stream);
return () => stream.close();
}, [address]);
return _jsx(MotiaStreamContext.Provider, { value: { stream }, children: children });
};

View File

@@ -0,0 +1,11 @@
/**
* A hook to get the stream context.
*
* @example
* ```tsx
* const { stream } = useMotiaStream()
* ```
*/
export declare const useMotiaStream: () => {
stream: import("@motiadev/stream-client-browser").Stream | null;
};

View File

@@ -0,0 +1,17 @@
import React from 'react';
import { MotiaStreamContext } from './motia-stream-context';
/**
* A hook to get the stream context.
*
* @example
* ```tsx
* const { stream } = useMotiaStream()
* ```
*/
export const useMotiaStream = () => {
const context = React.useContext(MotiaStreamContext);
if (!context) {
throw new Error('useMotiaStream must be used within a MotiaStreamProvider');
}
return context;
};

View File

@@ -0,0 +1,24 @@
import { StreamSubscription } from '@motiadev/stream-client-browser';
import { DependencyList } from 'react';
type UseStreamEventHandler = {
event: StreamSubscription | null;
type: string;
listener: (event: any) => void;
};
/**
* A hook to handle custom stream events.
*
* @example
* ```tsx
* const { event } = useStreamItem({ streamName: 'my-stream', id: '123' })
*
* const onEventHandled = (event: any) => {
* // this is going to be called whenever 'on-custom-event' is sent from the server
* console.log(event)
* }
*
* useStreamEventHandler({ event, type: 'on-custom-event', listener: onEventHandled }, [])
* ```
*/
export declare const useStreamEventHandler: ({ event, type, listener }: UseStreamEventHandler, dependencies: DependencyList) => void;
export {};

View File

@@ -0,0 +1,24 @@
import { useEffect } from 'react';
/**
* A hook to handle custom stream events.
*
* @example
* ```tsx
* const { event } = useStreamItem({ streamName: 'my-stream', id: '123' })
*
* const onEventHandled = (event: any) => {
* // this is going to be called whenever 'on-custom-event' is sent from the server
* console.log(event)
* }
*
* useStreamEventHandler({ event, type: 'on-custom-event', listener: onEventHandled }, [])
* ```
*/
export const useStreamEventHandler = ({ event, type, listener }, dependencies) => {
useEffect(() => {
if (event) {
event.onEvent(type, listener);
return () => event.offEvent(type, listener);
}
}, [event, type, ...dependencies]);
};

View File

@@ -0,0 +1,33 @@
import { StreamSubscription } from '@motiadev/stream-client-browser';
export type StreamGroupArgs<TData extends {
id: string;
}> = {
streamName: string;
groupId: string;
sortKey?: keyof TData;
};
/**
* A hook to get a group of items from a stream.
*
* @example
* ```tsx
* const { data } = useStreamGroup<{ id:string; name: string }>({
* streamName: 'my-stream',
* groupId: '123',
* })
*
* return (
* <div>
* {data.map((item) => (
* <div key={item.id}>{item.name}</div>
* ))}
* </div>
* )
* ```
*/
export declare const useStreamGroup: <TData extends {
id: string;
}>(args?: StreamGroupArgs<TData>) => {
data: TData[];
event: StreamSubscription<unknown, unknown> | null;
};

View File

@@ -0,0 +1,39 @@
import { useEffect, useState } from 'react';
import { useMotiaStream } from './use-motia-stream';
/**
* A hook to get a group of items from a stream.
*
* @example
* ```tsx
* const { data } = useStreamGroup<{ id:string; name: string }>({
* streamName: 'my-stream',
* groupId: '123',
* })
*
* return (
* <div>
* {data.map((item) => (
* <div key={item.id}>{item.name}</div>
* ))}
* </div>
* )
* ```
*/
export const useStreamGroup = (args) => {
const { stream } = useMotiaStream();
const [data, setData] = useState([]);
const [event, setEvent] = useState(null);
useEffect(() => {
if (!args?.streamName || !args?.groupId || !stream)
return;
const subscription = stream.subscribeGroup(args.streamName, args.groupId, args.sortKey);
subscription.addChangeListener((data) => setData(data));
setEvent(subscription);
return () => {
setData([]);
setEvent(null);
subscription.close();
};
}, [stream, args?.streamName, args?.groupId, args?.sortKey]);
return { data, event };
};

View File

@@ -0,0 +1,26 @@
import { StreamSubscription } from '@motiadev/stream-client-browser';
export type StreamItemArgs = {
streamName: string;
groupId: string;
id?: string;
};
/**
* A hook to get a single item from a stream.
*
* @example
* ```tsx
* const { data } = useStreamItem<{ id:string; name: string }>({
* streamName: 'my-stream',
* groupId: '123',
* id: '123',
* })
*
* return (
* <div>{data?.name}</div>
* )
* ```
*/
export declare const useStreamItem: <TData>(args?: StreamItemArgs) => {
data: TData | null;
event: StreamSubscription<unknown, unknown> | null;
};

View File

@@ -0,0 +1,36 @@
import { useEffect, useState } from 'react';
import { useMotiaStream } from './use-motia-stream';
/**
* A hook to get a single item from a stream.
*
* @example
* ```tsx
* const { data } = useStreamItem<{ id:string; name: string }>({
* streamName: 'my-stream',
* groupId: '123',
* id: '123',
* })
*
* return (
* <div>{data?.name}</div>
* )
* ```
*/
export const useStreamItem = (args) => {
const { stream } = useMotiaStream();
const [data, setData] = useState(null);
const [event, setEvent] = useState(null);
useEffect(() => {
if (!args?.streamName || !args?.groupId || !args?.id || !stream)
return;
const subscription = stream.subscribeItem(args.streamName, args.groupId, args.id);
subscription.addChangeListener((data) => setData(data));
setEvent(subscription);
return () => {
setData(null);
setEvent(null);
subscription.close();
};
}, [stream, args?.streamName, args?.groupId, args?.id]);
return { data, event };
};

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,8 @@
export { Stream } from '@motiadev/stream-client-browser'
export { StreamItemSubscription } from '@motiadev/stream-client-browser'
export { StreamGroupSubscription } from '@motiadev/stream-client-browser'
export { MotiaStreamProvider } from './src/motia-stream-provider'
export { useMotiaStream } from './src/use-motia-stream'
export { useStreamItem, type StreamItemArgs } from './src/use-stream-item'
export { useStreamGroup, type StreamGroupArgs } from './src/use-stream-group'
export { useStreamEventHandler } from './src/use-stream-event-handler'

View File

@@ -0,0 +1,22 @@
{
"name": "@motiadev/stream-client-react",
"description": "Motia Stream Client React Package Responsible for managing streams of data.",
"version": "0.8.2-beta.139",
"license": "MIT",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"peerDependencies": {
"react": "^19.1.0"
},
"dependencies": {
"@motiadev/stream-client-browser": "0.8.2-beta.139"
},
"devDependencies": {
"@types/react": "^19.0.7",
"typescript": "^5.7.2"
},
"scripts": {
"build": "rm -rf dist && tsc",
"lint": "eslint --config ../../eslint.config.js"
}
}

View File

@@ -0,0 +1,10 @@
import React from 'react'
import { Stream } from '@motiadev/stream-client-browser'
type MotiaStreamContextType = {
stream: Stream | null
}
export const MotiaStreamContext = React.createContext<MotiaStreamContextType>({
stream: null,
})

View File

@@ -0,0 +1,29 @@
import { Stream } from '@motiadev/stream-client-browser'
import React, { useEffect, useState } from 'react'
import { MotiaStreamContext } from './motia-stream-context'
type Props = React.PropsWithChildren<{
/**
* The address of the stream server.
*
* @example
* ```tsx
* <MotiaStreamProvider address="ws://localhost:3000">
* <App />
* </MotiaStreamProvider>
*/
address: string
}>
export const MotiaStreamProvider: React.FC<Props> = ({ children, address }) => {
const [stream, setStream] = useState<Stream | null>(null)
useEffect(() => {
const stream = new Stream(address)
setStream(stream)
return () => stream.close()
}, [address])
return <MotiaStreamContext.Provider value={{ stream }}>{children}</MotiaStreamContext.Provider>
}

View File

@@ -0,0 +1,20 @@
import React from 'react'
import { MotiaStreamContext } from './motia-stream-context'
/**
* A hook to get the stream context.
*
* @example
* ```tsx
* const { stream } = useMotiaStream()
* ```
*/
export const useMotiaStream = () => {
const context = React.useContext(MotiaStreamContext)
if (!context) {
throw new Error('useMotiaStream must be used within a MotiaStreamProvider')
}
return context
}

View File

@@ -0,0 +1,36 @@
import { StreamSubscription } from '@motiadev/stream-client-browser'
import { DependencyList, useEffect } from 'react'
type UseStreamEventHandler = {
event: StreamSubscription | null
type: string
// eslint-disable-next-line @typescript-eslint/no-explicit-any
listener: (event: any) => void
}
/**
* A hook to handle custom stream events.
*
* @example
* ```tsx
* const { event } = useStreamItem({ streamName: 'my-stream', id: '123' })
*
* const onEventHandled = (event: any) => {
* // this is going to be called whenever 'on-custom-event' is sent from the server
* console.log(event)
* }
*
* useStreamEventHandler({ event, type: 'on-custom-event', listener: onEventHandled }, [])
* ```
*/
export const useStreamEventHandler = (
{ event, type, listener }: UseStreamEventHandler,
dependencies: DependencyList,
) => {
useEffect(() => {
if (event) {
event.onEvent(type, listener)
return () => event.offEvent(type, listener)
}
}, [event, type, ...dependencies])
}

View File

@@ -0,0 +1,51 @@
import { useEffect, useState } from 'react'
import { useMotiaStream } from './use-motia-stream'
import { StreamSubscription } from '@motiadev/stream-client-browser'
export type StreamGroupArgs<TData extends { id: string }> = {
streamName: string
groupId: string
sortKey?: keyof TData
}
/**
* A hook to get a group of items from a stream.
*
* @example
* ```tsx
* const { data } = useStreamGroup<{ id:string; name: string }>({
* streamName: 'my-stream',
* groupId: '123',
* })
*
* return (
* <div>
* {data.map((item) => (
* <div key={item.id}>{item.name}</div>
* ))}
* </div>
* )
* ```
*/
export const useStreamGroup = <TData extends { id: string }>(args?: StreamGroupArgs<TData>) => {
const { stream } = useMotiaStream()
const [data, setData] = useState<TData[]>([])
const [event, setEvent] = useState<StreamSubscription | null>(null)
useEffect(() => {
if (!args?.streamName || !args?.groupId || !stream) return
const subscription = stream.subscribeGroup(args.streamName, args.groupId, args.sortKey)
subscription.addChangeListener((data) => setData(data as TData[]))
setEvent(subscription)
return () => {
setData([])
setEvent(null)
subscription.close()
}
}, [stream, args?.streamName, args?.groupId, args?.sortKey])
return { data, event }
}

View File

@@ -0,0 +1,48 @@
import { StreamSubscription } from '@motiadev/stream-client-browser'
import { useEffect, useState } from 'react'
import { useMotiaStream } from './use-motia-stream'
export type StreamItemArgs = {
streamName: string
groupId: string
id?: string
}
/**
* A hook to get a single item from a stream.
*
* @example
* ```tsx
* const { data } = useStreamItem<{ id:string; name: string }>({
* streamName: 'my-stream',
* groupId: '123',
* id: '123',
* })
*
* return (
* <div>{data?.name}</div>
* )
* ```
*/
export const useStreamItem = <TData>(args?: StreamItemArgs) => {
const { stream } = useMotiaStream()
const [data, setData] = useState<TData | null>(null)
const [event, setEvent] = useState<StreamSubscription | null>(null)
useEffect(() => {
if (!args?.streamName || !args?.groupId || !args?.id || !stream) return
const subscription = stream.subscribeItem(args.streamName, args.groupId, args.id)
subscription.addChangeListener((data) => setData(data as TData))
setEvent(subscription)
return () => {
setData(null)
setEvent(null)
subscription.close()
}
}, [stream, args?.streamName, args?.groupId, args?.id])
return { data, event }
}

View File

@@ -0,0 +1,31 @@
{
"compilerOptions": {
"baseUrl": ".",
"paths": {
"@/*": [
"./src/*"
]
},
"target": "ES2020",
"useDefineForClassFields": true,
"lib": ["ES2020", "DOM", "DOM.Iterable"],
"module": "ESNext",
"moduleResolution": "bundler",
"skipLibCheck": true,
"esModuleInterop": true,
"composite": true,
"declaration": true,
"isolatedModules": true,
"moduleDetection": "force",
"jsx": "react-jsx",
"outDir": "dist",
/* Linting */
"strict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noFallthroughCasesInSwitch": true
},
"include": ["src/**/*.ts", "src/**/*.tsx", "index.ts"]
}