@adonisjs/transmit-client 0.2.0 → 0.2.2

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 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
@@ -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 -->
@@ -77,12 +77,10 @@ Then, you have to call the `create` method on the subscription to register it on
77
77
  await subscription.create()
78
78
  ```
79
79
 
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.
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.
81
81
 
82
82
  ```ts
83
- import {subscribe} from 'node:diagnostics_channel'
84
-
85
- subscribe.onMessage((message) => {
83
+ subscription.onMessage((message) => {
86
84
  console.log(message)
87
85
  })
88
86
  ```
@@ -90,11 +88,13 @@ subscribe.onMessage((message) => {
90
88
  You can also listen only once for a message using the `onMessagetOnce` method.
91
89
 
92
90
  ```ts
93
- subscribe.onMessageOnce((message) => {
91
+ subscription.onMessageOnce((message) => {
94
92
  console.log('I will be called only once')
95
93
  })
96
94
  ```
97
95
 
96
+ Note listeners are local only; you can add them before or after registering your subscription on the server.
97
+
98
98
  ### Unsubscribing
99
99
 
100
100
  The `onMessage` method returns a function to remove the message handler from the subscription.
@@ -67,6 +67,7 @@ declare class Subscription {
67
67
  */
68
68
  $runHandler(message: unknown): void;
69
69
  create(): Promise<unknown>;
70
+ forceCreate(): Promise<unknown>;
70
71
  delete(): Promise<void>;
71
72
  onMessage<T>(handler: (message: T) => void): () => void;
72
73
  onMessageOnce<T>(handler: (message: T) => void): void;
@@ -79,6 +80,7 @@ interface TransmitOptions {
79
80
  withCredentials: boolean;
80
81
  }) => EventSource;
81
82
  eventTargetFactory?: () => EventTarget | null;
83
+ httpClientFactory?: (baseUrl: string, uid: string) => HttpClient;
82
84
  beforeSubscribe?: (request: RequestInit) => void;
83
85
  beforeUnsubscribe?: (request: RequestInit) => void;
84
86
  maxReconnectAttempts?: number;
@@ -100,4 +102,4 @@ declare class Transmit {
100
102
  close(): void;
101
103
  }
102
104
 
103
- export { Transmit };
105
+ export { Subscription, Transmit };
@@ -108,6 +108,9 @@ var Subscription = class {
108
108
  if (this.isCreated) {
109
109
  return;
110
110
  }
111
+ return this.forceCreate();
112
+ }
113
+ async forceCreate() {
111
114
  if (__privateGet(this, _getEventSourceStatus).call(this) !== TransmitStatus.Connected) {
112
115
  return new Promise((resolve) => {
113
116
  setTimeout(() => {
@@ -310,16 +313,16 @@ var Transmit = class {
310
313
  if (typeof options.eventTargetFactory === "undefined") {
311
314
  options.eventTargetFactory = () => new EventTarget();
312
315
  }
316
+ if (typeof options.httpClientFactory === "undefined") {
317
+ options.httpClientFactory = (baseUrl, uid) => new HttpClient({ baseUrl, uid });
318
+ }
313
319
  if (typeof options.maxReconnectAttempts === "undefined") {
314
320
  options.maxReconnectAttempts = 5;
315
321
  }
316
322
  __privateSet(this, _uid, options.uidGenerator());
317
323
  __privateSet(this, _eventTarget, options.eventTargetFactory());
318
324
  __privateSet(this, _hooks2, new Hook());
319
- __privateSet(this, _httpClient2, new HttpClient({
320
- baseUrl: options.baseUrl,
321
- uid: __privateGet(this, _uid)
322
- }));
325
+ __privateSet(this, _httpClient2, options.httpClientFactory(options.baseUrl, __privateGet(this, _uid)));
323
326
  if (options.beforeSubscribe) {
324
327
  __privateGet(this, _hooks2).register(HookEvent.BeforeSubscribe, options.beforeSubscribe);
325
328
  }
@@ -398,7 +401,9 @@ connect_fn = function() {
398
401
  __privateMethod(this, _changeStatus, changeStatus_fn).call(this, TransmitStatus.Connected);
399
402
  __privateSet(this, _reconnectAttempts, 0);
400
403
  for (const subscription of __privateGet(this, _subscriptions).values()) {
401
- void subscription.create();
404
+ if (subscription.isCreated) {
405
+ void subscription.forceCreate();
406
+ }
402
407
  }
403
408
  });
404
409
  };
@@ -430,5 +435,6 @@ onError_fn = function() {
430
435
  __privateWrapper(this, _reconnectAttempts)._++;
431
436
  };
432
437
  export {
438
+ Subscription,
433
439
  Transmit
434
440
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@adonisjs/transmit-client",
3
- "version": "0.2.0",
3
+ "version": "0.2.2",
4
4
  "description": "A client for the native Server-Sent-Event module of AdonisJS.",
5
5
  "keywords": [
6
6
  "client",
@@ -14,9 +14,9 @@
14
14
  "license": "MIT",
15
15
  "author": "Romain Lanz <romain.lanz@pm.me>",
16
16
  "type": "module",
17
- "source": "src/transmit.ts",
18
- "main": "./build/transmit.js",
19
- "types": "./build/transmit.d.ts",
17
+ "source": "index.ts",
18
+ "main": "./build/index.js",
19
+ "types": "./build/index.d.ts",
20
20
  "scripts": {
21
21
  "clean": "del-cli build",
22
22
  "typecheck": "tsc --noEmit",
@@ -67,7 +67,7 @@
67
67
  "target": "es2020",
68
68
  "outDir": "build",
69
69
  "entry": [
70
- "src/transmit.ts"
70
+ "index.ts"
71
71
  ]
72
72
  },
73
73
  "release-it": {
@@ -92,6 +92,10 @@ export class Subscription {
92
92
  return
93
93
  }
94
94
 
95
+ return this.forceCreate()
96
+ }
97
+
98
+ async forceCreate() {
95
99
  if (this.#getEventSourceStatus() !== TransmitStatus.Connected) {
96
100
  return new Promise((resolve) => {
97
101
  setTimeout(() => {
package/src/transmit.ts CHANGED
@@ -18,6 +18,7 @@ interface TransmitOptions {
18
18
  uidGenerator?: () => string
19
19
  eventSourceFactory?: (url: string | URL, options: { withCredentials: boolean }) => EventSource
20
20
  eventTargetFactory?: () => EventTarget | null
21
+ httpClientFactory?: (baseUrl: string, uid: string) => HttpClient
21
22
  beforeSubscribe?: (request: RequestInit) => void
22
23
  beforeUnsubscribe?: (request: RequestInit) => void
23
24
  maxReconnectAttempts?: number
@@ -94,6 +95,10 @@ export class Transmit {
94
95
  options.eventTargetFactory = () => new EventTarget()
95
96
  }
96
97
 
98
+ if (typeof options.httpClientFactory === 'undefined') {
99
+ options.httpClientFactory = (baseUrl, uid) => new HttpClient({ baseUrl, uid })
100
+ }
101
+
97
102
  if (typeof options.maxReconnectAttempts === 'undefined') {
98
103
  options.maxReconnectAttempts = 5
99
104
  }
@@ -101,10 +106,7 @@ export class Transmit {
101
106
  this.#uid = options.uidGenerator()
102
107
  this.#eventTarget = options.eventTargetFactory()
103
108
  this.#hooks = new Hook()
104
- this.#httpClient = new HttpClient({
105
- baseUrl: options.baseUrl,
106
- uid: this.#uid,
107
- })
109
+ this.#httpClient = options.httpClientFactory(options.baseUrl, this.#uid)
108
110
 
109
111
  if (options.beforeSubscribe) {
110
112
  this.#hooks.register(HookEvent.BeforeSubscribe, options.beforeSubscribe)
@@ -160,7 +162,9 @@ export class Transmit {
160
162
  this.#reconnectAttempts = 0
161
163
 
162
164
  for (const subscription of this.#subscriptions.values()) {
163
- void subscription.create()
165
+ if (subscription.isCreated) {
166
+ void subscription.forceCreate()
167
+ }
164
168
  }
165
169
  })
166
170
  }