@pluv/client 0.17.3 → 0.18.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/.turbo/turbo-build.log +8 -8
- package/CHANGELOG.md +152 -0
- package/README.md +2 -5
- package/dist/index.d.mts +3 -3
- package/dist/index.d.ts +3 -3
- package/dist/index.js +59 -129
- package/dist/index.mjs +59 -129
- package/package.json +5 -5
- package/src/AbstractRoom.ts +8 -33
- package/src/CrdtManager.ts +9 -33
- package/src/CrdtNotifier.ts +5 -11
- package/src/EventNotifier.ts +5 -11
- package/src/MockedRoom.ts +14 -57
- package/src/PluvClient.ts +4 -17
- package/src/PluvRoom.ts +87 -203
- package/src/StateNotifier.ts +4 -17
- package/src/UsersManager.ts +4 -20
- package/src/createClient.ts +1 -3
- package/src/index.ts +2 -12
- package/src/types.ts +2 -11
package/.turbo/turbo-build.log
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
|
|
2
|
-
> @pluv/client@0.
|
|
2
|
+
> @pluv/client@0.18.0 build /home/runner/work/pluv/pluv/packages/client
|
|
3
3
|
> tsup src/index.ts --format esm,cjs --dts
|
|
4
4
|
|
|
5
5
|
[34mCLI[39m Building entry: src/index.ts
|
|
@@ -8,11 +8,11 @@
|
|
|
8
8
|
[34mCLI[39m Target: es6
|
|
9
9
|
[34mESM[39m Build start
|
|
10
10
|
[34mCJS[39m Build start
|
|
11
|
-
[
|
|
12
|
-
[
|
|
13
|
-
[
|
|
14
|
-
[
|
|
11
|
+
[32mESM[39m [1mdist/index.mjs [22m[32m41.27 KB[39m
|
|
12
|
+
[32mESM[39m ⚡️ Build success in 104ms
|
|
13
|
+
[32mCJS[39m [1mdist/index.js [22m[32m42.53 KB[39m
|
|
14
|
+
[32mCJS[39m ⚡️ Build success in 104ms
|
|
15
15
|
[34mDTS[39m Build start
|
|
16
|
-
[32mDTS[39m ⚡️ Build success in
|
|
17
|
-
[32mDTS[39m [1mdist/index.d.mts [22m[
|
|
18
|
-
[32mDTS[39m [1mdist/index.d.ts [22m[
|
|
16
|
+
[32mDTS[39m ⚡️ Build success in 3547ms
|
|
17
|
+
[32mDTS[39m [1mdist/index.d.mts [22m[32m14.14 KB[39m
|
|
18
|
+
[32mDTS[39m [1mdist/index.d.ts [22m[32m14.14 KB[39m
|
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,157 @@
|
|
|
1
1
|
# @pluv/client
|
|
2
2
|
|
|
3
|
+
## 0.18.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- 99b5ca9: ## Breaking Changes
|
|
8
|
+
|
|
9
|
+
- `@pluv/io` has been updated to introduce `PluvProcedure`, `PluvRouter` and `PluvServer`. This change is intended to improve the ergonomics of declaring events and simplifying inferences of event types.
|
|
10
|
+
|
|
11
|
+
### Before:
|
|
12
|
+
|
|
13
|
+
```ts
|
|
14
|
+
// backend/io.ts
|
|
15
|
+
|
|
16
|
+
import { createIO } from "@pluv/io";
|
|
17
|
+
import { createPluvHandler, platformNode } from "@pluv/platform-node";
|
|
18
|
+
import { z } from "zod";
|
|
19
|
+
|
|
20
|
+
export const io = createIO({
|
|
21
|
+
platform: platformNode(),
|
|
22
|
+
})
|
|
23
|
+
.event("SEND_MESSAGE", {
|
|
24
|
+
input: z.object({ message: z.string() }),
|
|
25
|
+
resolver: ({ message }) => ({ RECEIVE_MESSAGE: { message } }),
|
|
26
|
+
})
|
|
27
|
+
.event("DOUBLE_VALUE", {
|
|
28
|
+
input: z.object({ value: z.number() }),
|
|
29
|
+
resolver: ({ value }) => ({ VALUE_DOUBLED: { value: value * 2 } }),
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
const Pluv = createPluvHandler({
|
|
33
|
+
io,
|
|
34
|
+
/* ... */
|
|
35
|
+
});
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
```ts
|
|
39
|
+
// frontend/pluv.ts
|
|
40
|
+
|
|
41
|
+
import { createClient } from "@pluv/react";
|
|
42
|
+
import type { io } from "../backend/io";
|
|
43
|
+
|
|
44
|
+
const client = createClient<typeof io>({
|
|
45
|
+
/* ... */
|
|
46
|
+
});
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
### Now:
|
|
50
|
+
|
|
51
|
+
```ts
|
|
52
|
+
import { createIO } from "@pluv/io";
|
|
53
|
+
import { createPluvHandler, platformNode } from "@pluv/platform-node";
|
|
54
|
+
import { z } from "zod";
|
|
55
|
+
|
|
56
|
+
const io = createIO({
|
|
57
|
+
platform: platformNode(),
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
const router = io.router({
|
|
61
|
+
SEND_MESSAGE: io.procedure
|
|
62
|
+
.input(z.object({ message: z.string() }))
|
|
63
|
+
.broadcast(({ message }) => ({
|
|
64
|
+
RECEIVE_MESSAGE: { message },
|
|
65
|
+
})),
|
|
66
|
+
DOUBLE_VALUE: io.procedure
|
|
67
|
+
.input(z.object({ value: z.number() }))
|
|
68
|
+
.broadcast(({ value }) => ({
|
|
69
|
+
VALUE_DOUBLED: { value: value * 2 },
|
|
70
|
+
})),
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
export const ioServer = io.server({ router });
|
|
74
|
+
|
|
75
|
+
const Pluv = createPluvHandler({
|
|
76
|
+
io: ioServer, // <- This uses the PluvServer now
|
|
77
|
+
/* ... */
|
|
78
|
+
});
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
```ts
|
|
82
|
+
// frontend/pluv.ts
|
|
83
|
+
|
|
84
|
+
import { createClient } from "@pluv/react";
|
|
85
|
+
import type { ioServer } from "../backend/io";
|
|
86
|
+
|
|
87
|
+
// This users the PluvServer type now
|
|
88
|
+
const client = createClient<typeof ioServer>({
|
|
89
|
+
/* ... */
|
|
90
|
+
});
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
- `PluvRouter` instances can also be merged via the `mergeRouters` method, which effectively performs an `Object.assign` of the events object and returns a new `PluvRouter` with the correct types:
|
|
94
|
+
|
|
95
|
+
```ts
|
|
96
|
+
const router = io.mergeRouters(router1, router2);
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
### Patch Changes
|
|
100
|
+
|
|
101
|
+
- 06c67be: Added broadcast proxy as a new way to broadcast events.
|
|
102
|
+
|
|
103
|
+
```ts
|
|
104
|
+
// backend
|
|
105
|
+
|
|
106
|
+
const router = io.router({
|
|
107
|
+
SEND_MESSAGE: io.procedure
|
|
108
|
+
.input(z.object({ message: z.string() }))
|
|
109
|
+
.broadcast(({ message }) => ({ RECEIVE_MESSAGE: { message } })),
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
// frontend
|
|
113
|
+
const client = createClient(/* ... */);
|
|
114
|
+
|
|
115
|
+
// Both of the examples below are equivalent.
|
|
116
|
+
|
|
117
|
+
client.broadcast("SEND_MESSAGE", { message: "Hello world~!" });
|
|
118
|
+
|
|
119
|
+
client.broadcast.SEND_MESSAGE({ message: "Hello world~!" });
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
- df1342c: Added event proxy as a new way to listen to events.
|
|
123
|
+
|
|
124
|
+
```ts
|
|
125
|
+
// backend
|
|
126
|
+
|
|
127
|
+
const router = io.router({
|
|
128
|
+
SEND_MESSAGE: io.procedure
|
|
129
|
+
.input(z.object({ message: z.string() }))
|
|
130
|
+
.broadcast(({ message }) => ({ RECEIVE_MESSAGE: { message } })),
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
// frontend
|
|
134
|
+
const client = createClient(/* ... */);
|
|
135
|
+
|
|
136
|
+
// Both of the examples below are equivalent.
|
|
137
|
+
|
|
138
|
+
client.event("RECEIVE_MESSAGE", ({ data }) => {
|
|
139
|
+
const { message } = data;
|
|
140
|
+
|
|
141
|
+
console.log(message);
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
client.event.RECEIVE_MESSAGE(({ data }) => {
|
|
145
|
+
const { message } = data;
|
|
146
|
+
|
|
147
|
+
console.log(message);
|
|
148
|
+
});
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
- Updated dependencies [99b5ca9]
|
|
152
|
+
- @pluv/types@0.18.0
|
|
153
|
+
- @pluv/crdt@0.18.0
|
|
154
|
+
|
|
3
155
|
## 0.17.3
|
|
4
156
|
|
|
5
157
|
### Patch Changes
|
package/README.md
CHANGED
|
@@ -93,14 +93,11 @@ const room = client.createRoom<Presence, Storage>("my-room", {
|
|
|
93
93
|
const main = async () => {
|
|
94
94
|
await client.enter(room);
|
|
95
95
|
|
|
96
|
-
const unsubscribe = client.event
|
|
96
|
+
const unsubscribe = client.event.RECEIVE_MESSAGE(({ data }) => {
|
|
97
97
|
console.log(data.message);
|
|
98
98
|
});
|
|
99
99
|
|
|
100
|
-
client.broadcast({
|
|
101
|
-
type: "SEND_MESSAGE",
|
|
102
|
-
data: { message: "Hello world!" },
|
|
103
|
-
});
|
|
100
|
+
client.broadcast.SEND_MESSAGE({ message: "Hello world!" });
|
|
104
101
|
|
|
105
102
|
unsubscribe();
|
|
106
103
|
|
package/dist/index.d.mts
CHANGED
|
@@ -191,12 +191,12 @@ declare class PluvRoom<TIO extends IOLike, TPresence extends JsonObject = {}, TS
|
|
|
191
191
|
constructor(room: string, options: RoomConfig<TIO, TPresence, TStorage>);
|
|
192
192
|
get storageLoaded(): boolean;
|
|
193
193
|
get webSocket(): WebSocket | null;
|
|
194
|
-
broadcast<TEvent extends keyof InferIOInput<TIO>>(event: TEvent, data: Id<InferIOInput<TIO>[TEvent]>): void;
|
|
194
|
+
broadcast: (<TEvent extends keyof InferIOInput<TIO>>(event: TEvent, data: Id<InferIOInput<TIO>[TEvent]>) => void) & { [event in keyof InferIOInput<TIO>]: (data: Id<InferIOInput<TIO>[event]>) => void; };
|
|
195
195
|
canRedo: () => boolean;
|
|
196
196
|
canUndo: () => boolean;
|
|
197
197
|
connect(): Promise<void>;
|
|
198
198
|
disconnect(): Promise<void>;
|
|
199
|
-
event: <TEvent extends keyof InferIOOutput<TIO>>(event: TEvent, callback: EventNotifierSubscriptionCallback<TIO, TEvent>) => (() => void
|
|
199
|
+
event: (<TEvent extends keyof InferIOOutput<TIO>>(event: TEvent, callback: EventNotifierSubscriptionCallback<TIO, TEvent>) => () => void) & { [event in keyof InferIOOutput<TIO>]: (callback: EventNotifierSubscriptionCallback<TIO, event>) => () => void; };
|
|
200
200
|
getConnection(): WebSocketConnection;
|
|
201
201
|
getDoc(): AbstractCrdtDoc<TStorage>;
|
|
202
202
|
getMyPresence: () => TPresence;
|
|
@@ -280,6 +280,6 @@ declare class PluvClient<TIO extends IOLike = IOLike> {
|
|
|
280
280
|
private _logDebug;
|
|
281
281
|
}
|
|
282
282
|
|
|
283
|
-
declare const createClient: <TIO extends IOLike<_pluv_types.IOAuthorize<any, any, any>, {}
|
|
283
|
+
declare const createClient: <TIO extends IOLike<_pluv_types.IOAuthorize<any, any, any>, {}> = IOLike<_pluv_types.IOAuthorize<any, any, any>, {}>>(options: PluvClientOptions<TIO>) => PluvClient<TIO>;
|
|
284
284
|
|
|
285
285
|
export { AbstractRoom, AbstractStorageStore, ConnectionState, MockedRoom, type MockedRoomConfig, type MockedRoomEvents, PluvClient, type PluvClientOptions, PluvRoom, type PluvRoomAddon, type PluvRoomAddonInput, type PluvRoomAddonResult, type PluvRoomDebug, type UserInfo, type WebSocketConnection, type WebSocketState, createClient };
|
package/dist/index.d.ts
CHANGED
|
@@ -191,12 +191,12 @@ declare class PluvRoom<TIO extends IOLike, TPresence extends JsonObject = {}, TS
|
|
|
191
191
|
constructor(room: string, options: RoomConfig<TIO, TPresence, TStorage>);
|
|
192
192
|
get storageLoaded(): boolean;
|
|
193
193
|
get webSocket(): WebSocket | null;
|
|
194
|
-
broadcast<TEvent extends keyof InferIOInput<TIO>>(event: TEvent, data: Id<InferIOInput<TIO>[TEvent]>): void;
|
|
194
|
+
broadcast: (<TEvent extends keyof InferIOInput<TIO>>(event: TEvent, data: Id<InferIOInput<TIO>[TEvent]>) => void) & { [event in keyof InferIOInput<TIO>]: (data: Id<InferIOInput<TIO>[event]>) => void; };
|
|
195
195
|
canRedo: () => boolean;
|
|
196
196
|
canUndo: () => boolean;
|
|
197
197
|
connect(): Promise<void>;
|
|
198
198
|
disconnect(): Promise<void>;
|
|
199
|
-
event: <TEvent extends keyof InferIOOutput<TIO>>(event: TEvent, callback: EventNotifierSubscriptionCallback<TIO, TEvent>) => (() => void
|
|
199
|
+
event: (<TEvent extends keyof InferIOOutput<TIO>>(event: TEvent, callback: EventNotifierSubscriptionCallback<TIO, TEvent>) => () => void) & { [event in keyof InferIOOutput<TIO>]: (callback: EventNotifierSubscriptionCallback<TIO, event>) => () => void; };
|
|
200
200
|
getConnection(): WebSocketConnection;
|
|
201
201
|
getDoc(): AbstractCrdtDoc<TStorage>;
|
|
202
202
|
getMyPresence: () => TPresence;
|
|
@@ -280,6 +280,6 @@ declare class PluvClient<TIO extends IOLike = IOLike> {
|
|
|
280
280
|
private _logDebug;
|
|
281
281
|
}
|
|
282
282
|
|
|
283
|
-
declare const createClient: <TIO extends IOLike<_pluv_types.IOAuthorize<any, any, any>, {}
|
|
283
|
+
declare const createClient: <TIO extends IOLike<_pluv_types.IOAuthorize<any, any, any>, {}> = IOLike<_pluv_types.IOAuthorize<any, any, any>, {}>>(options: PluvClientOptions<TIO>) => PluvClient<TIO>;
|
|
284
284
|
|
|
285
285
|
export { AbstractRoom, AbstractStorageStore, ConnectionState, MockedRoom, type MockedRoomConfig, type MockedRoomEvents, PluvClient, type PluvClientOptions, PluvRoom, type PluvRoomAddon, type PluvRoomAddonInput, type PluvRoomAddonResult, type PluvRoomDebug, type UserInfo, type WebSocketConnection, type WebSocketState, createClient };
|
package/dist/index.js
CHANGED
|
@@ -86,10 +86,7 @@ var import_crdt = require("@pluv/crdt");
|
|
|
86
86
|
var CrdtManager = class {
|
|
87
87
|
constructor(options) {
|
|
88
88
|
this.initialized = false;
|
|
89
|
-
const {
|
|
90
|
-
encodedState,
|
|
91
|
-
initialStorage = import_crdt.noop.doc({})
|
|
92
|
-
} = options;
|
|
89
|
+
const { encodedState, initialStorage = import_crdt.noop.doc({}) } = options;
|
|
93
90
|
this._docFactory = initialStorage;
|
|
94
91
|
this.doc = encodedState ? initialStorage.getFresh().applyEncodedState({ update: encodedState }) : initialStorage.getFresh();
|
|
95
92
|
}
|
|
@@ -121,18 +118,10 @@ var CrdtManager = class {
|
|
|
121
118
|
this.doc = this._applyDocUpdates(this.doc, updates, origin).track();
|
|
122
119
|
return this;
|
|
123
120
|
}
|
|
124
|
-
const onEmpty = this._applyDocUpdates(
|
|
125
|
-
this._docFactory.getEmpty(),
|
|
126
|
-
updates,
|
|
127
|
-
origin
|
|
128
|
-
);
|
|
121
|
+
const onEmpty = this._applyDocUpdates(this._docFactory.getEmpty(), updates, origin);
|
|
129
122
|
if (!onEmpty.isEmpty()) {
|
|
130
123
|
onEmpty.destroy();
|
|
131
|
-
this.doc = this._applyDocUpdates(
|
|
132
|
-
this._docFactory.getFresh(),
|
|
133
|
-
updates,
|
|
134
|
-
origin
|
|
135
|
-
).track();
|
|
124
|
+
this.doc = this._applyDocUpdates(this._docFactory.getFresh(), updates, origin).track();
|
|
136
125
|
this.initialized = true;
|
|
137
126
|
onInitialized == null ? void 0 : onInitialized(this.doc.getEncodedState());
|
|
138
127
|
return this;
|
|
@@ -145,9 +134,7 @@ var CrdtManager = class {
|
|
|
145
134
|
}
|
|
146
135
|
_applyDocUpdates(doc, updates, origin) {
|
|
147
136
|
doc.transact(() => {
|
|
148
|
-
doc.batchApplyEncodedState(
|
|
149
|
-
updates.map((update) => ({ origin, update }))
|
|
150
|
-
);
|
|
137
|
+
doc.batchApplyEncodedState(updates.map((update) => ({ origin, update })));
|
|
151
138
|
}, origin);
|
|
152
139
|
return doc;
|
|
153
140
|
}
|
|
@@ -462,9 +449,7 @@ var MockedRoom = class extends AbstractRoom {
|
|
|
462
449
|
});
|
|
463
450
|
}
|
|
464
451
|
getConnection() {
|
|
465
|
-
return Object.freeze(
|
|
466
|
-
JSON.parse(JSON.stringify(this._state.connection))
|
|
467
|
-
);
|
|
452
|
+
return Object.freeze(JSON.parse(JSON.stringify(this._state.connection)));
|
|
468
453
|
}
|
|
469
454
|
getDoc() {
|
|
470
455
|
return this._crdtManager.doc;
|
|
@@ -595,15 +580,37 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
595
580
|
};
|
|
596
581
|
this._windowListeners = null;
|
|
597
582
|
this._wsListeners = null;
|
|
583
|
+
this.broadcast = new Proxy(
|
|
584
|
+
(event, data) => {
|
|
585
|
+
if (!this._state.webSocket)
|
|
586
|
+
return;
|
|
587
|
+
if (this._state.connection.state !== "Open" /* Open */)
|
|
588
|
+
return;
|
|
589
|
+
const type = event.toString();
|
|
590
|
+
this._sendMessage({ data, type });
|
|
591
|
+
},
|
|
592
|
+
{
|
|
593
|
+
get(fn, prop) {
|
|
594
|
+
return (data) => {
|
|
595
|
+
return fn(prop, data);
|
|
596
|
+
};
|
|
597
|
+
}
|
|
598
|
+
}
|
|
599
|
+
);
|
|
598
600
|
this.canRedo = () => {
|
|
599
601
|
return !!this._crdtManager.doc.canRedo();
|
|
600
602
|
};
|
|
601
603
|
this.canUndo = () => {
|
|
602
604
|
return !!this._crdtManager.doc.canUndo();
|
|
603
605
|
};
|
|
604
|
-
this.event = (
|
|
605
|
-
|
|
606
|
-
|
|
606
|
+
this.event = new Proxy(
|
|
607
|
+
(event, callback) => this._eventNotifier.subscribe(event, callback),
|
|
608
|
+
{
|
|
609
|
+
get(fn, prop) {
|
|
610
|
+
return (callback) => fn(prop, callback);
|
|
611
|
+
}
|
|
612
|
+
}
|
|
613
|
+
);
|
|
607
614
|
this.getMyPresence = () => {
|
|
608
615
|
return this._usersManager.myPresence;
|
|
609
616
|
};
|
|
@@ -656,10 +663,7 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
656
663
|
const myself = (_a = this._usersManager.myself) != null ? _a : null;
|
|
657
664
|
this._stateNotifier.subjects["my-presence"].next(myPresence);
|
|
658
665
|
!!myself && this._stateNotifier.subjects["myself"].next(myself);
|
|
659
|
-
this.broadcast(
|
|
660
|
-
"$UPDATE_PRESENCE",
|
|
661
|
-
{ presence }
|
|
662
|
-
);
|
|
666
|
+
this.broadcast("$UPDATE_PRESENCE", { presence });
|
|
663
667
|
};
|
|
664
668
|
this._flattenStorageStore = debounce(
|
|
665
669
|
() => __async(this, null, function* () {
|
|
@@ -687,10 +691,7 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
687
691
|
this._listeners = {
|
|
688
692
|
onAuthorizationFail: (error) => {
|
|
689
693
|
this._clearTimeout(this._timeouts.reconnect);
|
|
690
|
-
this._timeouts.reconnect = setTimeout(
|
|
691
|
-
this._reconnect.bind(this),
|
|
692
|
-
RECONNECT_TIMEOUT_MS
|
|
693
|
-
);
|
|
694
|
+
this._timeouts.reconnect = setTimeout(this._reconnect.bind(this), RECONNECT_TIMEOUT_MS);
|
|
694
695
|
onAuthorizationFail == null ? void 0 : onAuthorizationFail(error);
|
|
695
696
|
}
|
|
696
697
|
};
|
|
@@ -708,23 +709,14 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
708
709
|
get webSocket() {
|
|
709
710
|
return this._state.webSocket;
|
|
710
711
|
}
|
|
711
|
-
broadcast(event, data) {
|
|
712
|
-
if (!this._state.webSocket)
|
|
713
|
-
return;
|
|
714
|
-
if (this._state.connection.state !== "Open" /* Open */)
|
|
715
|
-
return;
|
|
716
|
-
const type = event.toString();
|
|
717
|
-
this._sendMessage({ data, type });
|
|
718
|
-
}
|
|
719
712
|
connect() {
|
|
720
713
|
return __async(this, null, function* () {
|
|
721
714
|
if (!this._endpoints.wsEndpoint) {
|
|
722
715
|
throw new Error("Must provide an wsEndpoint.");
|
|
723
716
|
}
|
|
724
|
-
const canConnect = [
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
].some((state) => this._state.connection.state === state);
|
|
717
|
+
const canConnect = ["Closed" /* Closed */, "Untouched" /* Untouched */].some(
|
|
718
|
+
(state) => this._state.connection.state === state
|
|
719
|
+
);
|
|
728
720
|
if (!canConnect)
|
|
729
721
|
return;
|
|
730
722
|
this._clearTimeout(this._timeouts.reconnect);
|
|
@@ -762,10 +754,9 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
762
754
|
}
|
|
763
755
|
if (!this._state.webSocket)
|
|
764
756
|
return;
|
|
765
|
-
const canDisconnect = [
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
].some((state) => this._state.connection.state === state);
|
|
757
|
+
const canDisconnect = ["Connecting" /* Connecting */, "Open" /* Open */].some(
|
|
758
|
+
(state) => this._state.connection.state === state
|
|
759
|
+
);
|
|
769
760
|
if (!canDisconnect)
|
|
770
761
|
return;
|
|
771
762
|
this._clearInterval(this._intervals.heartbeat);
|
|
@@ -776,9 +767,7 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
776
767
|
});
|
|
777
768
|
}
|
|
778
769
|
getConnection() {
|
|
779
|
-
return Object.freeze(
|
|
780
|
-
JSON.parse(JSON.stringify(this._state.connection))
|
|
781
|
-
);
|
|
770
|
+
return Object.freeze(JSON.parse(JSON.stringify(this._state.connection)));
|
|
782
771
|
}
|
|
783
772
|
getDoc() {
|
|
784
773
|
return this._crdtManager.doc;
|
|
@@ -812,14 +801,8 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
812
801
|
onNavigatorOnline: this._onNavigatorOnline.bind(this),
|
|
813
802
|
onVisibilityChange: this._onVisibilityChange.bind(this)
|
|
814
803
|
};
|
|
815
|
-
window.addEventListener(
|
|
816
|
-
|
|
817
|
-
this._windowListeners.onNavigatorOnline
|
|
818
|
-
);
|
|
819
|
-
document.addEventListener(
|
|
820
|
-
"visibilitychange",
|
|
821
|
-
this._windowListeners.onVisibilityChange
|
|
822
|
-
);
|
|
804
|
+
window.addEventListener("online", this._windowListeners.onNavigatorOnline);
|
|
805
|
+
document.addEventListener("visibilitychange", this._windowListeners.onVisibilityChange);
|
|
823
806
|
}
|
|
824
807
|
_attachWsListeners() {
|
|
825
808
|
if (!this._state.webSocket)
|
|
@@ -830,22 +813,10 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
830
813
|
onMessage: this._onMessage.bind(this),
|
|
831
814
|
onOpen: this._onOpen.bind(this)
|
|
832
815
|
};
|
|
833
|
-
this._state.webSocket.addEventListener(
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
);
|
|
837
|
-
this._state.webSocket.addEventListener(
|
|
838
|
-
"error",
|
|
839
|
-
this._wsListeners.onError
|
|
840
|
-
);
|
|
841
|
-
this._state.webSocket.addEventListener(
|
|
842
|
-
"message",
|
|
843
|
-
this._wsListeners.onMessage
|
|
844
|
-
);
|
|
845
|
-
this._state.webSocket.addEventListener(
|
|
846
|
-
"open",
|
|
847
|
-
this._wsListeners.onOpen
|
|
848
|
-
);
|
|
816
|
+
this._state.webSocket.addEventListener("close", this._wsListeners.onClose);
|
|
817
|
+
this._state.webSocket.addEventListener("error", this._wsListeners.onError);
|
|
818
|
+
this._state.webSocket.addEventListener("message", this._wsListeners.onMessage);
|
|
819
|
+
this._state.webSocket.addEventListener("open", this._wsListeners.onOpen);
|
|
849
820
|
}
|
|
850
821
|
_clearInterval(interval) {
|
|
851
822
|
if (Number.isNaN(interval))
|
|
@@ -883,35 +854,17 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
883
854
|
return;
|
|
884
855
|
if (typeof document === "undefined")
|
|
885
856
|
return;
|
|
886
|
-
window.removeEventListener(
|
|
887
|
-
|
|
888
|
-
this._windowListeners.onNavigatorOnline
|
|
889
|
-
);
|
|
890
|
-
document.removeEventListener(
|
|
891
|
-
"visibilitychange",
|
|
892
|
-
this._windowListeners.onVisibilityChange
|
|
893
|
-
);
|
|
857
|
+
window.removeEventListener("online", this._windowListeners.onNavigatorOnline);
|
|
858
|
+
document.removeEventListener("visibilitychange", this._windowListeners.onVisibilityChange);
|
|
894
859
|
this._windowListeners = null;
|
|
895
860
|
}
|
|
896
861
|
_detachWsListeners() {
|
|
897
862
|
if (!this._state.webSocket || !this._wsListeners)
|
|
898
863
|
return;
|
|
899
|
-
this._state.webSocket.removeEventListener(
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
);
|
|
903
|
-
this._state.webSocket.removeEventListener(
|
|
904
|
-
"error",
|
|
905
|
-
this._wsListeners.onError
|
|
906
|
-
);
|
|
907
|
-
this._state.webSocket.removeEventListener(
|
|
908
|
-
"message",
|
|
909
|
-
this._wsListeners.onMessage
|
|
910
|
-
);
|
|
911
|
-
this._state.webSocket.removeEventListener(
|
|
912
|
-
"open",
|
|
913
|
-
this._wsListeners.onOpen
|
|
914
|
-
);
|
|
864
|
+
this._state.webSocket.removeEventListener("close", this._wsListeners.onClose);
|
|
865
|
+
this._state.webSocket.removeEventListener("error", this._wsListeners.onError);
|
|
866
|
+
this._state.webSocket.removeEventListener("message", this._wsListeners.onMessage);
|
|
867
|
+
this._state.webSocket.removeEventListener("open", this._wsListeners.onOpen);
|
|
915
868
|
this._wsListeners = null;
|
|
916
869
|
}
|
|
917
870
|
_emitSharedTypes() {
|
|
@@ -957,9 +910,7 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
957
910
|
try {
|
|
958
911
|
return yield res.text().then((text) => text.trim());
|
|
959
912
|
} catch (err) {
|
|
960
|
-
throw new Error(
|
|
961
|
-
err instanceof Error ? err.message : "Room is unauthorized"
|
|
962
|
-
);
|
|
913
|
+
throw new Error(err instanceof Error ? err.message : "Room is unauthorized");
|
|
963
914
|
}
|
|
964
915
|
});
|
|
965
916
|
}
|
|
@@ -993,10 +944,7 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
993
944
|
if (!this._state.webSocket)
|
|
994
945
|
throw new Error("Could not find WebSocket");
|
|
995
946
|
const data = message.data;
|
|
996
|
-
this._usersManager.patchPresence(
|
|
997
|
-
connectionId,
|
|
998
|
-
data.presence
|
|
999
|
-
);
|
|
947
|
+
this._usersManager.patchPresence(connectionId, data.presence);
|
|
1000
948
|
const myPresence = this._usersManager.myPresence;
|
|
1001
949
|
const myself = (_a = this._usersManager.myself) != null ? _a : null;
|
|
1002
950
|
if ((myself == null ? void 0 : myself.connectionId) === connectionId) {
|
|
@@ -1021,10 +969,7 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
1021
969
|
return;
|
|
1022
970
|
this._usersManager.setUser(connectionId, user);
|
|
1023
971
|
const other = this._usersManager.getOther(connectionId);
|
|
1024
|
-
!!presence && this._usersManager.patchPresence(
|
|
1025
|
-
connectionId,
|
|
1026
|
-
presence
|
|
1027
|
-
);
|
|
972
|
+
!!presence && this._usersManager.patchPresence(connectionId, presence);
|
|
1028
973
|
this._otherNotifier.subject(connectionId).next(other);
|
|
1029
974
|
});
|
|
1030
975
|
const others = this._usersManager.getOthers();
|
|
@@ -1043,10 +988,7 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
1043
988
|
oldState.authorization.user = user;
|
|
1044
989
|
return oldState;
|
|
1045
990
|
});
|
|
1046
|
-
this._usersManager.setMyself(
|
|
1047
|
-
connectionId,
|
|
1048
|
-
user
|
|
1049
|
-
);
|
|
991
|
+
this._usersManager.setMyself(connectionId, user);
|
|
1050
992
|
const presence = this._usersManager.myPresence;
|
|
1051
993
|
const myself = (_a = this._usersManager.myself) != null ? _a : null;
|
|
1052
994
|
this._stateNotifier.subjects["my-presence"].next(presence);
|
|
@@ -1124,11 +1066,7 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
1124
1066
|
if (myself.connectionId === connectionId) {
|
|
1125
1067
|
this._sendMessage({ type: "$GET_OTHERS", data: {} });
|
|
1126
1068
|
}
|
|
1127
|
-
this._usersManager.setUser(
|
|
1128
|
-
connectionId,
|
|
1129
|
-
data.user,
|
|
1130
|
-
data.presence
|
|
1131
|
-
);
|
|
1069
|
+
this._usersManager.setUser(connectionId, data.user, data.presence);
|
|
1132
1070
|
const other = this._usersManager.getOther(connectionId);
|
|
1133
1071
|
const others = this._usersManager.getOthers();
|
|
1134
1072
|
this._otherNotifier.subject(connectionId).next(other);
|
|
@@ -1136,10 +1074,7 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
1136
1074
|
}
|
|
1137
1075
|
_heartbeat() {
|
|
1138
1076
|
this._clearTimeout(this._timeouts.pong);
|
|
1139
|
-
this._timeouts.pong = setTimeout(
|
|
1140
|
-
this._reconnect.bind(this),
|
|
1141
|
-
PONG_TIMEOUT_MS
|
|
1142
|
-
);
|
|
1077
|
+
this._timeouts.pong = setTimeout(this._reconnect.bind(this), PONG_TIMEOUT_MS);
|
|
1143
1078
|
this._sendMessage({ type: "$PING", data: {} });
|
|
1144
1079
|
}
|
|
1145
1080
|
_logDebug(...data) {
|
|
@@ -1269,10 +1204,7 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
1269
1204
|
return oldState;
|
|
1270
1205
|
});
|
|
1271
1206
|
this._clearInterval(this._intervals.heartbeat);
|
|
1272
|
-
this._intervals.heartbeat = setInterval(
|
|
1273
|
-
this._heartbeat.bind(this),
|
|
1274
|
-
HEARTBEAT_INTERVAL_MS
|
|
1275
|
-
);
|
|
1207
|
+
this._intervals.heartbeat = setInterval(this._heartbeat.bind(this), HEARTBEAT_INTERVAL_MS);
|
|
1276
1208
|
}
|
|
1277
1209
|
_onNavigatorOnline() {
|
|
1278
1210
|
return __async(this, null, function* () {
|
|
@@ -1346,9 +1278,7 @@ var PluvRoom = class extends AbstractRoom {
|
|
|
1346
1278
|
_updateState(updater) {
|
|
1347
1279
|
let authorization;
|
|
1348
1280
|
try {
|
|
1349
|
-
authorization = JSON.parse(
|
|
1350
|
-
JSON.stringify(this._state.authorization)
|
|
1351
|
-
);
|
|
1281
|
+
authorization = JSON.parse(JSON.stringify(this._state.authorization));
|
|
1352
1282
|
} catch (e) {
|
|
1353
1283
|
throw new Error("User is not JSON serializable");
|
|
1354
1284
|
}
|