@rpgjs/client 5.0.0-alpha.43 → 5.0.0-alpha.44

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.
Files changed (137) hide show
  1. package/dist/Game/AnimationManager.js +25 -24
  2. package/dist/Game/AnimationManager.js.map +1 -1
  3. package/dist/Game/Event.js +11 -10
  4. package/dist/Game/Event.js.map +1 -1
  5. package/dist/Game/Map.js +78 -94
  6. package/dist/Game/Map.js.map +1 -1
  7. package/dist/Game/Object.js +182 -216
  8. package/dist/Game/Object.js.map +1 -1
  9. package/dist/Game/Player.js +11 -10
  10. package/dist/Game/Player.js.map +1 -1
  11. package/dist/Gui/Gui.js +440 -473
  12. package/dist/Gui/Gui.js.map +1 -1
  13. package/dist/Gui/NotificationManager.js +48 -50
  14. package/dist/Gui/NotificationManager.js.map +1 -1
  15. package/dist/Resource.js +132 -113
  16. package/dist/Resource.js.map +1 -1
  17. package/dist/RpgClientEngine.js +1333 -1486
  18. package/dist/RpgClientEngine.js.map +1 -1
  19. package/dist/Sound.js +162 -92
  20. package/dist/Sound.js.map +1 -1
  21. package/dist/_virtual/_@oxc-project_runtime@0.115.0/helpers/decorate.js +9 -0
  22. package/dist/_virtual/_@oxc-project_runtime@0.115.0/helpers/decorateMetadata.js +6 -0
  23. package/dist/components/animations/animation.ce.js +22 -19
  24. package/dist/components/animations/animation.ce.js.map +1 -1
  25. package/dist/components/animations/hit.ce.js +67 -68
  26. package/dist/components/animations/hit.ce.js.map +1 -1
  27. package/dist/components/animations/index.js +9 -8
  28. package/dist/components/animations/index.js.map +1 -1
  29. package/dist/components/character.ce.js +390 -314
  30. package/dist/components/character.ce.js.map +1 -1
  31. package/dist/components/dynamics/parse-value.js +42 -52
  32. package/dist/components/dynamics/parse-value.js.map +1 -1
  33. package/dist/components/dynamics/text.ce.js +71 -139
  34. package/dist/components/dynamics/text.ce.js.map +1 -1
  35. package/dist/components/gui/box.ce.js +26 -25
  36. package/dist/components/gui/box.ce.js.map +1 -1
  37. package/dist/components/gui/dialogbox/index.ce.js +202 -149
  38. package/dist/components/gui/dialogbox/index.ce.js.map +1 -1
  39. package/dist/components/gui/gameover.ce.js +190 -138
  40. package/dist/components/gui/gameover.ce.js.map +1 -1
  41. package/dist/components/gui/hud/hud.ce.js +90 -33
  42. package/dist/components/gui/hud/hud.ce.js.map +1 -1
  43. package/dist/components/gui/index.js +14 -0
  44. package/dist/components/gui/menu/equip-menu.ce.js +478 -346
  45. package/dist/components/gui/menu/equip-menu.ce.js.map +1 -1
  46. package/dist/components/gui/menu/exit-menu.ce.js +52 -33
  47. package/dist/components/gui/menu/exit-menu.ce.js.map +1 -1
  48. package/dist/components/gui/menu/items-menu.ce.js +341 -226
  49. package/dist/components/gui/menu/items-menu.ce.js.map +1 -1
  50. package/dist/components/gui/menu/main-menu.ce.js +414 -205
  51. package/dist/components/gui/menu/main-menu.ce.js.map +1 -1
  52. package/dist/components/gui/menu/options-menu.ce.js +46 -26
  53. package/dist/components/gui/menu/options-menu.ce.js.map +1 -1
  54. package/dist/components/gui/menu/skills-menu.ce.js +104 -50
  55. package/dist/components/gui/menu/skills-menu.ce.js.map +1 -1
  56. package/dist/components/gui/mobile/index.js +18 -21
  57. package/dist/components/gui/mobile/index.js.map +1 -1
  58. package/dist/components/gui/mobile/mobile.ce.js +76 -15
  59. package/dist/components/gui/mobile/mobile.ce.js.map +1 -1
  60. package/dist/components/gui/notification/notification.ce.js +62 -36
  61. package/dist/components/gui/notification/notification.ce.js.map +1 -1
  62. package/dist/components/gui/save-load.ce.js +386 -239
  63. package/dist/components/gui/save-load.ce.js.map +1 -1
  64. package/dist/components/gui/shop/shop.ce.js +649 -319
  65. package/dist/components/gui/shop/shop.ce.js.map +1 -1
  66. package/dist/components/gui/title-screen.ce.js +187 -145
  67. package/dist/components/gui/title-screen.ce.js.map +1 -1
  68. package/dist/components/index.js +4 -0
  69. package/dist/components/prebuilt/hp-bar.ce.js +114 -104
  70. package/dist/components/prebuilt/hp-bar.ce.js.map +1 -1
  71. package/dist/components/prebuilt/index.js +2 -0
  72. package/dist/components/prebuilt/light-halo.ce.js +92 -74
  73. package/dist/components/prebuilt/light-halo.ce.js.map +1 -1
  74. package/dist/components/scenes/canvas.ce.js +58 -42
  75. package/dist/components/scenes/canvas.ce.js.map +1 -1
  76. package/dist/components/scenes/draw-map.ce.js +87 -64
  77. package/dist/components/scenes/draw-map.ce.js.map +1 -1
  78. package/dist/components/scenes/event-layer.ce.js +26 -16
  79. package/dist/components/scenes/event-layer.ce.js.map +1 -1
  80. package/dist/core/inject.js +11 -10
  81. package/dist/core/inject.js.map +1 -1
  82. package/dist/core/setup.js +13 -13
  83. package/dist/core/setup.js.map +1 -1
  84. package/dist/index.js +44 -41
  85. package/dist/module.js +169 -168
  86. package/dist/module.js.map +1 -1
  87. package/dist/node_modules/.pnpm/@signe_di@2.8.3/node_modules/@signe/di/dist/index.js +209 -298
  88. package/dist/node_modules/.pnpm/@signe_di@2.8.3/node_modules/@signe/di/dist/index.js.map +1 -1
  89. package/dist/node_modules/.pnpm/@signe_reactive@2.8.3/node_modules/@signe/reactive/dist/index.js +430 -507
  90. package/dist/node_modules/.pnpm/@signe_reactive@2.8.3/node_modules/@signe/reactive/dist/index.js.map +1 -1
  91. package/dist/node_modules/.pnpm/@signe_room@2.8.3/node_modules/@signe/room/dist/index.js +2062 -2571
  92. package/dist/node_modules/.pnpm/@signe_room@2.8.3/node_modules/@signe/room/dist/index.js.map +1 -1
  93. package/dist/node_modules/.pnpm/@signe_sync@2.8.3/node_modules/@signe/sync/dist/chunk-7QVYU63E.js +8 -3
  94. package/dist/node_modules/.pnpm/@signe_sync@2.8.3/node_modules/@signe/sync/dist/chunk-7QVYU63E.js.map +1 -1
  95. package/dist/node_modules/.pnpm/@signe_sync@2.8.3/node_modules/@signe/sync/dist/client/index.js +78 -101
  96. package/dist/node_modules/.pnpm/@signe_sync@2.8.3/node_modules/@signe/sync/dist/client/index.js.map +1 -1
  97. package/dist/node_modules/.pnpm/@signe_sync@2.8.3/node_modules/@signe/sync/dist/index.js +264 -401
  98. package/dist/node_modules/.pnpm/@signe_sync@2.8.3/node_modules/@signe/sync/dist/index.js.map +1 -1
  99. package/dist/node_modules/.pnpm/dset@3.1.4/node_modules/dset/dist/index.js +9 -7
  100. package/dist/node_modules/.pnpm/dset@3.1.4/node_modules/dset/dist/index.js.map +1 -1
  101. package/dist/node_modules/.pnpm/partysocket@1.1.3/node_modules/partysocket/dist/chunk-HAC622V3.js +106 -165
  102. package/dist/node_modules/.pnpm/partysocket@1.1.3/node_modules/partysocket/dist/chunk-HAC622V3.js.map +1 -1
  103. package/dist/node_modules/.pnpm/partysocket@1.1.3/node_modules/partysocket/dist/chunk-S74YV6PU.js +361 -461
  104. package/dist/node_modules/.pnpm/partysocket@1.1.3/node_modules/partysocket/dist/chunk-S74YV6PU.js.map +1 -1
  105. package/dist/node_modules/.pnpm/partysocket@1.1.3/node_modules/partysocket/dist/index.js +2 -0
  106. package/dist/node_modules/.pnpm/zod@3.24.2/node_modules/zod/lib/index.js +3633 -4280
  107. package/dist/node_modules/.pnpm/zod@3.24.2/node_modules/zod/lib/index.js.map +1 -1
  108. package/dist/presets/animation.js +37 -25
  109. package/dist/presets/animation.js.map +1 -1
  110. package/dist/presets/faceset.js +49 -22
  111. package/dist/presets/faceset.js.map +1 -1
  112. package/dist/presets/icon.js +13 -13
  113. package/dist/presets/icon.js.map +1 -1
  114. package/dist/presets/index.js +15 -14
  115. package/dist/presets/index.js.map +1 -1
  116. package/dist/presets/lpc.js +96 -93
  117. package/dist/presets/lpc.js.map +1 -1
  118. package/dist/presets/rmspritesheet.js +40 -39
  119. package/dist/presets/rmspritesheet.js.map +1 -1
  120. package/dist/services/AbstractSocket.js +10 -8
  121. package/dist/services/AbstractSocket.js.map +1 -1
  122. package/dist/services/keyboardControls.js +20 -18
  123. package/dist/services/keyboardControls.js.map +1 -1
  124. package/dist/services/loadMap.js +120 -36
  125. package/dist/services/loadMap.js.map +1 -1
  126. package/dist/services/mmorpg.js +128 -136
  127. package/dist/services/mmorpg.js.map +1 -1
  128. package/dist/services/save.js +74 -66
  129. package/dist/services/save.js.map +1 -1
  130. package/dist/services/standalone.js +165 -167
  131. package/dist/services/standalone.js.map +1 -1
  132. package/dist/utils/getEntityProp.js +49 -51
  133. package/dist/utils/getEntityProp.js.map +1 -1
  134. package/package.json +8 -8
  135. package/src/components/character.ce +1 -1
  136. package/src/components/scenes/draw-map.ce +3 -1
  137. package/dist/index.js.map +0 -1
