@motiadev/stream-client-browser 0.2.1-beta.49 → 0.2.1-beta.51

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 ADDED
@@ -0,0 +1,185 @@
1
+ # @motiadev/stream-client-browser
2
+
3
+ Motia Stream Client Package – Responsible for managing real-time [Motia](https://motia.dev) streams of data in browser environments. This package provides a simple, type-safe interface for subscribing to item and group streams over WebSockets, handling live updates, and managing stream state in your web applications.
4
+
5
+ ## Features
6
+
7
+ - **WebSocket-based streaming** for real-time data updates
8
+ - **Item and group subscriptions** with automatic state management
9
+ - **Change listeners** for reactive UI updates
10
+ - **Custom event handling** for extensibility
11
+ - **TypeScript support** for strong typing and autocompletion
12
+
13
+ ---
14
+
15
+ ## Installation
16
+
17
+ ```bash
18
+ npm install @motiadev/stream-client-browser
19
+ ```
20
+
21
+ ---
22
+
23
+ ## Usage
24
+
25
+ ### 1. Creating a Stream Connection
26
+
27
+ ```typescript
28
+ import { Stream } from '@motiadev/stream-client-browser'
29
+
30
+ const stream = new Stream('wss://your-stream-server', () => {
31
+ console.log('WebSocket connection established!')
32
+ })
33
+ ```
34
+
35
+ ### 2. Subscribing to an Item Stream
36
+
37
+ ```typescript
38
+ const itemSubscription = stream.subscribeItem<{ id: string; name: string }>('users', 'user-123')
39
+
40
+ itemSubscription.addChangeListener((user) => {
41
+ console.log('User updated:', user)
42
+ })
43
+
44
+ // Listen for custom events
45
+ itemSubscription.onEvent('custom-event', (data) => {
46
+ console.log('Received custom event:', data)
47
+ })
48
+ ```
49
+
50
+ ### 3. Subscribing to a Group Stream
51
+
52
+ ```typescript
53
+ const groupSubscription = stream.subscribeGroup<{ id: string; name: string }>('users', 'group-abc')
54
+
55
+ groupSubscription.addChangeListener((users) => {
56
+ console.log('Group users updated:', users)
57
+ })
58
+ ```
59
+
60
+ ### 4. Cleaning Up
61
+
62
+ ```typescript
63
+ itemSubscription.close()
64
+ groupSubscription.close()
65
+ stream.close()
66
+ ```
67
+
68
+ ---
69
+
70
+ ## API Reference
71
+
72
+ ### `Stream`
73
+
74
+ - **constructor(address: string, onReady: () => void)**
75
+
76
+ - Establishes a WebSocket connection to the given address.
77
+ - Calls `onReady` when the connection is open.
78
+
79
+ - **subscribeItem<T>(streamName: string, id: string): StreamItemSubscription<T>**
80
+
81
+ - Subscribes to a single item in a stream.
82
+ - Returns a `StreamItemSubscription` instance.
83
+
84
+ - **subscribeGroup<T>(streamName: string, groupId: string): StreamGroupSubscription<T>**
85
+
86
+ - Subscribes to a group of items in a stream.
87
+ - Returns a `StreamGroupSubscription` instance.
88
+
89
+ - **close()**
90
+ - Closes the WebSocket connection.
91
+
92
+ ---
93
+
94
+ ### `StreamItemSubscription<T>`
95
+
96
+ - **addChangeListener(listener: (item: T | null) => void)**
97
+
98
+ - Registers a callback for when the item changes.
99
+
100
+ - **removeChangeListener(listener)**
101
+
102
+ - Unregisters a change listener.
103
+
104
+ - **onEvent(type: string, listener: (event: any) => void)**
105
+
106
+ - Registers a custom event listener.
107
+
108
+ - **offEvent(type: string, listener)**
109
+
110
+ - Unregisters a custom event listener.
111
+
112
+ - **getState(): T | null**
113
+
114
+ - Returns the current state of the item.
115
+
116
+ - **close()**
117
+ - Unsubscribes from the item stream and cleans up listeners.
118
+
119
+ ---
120
+
121
+ ### `StreamGroupSubscription<T>`
122
+
123
+ - **addChangeListener(listener: (items: T[]) => void)**
124
+
125
+ - Registers a callback for when the group changes.
126
+
127
+ - **removeChangeListener(listener)**
128
+
129
+ - Unregisters a change listener.
130
+
131
+ - **onEvent(type: string, listener: (event: any) => void)**
132
+
133
+ - Registers a custom event listener.
134
+
135
+ - **offEvent(type: string, listener: (event: any) => void)**
136
+
137
+ - Unregisters a custom event listener.
138
+
139
+ - **getState(): T[]**
140
+
141
+ - Returns the current state of the group.
142
+
143
+ - **close()**
144
+ - Unsubscribes from the group stream and cleans up listeners.
145
+
146
+ ---
147
+
148
+ ### `StreamSubscription` (Base Class)
149
+
150
+ - **onEvent(type: string, listener: (event: any) => void)**
151
+ - **offEvent(type: string, listener: (event: any) => void)**
152
+
153
+ ---
154
+
155
+ ## Types
156
+
157
+ All types are exported from `stream.types.ts` for advanced usage and type safety.
158
+
159
+ ---
160
+
161
+ ## Example
162
+
163
+ ```typescript
164
+ import { Stream } from '@motiadev/stream-client-browser'
165
+
166
+ const stream = new Stream('wss://example.com', () => {
167
+ const userSub = stream.subscribeItem<{ id: string; name: string }>('users', 'user-1')
168
+
169
+ userSub.addChangeListener((user) => {
170
+ // React to user changes
171
+ })
172
+
173
+ const groupSub = stream.subscribeGroup<{ id: string; name: string }>('users', 'group-1')
174
+
175
+ groupSub.addChangeListener((users) => {
176
+ // React to group changes
177
+ })
178
+ })
179
+ ```
180
+
181
+ ---
182
+
183
+ ## License
184
+
185
+ MIT
@@ -9,8 +9,20 @@ export declare class StreamGroupSubscription<TData extends {
9
9
  private listeners;
10
10
  private state;
11
11
  constructor(ws: WebSocket, sub: GroupJoinMessage);
12
+ /**
13
+ * Close the subscription.
14
+ */
12
15
  close(): void;
16
+ /**
17
+ * Add a change listener. This listener will be called whenever the state of the group changes.
18
+ */
13
19
  addChangeListener(listener: Listener<TData[]>): void;
20
+ /**
21
+ * Remove a change listener.
22
+ */
14
23
  removeChangeListener(listener: Listener<TData[]>): void;
24
+ /**
25
+ * Get the current state of the group.
26
+ */
15
27
  getState(): TData[];
16
28
  }
@@ -41,17 +41,29 @@ export class StreamGroupSubscription extends StreamSubscription {
41
41
  this.listeners.add(listenerWrapper);
42
42
  ws.send(JSON.stringify(message));
43
43
  }
44
+ /**
45
+ * Close the subscription.
46
+ */
44
47
  close() {
45
48
  const message = { type: 'leave', data: this.sub };
46
49
  this.ws.send(JSON.stringify(message));
47
50
  this.listeners.forEach((listener) => this.ws.removeEventListener('message', listener));
48
51
  }
52
+ /**
53
+ * Add a change listener. This listener will be called whenever the state of the group changes.
54
+ */
49
55
  addChangeListener(listener) {
50
56
  this.onChangeListeners.add(listener);
51
57
  }
58
+ /**
59
+ * Remove a change listener.
60
+ */
52
61
  removeChangeListener(listener) {
53
62
  this.onChangeListeners.delete(listener);
54
63
  }
64
+ /**
65
+ * Get the current state of the group.
66
+ */
55
67
  getState() {
56
68
  return this.state;
57
69
  }
@@ -9,8 +9,20 @@ export declare class StreamItemSubscription<TData extends {
9
9
  private listeners;
10
10
  private state;
11
11
  constructor(ws: WebSocket, sub: ItemJoinMessage);
12
+ /**
13
+ * Close the subscription.
14
+ */
12
15
  close(): void;
16
+ /**
17
+ * Add a change listener. This listener will be called whenever the state of the item changes.
18
+ */
13
19
  addChangeListener(listener: Listener<TData>): void;
20
+ /**
21
+ * Remove a change listener.
22
+ */
14
23
  removeChangeListener(listener: Listener<TData>): void;
24
+ /**
25
+ * Get the current state of the item.
26
+ */
15
27
  getState(): TData | null;
16
28
  }
@@ -29,17 +29,29 @@ export class StreamItemSubscription extends StreamSubscription {
29
29
  this.listeners.add(listenerWrapper);
30
30
  ws.send(JSON.stringify(message));
31
31
  }
32
+ /**
33
+ * Close the subscription.
34
+ */
32
35
  close() {
33
36
  const message = { type: 'leave', data: this.sub };
34
37
  this.ws.send(JSON.stringify(message));
35
38
  this.listeners.forEach((listener) => this.ws.removeEventListener('message', listener));
36
39
  }
40
+ /**
41
+ * Add a change listener. This listener will be called whenever the state of the item changes.
42
+ */
37
43
  addChangeListener(listener) {
38
44
  this.onChangeListeners.add(listener);
39
45
  }
46
+ /**
47
+ * Remove a change listener.
48
+ */
40
49
  removeChangeListener(listener) {
41
50
  this.onChangeListeners.delete(listener);
42
51
  }
52
+ /**
53
+ * Get the current state of the item.
54
+ */
43
55
  getState() {
44
56
  return this.state;
45
57
  }
@@ -3,7 +3,13 @@ type CustomEventListener = (event: any) => void;
3
3
  export declare abstract class StreamSubscription {
4
4
  private customEventListeners;
5
5
  protected onEventReceived(event: CustomEvent): void;
6
+ /**
7
+ * Add a custom event listener. This listener will be called whenever the custom event is received.
8
+ */
6
9
  onEvent(type: string, listener: CustomEventListener): void;
10
+ /**
11
+ * Remove a custom event listener.
12
+ */
7
13
  offEvent(type: string, listener: CustomEventListener): void;
8
14
  }
9
15
  export {};
@@ -9,10 +9,16 @@ export class StreamSubscription {
9
9
  customEventListeners.forEach((listener) => listener(eventData));
10
10
  }
11
11
  }
