wirejs-resources 0.1.74-realtime → 0.1.76-realtime

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.
@@ -55,6 +55,18 @@ async function callApi(INTERNAL_API_URL, method, ...args) {
55
55
  if (typeof value === 'object' && value.__wjstype === 'realtime') {
56
56
  const subscribers = [];
57
57
  const ws = new WebSocket(value.url, value.protocol);
58
+ ws.onopen = () => {
59
+ for (const subscriber of subscribers) {
60
+ if (subscriber.onopen) {
61
+ try {
62
+ subscriber.onopen();
63
+ }
64
+ catch (error) {
65
+ console.error('Error in subscriber onopen:', error);
66
+ }
67
+ }
68
+ }
69
+ };
58
70
  ws.onmessage = (event) => {
59
71
  const data = JSON.parse(event.data);
60
72
  if (data.data) {
@@ -3,10 +3,12 @@ export type MessageStream<T = any> = {
3
3
  /**
4
4
  * Returns a function to close the subscription.
5
5
  */
6
- subscribe(subscriber: {
7
- onmessage: (data: T) => void;
8
- onclose?: () => void;
9
- }): () => void;
6
+ subscribe(subscriber: MessageStreamSubscriber<T>): () => void;
7
+ };
8
+ export type MessageStreamSubscriber<T = any> = {
9
+ onopen?: () => void;
10
+ onmessage: (data: T) => void;
11
+ onclose?: () => void;
10
12
  };
11
13
  export declare class RealtimeService<T = any> extends Resource {
12
14
  #private;
@@ -15,6 +17,6 @@ export declare class RealtimeService<T = any> extends Resource {
15
17
  * The address the client will need to connect to.
16
18
  */
17
19
  get address(): string;
18
- publish(channel: string, data: T | T[]): Promise<void>;
20
+ publish(channel: string, events: T[]): Promise<void>;
19
21
  getStream(channel: string): Promise<MessageStream<T>>;
20
22
  }
@@ -111,13 +111,12 @@ export class RealtimeService extends Resource {
111
111
  throw new Error('Server address is not available');
112
112
  }
113
113
  }
114
- async publish(channel, data) {
114
+ async publish(channel, events) {
115
115
  this.#requireServer();
116
116
  this.#validateChannelName(channel);
117
- console.log('Publishing to channel:', channel, data);
117
+ console.log('Publishing to channel:', channel, events);
118
118
  (this.#channels.get(channel) || []).forEach((ws) => {
119
119
  if (ws.readyState === WebSocket.OPEN) {
120
- const events = Array.isArray(data) ? data : [data];
121
120
  for (const event of events) {
122
121
  ws.send(JSON.stringify({ data: event }));
123
122
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wirejs-resources",
3
- "version": "0.1.74-realtime",
3
+ "version": "0.1.76-realtime",
4
4
  "description": "Basic services and server-side resources for wirejs apps",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",