@adonisjs/transmit-client 0.1.6 → 0.2.1

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 CHANGED
@@ -35,10 +35,10 @@ AdonisJS Transmit Client is a client for the native Server-Sent-Event (SSE) modu
35
35
 
36
36
  - [Installation](#installation)
37
37
  - [Usage](#usage)
38
- - [Subscribing to channels](#subscribing-to-channels)
38
+ - [Creating a subscription](#creating-a-subscription)
39
+ - [Unsubscribing](#unsubscribing)
39
40
  - [Subscription Request](#subscription-request)
40
41
  - [Reconnecting](#reconnecting)
41
- - [Unsubscribing](#unsubscribing)
42
42
  - [Events](#events)
43
43
 
44
44
  <!-- END doctoc generated TOC please keep comment here to allow auto update -->
@@ -49,12 +49,6 @@ Install the package from the npm registry as follows:
49
49
 
50
50
  ```sh
51
51
  npm i @adonisjs/transmit-client
52
-
53
- # yarn
54
- yarn add @adonisjs/transmit-client
55
-
56
- # pnpm
57
- pnpm add @adonisjs/transmit-client
58
52
  ```
59
53
 
60
54
  ## Usage
@@ -69,63 +63,44 @@ const transmit = new Transmit({
69
63
  })
70
64
  ```
71
65
 
72
- ## Subscribing to channels
66
+ ## Creating a subscription
73
67
 
74
- The `listenOn` method accepts the channel name and a callback to be invoked when the event is received from the server.
68
+ The `subscription` method is used to create a subscription to a channel. The method accepts the channel name
75
69
 
76
70
  ```ts
77
- transmit.listenOn<{ message: string }>('chat/1', (payload) => {
78
- console.log(payload.message)
79
- })
71
+ const subscription = transmit.subscription('chat/1')
80
72
  ```
81
73
 
82
- You can also listen from a channel only once.
74
+ Then, you have to call the `create` method on the subscription to register it on the backend.
83
75
 
84
76
  ```ts
85
- transmit.listenOnce<{ message: string }>('chat/1', () => {
86
- console.log('first message received!')
87
- })
77
+ await subscription.create()
88
78
  ```
89
79
 
90
- ### Subscription Request
91
-
92
- You can alter the subscription request by using the `beforeSubscribe` or `beforeUnsubscribe` options.
80
+ You can listen for events on the channel using the `onMessage` method. You can define as many listeners as you want on the same subscription.
93
81
 
94
82
  ```ts
95
- const transmit = new Transmit({
96
- baseUrl: 'http://localhost:3333',
97
- beforeSubscribe: (_request: RequestInit) => {
98
- console.log('beforeSubscribe')
99
- },
100
- beforeUnsubscribe: (_request: RequestInit) => {
101
- console.log('beforeUnsubscribe')
102
- },
83
+ subscription.onMessage((message) => {
84
+ console.log(message)
103
85
  })
104
86
  ```
105
87
 
106
- ### Reconnecting
107
-
108
- The transmit client will automatically reconnect to the server when the connection is lost. You can change the number of retries and hook into the reconnect lifecycle as follows:
88
+ You can also listen only once for a message using the `onMessagetOnce` method.
109
89
 
110
90
  ```ts
111
- const transmit = new Transmit({
112
- baseUrl: 'http://localhost:3333',
113
- maxReconnectionAttempts: 5,
114
- onReconnectAttempt: (attempt) => {
115
- console.log('Reconnect attempt ' + attempt)
116
- },
117
- onReconnectFailed: () => {
118
- console.log('Reconnect failed')
119
- },
91
+ subscription.onMessageOnce((message) => {
92
+ console.log('I will be called only once')
120
93
  })
121
94
  ```
122
95
 
96
+ Note listeners are local only; you can add them before or after registering your subscription on the server.
97
+
123
98
  ### Unsubscribing
124
99
 
125
- The `listenOn` method returns a function to unsubscribe from the channel.
100
+ The `onMessage` method returns a function to remove the message handler from the subscription.
126
101
 
127
102
  ```ts
128
- const unsubscribe = transmit.listenOn('chat/1', () => {
103
+ const unsubscribe = subscription.onMessage(() => {
129
104
  console.log('message received!')
130
105
  })
131
106
 
@@ -133,31 +108,50 @@ const unsubscribe = transmit.listenOn('chat/1', () => {
133
108
  unsubscribe()
134
109
  ```
135
110
 
136
- When unsubscribing from a channel, the client will remove the local listener for that channel. By default, it will not send a request to the server when there are no more listener to unsubscribe from the channel. You can change this behavior by setting the `removeSubscriptionOnZeroListener` option to `true`.
111
+ If you want to entirely remove the subscription from the server, you can call the `delete` method.
112
+
113
+ ```ts
114
+ await subscription.delete()
115
+ ```
116
+
117
+ ### Subscription Request
118
+
119
+ You can alter the subscription request by using the `beforeSubscribe` or `beforeUnsubscribe` options.
137
120
 
138
121
  ```ts
139
122
  const transmit = new Transmit({
140
123
  baseUrl: 'http://localhost:3333',
141
- removeSubscriptionOnZeroListener: true,
124
+ beforeSubscribe: (_request: Request) => {
125
+ console.log('beforeSubscribe')
126
+ },
127
+ beforeUnsubscribe: (_request: Request) => {
128
+ console.log('beforeUnsubscribe')
129
+ },
142
130
  })
143
131
  ```
144
132
 
145
- You can also change the default settings locally by passing a boolean to the unsubscribe method.
133
+ ### Reconnecting
134
+
135
+ The transmit client will automatically reconnect to the server when the connection is lost. You can change the number of retries and hook into the reconnect lifecycle as follows:
146
136
 
147
137
  ```ts
148
- const unsubscribe = transmit.listenOn('chat/1', () => {
149
- console.log('message received!')
138
+ const transmit = new Transmit({
139
+ baseUrl: 'http://localhost:3333',
140
+ maxReconnectionAttempts: 5,
141
+ onReconnectAttempt: (attempt) => {
142
+ console.log('Reconnect attempt ' + attempt)
143
+ },
144
+ onReconnectFailed: () => {
145
+ console.log('Reconnect failed')
146
+ },
150
147
  })
151
-
152
- // later
153
- unsubscribe(true) // or false
154
148
  ```
155
149
 
156
150
  # Events
157
151
 
158
- The`Transmit` class extends the [`EventTarget`](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget) class and emits multiple events.
152
+ The`Transmit` class uses the [`EventTarget`](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget) class to emits multiple events.
159
153
 
160
- ```ts
154
+ ```ts
161
155
  transmit.on('connected', () => {
162
156
  console.log('connected')
163
157
  })
@@ -1,6 +1,86 @@
1
+ interface HttpClientOptions {
2
+ baseUrl: string;
3
+ uid: string;
4
+ }
5
+ declare class HttpClient {
6
+ #private;
7
+ constructor(options: HttpClientOptions);
8
+ send(request: Request): Promise<Response>;
9
+ createRequest(path: string, body: Record<string, unknown>): Request;
10
+ }
11
+
12
+ declare const HookEvent: {
13
+ readonly BeforeSubscribe: "beforeSubscribe";
14
+ readonly BeforeUnsubscribe: "beforeUnsubscribe";
15
+ readonly OnReconnectAttempt: "onReconnectAttempt";
16
+ readonly OnReconnectFailed: "onReconnectFailed";
17
+ readonly OnSubscribeFailed: "onSubscribeFailed";
18
+ readonly OnSubscription: "onSubscription";
19
+ readonly OnUnsubscription: "onUnsubscription";
20
+ };
21
+ type HookEvent = (typeof HookEvent)[keyof typeof HookEvent];
22
+
23
+ declare class Hook {
24
+ #private;
25
+ register(event: HookEvent, handler: (...args: any[]) => void): this;
26
+ beforeSubscribe(request: Request): this;
27
+ beforeUnsubscribe(request: Request): this;
28
+ onReconnectAttempt(attempt: number): this;
29
+ onReconnectFailed(): this;
30
+ onSubscribeFailed(response: Response): this;
31
+ onSubscription(channel: string): this;
32
+ onUnsubscription(channel: string): this;
33
+ }
34
+
35
+ declare const TransmitStatus: {
36
+ readonly Initializing: "initializing";
37
+ readonly Connecting: "connecting";
38
+ readonly Connected: "connected";
39
+ readonly Disconnected: "disconnected";
40
+ readonly Reconnecting: "reconnecting";
41
+ };
42
+ type TransmitStatus = (typeof TransmitStatus)[keyof typeof TransmitStatus];
43
+
44
+ interface SubscriptionOptions {
45
+ channel: string;
46
+ httpClient: HttpClient;
47
+ getEventSourceStatus: () => TransmitStatus;
48
+ hooks?: Hook;
49
+ }
50
+ declare class Subscription {
51
+ #private;
52
+ /**
53
+ * Returns if the subscription is created or not.
54
+ */
55
+ get isCreated(): boolean;
56
+ /**
57
+ * Returns if the subscription is deleted or not.
58
+ */
59
+ get isDeleted(): boolean;
60
+ /**
61
+ * Returns the number of registered handlers.
62
+ */
63
+ get handlerCount(): number;
64
+ constructor(options: SubscriptionOptions);
65
+ /**
66
+ * Run all registered handlers for the subscription.
67
+ */
68
+ $runHandler(message: unknown): void;
69
+ create(): Promise<unknown>;
70
+ forceCreate(): Promise<unknown>;
71
+ delete(): Promise<void>;
72
+ onMessage<T>(handler: (message: T) => void): () => void;
73
+ onMessageOnce<T>(handler: (message: T) => void): void;
74
+ }
75
+
1
76
  interface TransmitOptions {
2
77
  baseUrl: string;
3
- eventSourceConstructor?: typeof EventSource;
78
+ uidGenerator?: () => string;
79
+ eventSourceFactory?: (url: string | URL, options: {
80
+ withCredentials: boolean;
81
+ }) => EventSource;
82
+ eventTargetFactory?: () => EventTarget | null;
83
+ httpClientFactory?: (baseUrl: string, uid: string) => HttpClient;
4
84
  beforeSubscribe?: (request: RequestInit) => void;
5
85
  beforeUnsubscribe?: (request: RequestInit) => void;
6
86
  maxReconnectAttempts?: number;
@@ -9,24 +89,17 @@ interface TransmitOptions {
9
89
  onSubscribeFailed?: (response: Response) => void;
10
90
  onSubscription?: (channel: string) => void;
11
91
  onUnsubscription?: (channel: string) => void;
12
- removeSubscriptionOnZeroListener?: boolean;
13
92
  }
14
- export declare const TransmitStatus: {
15
- readonly Initializing: "initializing";
16
- readonly Connecting: "connecting";
17
- readonly Connected: "connected";
18
- readonly Disconnected: "disconnected";
19
- readonly Reconnecting: "reconnecting";
20
- };
21
- type TTransmitStatus = (typeof TransmitStatus)[keyof typeof TransmitStatus];
22
- export declare class Transmit extends EventTarget {
93
+ declare class Transmit {
23
94
  #private;
95
+ /**
96
+ * Returns the unique identifier of the client.
97
+ */
24
98
  get uid(): string;
25
- get listOfSubscriptions(): string[];
26
99
  constructor(options: TransmitOptions);
27
- on(event: Exclude<TTransmitStatus, 'connecting'>, callback: (event: CustomEvent) => void): void;
28
- listenOn<T = unknown>(channel: string, callback: (message: T) => void): (unsubscribeOnTheServer?: boolean) => void;
29
- listenOnce<T = unknown>(channel: string, callback: (message: T) => void): void;
100
+ subscription(channel: string): Subscription;
101
+ on(event: Exclude<TransmitStatus, 'connecting'>, callback: (event: CustomEvent) => void): void;
30
102
  close(): void;
31
103
  }
32
- export {};
104
+
105
+ export { Transmit };