12
+ /**
13
+ * Add a custom event listener. This listener will be called whenever the custom event is received.
14
+ */
12
15
  onEvent(type, listener) {
13
16
  const listeners = this.customEventListeners.get(type) || [];
14
17
  this.customEventListeners.set(type, [...listeners, listener]);
15
18
  }
19
+ /**
20
+ * Remove a custom event listener.
21
+ */
16
22
  offEvent(type, listener) {
17
23
  const listeners = this.customEventListeners.get(type) || [];
18
24
  this.customEventListeners.set(type, listeners.filter((l) => l !== listener));
@@ -3,9 +3,21 @@ import { StreamItemSubscription } from './stream-item';
3
3
  export declare class Stream {
4
4
  private ws;
5
5
  constructor(address: string, onReady: () => void);
6
+ /**
7
+ * Subscribe to an item in a stream.
8
+ *
9
+ * @argument streamName - The name of the stream to subscribe to.
10
+ * @argument id - The id of the item to subscribe to.
11
+ */
6
12
  subscribeItem<TData extends {
7
13
  id: string;
8
14
  }>(streamName: string, id: string): StreamItemSubscription<TData>;
15
+ /**
16
+ * Subscribe to a group in a stream.
17
+ *
18
+ * @argument streamName - The name of the stream to subscribe to.
19
+ * @argument groupId - The id of the group to subscribe to.
20
+ */
9
21
  subscribeGroup<TData extends {
10
22
  id: string;
11
23
  }>(streamName: string, groupId: string): StreamGroupSubscription<TData>;
@@ -6,10 +6,22 @@ export class Stream {
6
6
  this.ws = new WebSocket(address);
7
7
  this.ws.onopen = () => onReady();
8
8
  }
9
+ /**
10
+ * Subscribe to an item in a stream.
11
+ *
12
+ * @argument streamName - The name of the stream to subscribe to.
13
+ * @argument id - The id of the item to subscribe to.
14
+ */
9
15
  subscribeItem(streamName, id) {
10
16
  const subscriptionId = uuidv4();
11
17
  return new StreamItemSubscription(this.ws, { streamName, id, subscriptionId });
12
18
  }
19
+ /**
20
+ * Subscribe to a group in a stream.
21
+ *
22
+ * @argument streamName - The name of the stream to subscribe to.
23
+ * @argument groupId - The id of the group to subscribe to.
24
+ */
13
25
  subscribeGroup(streamName, groupId) {
14
26
  const subscriptionId = uuidv4();
15
27
  return new StreamGroupSubscription(this.ws, { streamName, groupId, subscriptionId });
package/package.json CHANGED
@@ -1,7 +1,8 @@
1
1
  {
2
2
  "name": "@motiadev/stream-client-browser",
3
3
  "description": "Motia Stream Client Package – Responsible for managing streams of data.",
4
- "version": "0.2.1-beta.49",
4
+ "version": "0.2.1-beta.51",
5
+ "license": "MIT",
5
6
  "main": "dist/index.js",
6
7
  "types": "dist/index.d.ts",
7
8
  "dependencies": {
@@ -48,20 +48,32 @@ export class StreamGroupSubscription<TData extends { id: string }> extends Strea
48
48
  ws.send(JSON.stringify(message))
49
49
  }
50
50
 
51
+ /**
52
+ * Close the subscription.
53
+ */
51
54
  close() {
52
55
  const message: Message = { type: 'leave', data: this.sub }
53
56
  this.ws.send(JSON.stringify(message))
54
57
  this.listeners.forEach((listener) => this.ws.removeEventListener('message', listener))
55
58
  }
56
59
 
60
+ /**
61
+ * Add a change listener. This listener will be called whenever the state of the group changes.
62
+ */
57
63
  addChangeListener(listener: Listener<TData[]>) {
58
64
  this.onChangeListeners.add(listener)
59
65
  }
60
66
 
67
+ /**
68
+ * Remove a change listener.
69
+ */
61
70
  removeChangeListener(listener: Listener<TData[]>) {
62
71
  this.onChangeListeners.delete(listener)
63
72
  }
64
73
 
74
+ /**
75
+ * Get the current state of the group.
76
+ */
65
77
  getState(): TData[] {
66
78
  return this.state
67
79
  }
@@ -36,20 +36,32 @@ export class StreamItemSubscription<TData extends { id: string }> extends Stream
36
36
  ws.send(JSON.stringify(message))
37
37
  }
38
38
 
39
+ /**
40
+ * Close the subscription.
41
+ */
39
42
  close() {
40
43
  const message: Message = { type: 'leave', data: this.sub }
41
44
  this.ws.send(JSON.stringify(message))
42
45
  this.listeners.forEach((listener) => this.ws.removeEventListener('message', listener))
43
46
  }
44
47
 
48
+ /**
49
+ * Add a change listener. This listener will be called whenever the state of the item changes.
50
+ */
45
51
  addChangeListener(listener: Listener<TData>) {
46
52
  this.onChangeListeners.add(listener)
47
53
  }
48
54
 
55
+ /**
56
+ * Remove a change listener.
57
+ */
49
58
  removeChangeListener(listener: Listener<TData>) {
50
59
  this.onChangeListeners.delete(listener)
51
60
  }
52
61
 
62
+ /**
63
+ * Get the current state of the item.
64
+ */
53
65
  getState(): TData | null {
54
66
  return this.state
55
67
  }
@@ -15,11 +15,17 @@ export abstract class StreamSubscription {
15
15
  }
16
16
  }
17
17
 
18
+ /**
19
+ * Add a custom event listener. This listener will be called whenever the custom event is received.
20
+ */
18
21
  onEvent(type: string, listener: CustomEventListener) {
19
22
  const listeners = this.customEventListeners.get(type) || []
20
23
  this.customEventListeners.set(type, [...listeners, listener])
21
24
  }
22
25
 
26
+ /**
27
+ * Remove a custom event listener.
28
+ */
23
29
  offEvent(type: string, listener: CustomEventListener) {
24
30
  const listeners = this.customEventListeners.get(type) || []
25
31
  this.customEventListeners.set(
package/src/stream.ts CHANGED
@@ -10,11 +10,23 @@ export class Stream {
10
10
  this.ws.onopen = () => onReady()
11
11
  }
12
12
 
13
+ /**
14
+ * Subscribe to an item in a stream.
15
+ *
16
+ * @argument streamName - The name of the stream to subscribe to.
17
+ * @argument id - The id of the item to subscribe to.
18
+ */
13
19
  subscribeItem<TData extends { id: string }>(streamName: string, id: string): StreamItemSubscription<TData> {
14
20
  const subscriptionId = uuidv4()
15
21
  return new StreamItemSubscription<TData>(this.ws, { streamName, id, subscriptionId })
16
22
  }
17
23
 
24
+ /**
25
+ * Subscribe to a group in a stream.
26
+ *
27
+ * @argument streamName - The name of the stream to subscribe to.
28
+ * @argument groupId - The id of the group to subscribe to.
29
+ */
18
30
  subscribeGroup<TData extends { id: string }>(streamName: string, groupId: string): StreamGroupSubscription<TData> {
19
31
  const subscriptionId = uuidv4()
20
32
  return new StreamGroupSubscription<TData>(this.ws, { streamName, groupId, subscriptionId })