@@ -1,6 +1,5 @@
1
- // src/ws.ts
2
- if (!globalThis.EventTarget || !globalThis.Event) {
3
- console.error(`
1
+ //#region ../../node_modules/.pnpm/partysocket@1.1.3/node_modules/partysocket/dist/chunk-S74YV6PU.mjs
2
+ if (!globalThis.EventTarget || !globalThis.Event) console.error(`
4
3
  PartySocket requires a global 'EventTarget' class to be available!
5
4
  You can polyfill this global by adding this to your code before any partysocket imports:
6
5
 
@@ -9,365 +8,284 @@ if (!globalThis.EventTarget || !globalThis.Event) {
9
8
  \`\`\`
10
9
  Please file an issue at https://github.com/partykit/partykit if you're still having trouble.
11
10
  `);
12
- }
13
11
  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;
21
- }
12
+ message;
13
+ error;
14
+ constructor(error, target) {
15
+ super("error", target);
16
+ this.message = error.message;
17
+ this.error = error;
18
+ }
22
19
  };
23
20
  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;
33
- }
21
+ code;
22
+ reason;
23
+ wasClean = true;
24
+ constructor(code = 1e3, reason = "", target) {
25
+ super("close", target);
26
+ this.code = code;
27
+ this.reason = reason;
28
+ }
34
29
  };
