@pluv/client 1.0.2 → 2.0.1
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/.turbo/turbo-build.log +8 -8
- package/CHANGELOG.md +19 -0
- package/dist/index.d.mts +111 -171
- package/dist/index.d.ts +111 -171
- package/dist/index.js +56 -67
- package/dist/index.mjs +38 -46
- package/package.json +5 -5
- package/src/CrdtManager.ts +11 -11
- package/src/EventNotifier.ts +7 -6
- package/src/MockedRoom.ts +61 -41
- package/src/OtherNotifier.ts +7 -6
- package/src/PluvClient.ts +36 -28
- package/src/PluvProcedure.ts +2 -1
- package/src/PluvRoom.ts +84 -68
- package/src/PluvRouter.ts +1 -23
- package/src/StateNotifier.ts +1 -2
- package/src/UsersManager.ts +2 -2
- package/src/createClient.ts +5 -5
- package/src/index.ts +14 -12
- package/src/types.ts +5 -19
- package/src/AbstractRoom.ts +0 -84
- package/src/enums.ts +0 -9
package/dist/index.js
CHANGED
|
@@ -57,9 +57,8 @@ var __async = (__this, __arguments, generator) => {
|
|
|
57
57
|
// src/index.ts
|
|
58
58
|
var index_exports = {};
|
|
59
59
|
__export(index_exports, {
|
|
60
|
-
AbstractRoom: () => AbstractRoom,
|
|
61
60
|
AbstractStorageStore: () => AbstractStorageStore,
|
|
62
|
-
ConnectionState: () => ConnectionState,
|
|
61
|
+
ConnectionState: () => import_types3.ConnectionState,
|
|
63
62
|
MockedRoom: () => MockedRoom,
|
|
64
63
|
PluvClient: () => PluvClient,
|
|
65
64
|
PluvRoom: () => PluvRoom,
|
|
@@ -68,13 +67,7 @@ __export(index_exports, {
|
|
|
68
67
|
register: () => register
|
|
69
68
|
});
|
|
70
69
|
module.exports = __toCommonJS(index_exports);
|
|
71
|
-
|
|
72
|
-
// src/AbstractRoom.ts
|
|
73
|
-
var AbstractRoom = class {
|
|
74
|
-
constructor(room) {
|
|
75
|
-
this.id = room;
|
|
76
|
-
}
|
|
77
|
-
};
|
|
70
|
+
var import_types3 = require("@pluv/types");
|
|
78
71
|
|
|
79
72
|
// src/AbstractStorageStore.ts
|
|
80
73
|
var AbstractStorageStore = class {
|
|
@@ -121,6 +114,9 @@ var PluvProcedure = class _PluvProcedure {
|
|
|
121
114
|
}
|
|
122
115
|
};
|
|
123
116
|
|
|
117
|
+
// src/PluvRoom.ts
|
|
118
|
+
var import_types = require("@pluv/types");
|
|
119
|
+
|
|
124
120
|
// src/CrdtManager.ts
|
|
125
121
|
var import_crdt = require("@pluv/crdt");
|
|
126
122
|
var CrdtManager = class {
|
|
@@ -554,15 +550,6 @@ var UsersManager = class {
|
|
|
554
550
|
}
|
|
555
551
|
};
|
|
556
552
|
|
|
557
|
-
// src/enums.ts
|
|
558
|
-
var ConnectionState = {
|
|
559
|
-
Closed: "Closed",
|
|
560
|
-
Connecting: "Connecting",
|
|
561
|
-
Open: "Open",
|
|
562
|
-
Unavailable: "Unavailable",
|
|
563
|
-
Untouched: "Untouched"
|
|
564
|
-
};
|
|
565
|
-
|
|
566
553
|
// src/PluvRoom.ts
|
|
567
554
|
var ADD_TO_STORAGE_STATE_DEBOUNCE_MS = 1e3;
|
|
568
555
|
var HEARTBEAT_INTERVAL_MS = 1e4;
|
|
@@ -575,24 +562,8 @@ var ORIGIN_STORAGE_UPDATED = "$storageUpdated";
|
|
|
575
562
|
var DEFAULT_PLUV_CLIENT_ADDON = (input) => ({
|
|
576
563
|
storage: new StorageStore(input.room.id)
|
|
577
564
|
});
|
|
578
|
-
var PluvRoom = class
|
|
565
|
+
var PluvRoom = class {
|
|
579
566
|
constructor(room, options) {
|
|
580
|
-
const {
|
|
581
|
-
addons = [],
|
|
582
|
-
authEndpoint,
|
|
583
|
-
debug = false,
|
|
584
|
-
initialPresence,
|
|
585
|
-
initialStorage,
|
|
586
|
-
limits,
|
|
587
|
-
metadata,
|
|
588
|
-
onAuthorizationFail,
|
|
589
|
-
presence,
|
|
590
|
-
publicKey,
|
|
591
|
-
reconnectTimeoutMs = RECONNECT_TIMEOUT_MS,
|
|
592
|
-
router,
|
|
593
|
-
wsEndpoint
|
|
594
|
-
} = options;
|
|
595
|
-
super(room);
|
|
596
567
|
this._crdtNotifier = new CrdtNotifier();
|
|
597
568
|
this._eventNotifier = new EventNotifier();
|
|
598
569
|
this._intervals = {
|
|
@@ -618,7 +589,7 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
618
589
|
attempts: 0,
|
|
619
590
|
count: 0,
|
|
620
591
|
id: null,
|
|
621
|
-
state: ConnectionState.Untouched
|
|
592
|
+
state: import_types.ConnectionState.Untouched
|
|
622
593
|
},
|
|
623
594
|
webSocket: null
|
|
624
595
|
};
|
|
@@ -627,7 +598,7 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
627
598
|
this.broadcast = new Proxy(
|
|
628
599
|
(event, data) => __async(this, null, function* () {
|
|
629
600
|
if (!this._state.webSocket) return;
|
|
630
|
-
if (this._state.connection.state !== ConnectionState.Open) return;
|
|
601
|
+
if (this._state.connection.state !== import_types.ConnectionState.Open) return;
|
|
631
602
|
const type = event.toString();
|
|
632
603
|
const procedure = this._router._defs.events[type];
|
|
633
604
|
if (!(procedure == null ? void 0 : procedure.config.broadcast)) {
|
|
@@ -751,14 +722,30 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
751
722
|
DEFAULT_PLUV_CLIENT_ADDON
|
|
752
723
|
);
|
|
753
724
|
};
|
|
725
|
+
const {
|
|
726
|
+
addons = [],
|
|
727
|
+
authEndpoint,
|
|
728
|
+
debug = false,
|
|
729
|
+
initialPresence,
|
|
730
|
+
initialStorage,
|
|
731
|
+
limits,
|
|
732
|
+
metadata,
|
|
733
|
+
onAuthorizationFail,
|
|
734
|
+
presence,
|
|
735
|
+
publicKey,
|
|
736
|
+
reconnectTimeoutMs = RECONNECT_TIMEOUT_MS,
|
|
737
|
+
router,
|
|
738
|
+
wsEndpoint
|
|
739
|
+
} = options;
|
|
740
|
+
this.id = room;
|
|
741
|
+
this.metadata = metadata;
|
|
754
742
|
const addon = this._getAddon(addons);
|
|
755
743
|
const { storage } = __spreadValues(__spreadValues({}, DEFAULT_PLUV_CLIENT_ADDON({ room: this })), addon({ room: this }));
|
|
756
|
-
this.
|
|
744
|
+
this._storageStore = storage;
|
|
757
745
|
this._debug = debug;
|
|
758
746
|
this._endpoints = { authEndpoint, wsEndpoint };
|
|
759
747
|
this._limits = limits;
|
|
760
748
|
this._reconnectTimeoutMs = reconnectTimeoutMs;
|
|
761
|
-
this._storageStore = storage;
|
|
762
749
|
if (!!publicKey) this._publicKey = publicKey;
|
|
763
750
|
this._listeners = {
|
|
764
751
|
onAuthorizationFail: (error) => {
|
|
@@ -786,14 +773,14 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
786
773
|
const metadata = params.metadata;
|
|
787
774
|
this._setMetadata(metadata);
|
|
788
775
|
const canConnect = [
|
|
789
|
-
ConnectionState.Closed,
|
|
790
|
-
ConnectionState.Unavailable,
|
|
791
|
-
ConnectionState.Untouched
|
|
776
|
+
import_types.ConnectionState.Closed,
|
|
777
|
+
import_types.ConnectionState.Unavailable,
|
|
778
|
+
import_types.ConnectionState.Untouched
|
|
792
779
|
].some((state) => this._state.connection.state === state);
|
|
793
780
|
if (!canConnect) return;
|
|
794
781
|
this._clearTimeout(this._timeouts.reconnect);
|
|
795
782
|
this._updateState((oldState) => {
|
|
796
|
-
oldState.connection.state = ConnectionState.Connecting;
|
|
783
|
+
oldState.connection.state = import_types.ConnectionState.Connecting;
|
|
797
784
|
return oldState;
|
|
798
785
|
});
|
|
799
786
|
yield this._storageStore.initialize();
|
|
@@ -830,7 +817,7 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
830
817
|
disconnect() {
|
|
831
818
|
return __async(this, null, function* () {
|
|
832
819
|
if (!this._state.webSocket) return;
|
|
833
|
-
const canDisconnect = [ConnectionState.Connecting, ConnectionState.Open].some(
|
|
820
|
+
const canDisconnect = [import_types.ConnectionState.Connecting, import_types.ConnectionState.Open].some(
|
|
834
821
|
(state) => this._state.connection.state === state
|
|
835
822
|
);
|
|
836
823
|
if (!canDisconnect) return;
|
|
@@ -839,7 +826,7 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
839
826
|
this._clearTimeout(this._timeouts.reconnect);
|
|
840
827
|
this._updateState((oldState) => {
|
|
841
828
|
oldState.authorization.token = null;
|
|
842
|
-
oldState.connection.state = ConnectionState.Closed;
|
|
829
|
+
oldState.connection.state = import_types.ConnectionState.Closed;
|
|
843
830
|
oldState.webSocket = null;
|
|
844
831
|
return oldState;
|
|
845
832
|
});
|
|
@@ -1185,7 +1172,8 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
1185
1172
|
}
|
|
1186
1173
|
_logDebug(...data) {
|
|
1187
1174
|
var _a;
|
|
1188
|
-
if (
|
|
1175
|
+
if (typeof process === "undefined") return;
|
|
1176
|
+
if (((_a = process.env) == null ? void 0 : _a.NODE_ENV) === "production") return;
|
|
1189
1177
|
if (this._debug) console.log(...data);
|
|
1190
1178
|
}
|
|
1191
1179
|
_observeCrdt() {
|
|
@@ -1213,7 +1201,7 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
1213
1201
|
const authError = error instanceof Error ? error : new Error("Authorization failed");
|
|
1214
1202
|
this._listeners.onAuthorizationFail(authError);
|
|
1215
1203
|
this._updateState((oldState) => {
|
|
1216
|
-
oldState.connection.state = ConnectionState.Unavailable;
|
|
1204
|
+
oldState.connection.state = import_types.ConnectionState.Unavailable;
|
|
1217
1205
|
return oldState;
|
|
1218
1206
|
});
|
|
1219
1207
|
}
|
|
@@ -1237,14 +1225,14 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
1237
1225
|
].some((okToRetryCode) => event.code === okToRetryCode);
|
|
1238
1226
|
this._updateState((oldState) => {
|
|
1239
1227
|
oldState.authorization.token = null;
|
|
1240
|
-
oldState.connection.state = shouldRetry ? ConnectionState.Unavailable : (
|
|
1228
|
+
oldState.connection.state = shouldRetry ? import_types.ConnectionState.Unavailable : (
|
|
1241
1229
|
/**
|
|
1242
1230
|
* !HACK
|
|
1243
1231
|
* @description Assume these are not recoverable. We're just going to mark this
|
|
1244
1232
|
* as Closed to avoid reconnect attempts.
|
|
1245
1233
|
* @date April 28, 2025
|
|
1246
1234
|
*/
|
|
1247
|
-
ConnectionState.Closed
|
|
1235
|
+
import_types.ConnectionState.Closed
|
|
1248
1236
|
);
|
|
1249
1237
|
oldState.webSocket = null;
|
|
1250
1238
|
return oldState;
|
|
@@ -1313,10 +1301,10 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
1313
1301
|
}
|
|
1314
1302
|
}
|
|
1315
1303
|
_onOpen() {
|
|
1316
|
-
if (this._state.connection.state !== ConnectionState.Connecting) return;
|
|
1304
|
+
if (this._state.connection.state !== import_types.ConnectionState.Connecting) return;
|
|
1317
1305
|
this._logDebug("WebSocket connected");
|
|
1318
1306
|
this._updateState((oldState) => {
|
|
1319
|
-
oldState.connection.state = ConnectionState.Open;
|
|
1307
|
+
oldState.connection.state = import_types.ConnectionState.Open;
|
|
1320
1308
|
return oldState;
|
|
1321
1309
|
});
|
|
1322
1310
|
this._clearInterval(this._intervals.heartbeat);
|
|
@@ -1327,16 +1315,16 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
1327
1315
|
}
|
|
1328
1316
|
_onNavigatorOffline() {
|
|
1329
1317
|
return __async(this, null, function* () {
|
|
1330
|
-
if (this._state.connection.state === ConnectionState.Closed) return;
|
|
1318
|
+
if (this._state.connection.state === import_types.ConnectionState.Closed) return;
|
|
1331
1319
|
this._updateState((oldState) => {
|
|
1332
|
-
oldState.connection.state = ConnectionState.Unavailable;
|
|
1320
|
+
oldState.connection.state = import_types.ConnectionState.Unavailable;
|
|
1333
1321
|
return oldState;
|
|
1334
1322
|
});
|
|
1335
1323
|
});
|
|
1336
1324
|
}
|
|
1337
1325
|
_onNavigatorOnline() {
|
|
1338
1326
|
return __async(this, null, function* () {
|
|
1339
|
-
if (this._state.connection.state !== ConnectionState.Unavailable) return;
|
|
1327
|
+
if (this._state.connection.state !== import_types.ConnectionState.Unavailable) return;
|
|
1340
1328
|
yield this._reconnect();
|
|
1341
1329
|
});
|
|
1342
1330
|
}
|
|
@@ -1345,12 +1333,12 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
1345
1333
|
if (typeof document === "undefined") return;
|
|
1346
1334
|
if (document.visibilityState !== "visible") return;
|
|
1347
1335
|
switch (this._state.connection.state) {
|
|
1348
|
-
case ConnectionState.Open: {
|
|
1336
|
+
case import_types.ConnectionState.Open: {
|
|
1349
1337
|
this._clearInterval(this._intervals.heartbeat);
|
|
1350
1338
|
setInterval(this._heartbeat.bind(this), HEARTBEAT_INTERVAL_MS);
|
|
1351
1339
|
return;
|
|
1352
1340
|
}
|
|
1353
|
-
case ConnectionState.Unavailable: {
|
|
1341
|
+
case import_types.ConnectionState.Unavailable: {
|
|
1354
1342
|
yield this._reconnect();
|
|
1355
1343
|
return;
|
|
1356
1344
|
}
|
|
@@ -1390,7 +1378,7 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
1390
1378
|
_reconnect() {
|
|
1391
1379
|
return __async(this, null, function* () {
|
|
1392
1380
|
this._closeWs();
|
|
1393
|
-
if (this._state.connection.state === ConnectionState.Closed) return;
|
|
1381
|
+
if (this._state.connection.state === import_types.ConnectionState.Closed) return;
|
|
1394
1382
|
const metadata = this._lastMetadata;
|
|
1395
1383
|
const params = typeof metadata === "undefined" ? [] : [{ metadata }];
|
|
1396
1384
|
yield this.connect(...params);
|
|
@@ -1445,7 +1433,7 @@ var PluvClient = class {
|
|
|
1445
1433
|
this._publicKey = null;
|
|
1446
1434
|
this._rooms = /* @__PURE__ */ new Map();
|
|
1447
1435
|
this.createRoom = (room, options = {}) => {
|
|
1448
|
-
var _a;
|
|
1436
|
+
var _a, _b;
|
|
1449
1437
|
const oldRoom = this.getRoom(room);
|
|
1450
1438
|
if (oldRoom) return oldRoom;
|
|
1451
1439
|
const newRoom = new PluvRoom(room, {
|
|
@@ -1453,12 +1441,12 @@ var PluvClient = class {
|
|
|
1453
1441
|
authEndpoint: this._authEndpoint,
|
|
1454
1442
|
debug: options.debug,
|
|
1455
1443
|
initialPresence: options.initialPresence,
|
|
1456
|
-
initialStorage: this._initialStorage,
|
|
1444
|
+
initialStorage: (_a = options.initialStorage) != null ? _a : this._initialStorage,
|
|
1457
1445
|
limits: this._limits,
|
|
1458
1446
|
metadata: this.metadata,
|
|
1459
1447
|
onAuthorizationFail: options.onAuthorizationFail,
|
|
1460
1448
|
presence: this._presence,
|
|
1461
|
-
publicKey: (
|
|
1449
|
+
publicKey: (_b = this._publicKey) != null ? _b : void 0,
|
|
1462
1450
|
reconnectTimeoutMs: options.reconnectTimeoutMs,
|
|
1463
1451
|
router: options.router,
|
|
1464
1452
|
wsEndpoint: this._wsEndpoint
|
|
@@ -1527,7 +1515,8 @@ var PluvClient = class {
|
|
|
1527
1515
|
}
|
|
1528
1516
|
_logDebug(...data) {
|
|
1529
1517
|
var _a;
|
|
1530
|
-
if (
|
|
1518
|
+
if (typeof process === "undefined") return;
|
|
1519
|
+
if (((_a = process.env) == null ? void 0 : _a.NODE_ENV) === "production") return;
|
|
1531
1520
|
if (this._debug) console.log(...data);
|
|
1532
1521
|
}
|
|
1533
1522
|
};
|
|
@@ -1541,10 +1530,9 @@ var createClient = (options) => {
|
|
|
1541
1530
|
var infer = (callback) => callback;
|
|
1542
1531
|
|
|
1543
1532
|
// src/MockedRoom.ts
|
|
1544
|
-
var
|
|
1533
|
+
var import_types2 = require("@pluv/types");
|
|
1534
|
+
var MockedRoom = class {
|
|
1545
1535
|
constructor(room, options) {
|
|
1546
|
-
const { events, initialPresence, initialStorage, limits, presence, router } = options;
|
|
1547
|
-
super(room);
|
|
1548
1536
|
this._crdtNotifier = new CrdtNotifier();
|
|
1549
1537
|
this._eventNotifier = new EventNotifier();
|
|
1550
1538
|
this._otherNotifier = new OtherNotifier();
|
|
@@ -1557,7 +1545,7 @@ var MockedRoom = class extends AbstractRoom {
|
|
|
1557
1545
|
attempts: 0,
|
|
1558
1546
|
count: 1,
|
|
1559
1547
|
id: null,
|
|
1560
|
-
state: ConnectionState.Untouched
|
|
1548
|
+
state: import_types2.ConnectionState.Untouched
|
|
1561
1549
|
},
|
|
1562
1550
|
webSocket: null
|
|
1563
1551
|
};
|
|
@@ -1568,7 +1556,7 @@ var MockedRoom = class extends AbstractRoom {
|
|
|
1568
1556
|
this.broadcast = new Proxy(
|
|
1569
1557
|
(event, data) => __async(this, null, function* () {
|
|
1570
1558
|
if (!this._state.webSocket) return;
|
|
1571
|
-
if (this._state.connection.state !== ConnectionState.Open) return;
|
|
1559
|
+
if (this._state.connection.state !== import_types2.ConnectionState.Open) return;
|
|
1572
1560
|
const type = event.toString();
|
|
1573
1561
|
const procedure = this._router._defs.events[type];
|
|
1574
1562
|
if (!(procedure == null ? void 0 : procedure.config.broadcast)) {
|
|
@@ -1670,6 +1658,8 @@ var MockedRoom = class extends AbstractRoom {
|
|
|
1670
1658
|
this._stateNotifier.subjects["my-presence"].next(myPresence);
|
|
1671
1659
|
if (!!myself) this._stateNotifier.subjects["myself"].next(myself);
|
|
1672
1660
|
};
|
|
1661
|
+
const { events, initialPresence, initialStorage, limits, presence, router } = options;
|
|
1662
|
+
this.id = room;
|
|
1673
1663
|
this._events = events;
|
|
1674
1664
|
this._limits = __spreadValues({
|
|
1675
1665
|
presenceMaxSize: MAX_PRESENCE_SIZE_BYTES
|
|
@@ -1746,7 +1736,6 @@ var register = (params) => {
|
|
|
1746
1736
|
};
|
|
1747
1737
|
// Annotate the CommonJS export names for ESM import in node:
|
|
1748
1738
|
0 && (module.exports = {
|
|
1749
|
-
AbstractRoom,
|
|
1750
1739
|
AbstractStorageStore,
|
|
1751
1740
|
ConnectionState,
|
|
1752
1741
|
MockedRoom,
|
package/dist/index.mjs
CHANGED
|
@@ -38,12 +38,8 @@ var __async = (__this, __arguments, generator) => {
|
|
|
38
38
|
});
|
|
39
39
|
};
|
|
40
40
|
|
|
41
|
-
// src/
|
|
42
|
-
|
|
43
|
-
constructor(room) {
|
|
44
|
-
this.id = room;
|
|
45
|
-
}
|
|
46
|
-
};
|
|
41
|
+
// src/index.ts
|
|
42
|
+
import { ConnectionState as ConnectionState3 } from "@pluv/types";
|
|
47
43
|
|
|
48
44
|
// src/AbstractStorageStore.ts
|
|
49
45
|
var AbstractStorageStore = class {
|
|
@@ -90,6 +86,9 @@ var PluvProcedure = class _PluvProcedure {
|
|
|
90
86
|
}
|
|
91
87
|
};
|
|
92
88
|
|
|
89
|
+
// src/PluvRoom.ts
|
|
90
|
+
import { ConnectionState } from "@pluv/types";
|
|
91
|
+
|
|
93
92
|
// src/CrdtManager.ts
|
|
94
93
|
import { noop } from "@pluv/crdt";
|
|
95
94
|
var CrdtManager = class {
|
|
@@ -523,15 +522,6 @@ var UsersManager = class {
|
|
|
523
522
|
}
|
|
524
523
|
};
|
|
525
524
|
|
|
526
|
-
// src/enums.ts
|
|
527
|
-
var ConnectionState = {
|
|
528
|
-
Closed: "Closed",
|
|
529
|
-
Connecting: "Connecting",
|
|
530
|
-
Open: "Open",
|
|
531
|
-
Unavailable: "Unavailable",
|
|
532
|
-
Untouched: "Untouched"
|
|
533
|
-
};
|
|
534
|
-
|
|
535
525
|
// src/PluvRoom.ts
|
|
536
526
|
var ADD_TO_STORAGE_STATE_DEBOUNCE_MS = 1e3;
|
|
537
527
|
var HEARTBEAT_INTERVAL_MS = 1e4;
|
|
@@ -544,24 +534,8 @@ var ORIGIN_STORAGE_UPDATED = "$storageUpdated";
|
|
|
544
534
|
var DEFAULT_PLUV_CLIENT_ADDON = (input) => ({
|
|
545
535
|
storage: new StorageStore(input.room.id)
|
|
546
536
|
});
|
|
547
|
-
var PluvRoom = class
|
|
537
|
+
var PluvRoom = class {
|
|
548
538
|
constructor(room, options) {
|
|
549
|
-
const {
|
|
550
|
-
addons = [],
|
|
551
|
-
authEndpoint,
|
|
552
|
-
debug = false,
|
|
553
|
-
initialPresence,
|
|
554
|
-
initialStorage,
|
|
555
|
-
limits,
|
|
556
|
-
metadata,
|
|
557
|
-
onAuthorizationFail,
|
|
558
|
-
presence,
|
|
559
|
-
publicKey,
|
|
560
|
-
reconnectTimeoutMs = RECONNECT_TIMEOUT_MS,
|
|
561
|
-
router,
|
|
562
|
-
wsEndpoint
|
|
563
|
-
} = options;
|
|
564
|
-
super(room);
|
|
565
539
|
this._crdtNotifier = new CrdtNotifier();
|
|
566
540
|
this._eventNotifier = new EventNotifier();
|
|
567
541
|
this._intervals = {
|
|
@@ -720,14 +694,30 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
720
694
|
DEFAULT_PLUV_CLIENT_ADDON
|
|
721
695
|
);
|
|
722
696
|
};
|
|
697
|
+
const {
|
|
698
|
+
addons = [],
|
|
699
|
+
authEndpoint,
|
|
700
|
+
debug = false,
|
|
701
|
+
initialPresence,
|
|
702
|
+
initialStorage,
|
|
703
|
+
limits,
|
|
704
|
+
metadata,
|
|
705
|
+
onAuthorizationFail,
|
|
706
|
+
presence,
|
|
707
|
+
publicKey,
|
|
708
|
+
reconnectTimeoutMs = RECONNECT_TIMEOUT_MS,
|
|
709
|
+
router,
|
|
710
|
+
wsEndpoint
|
|
711
|
+
} = options;
|
|
712
|
+
this.id = room;
|
|
713
|
+
this.metadata = metadata;
|
|
723
714
|
const addon = this._getAddon(addons);
|
|
724
715
|
const { storage } = __spreadValues(__spreadValues({}, DEFAULT_PLUV_CLIENT_ADDON({ room: this })), addon({ room: this }));
|
|
725
|
-
this.
|
|
716
|
+
this._storageStore = storage;
|
|
726
717
|
this._debug = debug;
|
|
727
718
|
this._endpoints = { authEndpoint, wsEndpoint };
|
|
728
719
|
this._limits = limits;
|
|
729
720
|
this._reconnectTimeoutMs = reconnectTimeoutMs;
|
|
730
|
-
this._storageStore = storage;
|
|
731
721
|
if (!!publicKey) this._publicKey = publicKey;
|
|
732
722
|
this._listeners = {
|
|
733
723
|
onAuthorizationFail: (error) => {
|
|
@@ -1154,7 +1144,8 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
1154
1144
|
}
|
|
1155
1145
|
_logDebug(...data) {
|
|
1156
1146
|
var _a;
|
|
1157
|
-
if (
|
|
1147
|
+
if (typeof process === "undefined") return;
|
|
1148
|
+
if (((_a = process.env) == null ? void 0 : _a.NODE_ENV) === "production") return;
|
|
1158
1149
|
if (this._debug) console.log(...data);
|
|
1159
1150
|
}
|
|
1160
1151
|
_observeCrdt() {
|
|
@@ -1414,7 +1405,7 @@ var PluvClient = class {
|
|
|
1414
1405
|
this._publicKey = null;
|
|
1415
1406
|
this._rooms = /* @__PURE__ */ new Map();
|
|
1416
1407
|
this.createRoom = (room, options = {}) => {
|
|
1417
|
-
var _a;
|
|
1408
|
+
var _a, _b;
|
|
1418
1409
|
const oldRoom = this.getRoom(room);
|
|
1419
1410
|
if (oldRoom) return oldRoom;
|
|
1420
1411
|
const newRoom = new PluvRoom(room, {
|
|
@@ -1422,12 +1413,12 @@ var PluvClient = class {
|
|
|
1422
1413
|
authEndpoint: this._authEndpoint,
|
|
1423
1414
|
debug: options.debug,
|
|
1424
1415
|
initialPresence: options.initialPresence,
|
|
1425
|
-
initialStorage: this._initialStorage,
|
|
1416
|
+
initialStorage: (_a = options.initialStorage) != null ? _a : this._initialStorage,
|
|
1426
1417
|
limits: this._limits,
|
|
1427
1418
|
metadata: this.metadata,
|
|
1428
1419
|
onAuthorizationFail: options.onAuthorizationFail,
|
|
1429
1420
|
presence: this._presence,
|
|
1430
|
-
publicKey: (
|
|
1421
|
+
publicKey: (_b = this._publicKey) != null ? _b : void 0,
|
|
1431
1422
|
reconnectTimeoutMs: options.reconnectTimeoutMs,
|
|
1432
1423
|
router: options.router,
|
|
1433
1424
|
wsEndpoint: this._wsEndpoint
|
|
@@ -1496,7 +1487,8 @@ var PluvClient = class {
|
|
|
1496
1487
|
}
|
|
1497
1488
|
_logDebug(...data) {
|
|
1498
1489
|
var _a;
|
|
1499
|
-
if (
|
|
1490
|
+
if (typeof process === "undefined") return;
|
|
1491
|
+
if (((_a = process.env) == null ? void 0 : _a.NODE_ENV) === "production") return;
|
|
1500
1492
|
if (this._debug) console.log(...data);
|
|
1501
1493
|
}
|
|
1502
1494
|
};
|
|
@@ -1510,10 +1502,9 @@ var createClient = (options) => {
|
|
|
1510
1502
|
var infer = (callback) => callback;
|
|
1511
1503
|
|
|
1512
1504
|
// src/MockedRoom.ts
|
|
1513
|
-
|
|
1505
|
+
import { ConnectionState as ConnectionState2 } from "@pluv/types";
|
|
1506
|
+
var MockedRoom = class {
|
|
1514
1507
|
constructor(room, options) {
|
|
1515
|
-
const { events, initialPresence, initialStorage, limits, presence, router } = options;
|
|
1516
|
-
super(room);
|
|
1517
1508
|
this._crdtNotifier = new CrdtNotifier();
|
|
1518
1509
|
this._eventNotifier = new EventNotifier();
|
|
1519
1510
|
this._otherNotifier = new OtherNotifier();
|
|
@@ -1526,7 +1517,7 @@ var MockedRoom = class extends AbstractRoom {
|
|
|
1526
1517
|
attempts: 0,
|
|
1527
1518
|
count: 1,
|
|
1528
1519
|
id: null,
|
|
1529
|
-
state:
|
|
1520
|
+
state: ConnectionState2.Untouched
|
|
1530
1521
|
},
|
|
1531
1522
|
webSocket: null
|
|
1532
1523
|
};
|
|
@@ -1537,7 +1528,7 @@ var MockedRoom = class extends AbstractRoom {
|
|
|
1537
1528
|
this.broadcast = new Proxy(
|
|
1538
1529
|
(event, data) => __async(this, null, function* () {
|
|
1539
1530
|
if (!this._state.webSocket) return;
|
|
1540
|
-
if (this._state.connection.state !==
|
|
1531
|
+
if (this._state.connection.state !== ConnectionState2.Open) return;
|
|
1541
1532
|
const type = event.toString();
|
|
1542
1533
|
const procedure = this._router._defs.events[type];
|
|
1543
1534
|
if (!(procedure == null ? void 0 : procedure.config.broadcast)) {
|
|
@@ -1639,6 +1630,8 @@ var MockedRoom = class extends AbstractRoom {
|
|
|
1639
1630
|
this._stateNotifier.subjects["my-presence"].next(myPresence);
|
|
1640
1631
|
if (!!myself) this._stateNotifier.subjects["myself"].next(myself);
|
|
1641
1632
|
};
|
|
1633
|
+
const { events, initialPresence, initialStorage, limits, presence, router } = options;
|
|
1634
|
+
this.id = room;
|
|
1642
1635
|
this._events = events;
|
|
1643
1636
|
this._limits = __spreadValues({
|
|
1644
1637
|
presenceMaxSize: MAX_PRESENCE_SIZE_BYTES
|
|
@@ -1714,9 +1707,8 @@ var register = (params) => {
|
|
|
1714
1707
|
};
|
|
1715
1708
|
};
|
|
1716
1709
|
export {
|
|
1717
|
-
AbstractRoom,
|
|
1718
1710
|
AbstractStorageStore,
|
|
1719
|
-
ConnectionState,
|
|
1711
|
+
ConnectionState3 as ConnectionState,
|
|
1720
1712
|
MockedRoom,
|
|
1721
1713
|
PluvClient,
|
|
1722
1714
|
PluvRoom,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pluv/client",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.1",
|
|
4
4
|
"description": "@pluv/io frontend client",
|
|
5
5
|
"author": "leedavidcs",
|
|
6
6
|
"license": "MIT",
|
|
@@ -18,15 +18,15 @@
|
|
|
18
18
|
},
|
|
19
19
|
"dependencies": {
|
|
20
20
|
"wonka": "^6.3.5",
|
|
21
|
-
"@pluv/crdt": "^
|
|
22
|
-
"@pluv/types": "^
|
|
21
|
+
"@pluv/crdt": "^2.0.1",
|
|
22
|
+
"@pluv/types": "^2.0.1"
|
|
23
23
|
},
|
|
24
24
|
"devDependencies": {
|
|
25
25
|
"eslint": "^9.25.1",
|
|
26
26
|
"tsup": "^8.4.0",
|
|
27
27
|
"typescript": "^5.8.3",
|
|
28
|
-
"@pluv/tsconfig": "^
|
|
29
|
-
"eslint-config-pluv": "^
|
|
28
|
+
"@pluv/tsconfig": "^2.0.1",
|
|
29
|
+
"eslint-config-pluv": "^2.0.1"
|
|
30
30
|
},
|
|
31
31
|
"scripts": {
|
|
32
32
|
"build": "tsup src/index.ts --format esm,cjs --dts",
|
package/src/CrdtManager.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { AbstractCrdtDocFactory, CrdtDocLike, CrdtType, InferStorage } from "@pluv/crdt";
|
|
2
2
|
import { noop } from "@pluv/crdt";
|
|
3
3
|
|
|
4
|
-
export type CrdtManagerOptions<
|
|
5
|
-
initialStorage?:
|
|
4
|
+
export type CrdtManagerOptions<TCrdt extends AbstractCrdtDocFactory<any>> = {
|
|
5
|
+
initialStorage?: TCrdt;
|
|
6
6
|
};
|
|
7
7
|
|
|
8
8
|
interface CrdtManagerInitializeParams {
|
|
@@ -15,14 +15,14 @@ interface ApplyUpdateParams {
|
|
|
15
15
|
update: string | readonly string[];
|
|
16
16
|
}
|
|
17
17
|
|
|
18
|
-
export class CrdtManager<
|
|
19
|
-
public doc:
|
|
18
|
+
export class CrdtManager<TCrdt extends AbstractCrdtDocFactory<any>> {
|
|
19
|
+
public doc: CrdtDocLike<InferStorage<TCrdt>>;
|
|
20
20
|
public initialized: boolean = false;
|
|
21
21
|
|
|
22
|
-
private readonly _docFactory:
|
|
22
|
+
private readonly _docFactory: TCrdt;
|
|
23
23
|
|
|
24
|
-
constructor(options: CrdtManagerOptions<
|
|
25
|
-
const { initialStorage = noop.doc({}) as
|
|
24
|
+
constructor(options: CrdtManagerOptions<TCrdt>) {
|
|
25
|
+
const { initialStorage = noop.doc({}) as any } = options;
|
|
26
26
|
|
|
27
27
|
this._docFactory = initialStorage;
|
|
28
28
|
this.doc = initialStorage.getEmpty();
|
|
@@ -52,7 +52,7 @@ export class CrdtManager<TStorage extends Record<string, CrdtType<any, any>>> {
|
|
|
52
52
|
this.doc = this._docFactory.getEmpty();
|
|
53
53
|
}
|
|
54
54
|
|
|
55
|
-
public get<TKey extends keyof
|
|
55
|
+
public get<TKey extends keyof InferStorage<TCrdt>>(key: TKey): InferStorage<TCrdt>[TKey] {
|
|
56
56
|
return this.doc.get(key);
|
|
57
57
|
}
|
|
58
58
|
|
|
@@ -110,10 +110,10 @@ export class CrdtManager<TStorage extends Record<string, CrdtType<any, any>>> {
|
|
|
110
110
|
}
|
|
111
111
|
|
|
112
112
|
private _applyDocUpdates(
|
|
113
|
-
doc:
|
|
113
|
+
doc: CrdtDocLike<InferStorage<TCrdt>>,
|
|
114
114
|
updates: readonly string[],
|
|
115
115
|
origin?: string,
|
|
116
|
-
):
|
|
116
|
+
): CrdtDocLike<InferStorage<TCrdt>> {
|
|
117
117
|
doc.transact(() => {
|
|
118
118
|
doc.batchApplyEncodedState({ origin, updates });
|
|
119
119
|
}, origin);
|
package/src/EventNotifier.ts
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type {
|
|
2
|
+
EventNotifierSubscriptionCallback,
|
|
3
|
+
Id,
|
|
4
|
+
InferIOOutput,
|
|
5
|
+
IOEventMessage,
|
|
6
|
+
IOLike,
|
|
7
|
+
} from "@pluv/types";
|
|
2
8
|
import type { Subject } from "wonka";
|
|
3
9
|
import { makeSubject, subscribe } from "wonka";
|
|
4
10
|
|
|
5
|
-
export type EventNotifierSubscriptionCallback<
|
|
6
|
-
TIO extends IOLike<any, any>,
|
|
7
|
-
TEvent extends keyof InferIOOutput<TIO>,
|
|
8
|
-
> = (value: Id<IOEventMessage<TIO, TEvent>>) => void;
|
|
9
|
-
|
|
10
11
|
export class EventNotifier<TIO extends IOLike> {
|
|
11
12
|
public subjects = new Map<
|
|
12
13
|
keyof InferIOOutput<TIO>,
|