@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
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* @adonisjs/transmit-client
|
|
3
|
+
*
|
|
4
|
+
* (c) AdonisJS
|
|
5
|
+
*
|
|
6
|
+
* For the full copyright and license information, please view the LICENSE
|
|
7
|
+
* file that was distributed with this source code.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { SubscriptionStatus } from './subscription_status.js'
|
|
11
|
+
import { HttpClient } from './http_client.js'
|
|
12
|
+
import { Hook } from './hook.js'
|
|
13
|
+
import { TransmitStatus } from './transmit_status.js'
|
|
14
|
+
|
|
15
|
+
interface SubscriptionOptions {
|
|
16
|
+
channel: string
|
|
17
|
+
httpClient: HttpClient
|
|
18
|
+
getEventSourceStatus: () => TransmitStatus
|
|
19
|
+
hooks?: Hook
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export class Subscription {
|
|
23
|
+
/**
|
|
24
|
+
* HTTP client instance.
|
|
25
|
+
*/
|
|
26
|
+
#httpClient: HttpClient
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Hook instance.
|
|
30
|
+
*/
|
|
31
|
+
#hooks: Hook | undefined
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Channel name.
|
|
35
|
+
*/
|
|
36
|
+
#channel: string
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Event source status getter.
|
|
40
|
+
*/
|
|
41
|
+
#getEventSourceStatus: () => TransmitStatus
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Registered message handlers.
|
|
45
|
+
*/
|
|
46
|
+
#handlers = new Set<(message: any) => void>()
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Current status of the subscription.
|
|
50
|
+
*/
|
|
51
|
+
#status: SubscriptionStatus = SubscriptionStatus.Pending
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Returns if the subscription is created or not.
|
|
55
|
+
*/
|
|
56
|
+
get isCreated() {
|
|
57
|
+
return this.#status === SubscriptionStatus.Created
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Returns if the subscription is deleted or not.
|
|
62
|
+
*/
|
|
63
|
+
get isDeleted() {
|
|
64
|
+
return this.#status === SubscriptionStatus.Deleted
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Returns the number of registered handlers.
|
|
69
|
+
*/
|
|
70
|
+
get handlerCount() {
|
|
71
|
+
return this.#handlers.size
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
constructor(options: SubscriptionOptions) {
|
|
75
|
+
this.#channel = options.channel
|
|
76
|
+
this.#httpClient = options.httpClient
|
|
77
|
+
this.#hooks = options.hooks
|
|
78
|
+
this.#getEventSourceStatus = options.getEventSourceStatus
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Run all registered handlers for the subscription.
|
|
83
|
+
*/
|
|
84
|
+
$runHandler(message: unknown) {
|
|
85
|
+
for (const handler of this.#handlers) {
|
|
86
|
+
handler(message)
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
async create() {
|
|
91
|
+
if (this.isCreated) {
|
|
92
|
+
return
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
if (this.#getEventSourceStatus() !== TransmitStatus.Connected) {
|
|
96
|
+
return new Promise((resolve) => {
|
|
97
|
+
setTimeout(() => {
|
|
98
|
+
resolve(this.create())
|
|
99
|
+
}, 100)
|
|
100
|
+
})
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const request = this.#httpClient.createRequest('/__transmit/subscribe', {
|
|
104
|
+
channel: this.#channel,
|
|
105
|
+
})
|
|
106
|
+
|
|
107
|
+
this.#hooks?.beforeSubscribe(request)
|
|
108
|
+
|
|
109
|
+
try {
|
|
110
|
+
const response = await this.#httpClient.send(request)
|
|
111
|
+
|
|
112
|
+
//? Dump the response text
|
|
113
|
+
void response.text()
|
|
114
|
+
|
|
115
|
+
if (!response.ok) {
|
|
116
|
+
this.#hooks?.onSubscribeFailed(response)
|
|
117
|
+
return
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
this.#status = SubscriptionStatus.Created
|
|
121
|
+
this.#hooks?.onSubscription(this.#channel)
|
|
122
|
+
} catch (error) {}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
async delete() {
|
|
126
|
+
if (this.isDeleted || !this.isCreated) {
|
|
127
|
+
return
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
const request = this.#httpClient.createRequest('/__transmit/unsubscribe', {
|
|
131
|
+
channel: this.#channel,
|
|
132
|
+
})
|
|
133
|
+
|
|
134
|
+
this.#hooks?.beforeUnsubscribe(request)
|
|
135
|
+
|
|
136
|
+
try {
|
|
137
|
+
const response = await this.#httpClient.send(request)
|
|
138
|
+
|
|
139
|
+
//? Dump the response text
|
|
140
|
+
void response.text()
|
|
141
|
+
|
|
142
|
+
if (!response.ok) {
|
|
143
|
+
return
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
this.#status = SubscriptionStatus.Deleted
|
|
147
|
+
this.#hooks?.onUnsubscription(this.#channel)
|
|
148
|
+
} catch (error) {}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
onMessage<T>(handler: (message: T) => void) {
|
|
152
|
+
this.#handlers.add(handler)
|
|
153
|
+
|
|
154
|
+
return () => {
|
|
155
|
+
this.#handlers.delete(handler)
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
onMessageOnce<T>(handler: (message: T) => void) {
|
|
160
|
+
const deleteHandler = this.onMessage<T>((message) => {
|
|
161
|
+
handler(message)
|
|
162
|
+
deleteHandler()
|
|
163
|
+
})
|
|
164
|
+
}
|
|
165
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* @adonisjs/transmit-client
|
|
3
|
+
*
|
|
4
|
+
* (c) AdonisJS
|
|
5
|
+
*
|
|
6
|
+
* For the full copyright and license information, please view the LICENSE
|
|
7
|
+
* file that was distributed with this source code.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
export const SubscriptionStatus = {
|
|
11
|
+
Pending: 0,
|
|
12
|
+
Created: 1,
|
|
13
|
+
Deleted: 2,
|
|
14
|
+
} as const
|
|
15
|
+
|
|
16
|
+
export type SubscriptionStatus = (typeof SubscriptionStatus)[keyof typeof SubscriptionStatus]
|
package/src/transmit.ts
CHANGED
|
@@ -1,6 +1,23 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* @adonisjs/transmit-client
|
|
3
|
+
*
|
|
4
|
+
* (c) AdonisJS
|
|
5
|
+
*
|
|
6
|
+
* For the full copyright and license information, please view the LICENSE
|
|
7
|
+
* file that was distributed with this source code.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { Subscription } from './subscription.js'
|
|
11
|
+
import { HttpClient } from './http_client.js'
|
|
12
|
+
import { TransmitStatus } from './transmit_status.js'
|
|
13
|
+
import { Hook } from './hook.js'
|
|
14
|
+
import { HookEvent } from './hook_event.js'
|
|
15
|
+
|
|
1
16
|
interface TransmitOptions {
|
|
2
17
|
baseUrl: string
|
|
3
|
-
|
|
18
|
+
uidGenerator?: () => string
|
|
19
|
+
eventSourceFactory?: (url: string | URL, options: { withCredentials: boolean }) => EventSource
|
|
20
|
+
eventTargetFactory?: () => EventTarget | null
|
|
4
21
|
beforeSubscribe?: (request: RequestInit) => void
|
|
5
22
|
beforeUnsubscribe?: (request: RequestInit) => void
|
|
6
23
|
maxReconnectAttempts?: number
|
|
@@ -9,24 +26,13 @@ interface TransmitOptions {
|
|
|
9
26
|
onSubscribeFailed?: (response: Response) => void
|
|
10
27
|
onSubscription?: (channel: string) => void
|
|
11
28
|
onUnsubscription?: (channel: string) => void
|
|
12
|
-
removeSubscriptionOnZeroListener?: boolean
|
|
13
29
|
}
|
|
14
30
|
|
|
15
|
-
export
|
|
16
|
-
Initializing: 'initializing',
|
|
17
|
-
Connecting: 'connecting',
|
|
18
|
-
Connected: 'connected',
|
|
19
|
-
Disconnected: 'disconnected',
|
|
20
|
-
Reconnecting: 'reconnecting',
|
|
21
|
-
} as const
|
|
22
|
-
|
|
23
|
-
type TTransmitStatus = (typeof TransmitStatus)[keyof typeof TransmitStatus]
|
|
24
|
-
|
|
25
|
-
export class Transmit extends EventTarget {
|
|
31
|
+
export class Transmit {
|
|
26
32
|
/**
|
|
27
33
|
* Unique identifier for this client.
|
|
28
34
|
*/
|
|
29
|
-
#uid: string
|
|
35
|
+
#uid: string
|
|
30
36
|
|
|
31
37
|
/**
|
|
32
38
|
* Options for this client.
|
|
@@ -34,19 +40,34 @@ export class Transmit extends EventTarget {
|
|
|
34
40
|
#options: TransmitOptions
|
|
35
41
|
|
|
36
42
|
/**
|
|
37
|
-
* Registered
|
|
43
|
+
* Registered subscriptions.
|
|
44
|
+
*/
|
|
45
|
+
#subscriptions = new Map<string, Subscription>()
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* HTTP client instance.
|
|
49
|
+
*/
|
|
50
|
+
#httpClient: HttpClient
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Hook instance.
|
|
38
54
|
*/
|
|
39
|
-
#
|
|
55
|
+
#hooks: Hook
|
|
40
56
|
|
|
41
57
|
/**
|
|
42
58
|
* Current status of the client.
|
|
43
59
|
*/
|
|
44
|
-
#status:
|
|
60
|
+
#status: TransmitStatus = TransmitStatus.Initializing
|
|
45
61
|
|
|
46
62
|
/**
|
|
47
63
|
* EventSource instance.
|
|
48
64
|
*/
|
|
49
|
-
#eventSource
|
|
65
|
+
#eventSource: EventSource | undefined
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* EventTarget instance.
|
|
69
|
+
*/
|
|
70
|
+
#eventTarget: EventTarget | null
|
|
50
71
|
|
|
51
72
|
/**
|
|
52
73
|
* Number of reconnect attempts.
|
|
@@ -54,40 +75,72 @@ export class Transmit extends EventTarget {
|
|
|
54
75
|
#reconnectAttempts: number = 0
|
|
55
76
|
|
|
56
77
|
/**
|
|
57
|
-
*
|
|
78
|
+
* Returns the unique identifier of the client.
|
|
58
79
|
*/
|
|
59
|
-
#channelSubscriptionLock: Set<string> = new Set()
|
|
60
|
-
|
|
61
80
|
get uid() {
|
|
62
81
|
return this.#uid
|
|
63
82
|
}
|
|
64
83
|
|
|
65
|
-
get listOfSubscriptions() {
|
|
66
|
-
return Array.from(this.#listeners.keys())
|
|
67
|
-
}
|
|
68
|
-
|
|
69
84
|
constructor(options: TransmitOptions) {
|
|
70
|
-
|
|
85
|
+
if (typeof options.uidGenerator === 'undefined') {
|
|
86
|
+
options.uidGenerator = () => crypto.randomUUID()
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
if (typeof options.eventSourceFactory === 'undefined') {
|
|
90
|
+
options.eventSourceFactory = (...args) => new EventSource(...args)
|
|
91
|
+
}
|
|
71
92
|
|
|
72
|
-
if (typeof options.
|
|
73
|
-
options.
|
|
93
|
+
if (typeof options.eventTargetFactory === 'undefined') {
|
|
94
|
+
options.eventTargetFactory = () => new EventTarget()
|
|
74
95
|
}
|
|
75
96
|
|
|
76
97
|
if (typeof options.maxReconnectAttempts === 'undefined') {
|
|
77
98
|
options.maxReconnectAttempts = 5
|
|
78
99
|
}
|
|
79
100
|
|
|
80
|
-
|
|
81
|
-
|
|
101
|
+
this.#uid = options.uidGenerator()
|
|
102
|
+
this.#eventTarget = options.eventTargetFactory()
|
|
103
|
+
this.#hooks = new Hook()
|
|
104
|
+
this.#httpClient = new HttpClient({
|
|
105
|
+
baseUrl: options.baseUrl,
|
|
106
|
+
uid: this.#uid,
|
|
107
|
+
})
|
|
108
|
+
|
|
109
|
+
if (options.beforeSubscribe) {
|
|
110
|
+
this.#hooks.register(HookEvent.BeforeSubscribe, options.beforeSubscribe)
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
if (options.beforeUnsubscribe) {
|
|
114
|
+
this.#hooks.register(HookEvent.BeforeUnsubscribe, options.beforeUnsubscribe)
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
if (options.onReconnectAttempt) {
|
|
118
|
+
this.#hooks.register(HookEvent.OnReconnectAttempt, options.onReconnectAttempt)
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
if (options.onReconnectFailed) {
|
|
122
|
+
this.#hooks.register(HookEvent.OnReconnectFailed, options.onReconnectFailed)
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
if (options.onSubscribeFailed) {
|
|
126
|
+
this.#hooks.register(HookEvent.OnSubscribeFailed, options.onSubscribeFailed)
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
if (options.onSubscription) {
|
|
130
|
+
this.#hooks.register(HookEvent.OnSubscription, options.onSubscription)
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
if (options.onUnsubscription) {
|
|
134
|
+
this.#hooks.register(HookEvent.OnUnsubscription, options.onUnsubscription)
|
|
82
135
|
}
|
|
83
136
|
|
|
84
137
|
this.#options = options
|
|
85
138
|
this.#connect()
|
|
86
139
|
}
|
|
87
140
|
|
|
88
|
-
#changeStatus(status:
|
|
141
|
+
#changeStatus(status: TransmitStatus) {
|
|
89
142
|
this.#status = status
|
|
90
|
-
this
|
|
143
|
+
this.#eventTarget?.dispatchEvent(new CustomEvent(status))
|
|
91
144
|
}
|
|
92
145
|
|
|
93
146
|
#connect() {
|
|
@@ -96,31 +149,35 @@ export class Transmit extends EventTarget {
|
|
|
96
149
|
const url = new URL(`${this.#options.baseUrl}/__transmit/events`)
|
|
97
150
|
url.searchParams.append('uid', this.#uid)
|
|
98
151
|
|
|
99
|
-
this.#eventSource =
|
|
152
|
+
this.#eventSource = this.#options.eventSourceFactory!(url, {
|
|
100
153
|
withCredentials: true,
|
|
101
154
|
})
|
|
155
|
+
|
|
102
156
|
this.#eventSource.addEventListener('message', this.#onMessage.bind(this))
|
|
103
157
|
this.#eventSource.addEventListener('error', this.#onError.bind(this))
|
|
104
158
|
this.#eventSource.addEventListener('open', () => {
|
|
105
159
|
this.#changeStatus(TransmitStatus.Connected)
|
|
106
160
|
this.#reconnectAttempts = 0
|
|
107
161
|
|
|
108
|
-
for (const
|
|
109
|
-
void
|
|
162
|
+
for (const subscription of this.#subscriptions.values()) {
|
|
163
|
+
void subscription.create()
|
|
110
164
|
}
|
|
111
165
|
})
|
|
112
166
|
}
|
|
113
167
|
|
|
114
168
|
#onMessage(event: MessageEvent) {
|
|
115
169
|
const data = JSON.parse(event.data)
|
|
116
|
-
const
|
|
170
|
+
const subscription = this.#subscriptions.get(data.channel)
|
|
117
171
|
|
|
118
|
-
if (typeof
|
|
172
|
+
if (typeof subscription === 'undefined') {
|
|
119
173
|
return
|
|
120
174
|
}
|
|
121
175
|
|
|
122
|
-
|
|
123
|
-
|
|
176
|
+
try {
|
|
177
|
+
subscription.$runHandler(data.payload)
|
|
178
|
+
} catch (error) {
|
|
179
|
+
// TODO: Rescue
|
|
180
|
+
console.log(error)
|
|
124
181
|
}
|
|
125
182
|
}
|
|
126
183
|
|
|
@@ -131,19 +188,15 @@ export class Transmit extends EventTarget {
|
|
|
131
188
|
|
|
132
189
|
this.#changeStatus(TransmitStatus.Reconnecting)
|
|
133
190
|
|
|
134
|
-
|
|
135
|
-
this.#options.onReconnectAttempt(this.#reconnectAttempts + 1)
|
|
136
|
-
}
|
|
191
|
+
this.#hooks.onReconnectAttempt(this.#reconnectAttempts + 1)
|
|
137
192
|
|
|
138
193
|
if (
|
|
139
194
|
this.#options.maxReconnectAttempts &&
|
|
140
195
|
this.#reconnectAttempts >= this.#options.maxReconnectAttempts
|
|
141
196
|
) {
|
|
142
|
-
this.#eventSource
|
|
197
|
+
this.#eventSource!.close()
|
|
143
198
|
|
|
144
|
-
|
|
145
|
-
this.#options.onReconnectFailed()
|
|
146
|
-
}
|
|
199
|
+
this.#hooks.onReconnectFailed()
|
|
147
200
|
|
|
148
201
|
return
|
|
149
202
|
}
|
|
@@ -151,122 +204,29 @@ export class Transmit extends EventTarget {
|
|
|
151
204
|
this.#reconnectAttempts++
|
|
152
205
|
}
|
|
153
206
|
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
})
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
const listeners = this.#listeners.get(channel)
|
|
164
|
-
|
|
165
|
-
if (typeof listeners !== 'undefined' && typeof callback !== 'undefined') {
|
|
166
|
-
this.#options.onSubscription?.(channel)
|
|
167
|
-
listeners.add(callback)
|
|
168
|
-
return
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
if (this.#channelSubscriptionLock.has(channel)) {
|
|
172
|
-
return new Promise((resolve) => {
|
|
173
|
-
setTimeout(() => {
|
|
174
|
-
resolve(this.#subscribe(channel, callback))
|
|
175
|
-
}, 100)
|
|
176
|
-
})
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
this.#channelSubscriptionLock.add(channel)
|
|
180
|
-
|
|
181
|
-
const request = new Request(`${this.#options.baseUrl}/__transmit/subscribe`, {
|
|
182
|
-
method: 'POST',
|
|
183
|
-
headers: {
|
|
184
|
-
'Content-Type': 'application/json',
|
|
185
|
-
},
|
|
186
|
-
body: JSON.stringify({ uid: this.#uid, channel }),
|
|
187
|
-
credentials: 'include',
|
|
207
|
+
subscription(channel: string) {
|
|
208
|
+
const subscription = new Subscription({
|
|
209
|
+
channel,
|
|
210
|
+
httpClient: this.#httpClient,
|
|
211
|
+
hooks: this.#hooks,
|
|
212
|
+
getEventSourceStatus: () => this.#status,
|
|
188
213
|
})
|
|
189
214
|
|
|
190
|
-
this.#
|
|
191
|
-
|
|
192
|
-
try {
|
|
193
|
-
const response = await fetch(request)
|
|
194
|
-
|
|
195
|
-
if (!response.ok) {
|
|
196
|
-
this.#options.onSubscribeFailed?.(response)
|
|
197
|
-
this.#channelSubscriptionLock.delete(channel)
|
|
198
|
-
return
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
if (typeof callback !== 'undefined') {
|
|
202
|
-
const listeners = this.#listeners.get(channel)
|
|
203
|
-
|
|
204
|
-
if (typeof listeners === 'undefined') {
|
|
205
|
-
this.#listeners.set(channel, new Set([callback]))
|
|
206
|
-
} else {
|
|
207
|
-
listeners.add(callback)
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
this.#options.onSubscription?.(channel)
|
|
211
|
-
}
|
|
212
|
-
} finally {
|
|
213
|
-
this.#channelSubscriptionLock.delete(channel)
|
|
215
|
+
if (this.#subscriptions.has(channel)) {
|
|
216
|
+
return this.#subscriptions.get(channel)!
|
|
214
217
|
}
|
|
215
|
-
}
|
|
216
218
|
|
|
217
|
-
|
|
218
|
-
const request = new Request(`${this.#options.baseUrl}/__transmit/unsubscribe`, {
|
|
219
|
-
method: 'POST',
|
|
220
|
-
headers: {
|
|
221
|
-
'Content-Type': 'application/json',
|
|
222
|
-
},
|
|
223
|
-
body: JSON.stringify({ uid: this.#uid, channel }),
|
|
224
|
-
credentials: 'include',
|
|
225
|
-
})
|
|
226
|
-
|
|
227
|
-
this.#options.beforeUnsubscribe?.(request)
|
|
228
|
-
|
|
229
|
-
const response = await fetch(request)
|
|
219
|
+
this.#subscriptions.set(channel, subscription)
|
|
230
220
|
|
|
231
|
-
|
|
232
|
-
return
|
|
233
|
-
}
|
|
221
|
+
return subscription
|
|
234
222
|
}
|
|
235
223
|
|
|
236
|
-
on(event: Exclude<
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
listenOn<T = unknown>(channel: string, callback: (message: T) => void) {
|
|
241
|
-
void this.#subscribe(channel, callback)
|
|
242
|
-
|
|
243
|
-
return (unsubscribeOnTheServer?: boolean) => {
|
|
244
|
-
const listeners = this.#listeners.get(channel)
|
|
245
|
-
|
|
246
|
-
if (typeof listeners === 'undefined') {
|
|
247
|
-
return
|
|
248
|
-
}
|
|
249
|
-
|
|
250
|
-
listeners.delete(callback)
|
|
251
|
-
this.#options.onUnsubscription?.(channel)
|
|
252
|
-
|
|
253
|
-
if (
|
|
254
|
-
(unsubscribeOnTheServer ?? this.#options.removeSubscriptionOnZeroListener) &&
|
|
255
|
-
listeners.size === 0
|
|
256
|
-
) {
|
|
257
|
-
void this.#unsubscribe(channel)
|
|
258
|
-
}
|
|
259
|
-
}
|
|
260
|
-
}
|
|
261
|
-
|
|
262
|
-
listenOnce<T = unknown>(channel: string, callback: (message: T) => void) {
|
|
263
|
-
const unsubscribe = this.listenOn<T>(channel, (message) => {
|
|
264
|
-
callback(message)
|
|
265
|
-
unsubscribe()
|
|
266
|
-
})
|
|
224
|
+
on(event: Exclude<TransmitStatus, 'connecting'>, callback: (event: CustomEvent) => void) {
|
|
225
|
+
// @ts-ignore
|
|
226
|
+
this.#eventTarget?.addEventListener(event, callback)
|
|
267
227
|
}
|
|
268
228
|
|
|
269
229
|
close() {
|
|
270
|
-
this.#eventSource
|
|
230
|
+
this.#eventSource?.close()
|
|
271
231
|
}
|
|
272
232
|
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* @adonisjs/transmit-client
|
|
3
|
+
*
|
|
4
|
+
* (c) AdonisJS
|
|
5
|
+
*
|
|
6
|
+
* For the full copyright and license information, please view the LICENSE
|
|
7
|
+
* file that was distributed with this source code.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
export const TransmitStatus = {
|
|
11
|
+
Initializing: 'initializing',
|
|
12
|
+
Connecting: 'connecting',
|
|
13
|
+
Connected: 'connected',
|
|
14
|
+
Disconnected: 'disconnected',
|
|
15
|
+
Reconnecting: 'reconnecting',
|
|
16
|
+
} as const
|
|
17
|
+
|
|
18
|
+
export type TransmitStatus = (typeof TransmitStatus)[keyof typeof TransmitStatus]
|
package/build/transmit.cjs
DELETED
|
@@ -1,2 +0,0 @@
|
|
|
1
|
-
function e(){try{var t=!Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],function(){}))}catch(t){}return(e=function(){return!!t})()}function t(e){var t=function(e,t){if("object"!=typeof e||!e)return e;var n=e[Symbol.toPrimitive];if(void 0!==n){var r=n.call(e,"string");if("object"!=typeof r)return r;throw new TypeError("@@toPrimitive must return a primitive value.")}return String(e)}(e);return"symbol"==typeof t?t:String(t)}function n(e){return n=Object.setPrototypeOf?Object.getPrototypeOf.bind():function(e){return e.__proto__||Object.getPrototypeOf(e)},n(e)}function r(e,t){return r=Object.setPrototypeOf?Object.setPrototypeOf.bind():function(e,t){return e.__proto__=t,e},r(e,t)}function i(t){var o="function"==typeof Map?new Map:void 0;return i=function(t){if(null===t||!function(e){try{return-1!==Function.toString.call(e).indexOf("[native code]")}catch(t){return"function"==typeof e}}(t))return t;if("function"!=typeof t)throw new TypeError("Super expression must either be null or a function");if(void 0!==o){if(o.has(t))return o.get(t);o.set(t,i)}function i(){return function(t,n,i){if(e())return Reflect.construct.apply(null,arguments);var o=[null];o.push.apply(o,n);var c=new(t.bind.apply(t,o));return i&&r(c,i.prototype),c}(t,arguments,n(this).constructor)}return i.prototype=Object.create(t.prototype,{constructor:{value:i,enumerable:!1,writable:!0,configurable:!0}}),r(i,t)},i(t)}function o(e){if(void 0===e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return e}function c(e,t){(null==t||t>e.length)&&(t=e.length);for(var n=0,r=new Array(t);n<t;n++)r[n]=e[n];return r}function u(e,t){var n="undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(n)return(n=n.call(e)).next.bind(n);if(Array.isArray(e)||(n=function(e,t){if(e){if("string"==typeof e)return c(e,t);var n=Object.prototype.toString.call(e).slice(8,-1);return"Object"===n&&e.constructor&&(n=e.constructor.name),"Map"===n||"Set"===n?Array.from(e):"Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)?c(e,t):void 0}}(e))||t&&e&&"number"==typeof e.length){n&&(e=n);var r=0;return function(){return r>=e.length?{done:!0}:{done:!1,value:e[r++]}}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var a=0;function s(e){return"__private_"+a+++"_"+e}function l(e,t){if(!Object.prototype.hasOwnProperty.call(e,t))throw new TypeError("attempted to use private field on non-instance");return e}var f=function(e){try{var t,n,r=this,i=new Request(l(r,b)[b].baseUrl+"/__transmit/unsubscribe",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({uid:l(r,v)[v],channel:e}),credentials:"include"});return null==(t=(n=l(r,b)[b]).beforeUnsubscribe)||t.call(n,i),Promise.resolve(fetch(i)).then(function(e){})}catch(e){return Promise.reject(e)}},p=function(e,t){try{var n,r,i=this;if(l(i,y)[y]!==d.Connected)return Promise.resolve(new Promise(function(n){setTimeout(function(){n(l(i,_)[_](e,t))},100)}));var o,c,u=l(i,h)[h].get(e);if(void 0!==u&&void 0!==t)return null==(o=(c=l(i,b)[b]).onSubscription)||o.call(c,e),u.add(t),Promise.resolve();if(l(i,g)[g].has(e))return Promise.resolve(new Promise(function(n){setTimeout(function(){n(l(i,_)[_](e,t))},100)}));l(i,g)[g].add(e);var a=new Request(l(i,b)[b].baseUrl+"/__transmit/subscribe",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({uid:l(i,v)[v],channel:e}),credentials:"include"});return null==(n=(r=l(i,b)[b]).beforeSubscribe)||n.call(r,a),Promise.resolve(function(n,r){try{var o=Promise.resolve(fetch(a)).then(function(n){var r,o;if(!n.ok)return null==(r=(o=l(i,b)[b]).onSubscribeFailed)||r.call(o,n),void l(i,g)[g].delete(e);if(void 0!==t){var c,u,a=l(i,h)[h].get(e);void 0===a?l(i,h)[h].set(e,new Set([t])):a.add(t),null==(c=(u=l(i,b)[b]).onSubscription)||c.call(u,e)}})}catch(e){return r(!0,e)}return o&&o.then?o.then(r.bind(null,!1),r.bind(null,!0)):r(!1,o)}(0,function(t,n){if(l(i,g)[g].delete(e),t)throw n;return n}))}catch(e){return Promise.reject(e)}},d={Initializing:"initializing",Connecting:"connecting",Connected:"connected",Disconnected:"disconnected",Reconnecting:"reconnecting"},v=/*#__PURE__*/s("uid"),b=/*#__PURE__*/s("options"),h=/*#__PURE__*/s("listeners"),y=/*#__PURE__*/s("status"),m=/*#__PURE__*/s("eventSource"),O=/*#__PURE__*/s("reconnectAttempts"),g=/*#__PURE__*/s("channelSubscriptionLock"),w=/*#__PURE__*/s("changeStatus"),P=/*#__PURE__*/s("connect"),S=/*#__PURE__*/s("onMessage"),j=/*#__PURE__*/s("onError"),_=/*#__PURE__*/s("subscribe"),R=/*#__PURE__*/s("unsubscribe"),A=/*#__PURE__*/function(e){var n,i;function c(t){var n;return n=e.call(this)||this,Object.defineProperty(o(n),R,{value:f}),Object.defineProperty(o(n),_,{value:p}),Object.defineProperty(o(n),j,{value:x}),Object.defineProperty(o(n),S,{value:T}),Object.defineProperty(o(n),P,{value:C}),Object.defineProperty(o(n),w,{value:E}),Object.defineProperty(o(n),v,{writable:!0,value:crypto.randomUUID()}),Object.defineProperty(o(n),b,{writable:!0,value:void 0}),Object.defineProperty(o(n),h,{writable:!0,value:new Map}),Object.defineProperty(o(n),y,{writable:!0,value:d.Initializing}),Object.defineProperty(o(n),m,{writable:!0,value:void 0}),Object.defineProperty(o(n),O,{writable:!0,value:0}),Object.defineProperty(o(n),g,{writable:!0,value:new Set}),void 0===t.eventSourceConstructor&&(t.eventSourceConstructor=EventSource),void 0===t.maxReconnectAttempts&&(t.maxReconnectAttempts=5),void 0===t.removeSubscriptionOnZeroListener&&(t.removeSubscriptionOnZeroListener=!1),l(o(n),b)[b]=t,l(o(n),P)[P](),n}i=e,(n=c).prototype=Object.create(i.prototype),n.prototype.constructor=n,r(n,i);var u,a,s=c.prototype;return s.on=function(e,t){this.addEventListener(e,t)},s.listenOn=function(e,t){var n=this;return l(this,_)[_](e,t),function(r){var i,o,c=l(n,h)[h].get(e);void 0!==c&&(c.delete(t),null==(i=(o=l(n,b)[b]).onUnsubscription)||i.call(o,e),(null!=r?r:l(n,b)[b].removeSubscriptionOnZeroListener)&&0===c.size&&l(n,R)[R](e))}},s.listenOnce=function(e,t){var n=this.listenOn(e,function(e){t(e),n()})},s.close=function(){l(this,m)[m].close()},u=c,(a=[{key:"uid",get:function(){return l(this,v)[v]}},{key:"listOfSubscriptions",get:function(){return Array.from(l(this,h)[h].keys())}}])&&function(e,n){for(var r=0;r<n.length;r++){var i=n[r];i.enumerable=i.enumerable||!1,i.configurable=!0,"value"in i&&(i.writable=!0),Object.defineProperty(e,t(i.key),i)}}(u.prototype,a),Object.defineProperty(u,"prototype",{writable:!1}),c}(/*#__PURE__*/i(EventTarget));function E(e){l(this,y)[y]=e,this.dispatchEvent(new CustomEvent(e))}function C(){var e=this;l(this,w)[w](d.Connecting);var t=new URL(l(this,b)[b].baseUrl+"/__transmit/events");t.searchParams.append("uid",l(this,v)[v]),l(this,m)[m]=new(l(this,b)[b].eventSourceConstructor)(t.toString(),{withCredentials:!0}),l(this,m)[m].addEventListener("message",l(this,S)[S].bind(this)),l(this,m)[m].addEventListener("error",l(this,j)[j].bind(this)),l(this,m)[m].addEventListener("open",function(){l(e,w)[w](d.Connected),l(e,O)[O]=0;for(var t,n=u(l(e,h)[h].keys());!(t=n()).done;){var r=t.value;l(e,_)[_](r)}})}function T(e){var t=JSON.parse(e.data),n=l(this,h)[h].get(t.channel);if(void 0!==n)for(var r,i=u(n);!(r=i()).done;)(0,r.value)(t.payload)}function x(){if(l(this,y)[y]!==d.Reconnecting&&l(this,w)[w](d.Disconnected),l(this,w)[w](d.Reconnecting),l(this,b)[b].onReconnectAttempt&&l(this,b)[b].onReconnectAttempt(l(this,O)[O]+1),l(this,b)[b].maxReconnectAttempts&&l(this,O)[O]>=l(this,b)[b].maxReconnectAttempts)return l(this,m)[m].close(),void(l(this,b)[b].onReconnectFailed&&l(this,b)[b].onReconnectFailed());l(this,O)[O]++}exports.Transmit=A,exports.TransmitStatus=d;
|
|
2
|
-
//# sourceMappingURL=transmit.cjs.map
|
package/build/transmit.cjs.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"transmit.cjs","sources":["../src/transmit.ts"],"sourcesContent":["interface TransmitOptions {\n baseUrl: string\n eventSourceConstructor?: typeof EventSource\n beforeSubscribe?: (request: RequestInit) => void\n beforeUnsubscribe?: (request: RequestInit) => void\n maxReconnectAttempts?: number\n onReconnectAttempt?: (attempt: number) => void\n onReconnectFailed?: () => void\n onSubscribeFailed?: (response: Response) => void\n onSubscription?: (channel: string) => void\n onUnsubscription?: (channel: string) => void\n removeSubscriptionOnZeroListener?: boolean\n}\n\nexport const TransmitStatus = {\n Initializing: 'initializing',\n Connecting: 'connecting',\n Connected: 'connected',\n Disconnected: 'disconnected',\n Reconnecting: 'reconnecting',\n} as const\n\ntype TTransmitStatus = (typeof TransmitStatus)[keyof typeof TransmitStatus]\n\nexport class Transmit extends EventTarget {\n /**\n * Unique identifier for this client.\n */\n #uid: string = crypto.randomUUID()\n\n /**\n * Options for this client.\n */\n #options: TransmitOptions\n\n /**\n * Registered listeners.\n */\n #listeners: Map<string, Set<(message: any) => void>> = new Map()\n\n /**\n * Current status of the client.\n */\n #status: TTransmitStatus = TransmitStatus.Initializing\n\n /**\n * EventSource instance.\n */\n #eventSource!: EventSource\n\n /**\n * Number of reconnect attempts.\n */\n #reconnectAttempts: number = 0\n\n /**\n * Locks for channel subscriptions.\n */\n #channelSubscriptionLock: Set<string> = new Set()\n\n get uid() {\n return this.#uid\n }\n\n get listOfSubscriptions() {\n return Array.from(this.#listeners.keys())\n }\n\n constructor(options: TransmitOptions) {\n super()\n\n if (typeof options.eventSourceConstructor === 'undefined') {\n options.eventSourceConstructor = EventSource\n }\n\n if (typeof options.maxReconnectAttempts === 'undefined') {\n options.maxReconnectAttempts = 5\n }\n\n if (typeof options.removeSubscriptionOnZeroListener === 'undefined') {\n options.removeSubscriptionOnZeroListener = false\n }\n\n this.#options = options\n this.#connect()\n }\n\n #changeStatus(status: TTransmitStatus) {\n this.#status = status\n this.dispatchEvent(new CustomEvent(status))\n }\n\n #connect() {\n this.#changeStatus(TransmitStatus.Connecting)\n\n const url = new URL(`${this.#options.baseUrl}/__transmit/events`)\n url.searchParams.append('uid', this.#uid)\n\n this.#eventSource = new this.#options.eventSourceConstructor(url.toString(), {\n withCredentials: true,\n })\n this.#eventSource.addEventListener('message', this.#onMessage.bind(this))\n this.#eventSource.addEventListener('error', this.#onError.bind(this))\n this.#eventSource.addEventListener('open', () => {\n this.#changeStatus(TransmitStatus.Connected)\n this.#reconnectAttempts = 0\n\n for (const channel of this.#listeners.keys()) {\n void this.#subscribe(channel)\n }\n })\n }\n\n #onMessage(event: MessageEvent) {\n const data = JSON.parse(event.data)\n const listeners = this.#listeners.get(data.channel)\n\n if (typeof listeners === 'undefined') {\n return\n }\n\n for (const listener of listeners) {\n listener(data.payload)\n }\n }\n\n #onError() {\n if (this.#status !== TransmitStatus.Reconnecting) {\n this.#changeStatus(TransmitStatus.Disconnected)\n }\n\n this.#changeStatus(TransmitStatus.Reconnecting)\n\n if (this.#options.onReconnectAttempt) {\n this.#options.onReconnectAttempt(this.#reconnectAttempts + 1)\n }\n\n if (\n this.#options.maxReconnectAttempts &&\n this.#reconnectAttempts >= this.#options.maxReconnectAttempts\n ) {\n this.#eventSource.close()\n\n if (this.#options.onReconnectFailed) {\n this.#options.onReconnectFailed()\n }\n\n return\n }\n\n this.#reconnectAttempts++\n }\n\n async #subscribe(channel: string, callback?: any) {\n if (this.#status !== TransmitStatus.Connected) {\n return new Promise((resolve) => {\n setTimeout(() => {\n resolve(this.#subscribe(channel, callback))\n }, 100)\n })\n }\n\n const listeners = this.#listeners.get(channel)\n\n if (typeof listeners !== 'undefined' && typeof callback !== 'undefined') {\n this.#options.onSubscription?.(channel)\n listeners.add(callback)\n return\n }\n\n if (this.#channelSubscriptionLock.has(channel)) {\n return new Promise((resolve) => {\n setTimeout(() => {\n resolve(this.#subscribe(channel, callback))\n }, 100)\n })\n }\n\n this.#channelSubscriptionLock.add(channel)\n\n const request = new Request(`${this.#options.baseUrl}/__transmit/subscribe`, {\n method: 'POST',\n headers: {\n 'Content-Type': 'application/json',\n },\n body: JSON.stringify({ uid: this.#uid, channel }),\n credentials: 'include',\n })\n\n this.#options.beforeSubscribe?.(request)\n\n try {\n const response = await fetch(request)\n\n if (!response.ok) {\n this.#options.onSubscribeFailed?.(response)\n this.#channelSubscriptionLock.delete(channel)\n return\n }\n\n if (typeof callback !== 'undefined') {\n const listeners = this.#listeners.get(channel)\n\n if (typeof listeners === 'undefined') {\n this.#listeners.set(channel, new Set([callback]))\n } else {\n listeners.add(callback)\n }\n\n this.#options.onSubscription?.(channel)\n }\n } finally {\n this.#channelSubscriptionLock.delete(channel)\n }\n }\n\n async #unsubscribe(channel: string) {\n const request = new Request(`${this.#options.baseUrl}/__transmit/unsubscribe`, {\n method: 'POST',\n headers: {\n 'Content-Type': 'application/json',\n },\n body: JSON.stringify({ uid: this.#uid, channel }),\n credentials: 'include',\n })\n\n this.#options.beforeUnsubscribe?.(request)\n\n const response = await fetch(request)\n\n if (!response.ok) {\n return\n }\n }\n\n on(event: Exclude<TTransmitStatus, 'connecting'>, callback: (event: CustomEvent) => void) {\n this.addEventListener(event, callback)\n }\n\n listenOn<T = unknown>(channel: string, callback: (message: T) => void) {\n void this.#subscribe(channel, callback)\n\n return (unsubscribeOnTheServer?: boolean) => {\n const listeners = this.#listeners.get(channel)\n\n if (typeof listeners === 'undefined') {\n return\n }\n\n listeners.delete(callback)\n this.#options.onUnsubscription?.(channel)\n\n if (\n (unsubscribeOnTheServer ?? this.#options.removeSubscriptionOnZeroListener) &&\n listeners.size === 0\n ) {\n void this.#unsubscribe(channel)\n }\n }\n }\n\n listenOnce<T = unknown>(channel: string, callback: (message: T) => void) {\n const unsubscribe = this.listenOn<T>(channel, (message) => {\n callback(message)\n unsubscribe()\n })\n }\n\n close() {\n this.#eventSource.close()\n }\n}\n"],"names":["channel","_classPrivateFieldLoo11","_classPrivateFieldLoo12","_this5","this","request","Request","_classPrivateFieldLooseBase","_options","baseUrl","method","headers","body","JSON","stringify","uid","_uid","credentials","beforeUnsubscribe","call","Promise","resolve","fetch","then","response","e","reject","_subscribe2","callback","_classPrivateFieldLoo5","_classPrivateFieldLoo6","_this4","_status","TransmitStatus","Connected","setTimeout","_subscribe","_classPrivateFieldLoo3","_classPrivateFieldLoo4","listeners","_listeners","get","onSubscription","add","_channelSubscriptionLock","has","beforeSubscribe","_classPrivateFieldLoo7","_classPrivateFieldLoo8","ok","onSubscribeFailed","_classPrivateFieldLoo9","_classPrivateFieldLoo10","set","Set","_finallyRethrows","_wasThrown","_result","Initializing","Connecting","Disconnected","Reconnecting","_classPrivateFieldLooseKey","_eventSource","_reconnectAttempts","_changeStatus","_connect","_onMessage","_onError","_unsubscribe","Transmit","_EventTarget","options","_this","Object","defineProperty","_assertThisInitialized","value","_unsubscribe2","_onError2","_onMessage2","_connect2","_changeStatus2","writable","crypto","randomUUID","Map","eventSourceConstructor","EventSource","maxReconnectAttempts","removeSubscriptionOnZeroListener","_proto","prototype","on","event","addEventListener","listenOn","_this2","unsubscribeOnTheServer","_classPrivateFieldLoo","_classPrivateFieldLoo2","onUnsubscription","size","listenOnce","unsubscribe","message","close","key","Array","from","keys","_wrapNativeSuper","EventTarget","status","dispatchEvent","CustomEvent","_this3","url","URL","searchParams","append","toString","withCredentials","bind","_step","_iterator","_createForOfIteratorHelperLoose","done","data","parse","_step2","_iterator2","listener","payload","onReconnectAttempt","onReconnectFailed"],"mappings":"8/EAwNqBA,GAAe,IAAA,IAAAC,EAAAC,EAAAC,EACDC,KAAzBC,EAAU,IAAIC,QAAWC,EAAAJ,EAAAK,GAAAA,GAAcC,QAAO,0BAA2B,CAC7EC,OAAQ,OACRC,QAAS,CACP,eAAgB,oBAElBC,KAAMC,KAAKC,UAAU,CAAEC,IAAGR,EAAAJ,EAAAa,GAAAA,GAAahB,QAAAA,IACvCiB,YAAa,YAG2B,OAAX,OAA/BhB,GAAAC,EAAAK,EAAAJ,EAAAK,GAAAA,IAAcU,oBAAdjB,EAAAkB,KAAAjB,EAAkCG,GAAQe,QAAAC,QAEnBC,MAAMjB,IAAQkB,KAAA,SAA/BC,GAEU,EAGlB,CAAC,MAAAC,GAAAL,OAAAA,QAAAM,OAAAD,KAAAE,EAAA,SAhFgB3B,EAAiB4B,OAAcC,IAAAA,EAAAC,EAAAC,EAC1C3B,KAAJ,GAAIG,EAAAwB,EAAAC,GAAAA,KAAiBC,EAAeC,UAClC,OAAAd,QAAAC,QAAO,IAAID,QAAQ,SAACC,GAClBc,WAAW,WACTd,EAAOd,EAAAwB,EAAAK,GAAAA,GAAiBpC,EAAS4B,GACnC,EAAG,IACL,IAGF,IAEyES,EAAAC,EAFnEC,EAAYhC,EAAAwB,EAAAS,GAAAA,GAAgBC,IAAIzC,GAEtC,QAAyB,IAAduC,QAAiD,IAAbX,EAG7C,cAFAS,GAAAC,EAAA/B,EAAAwB,EAAAvB,GAAAA,IAAckC,iBAAdL,EAAAlB,KAAAmB,EAA+BtC,GAC/BuC,EAAUI,IAAIf,GACdR,QAAAC,UAGF,GAAId,EAAAwB,EAAAa,GAAAA,GAA8BC,IAAI7C,GACpC,OAAAoB,QAAAC,QAAO,IA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oC,GAAAA,GAAYC,IAAIuF,EAAKhI,SAE3C,QAAyB,IAAduC,EAIX,IAAA,IAAgC2F,EAAhCC,EAAAL,EAAuBvF,KAAS2F,EAAAC,KAAAJ,OAC9BK,EADiBF,EAAArD,OACRmD,EAAKK,QAElB,CAAC,SAAAtD,IAaC,GAVIxE,EAAIH,KAAA4B,GAAAA,KAAaC,EAAe4B,cAClCtD,EAAAH,KAAI6D,GAAAA,GAAehC,EAAe2B,cAGpCrD,EAAIH,KAAA6D,GAAAA,GAAehC,EAAe4B,cAE9BtD,EAAIH,KAAAI,GAAAA,GAAU8H,oBAChB/H,EAAAH,KAAII,GAAAA,GAAU8H,mBAAmB/H,EAAAH,KAAI4D,GAAAA,GAAsB,GAI3DzD,EAAIH,KAAAI,GAAAA,GAAUiF,sBACdlF,EAAIH,KAAA4D,GAAAA,IAAuBzD,EAAIH,KAAAI,GAAAA,GAAUiF,qBAQzC,OANAlF,EAAIH,KAAA2D,GAAAA,GAAc2C,aAEdnG,EAAAH,KAAII,GAAAA,GAAU+H,mBAChBhI,EAAAH,KAAII,GAAAA,GAAU+H,qBAMlBhI,EAAAH,KAAI4D,GAAAA,IACN"}
|
package/build/transmit.modern.js
DELETED
|
@@ -1,2 +0,0 @@
|
|
|
1
|
-
var e=0;function t(t){return"__private_"+e+++"_"+t}function n(e,t){if(!Object.prototype.hasOwnProperty.call(e,t))throw new TypeError("attempted to use private field on non-instance");return e}const i={Initializing:"initializing",Connecting:"connecting",Connected:"connected",Disconnected:"disconnected",Reconnecting:"reconnecting"};var s=/*#__PURE__*/t("uid"),o=/*#__PURE__*/t("options"),r=/*#__PURE__*/t("listeners"),c=/*#__PURE__*/t("status"),a=/*#__PURE__*/t("eventSource"),h=/*#__PURE__*/t("reconnectAttempts"),l=/*#__PURE__*/t("channelSubscriptionLock"),d=/*#__PURE__*/t("changeStatus"),u=/*#__PURE__*/t("connect"),v=/*#__PURE__*/t("onMessage"),b=/*#__PURE__*/t("onError"),p=/*#__PURE__*/t("subscribe"),f=/*#__PURE__*/t("unsubscribe");class y extends EventTarget{get uid(){return n(this,s)[s]}get listOfSubscriptions(){return Array.from(n(this,r)[r].keys())}constructor(e){super(),Object.defineProperty(this,f,{value:P}),Object.defineProperty(this,p,{value:S}),Object.defineProperty(this,b,{value:w}),Object.defineProperty(this,v,{value:g}),Object.defineProperty(this,u,{value:O}),Object.defineProperty(this,d,{value:m}),Object.defineProperty(this,s,{writable:!0,value:crypto.randomUUID()}),Object.defineProperty(this,o,{writable:!0,value:void 0}),Object.defineProperty(this,r,{writable:!0,value:new Map}),Object.defineProperty(this,c,{writable:!0,value:i.Initializing}),Object.defineProperty(this,a,{writable:!0,value:void 0}),Object.defineProperty(this,h,{writable:!0,value:0}),Object.defineProperty(this,l,{writable:!0,value:new Set}),void 0===e.eventSourceConstructor&&(e.eventSourceConstructor=EventSource),void 0===e.maxReconnectAttempts&&(e.maxReconnectAttempts=5),void 0===e.removeSubscriptionOnZeroListener&&(e.removeSubscriptionOnZeroListener=!1),n(this,o)[o]=e,n(this,u)[u]()}on(e,t){this.addEventListener(e,t)}listenOn(e,t){return n(this,p)[p](e,t),i=>{var s,c;const a=n(this,r)[r].get(e);void 0!==a&&(a.delete(t),null==(s=(c=n(this,o)[o]).onUnsubscription)||s.call(c,e),(null!=i?i:n(this,o)[o].removeSubscriptionOnZeroListener)&&0===a.size&&n(this,f)[f](e))}}listenOnce(e,t){const n=this.listenOn(e,e=>{t(e),n()})}close(){n(this,a)[a].close()}}function m(e){n(this,c)[c]=e,this.dispatchEvent(new CustomEvent(e))}function O(){n(this,d)[d](i.Connecting);const e=new URL(`${n(this,o)[o].baseUrl}/__transmit/events`);e.searchParams.append("uid",n(this,s)[s]),n(this,a)[a]=new(n(this,o)[o].eventSourceConstructor)(e.toString(),{withCredentials:!0}),n(this,a)[a].addEventListener("message",n(this,v)[v].bind(this)),n(this,a)[a].addEventListener("error",n(this,b)[b].bind(this)),n(this,a)[a].addEventListener("open",()=>{n(this,d)[d](i.Connected),n(this,h)[h]=0;for(const e of n(this,r)[r].keys())n(this,p)[p](e)})}function g(e){const t=JSON.parse(e.data),i=n(this,r)[r].get(t.channel);if(void 0!==i)for(const e of i)e(t.payload)}function w(){if(n(this,c)[c]!==i.Reconnecting&&n(this,d)[d](i.Disconnected),n(this,d)[d](i.Reconnecting),n(this,o)[o].onReconnectAttempt&&n(this,o)[o].onReconnectAttempt(n(this,h)[h]+1),n(this,o)[o].maxReconnectAttempts&&n(this,h)[h]>=n(this,o)[o].maxReconnectAttempts)return n(this,a)[a].close(),void(n(this,o)[o].onReconnectFailed&&n(this,o)[o].onReconnectFailed());n(this,h)[h]++}async function S(e,t){var a,h;if(n(this,c)[c]!==i.Connected)return new Promise(i=>{setTimeout(()=>{i(n(this,p)[p](e,t))},100)});const d=n(this,r)[r].get(e);var u,v;if(void 0!==d&&void 0!==t)return null==(u=(v=n(this,o)[o]).onSubscription)||u.call(v,e),void d.add(t);if(n(this,l)[l].has(e))return new Promise(i=>{setTimeout(()=>{i(n(this,p)[p](e,t))},100)});n(this,l)[l].add(e);const b=new Request(`${n(this,o)[o].baseUrl}/__transmit/subscribe`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({uid:n(this,s)[s],channel:e}),credentials:"include"});null==(a=(h=n(this,o)[o]).beforeSubscribe)||a.call(h,b);try{const i=await fetch(b);var f,y;if(!i.ok)return null==(f=(y=n(this,o)[o]).onSubscribeFailed)||f.call(y,i),void n(this,l)[l].delete(e);if(void 0!==t){var m,O;const i=n(this,r)[r].get(e);void 0===i?n(this,r)[r].set(e,new Set([t])):i.add(t),null==(m=(O=n(this,o)[o]).onSubscription)||m.call(O,e)}}finally{n(this,l)[l].delete(e)}}async function P(e){var t,i;const r=new Request(`${n(this,o)[o].baseUrl}/__transmit/unsubscribe`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({uid:n(this,s)[s],channel:e}),credentials:"include"});null==(t=(i=n(this,o)[o]).beforeUnsubscribe)||t.call(i,r),await fetch(r)}export{y as Transmit,i as TransmitStatus};
|
|
2
|
-
//# sourceMappingURL=transmit.modern.js.map
|