35
30
  var Events = {
36
- Event,
37
- ErrorEvent,
38
- CloseEvent
31
+ Event,
32
+ ErrorEvent,
33
+ CloseEvent
39
34
  };
40
35
  function assert(condition, msg) {
41
- if (!condition) {
42
- throw new Error(msg);
43
- }
36
+ if (!condition) throw new Error(msg);
44
37
  }
45
38
  function cloneEventBrowser(e) {
46
- return new e.constructor(e.type, e);
39
+ return new e.constructor(e.type, e);
47
40
  }
48
41
  function cloneEventNode(e) {
49
- if ("data" in e) {
50
- const evt2 = new MessageEvent(e.type, e);
51
- return evt2;
52
- }
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;
62
- }
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;
42
+ if ("data" in e) return new MessageEvent(e.type, e);
43
+ if ("code" in e || "reason" in e) return new CloseEvent(e.code || 1999, e.reason || "unknown reason", e);
44
+ if ("error" in e) return new ErrorEvent(e.error, e);
45
+ return new Event(e.type, e);
69
46
  }
70
47
  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;
48
+ var cloneEvent = typeof process !== "undefined" && typeof ((_a = process.versions) == null ? void 0 : _a.node) !== "undefined" && typeof document === "undefined" ? cloneEventNode : cloneEventBrowser;
76
49
  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};
