@rpgjs/client 5.0.0-alpha.2 → 5.0.0-alpha.4
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/dist/index20.js +3 -3
- package/dist/index21.js +2 -2
- package/dist/index29.js +1 -1
- package/dist/index30.js +1 -1
- package/dist/index31.js +9 -171
- package/dist/index31.js.map +1 -1
- package/dist/index32.js +4395 -493
- package/dist/index32.js.map +1 -1
- package/dist/index33.js +313 -9
- package/dist/index33.js.map +1 -1
- package/dist/index34.js +170 -4399
- package/dist/index34.js.map +1 -1
- package/dist/index35.js +483 -298
- package/dist/index35.js.map +1 -1
- package/dist/index37.js +1 -1
- package/dist/index38.js +17 -17
- package/dist/index38.js.map +1 -1
- package/dist/index39.js +17 -17
- package/dist/index39.js.map +1 -1
- package/package.json +8 -8
package/dist/index35.js
CHANGED
|
@@ -1,316 +1,501 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
if (typeof origMethod === "function") {
|
|
22
|
-
return (...args) => {
|
|
23
|
-
let changeType = "update";
|
|
24
|
-
let index = void 0;
|
|
25
|
-
let isMutateFn = false;
|
|
26
|
-
let itemsToEmit = [];
|
|
27
|
-
let changeSplice = true;
|
|
28
|
-
switch (prop) {
|
|
29
|
-
case "push":
|
|
30
|
-
index = target.length;
|
|
31
|
-
changeType = "add";
|
|
32
|
-
isMutateFn = true;
|
|
33
|
-
break;
|
|
34
|
-
case "pop":
|
|
35
|
-
index = target.length - 1;
|
|
36
|
-
changeType = "remove";
|
|
37
|
-
isMutateFn = true;
|
|
38
|
-
break;
|
|
39
|
-
case "unshift":
|
|
40
|
-
index = 0;
|
|
41
|
-
changeType = "add";
|
|
42
|
-
isMutateFn = true;
|
|
43
|
-
break;
|
|
44
|
-
case "shift":
|
|
45
|
-
index = 0;
|
|
46
|
-
changeType = "remove";
|
|
47
|
-
isMutateFn = true;
|
|
48
|
-
break;
|
|
49
|
-
case "splice":
|
|
50
|
-
index = args[0];
|
|
51
|
-
const deleteCount = args[1];
|
|
52
|
-
const newItems = args.slice(2);
|
|
53
|
-
itemsToEmit = newItems;
|
|
54
|
-
if (deleteCount > 0 && newItems.length === 0) {
|
|
55
|
-
changeType = "remove";
|
|
56
|
-
} else if (deleteCount === 0 && newItems.length > 0) {
|
|
57
|
-
changeType = "add";
|
|
58
|
-
} else if (deleteCount === 0 && newItems.length === 0) {
|
|
59
|
-
changeSplice = false;
|
|
60
|
-
} else {
|
|
61
|
-
changeType = "update";
|
|
62
|
-
}
|
|
63
|
-
isMutateFn = true;
|
|
64
|
-
break;
|
|
65
|
-
}
|
|
66
|
-
const result = origMethod.apply(target, args);
|
|
67
|
-
if (isMutateFn && changeSplice) {
|
|
68
|
-
if (prop === "splice") {
|
|
69
|
-
this.next({
|
|
70
|
-
type: changeType,
|
|
71
|
-
index,
|
|
72
|
-
items: itemsToEmit
|
|
73
|
-
});
|
|
74
|
-
} else {
|
|
75
|
-
this.next({
|
|
76
|
-
type: changeType,
|
|
77
|
-
index,
|
|
78
|
-
items: args
|
|
79
|
-
});
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
return result;
|
|
83
|
-
};
|
|
84
|
-
}
|
|
85
|
-
return Reflect.get(target, prop, receiver);
|
|
86
|
-
}, "get"),
|
|
87
|
-
set: /* @__PURE__ */ __name((target, prop, value) => {
|
|
88
|
-
const index = !isNaN(Number(prop)) ? Number(prop) : void 0;
|
|
89
|
-
target[prop] = value;
|
|
90
|
-
this.next({
|
|
91
|
-
type: "update",
|
|
92
|
-
index,
|
|
93
|
-
items: [
|
|
94
|
-
value
|
|
95
|
-
]
|
|
96
|
-
});
|
|
97
|
-
return true;
|
|
98
|
-
}, "set")
|
|
99
|
-
});
|
|
100
|
-
}
|
|
101
|
-
get items() {
|
|
102
|
-
return this._items;
|
|
1
|
+
// src/ws.ts
|
|
2
|
+
if (!globalThis.EventTarget || !globalThis.Event) {
|
|
3
|
+
console.error(`
|
|
4
|
+
PartySocket requires a global 'EventTarget' class to be available!
|
|
5
|
+
You can polyfill this global by adding this to your code before any partysocket imports:
|
|
6
|
+
|
|
7
|
+
\`\`\`
|
|
8
|
+
import 'partysocket/event-target-polyfill';
|
|
9
|
+
\`\`\`
|
|
10
|
+
Please file an issue at https://github.com/partykit/partykit if you're still having trouble.
|
|
11
|
+
`);
|
|
12
|
+
}
|
|
13
|
+
var ErrorEvent = class extends Event {
|
|
14
|
+
message;
|
|
15
|
+
error;
|
|
16
|
+
// biome-ignore lint/suspicious/noExplicitAny: vibes
|
|
17
|
+
constructor(error, target) {
|
|
18
|
+
super("error", target);
|
|
19
|
+
this.message = error.message;
|
|
20
|
+
this.error = error;
|
|
103
21
|
}
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
22
|
+
};
|
|
23
|
+
var CloseEvent = class extends Event {
|
|
24
|
+
code;
|
|
25
|
+
reason;
|
|
26
|
+
wasClean = true;
|
|
27
|
+
// biome-ignore lint/style/useDefaultParameterLast: legacy
|
|
28
|
+
// biome-ignore lint/suspicious/noExplicitAny: legacy
|
|
29
|
+
constructor(code = 1e3, reason = "", target) {
|
|
30
|
+
super("close", target);
|
|
31
|
+
this.code = code;
|
|
32
|
+
this.reason = reason;
|
|
110
33
|
}
|
|
111
34
|
};
|
|
112
|
-
var
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
value: obj
|
|
121
|
-
});
|
|
122
|
-
this.createProxy(obj);
|
|
123
|
-
}
|
|
124
|
-
createProxy(obj) {
|
|
125
|
-
this._obj = new Proxy(obj, {
|
|
126
|
-
get: /* @__PURE__ */ __name((target, prop, receiver) => {
|
|
127
|
-
return Reflect.get(target, prop, receiver);
|
|
128
|
-
}, "get"),
|
|
129
|
-
set: /* @__PURE__ */ __name((target, prop, value, receiver) => {
|
|
130
|
-
const key = prop;
|
|
131
|
-
const changeType = key in target ? "update" : "add";
|
|
132
|
-
target[key] = value;
|
|
133
|
-
this.next({
|
|
134
|
-
type: changeType,
|
|
135
|
-
key,
|
|
136
|
-
value
|
|
137
|
-
});
|
|
138
|
-
return true;
|
|
139
|
-
}, "set"),
|
|
140
|
-
deleteProperty: /* @__PURE__ */ __name((target, prop) => {
|
|
141
|
-
const key = prop;
|
|
142
|
-
if (key in target) {
|
|
143
|
-
const value = target[key];
|
|
144
|
-
delete target[key];
|
|
145
|
-
this.next({
|
|
146
|
-
type: "remove",
|
|
147
|
-
key,
|
|
148
|
-
value
|
|
149
|
-
});
|
|
150
|
-
return true;
|
|
151
|
-
}
|
|
152
|
-
return false;
|
|
153
|
-
}, "deleteProperty")
|
|
154
|
-
});
|
|
35
|
+
var Events = {
|
|
36
|
+
Event,
|
|
37
|
+
ErrorEvent,
|
|
38
|
+
CloseEvent
|
|
39
|
+
};
|
|
40
|
+
function assert(condition, msg) {
|
|
41
|
+
if (!condition) {
|
|
42
|
+
throw new Error(msg);
|
|
155
43
|
}
|
|
156
|
-
|
|
157
|
-
|
|
44
|
+
}
|
|
45
|
+
function cloneEventBrowser(e) {
|
|
46
|
+
return new e.constructor(e.type, e);
|
|
47
|
+
}
|
|
48
|
+
function cloneEventNode(e) {
|
|
49
|
+
if ("data" in e) {
|
|
50
|
+
const evt2 = new MessageEvent(e.type, e);
|
|
51
|
+
return evt2;
|
|
158
52
|
}
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
53
|
+
if ("code" in e || "reason" in e) {
|
|
54
|
+
const evt2 = new CloseEvent(
|
|
55
|
+
// @ts-expect-error we need to fix event/listener types
|
|
56
|
+
e.code || 1999,
|
|
57
|
+
// @ts-expect-error we need to fix event/listener types
|
|
58
|
+
e.reason || "unknown reason",
|
|
59
|
+
e
|
|
60
|
+
);
|
|
61
|
+
return evt2;
|
|
165
62
|
}
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
63
|
+
if ("error" in e) {
|
|
64
|
+
const evt2 = new ErrorEvent(e.error, e);
|
|
65
|
+
return evt2;
|
|
66
|
+
}
|
|
67
|
+
const evt = new Event(e.type, e);
|
|
68
|
+
return evt;
|
|
69
|
+
}
|
|
70
|
+
var _a;
|
|
71
|
+
var isNode =
|
|
72
|
+
typeof process !== "undefined" &&
|
|
73
|
+
typeof ((_a = process.versions) == null ? void 0 : _a.node) !== "undefined" &&
|
|
74
|
+
typeof document === "undefined";
|
|
75
|
+
var cloneEvent = isNode ? cloneEventNode : cloneEventBrowser;
|
|
76
|
+
var DEFAULT = {
|
|
77
|
+
maxReconnectionDelay: 1e4,
|
|
78
|
+
minReconnectionDelay: 1e3 + Math.random() * 4e3,
|
|
79
|
+
minUptime: 5e3,
|
|
80
|
+
reconnectionDelayGrowFactor: 1.3,
|
|
81
|
+
connectionTimeout: 4e3,
|
|
82
|
+
maxRetries: Number.POSITIVE_INFINITY,
|
|
83
|
+
maxEnqueuedMessages: Number.POSITIVE_INFINITY};
|
|
84
|
+
var didWarnAboutMissingWebSocket = false;
|
|
85
|
+
var ReconnectingWebSocket = class _ReconnectingWebSocket extends EventTarget {
|
|
86
|
+
_ws;
|
|
87
|
+
_retryCount = -1;
|
|
88
|
+
_uptimeTimeout;
|
|
89
|
+
_connectTimeout;
|
|
90
|
+
_shouldReconnect = true;
|
|
91
|
+
_connectLock = false;
|
|
92
|
+
_binaryType = "blob";
|
|
93
|
+
_closeCalled = false;
|
|
94
|
+
_messageQueue = [];
|
|
95
|
+
_debugLogger = console.log.bind(console);
|
|
96
|
+
_url;
|
|
97
|
+
_protocols;
|
|
98
|
+
_options;
|
|
99
|
+
constructor(url, protocols, options = {}) {
|
|
100
|
+
super();
|
|
101
|
+
this._url = url;
|
|
102
|
+
this._protocols = protocols;
|
|
103
|
+
this._options = options;
|
|
104
|
+
if (this._options.startClosed) {
|
|
105
|
+
this._shouldReconnect = false;
|
|
106
|
+
}
|
|
107
|
+
if (this._options.debugLogger) {
|
|
108
|
+
this._debugLogger = this._options.debugLogger;
|
|
109
|
+
}
|
|
110
|
+
this._connect();
|
|
111
|
+
}
|
|
112
|
+
static get CONNECTING() {
|
|
113
|
+
return 0;
|
|
114
|
+
}
|
|
115
|
+
static get OPEN() {
|
|
116
|
+
return 1;
|
|
117
|
+
}
|
|
118
|
+
static get CLOSING() {
|
|
119
|
+
return 2;
|
|
120
|
+
}
|
|
121
|
+
static get CLOSED() {
|
|
122
|
+
return 3;
|
|
123
|
+
}
|
|
124
|
+
get CONNECTING() {
|
|
125
|
+
return _ReconnectingWebSocket.CONNECTING;
|
|
126
|
+
}
|
|
127
|
+
get OPEN() {
|
|
128
|
+
return _ReconnectingWebSocket.OPEN;
|
|
129
|
+
}
|
|
130
|
+
get CLOSING() {
|
|
131
|
+
return _ReconnectingWebSocket.CLOSING;
|
|
132
|
+
}
|
|
133
|
+
get CLOSED() {
|
|
134
|
+
return _ReconnectingWebSocket.CLOSED;
|
|
135
|
+
}
|
|
136
|
+
get binaryType() {
|
|
137
|
+
return this._ws ? this._ws.binaryType : this._binaryType;
|
|
138
|
+
}
|
|
139
|
+
set binaryType(value) {
|
|
140
|
+
this._binaryType = value;
|
|
141
|
+
if (this._ws) {
|
|
142
|
+
this._ws.binaryType = value;
|
|
175
143
|
}
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
},
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Returns the number or connection retries
|
|
147
|
+
*/
|
|
148
|
+
get retryCount() {
|
|
149
|
+
return Math.max(this._retryCount, 0);
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* The number of bytes of data that have been queued using calls to send() but not yet
|
|
153
|
+
* transmitted to the network. This value resets to zero once all queued data has been sent.
|
|
154
|
+
* This value does not reset to zero when the connection is closed; if you keep calling send(),
|
|
155
|
+
* this will continue to climb. Read only
|
|
156
|
+
*/
|
|
157
|
+
get bufferedAmount() {
|
|
158
|
+
const bytes = this._messageQueue.reduce((acc, message) => {
|
|
159
|
+
if (typeof message === "string") {
|
|
160
|
+
acc += message.length;
|
|
161
|
+
} else if (message instanceof Blob) {
|
|
162
|
+
acc += message.size;
|
|
163
|
+
} else {
|
|
164
|
+
acc += message.byteLength;
|
|
165
|
+
}
|
|
166
|
+
return acc;
|
|
167
|
+
}, 0);
|
|
168
|
+
return bytes + (this._ws ? this._ws.bufferedAmount : 0);
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* The extensions selected by the server. This is currently only the empty string or a list of
|
|
172
|
+
* extensions as negotiated by the connection
|
|
173
|
+
*/
|
|
174
|
+
get extensions() {
|
|
175
|
+
return this._ws ? this._ws.extensions : "";
|
|
176
|
+
}
|
|
177
|
+
/**
|
|
178
|
+
* A string indicating the name of the sub-protocol the server selected;
|
|
179
|
+
* this will be one of the strings specified in the protocols parameter when creating the
|
|
180
|
+
* WebSocket object
|
|
181
|
+
*/
|
|
182
|
+
get protocol() {
|
|
183
|
+
return this._ws ? this._ws.protocol : "";
|
|
184
|
+
}
|
|
185
|
+
/**
|
|
186
|
+
* The current state of the connection; this is one of the Ready state constants
|
|
187
|
+
*/
|
|
188
|
+
get readyState() {
|
|
189
|
+
if (this._ws) {
|
|
190
|
+
return this._ws.readyState;
|
|
220
191
|
}
|
|
221
|
-
return
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
192
|
+
return this._options.startClosed
|
|
193
|
+
? _ReconnectingWebSocket.CLOSED
|
|
194
|
+
: _ReconnectingWebSocket.CONNECTING;
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
* The URL as resolved by the constructor
|
|
198
|
+
*/
|
|
199
|
+
get url() {
|
|
200
|
+
return this._ws ? this._ws.url : "";
|
|
201
|
+
}
|
|
202
|
+
/**
|
|
203
|
+
* Whether the websocket object is now in reconnectable state
|
|
204
|
+
*/
|
|
205
|
+
get shouldReconnect() {
|
|
206
|
+
return this._shouldReconnect;
|
|
207
|
+
}
|
|
208
|
+
/**
|
|
209
|
+
* An event listener to be called when the WebSocket connection's readyState changes to CLOSED
|
|
210
|
+
*/
|
|
211
|
+
onclose = null;
|
|
212
|
+
/**
|
|
213
|
+
* An event listener to be called when an error occurs
|
|
214
|
+
*/
|
|
215
|
+
onerror = null;
|
|
216
|
+
/**
|
|
217
|
+
* An event listener to be called when a message is received from the server
|
|
218
|
+
*/
|
|
219
|
+
onmessage = null;
|
|
220
|
+
/**
|
|
221
|
+
* An event listener to be called when the WebSocket connection's readyState changes to OPEN;
|
|
222
|
+
* this indicates that the connection is ready to send and receive data
|
|
223
|
+
*/
|
|
224
|
+
onopen = null;
|
|
225
|
+
/**
|
|
226
|
+
* Closes the WebSocket connection or connection attempt, if any. If the connection is already
|
|
227
|
+
* CLOSED, this method does nothing
|
|
228
|
+
*/
|
|
229
|
+
close(code = 1e3, reason) {
|
|
230
|
+
this._closeCalled = true;
|
|
231
|
+
this._shouldReconnect = false;
|
|
232
|
+
this._clearTimeouts();
|
|
233
|
+
if (!this._ws) {
|
|
234
|
+
this._debug("close enqueued: no ws instance");
|
|
235
|
+
return;
|
|
236
|
+
}
|
|
237
|
+
if (this._ws.readyState === this.CLOSED) {
|
|
238
|
+
this._debug("close: already closed");
|
|
239
|
+
return;
|
|
240
|
+
}
|
|
241
|
+
this._ws.close(code, reason);
|
|
242
|
+
}
|
|
243
|
+
/**
|
|
244
|
+
* Closes the WebSocket connection or connection attempt and connects again.
|
|
245
|
+
* Resets retry counter;
|
|
246
|
+
*/
|
|
247
|
+
reconnect(code, reason) {
|
|
248
|
+
this._shouldReconnect = true;
|
|
249
|
+
this._closeCalled = false;
|
|
250
|
+
this._retryCount = -1;
|
|
251
|
+
if (!this._ws || this._ws.readyState === this.CLOSED) {
|
|
252
|
+
this._connect();
|
|
232
253
|
} else {
|
|
233
|
-
|
|
254
|
+
this._disconnect(code, reason);
|
|
255
|
+
this._connect();
|
|
234
256
|
}
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
subject.next({
|
|
244
|
-
type: "init",
|
|
245
|
-
items: subject.items
|
|
246
|
-
});
|
|
247
|
-
} else if (subject instanceof ObjectSubject) {
|
|
248
|
-
subject.next({
|
|
249
|
-
type: "init",
|
|
250
|
-
value: subject.obj
|
|
251
|
-
});
|
|
257
|
+
}
|
|
258
|
+
/**
|
|
259
|
+
* Enqueue specified data to be transmitted to the server over the WebSocket connection
|
|
260
|
+
*/
|
|
261
|
+
send(data) {
|
|
262
|
+
if (this._ws && this._ws.readyState === this.OPEN) {
|
|
263
|
+
this._debug("send", data);
|
|
264
|
+
this._ws.send(data);
|
|
252
265
|
} else {
|
|
253
|
-
|
|
266
|
+
const { maxEnqueuedMessages = DEFAULT.maxEnqueuedMessages } =
|
|
267
|
+
this._options;
|
|
268
|
+
if (this._messageQueue.length < maxEnqueuedMessages) {
|
|
269
|
+
this._debug("enqueue", data);
|
|
270
|
+
this._messageQueue.push(data);
|
|
271
|
+
}
|
|
254
272
|
}
|
|
273
|
+
}
|
|
274
|
+
_debug(...args) {
|
|
275
|
+
if (this._options.debug) {
|
|
276
|
+
this._debugLogger("RWS>", ...args);
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
_getNextDelay() {
|
|
280
|
+
const {
|
|
281
|
+
reconnectionDelayGrowFactor = DEFAULT.reconnectionDelayGrowFactor,
|
|
282
|
+
minReconnectionDelay = DEFAULT.minReconnectionDelay,
|
|
283
|
+
maxReconnectionDelay = DEFAULT.maxReconnectionDelay
|
|
284
|
+
} = this._options;
|
|
285
|
+
let delay = 0;
|
|
286
|
+
if (this._retryCount > 0) {
|
|
287
|
+
delay =
|
|
288
|
+
minReconnectionDelay *
|
|
289
|
+
reconnectionDelayGrowFactor ** (this._retryCount - 1);
|
|
290
|
+
if (delay > maxReconnectionDelay) {
|
|
291
|
+
delay = maxReconnectionDelay;
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
this._debug("next delay", delay);
|
|
295
|
+
return delay;
|
|
296
|
+
}
|
|
297
|
+
_wait() {
|
|
298
|
+
return new Promise((resolve) => {
|
|
299
|
+
setTimeout(resolve, this._getNextDelay());
|
|
300
|
+
});
|
|
301
|
+
}
|
|
302
|
+
_getNextProtocols(protocolsProvider) {
|
|
303
|
+
if (!protocolsProvider) return Promise.resolve(null);
|
|
304
|
+
if (
|
|
305
|
+
typeof protocolsProvider === "string" ||
|
|
306
|
+
Array.isArray(protocolsProvider)
|
|
307
|
+
) {
|
|
308
|
+
return Promise.resolve(protocolsProvider);
|
|
309
|
+
}
|
|
310
|
+
if (typeof protocolsProvider === "function") {
|
|
311
|
+
const protocols = protocolsProvider();
|
|
312
|
+
if (!protocols) return Promise.resolve(null);
|
|
313
|
+
if (typeof protocols === "string" || Array.isArray(protocols)) {
|
|
314
|
+
return Promise.resolve(protocols);
|
|
315
|
+
}
|
|
316
|
+
if (protocols.then) {
|
|
317
|
+
return protocols;
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
throw Error("Invalid protocols");
|
|
321
|
+
}
|
|
322
|
+
_getNextUrl(urlProvider) {
|
|
323
|
+
if (typeof urlProvider === "string") {
|
|
324
|
+
return Promise.resolve(urlProvider);
|
|
325
|
+
}
|
|
326
|
+
if (typeof urlProvider === "function") {
|
|
327
|
+
const url = urlProvider();
|
|
328
|
+
if (typeof url === "string") {
|
|
329
|
+
return Promise.resolve(url);
|
|
330
|
+
}
|
|
331
|
+
if (url.then) {
|
|
332
|
+
return url;
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
throw Error("Invalid URL");
|
|
336
|
+
}
|
|
337
|
+
_connect() {
|
|
338
|
+
if (this._connectLock || !this._shouldReconnect) {
|
|
339
|
+
return;
|
|
340
|
+
}
|
|
341
|
+
this._connectLock = true;
|
|
342
|
+
const {
|
|
343
|
+
maxRetries = DEFAULT.maxRetries,
|
|
344
|
+
connectionTimeout = DEFAULT.connectionTimeout
|
|
345
|
+
} = this._options;
|
|
346
|
+
if (this._retryCount >= maxRetries) {
|
|
347
|
+
this._debug("max retries reached", this._retryCount, ">=", maxRetries);
|
|
348
|
+
return;
|
|
349
|
+
}
|
|
350
|
+
this._retryCount++;
|
|
351
|
+
this._debug("connect", this._retryCount);
|
|
352
|
+
this._removeListeners();
|
|
353
|
+
this._wait()
|
|
354
|
+
.then(() =>
|
|
355
|
+
Promise.all([
|
|
356
|
+
this._getNextUrl(this._url),
|
|
357
|
+
this._getNextProtocols(this._protocols || null)
|
|
358
|
+
])
|
|
359
|
+
)
|
|
360
|
+
.then(([url, protocols]) => {
|
|
361
|
+
if (this._closeCalled) {
|
|
362
|
+
this._connectLock = false;
|
|
363
|
+
return;
|
|
364
|
+
}
|
|
365
|
+
if (
|
|
366
|
+
!this._options.WebSocket &&
|
|
367
|
+
typeof WebSocket === "undefined" &&
|
|
368
|
+
!didWarnAboutMissingWebSocket
|
|
369
|
+
) {
|
|
370
|
+
console.error(`\u203C\uFE0F No WebSocket implementation available. You should define options.WebSocket.
|
|
371
|
+
|
|
372
|
+
For example, if you're using node.js, run \`npm install ws\`, and then in your code:
|
|
373
|
+
|
|
374
|
+
import PartySocket from 'partysocket';
|
|
375
|
+
import WS from 'ws';
|
|
376
|
+
|
|
377
|
+
const partysocket = new PartySocket({
|
|
378
|
+
host: "127.0.0.1:1999",
|
|
379
|
+
room: "test-room",
|
|
380
|
+
WebSocket: WS
|
|
381
|
+
});
|
|
382
|
+
|
|
383
|
+
`);
|
|
384
|
+
didWarnAboutMissingWebSocket = true;
|
|
385
|
+
}
|
|
386
|
+
const WS = this._options.WebSocket || WebSocket;
|
|
387
|
+
this._debug("connect", { url, protocols });
|
|
388
|
+
this._ws = protocols ? new WS(url, protocols) : new WS(url);
|
|
389
|
+
this._ws.binaryType = this._binaryType;
|
|
390
|
+
this._connectLock = false;
|
|
391
|
+
this._addListeners();
|
|
392
|
+
this._connectTimeout = setTimeout(
|
|
393
|
+
() => this._handleTimeout(),
|
|
394
|
+
connectionTimeout
|
|
395
|
+
);
|
|
396
|
+
})
|
|
397
|
+
.catch((err) => {
|
|
398
|
+
this._connectLock = false;
|
|
399
|
+
this._handleError(new Events.ErrorEvent(Error(err.message), this));
|
|
400
|
+
});
|
|
401
|
+
}
|
|
402
|
+
_handleTimeout() {
|
|
403
|
+
this._debug("timeout event");
|
|
404
|
+
this._handleError(new Events.ErrorEvent(Error("TIMEOUT"), this));
|
|
405
|
+
}
|
|
406
|
+
_disconnect(code = 1e3, reason) {
|
|
407
|
+
this._clearTimeouts();
|
|
408
|
+
if (!this._ws) {
|
|
409
|
+
return;
|
|
410
|
+
}
|
|
411
|
+
this._removeListeners();
|
|
412
|
+
try {
|
|
413
|
+
if (this._ws.readyState === this.OPEN) {
|
|
414
|
+
this._ws.close(code, reason);
|
|
415
|
+
}
|
|
416
|
+
this._handleClose(new Events.CloseEvent(code, reason, this));
|
|
417
|
+
} catch (error) {}
|
|
418
|
+
}
|
|
419
|
+
_acceptOpen() {
|
|
420
|
+
this._debug("accept open");
|
|
421
|
+
this._retryCount = 0;
|
|
422
|
+
}
|
|
423
|
+
_handleOpen = (event) => {
|
|
424
|
+
this._debug("open event");
|
|
425
|
+
const { minUptime = DEFAULT.minUptime } = this._options;
|
|
426
|
+
clearTimeout(this._connectTimeout);
|
|
427
|
+
this._uptimeTimeout = setTimeout(() => this._acceptOpen(), minUptime);
|
|
428
|
+
assert(this._ws, "WebSocket is not defined");
|
|
429
|
+
this._ws.binaryType = this._binaryType;
|
|
430
|
+
this._messageQueue.forEach((message) => {
|
|
431
|
+
var _a2;
|
|
432
|
+
return (_a2 = this._ws) == null ? void 0 : _a2.send(message);
|
|
433
|
+
});
|
|
434
|
+
this._messageQueue = [];
|
|
435
|
+
if (this.onopen) {
|
|
436
|
+
this.onopen(event);
|
|
437
|
+
}
|
|
438
|
+
this.dispatchEvent(cloneEvent(event));
|
|
255
439
|
};
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
440
|
+
_handleMessage = (event) => {
|
|
441
|
+
this._debug("message event");
|
|
442
|
+
if (this.onmessage) {
|
|
443
|
+
this.onmessage(event);
|
|
444
|
+
}
|
|
445
|
+
this.dispatchEvent(cloneEvent(event));
|
|
259
446
|
};
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
447
|
+
_handleError = (event) => {
|
|
448
|
+
this._debug("error event", event.message);
|
|
449
|
+
this._disconnect(void 0, event.message === "TIMEOUT" ? "timeout" : void 0);
|
|
450
|
+
if (this.onerror) {
|
|
451
|
+
this.onerror(event);
|
|
452
|
+
}
|
|
453
|
+
this._debug("exec error listeners");
|
|
454
|
+
this.dispatchEvent(cloneEvent(event));
|
|
455
|
+
this._connect();
|
|
263
456
|
};
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
return isSignal(value) && !!value.dependencies;
|
|
275
|
-
}
|
|
276
|
-
__name(isComputed, "isComputed");
|
|
277
|
-
function computed(computeFunction, disposableFn) {
|
|
278
|
-
const dependencies = /* @__PURE__ */ new Set();
|
|
279
|
-
let init = true;
|
|
280
|
-
let lastComputedValue;
|
|
281
|
-
const previousTracker = reactiveStore.currentDependencyTracker;
|
|
282
|
-
reactiveStore.currentDependencyTracker = (signal2) => {
|
|
283
|
-
dependencies.add(signal2);
|
|
457
|
+
_handleClose = (event) => {
|
|
458
|
+
this._debug("close event");
|
|
459
|
+
this._clearTimeouts();
|
|
460
|
+
if (this._shouldReconnect) {
|
|
461
|
+
this._connect();
|
|
462
|
+
}
|
|
463
|
+
if (this.onclose) {
|
|
464
|
+
this.onclose(event);
|
|
465
|
+
}
|
|
466
|
+
this.dispatchEvent(cloneEvent(event));
|
|
284
467
|
};
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
468
|
+
_removeListeners() {
|
|
469
|
+
if (!this._ws) {
|
|
470
|
+
return;
|
|
471
|
+
}
|
|
472
|
+
this._debug("removeListeners");
|
|
473
|
+
this._ws.removeEventListener("open", this._handleOpen);
|
|
474
|
+
this._ws.removeEventListener("close", this._handleClose);
|
|
475
|
+
this._ws.removeEventListener("message", this._handleMessage);
|
|
476
|
+
this._ws.removeEventListener("error", this._handleError);
|
|
477
|
+
}
|
|
478
|
+
_addListeners() {
|
|
479
|
+
if (!this._ws) {
|
|
480
|
+
return;
|
|
481
|
+
}
|
|
482
|
+
this._debug("addListeners");
|
|
483
|
+
this._ws.addEventListener("open", this._handleOpen);
|
|
484
|
+
this._ws.addEventListener("close", this._handleClose);
|
|
485
|
+
this._ws.addEventListener("message", this._handleMessage);
|
|
486
|
+
this._ws.addEventListener("error", this._handleError);
|
|
487
|
+
}
|
|
488
|
+
_clearTimeouts() {
|
|
489
|
+
clearTimeout(this._connectTimeout);
|
|
490
|
+
clearTimeout(this._uptimeTimeout);
|
|
491
|
+
}
|
|
492
|
+
};
|
|
493
|
+
/*!
|
|
494
|
+
* Reconnecting WebSocket
|
|
495
|
+
* by Pedro Ladaria <pedro.ladaria@gmail.com>
|
|
496
|
+
* https://github.com/pladaria/reconnecting-websocket
|
|
497
|
+
* License MIT
|
|
498
|
+
*/
|
|
314
499
|
|
|
315
|
-
export {
|
|
500
|
+
export { CloseEvent, ErrorEvent, ReconnectingWebSocket };
|
|
316
501
|
//# sourceMappingURL=index35.js.map
|