@adonisjs/transmit-client 0.1.5 → 0.2.0
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/LICENSE.md +9 -9
- package/README.md +45 -51
- package/build/transmit.d.ts +87 -16
- package/build/transmit.js +434 -0
- package/package.json +36 -20
- package/src/hook.ts +66 -0
- package/src/hook_event.ts +20 -0
- package/src/http_client.ts +46 -0
- package/src/subscription.ts +165 -0
- package/src/subscription_status.ts +16 -0
- package/src/transmit.ts +113 -153
- package/src/transmit_status.ts +18 -0
- package/build/transmit.cjs +0 -2
- package/build/transmit.cjs.map +0 -1
- package/build/transmit.modern.js +0 -2
- package/build/transmit.modern.js.map +0 -1
- package/build/transmit.module.js +0 -2
- package/build/transmit.module.js.map +0 -1
- package/build/transmit.umd.js +0 -2
- package/build/transmit.umd.js.map +0 -1
package/LICENSE.md
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
# The MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2023, Romain Lanz, AdonisJS Core Team, contributors
|
|
4
|
-
|
|
5
|
-
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:
|
|
6
|
-
|
|
7
|
-
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
8
|
-
|
|
9
|
-
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.
|
|
1
|
+
# The MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023, Romain Lanz, AdonisJS Core Team, contributors
|
|
4
|
+
|
|
5
|
+
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:
|
|
6
|
+
|
|
7
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
8
|
+
|
|
9
|
+
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.
|
package/README.md
CHANGED
|
@@ -27,7 +27,7 @@
|
|
|
27
27
|
|
|
28
28
|
<hr />
|
|
29
29
|
|
|
30
|
-
AdonisJS Transmit Client is a client for the native Server-Sent-Event (SSE) module of AdonisJS. It is built on top of the [EventSource](https://developer.mozilla.org/en-US/docs/Web/API/
|
|
30
|
+
AdonisJS Transmit Client is a client for the native Server-Sent-Event (SSE) module of AdonisJS. It is built on top of the [EventSource](https://developer.mozilla.org/en-US/docs/Web/API/EventSource) API and provides a simple API to receive events from the server.
|
|
31
31
|
|
|
32
32
|
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
|
|
33
33
|
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO 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
|
-
##
|
|
66
|
+
## Creating a subscription
|
|
73
67
|
|
|
74
|
-
The `
|
|
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
|
-
|
|
78
|
-
console.log(payload.message)
|
|
79
|
-
})
|
|
71
|
+
const subscription = transmit.subscription('chat/1')
|
|
80
72
|
```
|
|
81
73
|
|
|
82
|
-
|
|
74
|
+
Then, you have to call the `create` method on the subscription to register it on the backend.
|
|
83
75
|
|
|
84
76
|
```ts
|
|
85
|
-
|
|
86
|
-
console.log('first message received!')
|
|
87
|
-
})
|
|
77
|
+
await subscription.create()
|
|
88
78
|
```
|
|
89
79
|
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
You can alter the subscription request by using the `beforeSubscribe` or `beforeUnsubscribe` options.
|
|
80
|
+
Once the subscription is created, you can listen for events on the channel using the `onMessage` method. You can define as many listeners as you want.
|
|
93
81
|
|
|
94
82
|
```ts
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
},
|
|
100
|
-
beforeUnsubscribe: (_request: RequestInit) => {
|
|
101
|
-
console.log('beforeUnsubscribe')
|
|
102
|
-
},
|
|
83
|
+
import {subscribe} from 'node:diagnostics_channel'
|
|
84
|
+
|
|
85
|
+
subscribe.onMessage((message) => {
|
|
86
|
+
console.log(message)
|
|
103
87
|
})
|
|
104
88
|
```
|
|
105
89
|
|
|
106
|
-
|
|
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:
|
|
90
|
+
You can also listen only once for a message using the `onMessagetOnce` method.
|
|
109
91
|
|
|
110
92
|
```ts
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
maxReconnectionAttempts: 5,
|
|
114
|
-
onReconnectAttempt: (attempt) => {
|
|
115
|
-
console.log('Reconnect attempt ' + attempt)
|
|
116
|
-
},
|
|
117
|
-
onReconnectFailed: () => {
|
|
118
|
-
console.log('Reconnect failed')
|
|
119
|
-
},
|
|
93
|
+
subscribe.onMessageOnce((message) => {
|
|
94
|
+
console.log('I will be called only once')
|
|
120
95
|
})
|
|
121
96
|
```
|
|
122
97
|
|
|
123
98
|
### Unsubscribing
|
|
124
99
|
|
|
125
|
-
The `
|
|
100
|
+
The `onMessage` method returns a function to remove the message handler from the subscription.
|
|
126
101
|
|
|
127
102
|
```ts
|
|
128
|
-
const unsubscribe =
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
|
149
|
-
|
|
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
|
|
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
|
})
|
package/build/transmit.d.ts
CHANGED
|
@@ -1,6 +1,84 @@
|
|
|
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
|
+
delete(): Promise<void>;
|
|
71
|
+
onMessage<T>(handler: (message: T) => void): () => void;
|
|
72
|
+
onMessageOnce<T>(handler: (message: T) => void): void;
|
|
73
|
+
}
|
|
74
|
+
|
|
1
75
|
interface TransmitOptions {
|
|
2
76
|
baseUrl: string;
|
|
3
|
-
|
|
77
|
+
uidGenerator?: () => string;
|
|
78
|
+
eventSourceFactory?: (url: string | URL, options: {
|
|
79
|
+
withCredentials: boolean;
|
|
80
|
+
}) => EventSource;
|
|
81
|
+
eventTargetFactory?: () => EventTarget | null;
|
|
4
82
|
beforeSubscribe?: (request: RequestInit) => void;
|
|
5
83
|
beforeUnsubscribe?: (request: RequestInit) => void;
|
|
6
84
|
maxReconnectAttempts?: number;
|
|
@@ -9,24 +87,17 @@ interface TransmitOptions {
|
|
|
9
87
|
onSubscribeFailed?: (response: Response) => void;
|
|
10
88
|
onSubscription?: (channel: string) => void;
|
|
11
89
|
onUnsubscription?: (channel: string) => void;
|
|
12
|
-
removeSubscriptionOnZeroListener?: boolean;
|
|
13
90
|
}
|
|
14
|
-
|
|
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 {
|
|
91
|
+
declare class Transmit {
|
|
23
92
|
#private;
|
|
93
|
+
/**
|
|
94
|
+
* Returns the unique identifier of the client.
|
|
95
|
+
*/
|
|
24
96
|
get uid(): string;
|
|
25
|
-
get listOfSubscriptions(): string[];
|
|
26
97
|
constructor(options: TransmitOptions);
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
listenOnce<T = unknown>(channel: string, callback: (message: T) => void): void;
|
|
98
|
+
subscription(channel: string): Subscription;
|
|
99
|
+
on(event: Exclude<TransmitStatus, 'connecting'>, callback: (event: CustomEvent) => void): void;
|
|
30
100
|
close(): void;
|
|
31
101
|
}
|
|
32
|
-
|
|
102
|
+
|
|
103
|
+
export { Transmit };
|