50
+ maxReconnectionDelay: 1e4,
51
+ minReconnectionDelay: 1e3 + Math.random() * 4e3,
52
+ minUptime: 5e3,
53
+ reconnectionDelayGrowFactor: 1.3,
54
+ connectionTimeout: 4e3,
55
+ maxRetries: Number.POSITIVE_INFINITY,
56
+ maxEnqueuedMessages: Number.POSITIVE_INFINITY,
57
+ startClosed: false,
58
+ debug: false
59
+ };
84
60
  var didWarnAboutMissingWebSocket = false;
85
61
  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;
143
- }
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;
191
- }
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();
253
- } else {
254
- this._disconnect(code, reason);
255
- this._connect();
256
- }
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);
265
- } else {
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
- }
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.
62
+ _ws;
63
+ _retryCount = -1;
64
+ _uptimeTimeout;
65
+ _connectTimeout;
66
+ _shouldReconnect = true;
67
+ _connectLock = false;
68
+ _binaryType = "blob";
69
+ _closeCalled = false;
70
+ _messageQueue = [];
71
+ _debugLogger = console.log.bind(console);
72
+ _url;
73
+ _protocols;
74
+ _options;
75
+ constructor(url, protocols, options = {}) {
76
+ super();
77
+ this._url = url;
78
+ this._protocols = protocols;
79
+ this._options = options;
80
+ if (this._options.startClosed) this._shouldReconnect = false;
81
+ if (this._options.debugLogger) this._debugLogger = this._options.debugLogger;
82
+ this._connect();
83
+ }
84
+ static get CONNECTING() {
85
+ return 0;
86
+ }
87
+ static get OPEN() {
88
+ return 1;
89
+ }
90
+ static get CLOSING() {
91
+ return 2;
92
+ }
93
+ static get CLOSED() {
94
+ return 3;
95
+ }
96
+ get CONNECTING() {
97
+ return _ReconnectingWebSocket.CONNECTING;
98
+ }
99
+ get OPEN() {
100
+ return _ReconnectingWebSocket.OPEN;
101
+ }
102
+ get CLOSING() {
103
+ return _ReconnectingWebSocket.CLOSING;
104
+ }
105
+ get CLOSED() {
106
+ return _ReconnectingWebSocket.CLOSED;
107
+ }
108
+ get binaryType() {
109
+ return this._ws ? this._ws.binaryType : this._binaryType;
110
+ }
111
+ set binaryType(value) {
112
+ this._binaryType = value;
113
+ if (this._ws) this._ws.binaryType = value;
114
+ }
115
+ /**
116
+ * Returns the number or connection retries
117
+ */
118
+ get retryCount() {
119
+ return Math.max(this._retryCount, 0);
120
+ }
121
+ /**
122
+ * The number of bytes of data that have been queued using calls to send() but not yet
123
+ * transmitted to the network. This value resets to zero once all queued data has been sent.
124
+ * This value does not reset to zero when the connection is closed; if you keep calling send(),
125
+ * this will continue to climb. Read only
126
+ */
127
+ get bufferedAmount() {
128
+ return this._messageQueue.reduce((acc, message) => {
129
+ if (typeof message === "string") acc += message.length;
130
+ else if (message instanceof Blob) acc += message.size;
131
+ else acc += message.byteLength;
132
+ return acc;
133
+ }, 0) + (this._ws ? this._ws.bufferedAmount : 0);
134
+ }
135
+ /**
136
+ * The extensions selected by the server. This is currently only the empty string or a list of
137
+ * extensions as negotiated by the connection
138
+ */
139
+ get extensions() {
140
+ return this._ws ? this._ws.extensions : "";
141
+ }
142
+ /**
143
+ * A string indicating the name of the sub-protocol the server selected;
144
+ * this will be one of the strings specified in the protocols parameter when creating the
145
+ * WebSocket object
146
+ */
147
+ get protocol() {
148
+ return this._ws ? this._ws.protocol : "";
149
+ }
150
+ /**
151
+ * The current state of the connection; this is one of the Ready state constants
152
+ */
153
+ get readyState() {
154
+ if (this._ws) return this._ws.readyState;
155
+ return this._options.startClosed ? _ReconnectingWebSocket.CLOSED : _ReconnectingWebSocket.CONNECTING;
156
+ }
157
+ /**
158
+ * The URL as resolved by the constructor
159
+ */
160
+ get url() {
161
+ return this._ws ? this._ws.url : "";
162
+ }
163
+ /**
164
+ * Whether the websocket object is now in reconnectable state
165
+ */
166
+ get shouldReconnect() {
167
+ return this._shouldReconnect;
168
+ }
169
+ /**
170
+ * An event listener to be called when the WebSocket connection's readyState changes to CLOSED
171
+ */
172
+ onclose = null;
173
+ /**
174
+ * An event listener to be called when an error occurs
175
+ */
176
+ onerror = null;
177
+ /**
178
+ * An event listener to be called when a message is received from the server
179
+ */
180
+ onmessage = null;
181
+ /**
182
+ * An event listener to be called when the WebSocket connection's readyState changes to OPEN;
183
+ * this indicates that the connection is ready to send and receive data
184
+ */
185
+ onopen = null;
186
+ /**
187
+ * Closes the WebSocket connection or connection attempt, if any. If the connection is already
188
+ * CLOSED, this method does nothing
189
+ */
190
+ close(code = 1e3, reason) {
191
+ this._closeCalled = true;
192
+ this._shouldReconnect = false;
193
+ this._clearTimeouts();
194
+ if (!this._ws) {
195
+ this._debug("close enqueued: no ws instance");
196
+ return;
197
+ }
198
+ if (this._ws.readyState === this.CLOSED) {
199
+ this._debug("close: already closed");
200
+ return;
201
+ }
202
+ this._ws.close(code, reason);
203
+ }
204
+ /**
205
+ * Closes the WebSocket connection or connection attempt and connects again.
206
+ * Resets retry counter;
207
+ */
208
+ reconnect(code, reason) {
209
+ this._shouldReconnect = true;
210
+ this._closeCalled = false;
211
+ this._retryCount = -1;
212
+ if (!this._ws || this._ws.readyState === this.CLOSED) this._connect();
213
+ else {
214
+ this._disconnect(code, reason);
215
+ this._connect();
216
+ }
217
+ }
218
+ /**
219
+ * Enqueue specified data to be transmitted to the server over the WebSocket connection
220
+ */
221
+ send(data) {
222
+ if (this._ws && this._ws.readyState === this.OPEN) {
223
+ this._debug("send", data);
224
+ this._ws.send(data);
225
+ } else {
226
+ const { maxEnqueuedMessages = DEFAULT.maxEnqueuedMessages } = this._options;
227
+ if (this._messageQueue.length < maxEnqueuedMessages) {
228
+ this._debug("enqueue", data);
229
+ this._messageQueue.push(data);
230
+ }
231
+ }
232
+ }
233
+ _debug(...args) {
234
+ if (this._options.debug) this._debugLogger("RWS>", ...args);
235
+ }
236
+ _getNextDelay() {
237
+ const { reconnectionDelayGrowFactor = DEFAULT.reconnectionDelayGrowFactor, minReconnectionDelay = DEFAULT.minReconnectionDelay, maxReconnectionDelay = DEFAULT.maxReconnectionDelay } = this._options;
238
+ let delay = 0;
239
+ if (this._retryCount > 0) {
240
+ delay = minReconnectionDelay * reconnectionDelayGrowFactor ** (this._retryCount - 1);
241
+ if (delay > maxReconnectionDelay) delay = maxReconnectionDelay;
242
+ }
243
+ this._debug("next delay", delay);
244
+ return delay;
245
+ }
246
+ _wait() {
247
+ return new Promise((resolve) => {
248
+ setTimeout(resolve, this._getNextDelay());
249
+ });
250
+ }
251
+ _getNextProtocols(protocolsProvider) {
252
+ if (!protocolsProvider) return Promise.resolve(null);
253
+ if (typeof protocolsProvider === "string" || Array.isArray(protocolsProvider)) return Promise.resolve(protocolsProvider);
254
+ if (typeof protocolsProvider === "function") {
255
+ const protocols = protocolsProvider();
256
+ if (!protocols) return Promise.resolve(null);
257
+ if (typeof protocols === "string" || Array.isArray(protocols)) return Promise.resolve(protocols);
258
+ if (protocols.then) return protocols;
259
+ }
260
+ throw Error("Invalid protocols");
261
+ }
262
+ _getNextUrl(urlProvider) {
263
+ if (typeof urlProvider === "string") return Promise.resolve(urlProvider);
264
+ if (typeof urlProvider === "function") {
265
+ const url = urlProvider();
266
+ if (typeof url === "string") return Promise.resolve(url);
267
+ if (url.then) return url;
268
+ }
269
+ throw Error("Invalid URL");
270
+ }
271
+ _connect() {
272
+ if (this._connectLock || !this._shouldReconnect) return;
273
+ this._connectLock = true;
274
+ const { maxRetries = DEFAULT.maxRetries, connectionTimeout = DEFAULT.connectionTimeout } = this._options;
275
+ if (this._retryCount >= maxRetries) {
276
+ this._debug("max retries reached", this._retryCount, ">=", maxRetries);
277
+ return;
278
+ }
279
+ this._retryCount++;
280
+ this._debug("connect", this._retryCount);
281
+ this._removeListeners();
282
+ this._wait().then(() => Promise.all([this._getNextUrl(this._url), this._getNextProtocols(this._protocols || null)])).then(([url, protocols]) => {
283
+ if (this._closeCalled) {
284
+ this._connectLock = false;
285
+ return;
286
+ }
287
+ if (!this._options.WebSocket && typeof WebSocket === "undefined" && !didWarnAboutMissingWebSocket) {
288
+ console.error(`\u203C\uFE0F No WebSocket implementation available. You should define options.WebSocket.
371
289
 
372
290
  For example, if you're using node.js, run \`npm install ws\`, and then in your code:
373
291
 
@@ -381,121 +299,103 @@ const partysocket = new PartySocket({
381
299
  });
382
300
 
383
301
  `);
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));
439
- };
440
- _handleMessage = (event) => {
441
- this._debug("message event");
442
- if (this.onmessage) {
443
- this.onmessage(event);
444
- }
445
- this.dispatchEvent(cloneEvent(event));
446
- };
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();
456
- };
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));
467
- };
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
- }
302
+ didWarnAboutMissingWebSocket = true;
303
+ }
304
+ const WS = this._options.WebSocket || WebSocket;
305
+ this._debug("connect", {
306
+ url,
307
+ protocols
308
+ });
309
+ this._ws = protocols ? new WS(url, protocols) : new WS(url);
310
+ this._ws.binaryType = this._binaryType;
311
+ this._connectLock = false;
312
+ this._addListeners();
313
+ this._connectTimeout = setTimeout(() => this._handleTimeout(), connectionTimeout);
314
+ }).catch((err) => {
315
+ this._connectLock = false;
316
+ this._handleError(new Events.ErrorEvent(Error(err.message), this));
317
+ });
318
+ }
319
+ _handleTimeout() {
320
+ this._debug("timeout event");
321
+ this._handleError(new Events.ErrorEvent(Error("TIMEOUT"), this));
322
+ }
323
+ _disconnect(code = 1e3, reason) {
324
+ this._clearTimeouts();
325
+ if (!this._ws) return;
326
+ this._removeListeners();
327
+ try {
328
+ if (this._ws.readyState === this.OPEN) this._ws.close(code, reason);
329
+ this._handleClose(new Events.CloseEvent(code, reason, this));
330
+ } catch (error) {}
331
+ }
332
+ _acceptOpen() {
333
+ this._debug("accept open");
334
+ this._retryCount = 0;
335
+ }
336
+ _handleOpen = (event) => {
337
+ this._debug("open event");
338
+ const { minUptime = DEFAULT.minUptime } = this._options;
339
+ clearTimeout(this._connectTimeout);
340
+ this._uptimeTimeout = setTimeout(() => this._acceptOpen(), minUptime);
341
+ assert(this._ws, "WebSocket is not defined");
342
+ this._ws.binaryType = this._binaryType;
343
+ this._messageQueue.forEach((message) => {
344
+ var _a2;
345
+ return (_a2 = this._ws) == null ? void 0 : _a2.send(message);
346
+ });
347
+ this._messageQueue = [];
348
+ if (this.onopen) this.onopen(event);
349
+ this.dispatchEvent(cloneEvent(event));
350
+ };
351
+ _handleMessage = (event) => {
352
+ this._debug("message event");
353
+ if (this.onmessage) this.onmessage(event);
354
+ this.dispatchEvent(cloneEvent(event));
355
+ };
356
+ _handleError = (event) => {
357
+ this._debug("error event", event.message);
358
+ this._disconnect(void 0, event.message === "TIMEOUT" ? "timeout" : void 0);
359
+ if (this.onerror) this.onerror(event);
360
+ this._debug("exec error listeners");
361
+ this.dispatchEvent(cloneEvent(event));
362
+ this._connect();
363
+ };
364
+ _handleClose = (event) => {
365
+ this._debug("close event");
366
+ this._clearTimeouts();
367
+ if (this._shouldReconnect) this._connect();
368
+ if (this.onclose) this.onclose(event);
369
+ this.dispatchEvent(cloneEvent(event));
370
+ };
371
+ _removeListeners() {
372
+ if (!this._ws) return;
373
+ this._debug("removeListeners");
374
+ this._ws.removeEventListener("open", this._handleOpen);
375
+ this._ws.removeEventListener("close", this._handleClose);
376
+ this._ws.removeEventListener("message", this._handleMessage);
377
+ this._ws.removeEventListener("error", this._handleError);
378
+ }
379
+ _addListeners() {
380
+ if (!this._ws) return;
381
+ this._debug("addListeners");
382
+ this._ws.addEventListener("open", this._handleOpen);
383
+ this._ws.addEventListener("close", this._handleClose);
384
+ this._ws.addEventListener("message", this._handleMessage);
385
+ this._ws.addEventListener("error", this._handleError);
386
+ }
387
+ _clearTimeouts() {
388
+ clearTimeout(this._connectTimeout);
389
+ clearTimeout(this._uptimeTimeout);
390
+ }
492
391
  };
493
392
  /*!
494
- * Reconnecting WebSocket
495
- * by Pedro Ladaria <pedro.ladaria@gmail.com>
496
- * https://github.com/pladaria/reconnecting-websocket
497
- * License MIT
498
- */
393
+ * Reconnecting WebSocket
394
+ * by Pedro Ladaria <pedro.ladaria@gmail.com>
395
+ * https://github.com/pladaria/reconnecting-websocket
396
+ * License MIT
397
+ */
398
+ //#endregion
399
+ export { ReconnectingWebSocket };
499
400
 
500
- export { CloseEvent, ErrorEvent, ReconnectingWebSocket };
501
- //# sourceMappingURL=chunk-S74YV6PU.js.map
401
+ //# sourceMappingURL=chunk-S74YV6PU.js.map