@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/src/PluvRoom.ts
CHANGED
|
@@ -1,8 +1,16 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type {
|
|
2
|
+
AbstractCrdtDocFactory,
|
|
3
|
+
CrdtType,
|
|
4
|
+
InferCrdtJson,
|
|
5
|
+
InferStorage,
|
|
6
|
+
NoopCrdtDocFactory,
|
|
7
|
+
} from "@pluv/crdt";
|
|
2
8
|
import type {
|
|
3
9
|
BaseIOEventRecord,
|
|
4
10
|
BaseUser,
|
|
11
|
+
CrdtDocLike,
|
|
5
12
|
EventMessage,
|
|
13
|
+
EventNotifierSubscriptionCallback,
|
|
6
14
|
IOEventMessage,
|
|
7
15
|
IOLike,
|
|
8
16
|
Id,
|
|
@@ -12,26 +20,27 @@ import type {
|
|
|
12
20
|
InferIOOutput,
|
|
13
21
|
InputZodLike,
|
|
14
22
|
JsonObject,
|
|
23
|
+
MergeEvents,
|
|
15
24
|
OptionalProps,
|
|
25
|
+
OtherNotifierSubscriptionCallback,
|
|
26
|
+
RoomLike,
|
|
27
|
+
UpdateMyPresenceAction,
|
|
28
|
+
UserInfo,
|
|
29
|
+
WebSocketState,
|
|
16
30
|
} from "@pluv/types";
|
|
17
|
-
import {
|
|
31
|
+
import { ConnectionState } from "@pluv/types";
|
|
18
32
|
import type { AbstractStorageStore } from "./AbstractStorageStore";
|
|
19
33
|
import type { CrdtManagerOptions } from "./CrdtManager";
|
|
20
34
|
import { CrdtManager } from "./CrdtManager";
|
|
21
35
|
import { CrdtNotifier } from "./CrdtNotifier";
|
|
22
|
-
import type { EventNotifierSubscriptionCallback } from "./EventNotifier";
|
|
23
36
|
import { EventNotifier } from "./EventNotifier";
|
|
24
|
-
import type { OtherNotifierSubscriptionCallback } from "./OtherNotifier";
|
|
25
37
|
import { OtherNotifier } from "./OtherNotifier";
|
|
26
38
|
import { PluvProcedure } from "./PluvProcedure";
|
|
27
|
-
import type {
|
|
39
|
+
import type { PluvRouterEventConfig } from "./PluvRouter";
|
|
28
40
|
import { PluvRouter } from "./PluvRouter";
|
|
29
41
|
import type { StateNotifierSubjects, SubscriptionCallback } from "./StateNotifier";
|
|
30
42
|
import { StateNotifier } from "./StateNotifier";
|
|
31
43
|
import { StorageStore } from "./StorageStore";
|
|
32
|
-
import type { UsersManagerConfig } from "./UsersManager";
|
|
33
|
-
import { UsersManager } from "./UsersManager";
|
|
34
|
-
import { ConnectionState } from "./enums";
|
|
35
44
|
import type {
|
|
36
45
|
AuthorizationState,
|
|
37
46
|
EventResolver,
|
|
@@ -39,12 +48,11 @@ import type {
|
|
|
39
48
|
InternalSubscriptions,
|
|
40
49
|
PluvClientLimits,
|
|
41
50
|
PublicKey,
|
|
42
|
-
UpdateMyPresenceAction,
|
|
43
|
-
UserInfo,
|
|
44
51
|
WebSocketConnection,
|
|
45
|
-
WebSocketState,
|
|
46
52
|
WithMetadata,
|
|
47
53
|
} from "./types";
|
|
54
|
+
import type { UsersManagerConfig } from "./UsersManager";
|
|
55
|
+
import { UsersManager } from "./UsersManager";
|
|
48
56
|
import { debounce } from "./utils";
|
|
49
57
|
|
|
50
58
|
const ADD_TO_STORAGE_STATE_DEBOUNCE_MS = 1_000;
|
|
@@ -68,12 +76,12 @@ declare global {
|
|
|
68
76
|
}
|
|
69
77
|
|
|
70
78
|
export const DEFAULT_PLUV_CLIENT_ADDON = <
|
|
71
|
-
TIO extends IOLike
|
|
72
|
-
TMetadata extends JsonObject
|
|
73
|
-
TPresence extends JsonObject
|
|
74
|
-
|
|
79
|
+
TIO extends IOLike,
|
|
80
|
+
TMetadata extends JsonObject,
|
|
81
|
+
TPresence extends JsonObject,
|
|
82
|
+
TCrdt extends AbstractCrdtDocFactory<any>,
|
|
75
83
|
>(
|
|
76
|
-
input: PluvRoomAddonInput<TIO, TMetadata, TPresence,
|
|
84
|
+
input: PluvRoomAddonInput<TIO, TMetadata, TPresence, TCrdt>,
|
|
77
85
|
): PluvRoomAddonResult => ({
|
|
78
86
|
storage: new StorageStore(input.room.id),
|
|
79
87
|
});
|
|
@@ -126,21 +134,19 @@ interface InternalListeners {
|
|
|
126
134
|
}
|
|
127
135
|
|
|
128
136
|
export type PluvRoomAddon<
|
|
129
|
-
TIO extends IOLike
|
|
130
|
-
TMetadata extends JsonObject
|
|
131
|
-
TPresence extends JsonObject
|
|
132
|
-
|
|
133
|
-
> = (
|
|
134
|
-
input: PluvRoomAddonInput<TIO, TMetadata, TPresence, TStorage>,
|
|
135
|
-
) => Partial<PluvRoomAddonResult>;
|
|
137
|
+
TIO extends IOLike,
|
|
138
|
+
TMetadata extends JsonObject,
|
|
139
|
+
TPresence extends JsonObject,
|
|
140
|
+
TCrdt extends AbstractCrdtDocFactory<any>,
|
|
141
|
+
> = (input: PluvRoomAddonInput<TIO, TMetadata, TPresence, TCrdt>) => Partial<PluvRoomAddonResult>;
|
|
136
142
|
|
|
137
143
|
export interface PluvRoomAddonInput<
|
|
138
|
-
TIO extends IOLike
|
|
139
|
-
TMetadata extends JsonObject
|
|
140
|
-
TPresence extends JsonObject
|
|
141
|
-
|
|
144
|
+
TIO extends IOLike,
|
|
145
|
+
TMetadata extends JsonObject,
|
|
146
|
+
TPresence extends JsonObject,
|
|
147
|
+
TCrdt extends AbstractCrdtDocFactory<any>,
|
|
142
148
|
> {
|
|
143
|
-
room: PluvRoom<TIO, TMetadata, TPresence,
|
|
149
|
+
room: PluvRoom<TIO, TMetadata, TPresence, TCrdt>;
|
|
144
150
|
}
|
|
145
151
|
|
|
146
152
|
export interface PluvRoomAddonResult {
|
|
@@ -167,22 +173,22 @@ export type ReconnectTimeoutMs = number | ((params: ReconnectTimeoutMsParams) =>
|
|
|
167
173
|
|
|
168
174
|
export type RoomConfig<
|
|
169
175
|
TIO extends IOLike,
|
|
170
|
-
TMetadata extends JsonObject
|
|
171
|
-
TPresence extends JsonObject
|
|
172
|
-
|
|
173
|
-
TEvents extends PluvRouterEventConfig<TIO, TPresence,
|
|
176
|
+
TMetadata extends JsonObject,
|
|
177
|
+
TPresence extends JsonObject,
|
|
178
|
+
TCrdt extends AbstractCrdtDocFactory<any>,
|
|
179
|
+
TEvents extends PluvRouterEventConfig<TIO, TPresence, InferStorage<TCrdt>>,
|
|
174
180
|
> = Id<
|
|
175
181
|
{
|
|
176
|
-
addons?: readonly PluvRoomAddon<TIO, TMetadata, TPresence,
|
|
182
|
+
addons?: readonly PluvRoomAddon<TIO, TMetadata, TPresence, TCrdt>[];
|
|
177
183
|
debug?: boolean | PluvRoomDebug<TIO>;
|
|
178
184
|
limits: PluvClientLimits;
|
|
179
185
|
onAuthorizationFail?: (error: Error) => void;
|
|
180
186
|
metadata?: InputZodLike<TMetadata>;
|
|
181
187
|
publicKey?: PublicKey<TMetadata>;
|
|
182
188
|
reconnectTimeoutMs?: ReconnectTimeoutMs;
|
|
183
|
-
router?: PluvRouter<TIO, TPresence,
|
|
189
|
+
router?: PluvRouter<TIO, TPresence, InferStorage<TCrdt>, TEvents>;
|
|
184
190
|
} & RoomEndpoints<TIO, TMetadata> &
|
|
185
|
-
Pick<CrdtManagerOptions<
|
|
191
|
+
Pick<CrdtManagerOptions<TCrdt>, "initialStorage"> &
|
|
186
192
|
UsersManagerConfig<TPresence>
|
|
187
193
|
>;
|
|
188
194
|
|
|
@@ -190,15 +196,17 @@ export class PluvRoom<
|
|
|
190
196
|
TIO extends IOLike<any, any>,
|
|
191
197
|
TMetadata extends JsonObject = {},
|
|
192
198
|
TPresence extends JsonObject = {},
|
|
193
|
-
|
|
194
|
-
TEvents extends PluvRouterEventConfig<TIO, TPresence,
|
|
195
|
-
>
|
|
199
|
+
TCrdt extends AbstractCrdtDocFactory<any> = NoopCrdtDocFactory,
|
|
200
|
+
TEvents extends PluvRouterEventConfig<TIO, TPresence, InferStorage<TCrdt>> = {},
|
|
201
|
+
> implements RoomLike<TIO, TPresence, InferStorage<TCrdt>>
|
|
202
|
+
{
|
|
196
203
|
readonly _endpoints: RoomEndpoints<TIO, TMetadata>;
|
|
197
204
|
|
|
205
|
+
public readonly id: string;
|
|
198
206
|
public readonly metadata?: InputZodLike<TMetadata>;
|
|
199
207
|
|
|
200
|
-
private readonly _crdtManager: CrdtManager<
|
|
201
|
-
private readonly _crdtNotifier = new CrdtNotifier<
|
|
208
|
+
private readonly _crdtManager: CrdtManager<TCrdt>;
|
|
209
|
+
private readonly _crdtNotifier = new CrdtNotifier<InferStorage<TCrdt>>();
|
|
202
210
|
private readonly _debug: boolean | PluvRoomDebug<TIO>;
|
|
203
211
|
private readonly _eventNotifier = new EventNotifier<MergeEvents<TEvents, TIO>>();
|
|
204
212
|
private readonly _intervals: IntervalIds = {
|
|
@@ -209,7 +217,7 @@ export class PluvRoom<
|
|
|
209
217
|
private readonly _otherNotifier = new OtherNotifier<TIO, TPresence>();
|
|
210
218
|
private readonly _publicKey: PublicKey<TMetadata> | null = null;
|
|
211
219
|
private readonly _reconnectTimeoutMs: ReconnectTimeoutMs;
|
|
212
|
-
private readonly _router: PluvRouter<TIO, TPresence,
|
|
220
|
+
private readonly _router: PluvRouter<TIO, TPresence, InferStorage<TCrdt>, TEvents>;
|
|
213
221
|
private readonly _stateNotifier = new StateNotifier<TIO, TPresence>();
|
|
214
222
|
private readonly _storageStore: AbstractStorageStore;
|
|
215
223
|
private readonly _subscriptions: InternalSubscriptions = {
|
|
@@ -238,7 +246,7 @@ export class PluvRoom<
|
|
|
238
246
|
private _windowListeners: WindowListeners | null = null;
|
|
239
247
|
private _wsListeners: WebSocketListeners | null = null;
|
|
240
248
|
|
|
241
|
-
constructor(room: string, options: RoomConfig<TIO, TMetadata, TPresence,
|
|
249
|
+
constructor(room: string, options: RoomConfig<TIO, TMetadata, TPresence, TCrdt, TEvents>) {
|
|
242
250
|
const {
|
|
243
251
|
addons = [],
|
|
244
252
|
authEndpoint,
|
|
@@ -255,22 +263,20 @@ export class PluvRoom<
|
|
|
255
263
|
wsEndpoint,
|
|
256
264
|
} = options;
|
|
257
265
|
|
|
258
|
-
|
|
266
|
+
this.id = room;
|
|
267
|
+
this.metadata = metadata;
|
|
259
268
|
|
|
260
269
|
const addon = this._getAddon(addons);
|
|
261
|
-
|
|
262
270
|
const { storage } = {
|
|
263
271
|
...DEFAULT_PLUV_CLIENT_ADDON({ room: this }),
|
|
264
272
|
...addon({ room: this }),
|
|
265
273
|
};
|
|
266
|
-
|
|
267
|
-
this.metadata = metadata;
|
|
274
|
+
this._storageStore = storage;
|
|
268
275
|
|
|
269
276
|
this._debug = debug;
|
|
270
277
|
this._endpoints = { authEndpoint, wsEndpoint } as RoomEndpoints<TIO, TMetadata>;
|
|
271
278
|
this._limits = limits;
|
|
272
279
|
this._reconnectTimeoutMs = reconnectTimeoutMs;
|
|
273
|
-
this._storageStore = storage;
|
|
274
280
|
|
|
275
281
|
if (!!publicKey) this._publicKey = publicKey;
|
|
276
282
|
|
|
@@ -281,13 +287,14 @@ export class PluvRoom<
|
|
|
281
287
|
};
|
|
282
288
|
|
|
283
289
|
this._router =
|
|
284
|
-
router ??
|
|
290
|
+
router ??
|
|
291
|
+
(new PluvRouter({}) as PluvRouter<TIO, TPresence, InferStorage<TCrdt>, TEvents>);
|
|
285
292
|
this._usersManager = new UsersManager<TIO, TPresence>({
|
|
286
293
|
initialPresence,
|
|
287
294
|
limits: this._limits,
|
|
288
295
|
presence,
|
|
289
296
|
});
|
|
290
|
-
this._crdtManager = new CrdtManager<
|
|
297
|
+
this._crdtManager = new CrdtManager<TCrdt>({ initialStorage });
|
|
291
298
|
}
|
|
292
299
|
|
|
293
300
|
public get storageLoaded(): boolean {
|
|
@@ -313,7 +320,7 @@ export class PluvRoom<
|
|
|
313
320
|
any,
|
|
314
321
|
any,
|
|
315
322
|
TPresence,
|
|
316
|
-
|
|
323
|
+
InferStorage<TCrdt>
|
|
317
324
|
> | null;
|
|
318
325
|
|
|
319
326
|
if (!procedure?.config.broadcast) {
|
|
@@ -327,7 +334,7 @@ export class PluvRoom<
|
|
|
327
334
|
if (!myself) return;
|
|
328
335
|
|
|
329
336
|
const parsed = procedure.config.input ? procedure.config.input.parse(data) : data;
|
|
330
|
-
const context: EventResolverContext<TIO, TPresence,
|
|
337
|
+
const context: EventResolverContext<TIO, TPresence, InferStorage<TCrdt>> = {
|
|
331
338
|
doc: this._crdtManager.doc,
|
|
332
339
|
others: this._usersManager.getOthers(),
|
|
333
340
|
room: this.id,
|
|
@@ -335,7 +342,13 @@ export class PluvRoom<
|
|
|
335
342
|
};
|
|
336
343
|
|
|
337
344
|
const output = await (
|
|
338
|
-
procedure.config.broadcast as EventResolver<
|
|
345
|
+
procedure.config.broadcast as EventResolver<
|
|
346
|
+
TIO,
|
|
347
|
+
any,
|
|
348
|
+
any,
|
|
349
|
+
TPresence,
|
|
350
|
+
InferStorage<TCrdt>
|
|
351
|
+
>
|
|
339
352
|
)(parsed, context);
|
|
340
353
|
|
|
341
354
|
Object.entries(output).forEach(([_type, _data]) => {
|
|
@@ -483,7 +496,7 @@ export class PluvRoom<
|
|
|
483
496
|
return Object.freeze(JSON.parse(JSON.stringify(this._state.connection)));
|
|
484
497
|
};
|
|
485
498
|
|
|
486
|
-
public getDoc = ():
|
|
499
|
+
public getDoc = (): CrdtDocLike<InferStorage<TCrdt>> => {
|
|
487
500
|
return this._crdtManager.doc;
|
|
488
501
|
};
|
|
489
502
|
|
|
@@ -503,7 +516,9 @@ export class PluvRoom<
|
|
|
503
516
|
return this._usersManager.getOthers();
|
|
504
517
|
};
|
|
505
518
|
|
|
506
|
-
public getStorage = <TKey extends keyof
|
|
519
|
+
public getStorage = <TKey extends keyof InferStorage<TCrdt>>(
|
|
520
|
+
type: TKey,
|
|
521
|
+
): InferStorage<TCrdt>[TKey] | null => {
|
|
507
522
|
const sharedType = this._crdtManager.get(type);
|
|
508
523
|
|
|
509
524
|
if (typeof sharedType === "undefined") return null;
|
|
@@ -511,11 +526,11 @@ export class PluvRoom<
|
|
|
511
526
|
return sharedType;
|
|
512
527
|
};
|
|
513
528
|
|
|
514
|
-
public getStorageJson(): InferCrdtJson<
|
|
515
|
-
public getStorageJson<TKey extends keyof
|
|
529
|
+
public getStorageJson(): InferCrdtJson<InferStorage<TCrdt>> | null;
|
|
530
|
+
public getStorageJson<TKey extends keyof InferStorage<TCrdt>>(
|
|
516
531
|
type: TKey,
|
|
517
|
-
): InferCrdtJson<
|
|
518
|
-
public getStorageJson<TKey extends keyof
|
|
532
|
+
): InferCrdtJson<InferStorage<TCrdt>[TKey]> | null;
|
|
533
|
+
public getStorageJson<TKey extends keyof InferStorage<TCrdt>>(type?: TKey) {
|
|
519
534
|
if (this._state.connection.id === null) return null;
|
|
520
535
|
|
|
521
536
|
if (typeof type === "undefined") return this._crdtManager.doc.toJson();
|
|
@@ -542,16 +557,16 @@ export class PluvRoom<
|
|
|
542
557
|
this._crdtManager.doc.redo();
|
|
543
558
|
};
|
|
544
559
|
|
|
545
|
-
public storage = <TKey extends keyof
|
|
560
|
+
public storage = <TKey extends keyof InferStorage<TCrdt>>(
|
|
546
561
|
key: TKey,
|
|
547
|
-
fn: (value: InferCrdtJson<
|
|
562
|
+
fn: (value: InferCrdtJson<InferStorage<TCrdt>[TKey]>) => void,
|
|
548
563
|
): (() => void) => {
|
|
549
564
|
return this._crdtNotifier.subscribe(key, fn);
|
|
550
565
|
};
|
|
551
566
|
|
|
552
567
|
public storageRoot = (
|
|
553
568
|
fn: (value: {
|
|
554
|
-
[P in keyof
|
|
569
|
+
[P in keyof InferStorage<TCrdt>]: InferCrdtJson<InferStorage<TCrdt>[P]>;
|
|
555
570
|
}) => void,
|
|
556
571
|
): (() => void) => {
|
|
557
572
|
return this._crdtNotifier.subcribeRoot(fn);
|
|
@@ -564,7 +579,7 @@ export class PluvRoom<
|
|
|
564
579
|
return this._stateNotifier.subscribe(name, callback);
|
|
565
580
|
};
|
|
566
581
|
|
|
567
|
-
public transact = (fn: (storage:
|
|
582
|
+
public transact = (fn: (storage: InferStorage<TCrdt>) => void, origin?: string): void => {
|
|
568
583
|
const _origin = origin ?? this._state.connection.id;
|
|
569
584
|
|
|
570
585
|
/**
|
|
@@ -707,7 +722,7 @@ export class PluvRoom<
|
|
|
707
722
|
|
|
708
723
|
return { ...acc, [prop]: serialized };
|
|
709
724
|
},
|
|
710
|
-
{} as { [P in keyof
|
|
725
|
+
{} as { [P in keyof InferStorage<TCrdt>]: InferCrdtJson<InferStorage<TCrdt>[P]> },
|
|
711
726
|
);
|
|
712
727
|
|
|
713
728
|
this._crdtNotifier.rootSubject.next(storageRoot);
|
|
@@ -731,9 +746,9 @@ export class PluvRoom<
|
|
|
731
746
|
);
|
|
732
747
|
|
|
733
748
|
private _getAddon = (
|
|
734
|
-
addons: readonly PluvRoomAddon<TIO, TMetadata, TPresence,
|
|
735
|
-
): PluvRoomAddon<TIO, TMetadata, TPresence,
|
|
736
|
-
return addons.reduce<PluvRoomAddon<TIO, TMetadata, TPresence,
|
|
749
|
+
addons: readonly PluvRoomAddon<TIO, TMetadata, TPresence, TCrdt>[],
|
|
750
|
+
): PluvRoomAddon<TIO, TMetadata, TPresence, TCrdt> => {
|
|
751
|
+
return addons.reduce<PluvRoomAddon<TIO, TMetadata, TPresence, TCrdt>>(
|
|
737
752
|
(acc, addon) => () => ({
|
|
738
753
|
...acc({ room: this }),
|
|
739
754
|
...addon({ room: this }),
|
|
@@ -1044,7 +1059,7 @@ export class PluvRoom<
|
|
|
1044
1059
|
return { ...acc, [prop]: serialized };
|
|
1045
1060
|
},
|
|
1046
1061
|
{} as {
|
|
1047
|
-
[P in keyof
|
|
1062
|
+
[P in keyof InferStorage<TCrdt>]: InferCrdtJson<InferStorage<TCrdt>[P]>;
|
|
1048
1063
|
},
|
|
1049
1064
|
);
|
|
1050
1065
|
|
|
@@ -1121,7 +1136,8 @@ export class PluvRoom<
|
|
|
1121
1136
|
}
|
|
1122
1137
|
|
|
1123
1138
|
private _logDebug(...data: any[]): void {
|
|
1124
|
-
if (
|
|
1139
|
+
if (typeof process === "undefined") return;
|
|
1140
|
+
if (process.env?.NODE_ENV === "production") return;
|
|
1125
1141
|
if (this._debug) console.log(...data);
|
|
1126
1142
|
}
|
|
1127
1143
|
|
package/src/PluvRouter.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { CrdtType } from "@pluv/crdt";
|
|
2
|
-
import type {
|
|
2
|
+
import type { IOLike, IORouterLike, JsonObject } from "@pluv/types";
|
|
3
3
|
import type { PluvProcedure } from "./PluvProcedure";
|
|
4
4
|
|
|
5
5
|
export type PluvRouterEventConfig<
|
|
@@ -21,28 +21,6 @@ export type MergedRouter<
|
|
|
21
21
|
? MergedRouter<ITail, TIO, TPresence, TStorage, TRoot & IHead["_defs"]["events"]>
|
|
22
22
|
: PluvRouter<TIO, TPresence, TStorage, TRoot>;
|
|
23
23
|
|
|
24
|
-
export type MergeEvents<
|
|
25
|
-
TClientEvents extends PluvRouterEventConfig<any, any, any>,
|
|
26
|
-
TServerIO extends IOLike<any, any>,
|
|
27
|
-
> =
|
|
28
|
-
TServerIO extends IOLike<infer IAuthorize, infer IServerEvents>
|
|
29
|
-
? IOLike<
|
|
30
|
-
IAuthorize,
|
|
31
|
-
{
|
|
32
|
-
[P in keyof TClientEvents]: TClientEvents[P] extends ProcedureLike<
|
|
33
|
-
infer IClientInput,
|
|
34
|
-
infer IClientOutput
|
|
35
|
-
>
|
|
36
|
-
? {
|
|
37
|
-
[K in keyof IClientOutput]: K extends keyof IServerEvents
|
|
38
|
-
? IServerEvents[K]
|
|
39
|
-
: ProcedureLike<IClientInput, Id<Pick<IClientOutput, K>>>;
|
|
40
|
-
}[keyof IClientOutput]
|
|
41
|
-
: never;
|
|
42
|
-
} & IServerEvents
|
|
43
|
-
>
|
|
44
|
-
: never;
|
|
45
|
-
|
|
46
24
|
export class PluvRouter<
|
|
47
25
|
TIO extends IOLike = IOLike,
|
|
48
26
|
TPresence extends JsonObject = {},
|
package/src/StateNotifier.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
import type { Id, IOLike, JsonObject } from "@pluv/types";
|
|
1
|
+
import type { Id, IOLike, JsonObject, UserInfo, WebSocketState } from "@pluv/types";
|
|
2
2
|
import type { Subject } from "wonka";
|
|
3
3
|
import { makeSubject, subscribe } from "wonka";
|
|
4
|
-
import type { UserInfo, WebSocketState } from "./types";
|
|
5
4
|
|
|
6
5
|
export interface StateNotifierSubjects<TIO extends IOLike, TPresence extends JsonObject> {
|
|
7
6
|
connection: Subject<Id<WebSocketState<TIO>>>;
|
package/src/UsersManager.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type { InputZodLike, IOLike, JsonObject, OptionalProps } from "@pluv/types";
|
|
2
|
-
import type { PluvClientLimits
|
|
1
|
+
import type { InputZodLike, IOLike, JsonObject, OptionalProps, UserInfo } from "@pluv/types";
|
|
2
|
+
import type { PluvClientLimits } from "./types";
|
|
3
3
|
import { pickBy } from "./utils";
|
|
4
4
|
|
|
5
5
|
export type Presence = Record<string, unknown>;
|
package/src/createClient.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { AbstractCrdtDocFactory, NoopCrdtDocFactory } from "@pluv/crdt";
|
|
2
2
|
import type { IOLike, JsonObject } from "@pluv/types";
|
|
3
3
|
import type { PluvClientOptions } from "./PluvClient";
|
|
4
4
|
import { PluvClient } from "./PluvClient";
|
|
@@ -6,10 +6,10 @@ import { PluvClient } from "./PluvClient";
|
|
|
6
6
|
export const createClient = <
|
|
7
7
|
TIO extends IOLike<any, any>,
|
|
8
8
|
TPresence extends JsonObject = {},
|
|
9
|
-
|
|
9
|
+
TCrdt extends AbstractCrdtDocFactory<any> = NoopCrdtDocFactory,
|
|
10
10
|
TMetadata extends JsonObject = {},
|
|
11
11
|
>(
|
|
12
|
-
options: PluvClientOptions<TIO, TPresence,
|
|
13
|
-
): PluvClient<TIO, TPresence,
|
|
14
|
-
return new PluvClient<TIO, TPresence,
|
|
12
|
+
options: PluvClientOptions<TIO, TPresence, TCrdt, TMetadata>,
|
|
13
|
+
): PluvClient<TIO, TPresence, TCrdt, TMetadata> => {
|
|
14
|
+
return new PluvClient<TIO, TPresence, TCrdt, TMetadata>(options);
|
|
15
15
|
};
|
package/src/index.ts
CHANGED
|
@@ -1,9 +1,18 @@
|
|
|
1
1
|
export type { CrdtType, InferCrdtJson } from "@pluv/crdt";
|
|
2
|
-
export
|
|
3
|
-
export {
|
|
2
|
+
export { ConnectionState } from "@pluv/types";
|
|
3
|
+
export type {
|
|
4
|
+
BaseUser,
|
|
5
|
+
EventMessage,
|
|
6
|
+
EventNotifierSubscriptionCallback,
|
|
7
|
+
EventRecord,
|
|
8
|
+
IOEventMessage,
|
|
9
|
+
MergeEvents,
|
|
10
|
+
RoomLike,
|
|
11
|
+
UserInfo,
|
|
12
|
+
WebSocketState,
|
|
13
|
+
} from "@pluv/types";
|
|
4
14
|
export { AbstractStorageStore } from "./AbstractStorageStore";
|
|
5
15
|
export { createClient } from "./createClient";
|
|
6
|
-
export { ConnectionState } from "./enums";
|
|
7
16
|
export { infer } from "./infer";
|
|
8
17
|
export type { InferCallback } from "./infer";
|
|
9
18
|
export { MockedRoom } from "./MockedRoom";
|
|
@@ -20,14 +29,7 @@ export type {
|
|
|
20
29
|
RoomConnectParams,
|
|
21
30
|
RoomEndpoints,
|
|
22
31
|
} from "./PluvRoom";
|
|
23
|
-
export type {
|
|
32
|
+
export type { PluvRouter, PluvRouterEventConfig } from "./PluvRouter";
|
|
24
33
|
export { register } from "./register";
|
|
25
34
|
export type { RegisterParams } from "./register";
|
|
26
|
-
export type {
|
|
27
|
-
InferMetadata,
|
|
28
|
-
PublicKey,
|
|
29
|
-
PublicKeyParams,
|
|
30
|
-
UserInfo,
|
|
31
|
-
WebSocketConnection,
|
|
32
|
-
WebSocketState,
|
|
33
|
-
} from "./types";
|
|
35
|
+
export type { InferMetadata, PublicKey, PublicKeyParams, WebSocketConnection } from "./types";
|
package/src/types.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { CrdtType } from "@pluv/crdt";
|
|
2
2
|
import type {
|
|
3
|
+
ConnectionState,
|
|
4
|
+
CrdtDocLike,
|
|
3
5
|
EventRecord,
|
|
4
6
|
Id,
|
|
5
7
|
InferIOAuthorize,
|
|
@@ -7,8 +9,8 @@ import type {
|
|
|
7
9
|
IOLike,
|
|
8
10
|
JsonObject,
|
|
9
11
|
MaybePromise,
|
|
12
|
+
UserInfo,
|
|
10
13
|
} from "@pluv/types";
|
|
11
|
-
import type { ConnectionState } from "./enums";
|
|
12
14
|
import type { PluvClient } from "./PluvClient";
|
|
13
15
|
|
|
14
16
|
export interface AuthorizationState<TIO extends IOLike> {
|
|
@@ -32,7 +34,7 @@ export interface EventResolverContext<
|
|
|
32
34
|
TPresence extends JsonObject = {},
|
|
33
35
|
TStorage extends Record<string, CrdtType<any, any>> = {},
|
|
34
36
|
> {
|
|
35
|
-
doc:
|
|
37
|
+
doc: CrdtDocLike<TStorage>;
|
|
36
38
|
others: readonly UserInfo<TIO, TPresence>[];
|
|
37
39
|
room: string;
|
|
38
40
|
user: UserInfo<TIO, TPresence>;
|
|
@@ -74,16 +76,6 @@ export type PublicKey<TMetadata extends JsonObject> =
|
|
|
74
76
|
| string
|
|
75
77
|
| ((params: PublicKeyParams<TMetadata>) => string);
|
|
76
78
|
|
|
77
|
-
export type UpdateMyPresenceAction<TPresence extends JsonObject> =
|
|
78
|
-
| Partial<TPresence>
|
|
79
|
-
| ((oldPresence: TPresence | null) => Partial<TPresence>);
|
|
80
|
-
|
|
81
|
-
export interface UserInfo<TIO extends IOLike, TPresence extends JsonObject = {}> {
|
|
82
|
-
connectionId: string;
|
|
83
|
-
presence: TPresence;
|
|
84
|
-
user: Id<InferIOAuthorizeUser<InferIOAuthorize<TIO>>>;
|
|
85
|
-
}
|
|
86
|
-
|
|
87
79
|
export interface WebSocketConnection {
|
|
88
80
|
/**
|
|
89
81
|
* @description How many times a connection attempt was made. This will increment upon each
|
|
@@ -103,12 +95,6 @@ export interface WebSocketConnection {
|
|
|
103
95
|
state: ConnectionState;
|
|
104
96
|
}
|
|
105
97
|
|
|
106
|
-
export interface WebSocketState<TIO extends IOLike> {
|
|
107
|
-
authorization: AuthorizationState<TIO>;
|
|
108
|
-
connection: WebSocketConnection;
|
|
109
|
-
webSocket: WebSocket | null;
|
|
110
|
-
}
|
|
111
|
-
|
|
112
98
|
export type WithMetadata<TMetadata extends JsonObject = {}> = keyof TMetadata extends never
|
|
113
99
|
? { metadata?: undefined }
|
|
114
100
|
: { metadata: TMetadata };
|
package/src/AbstractRoom.ts
DELETED
|
@@ -1,84 +0,0 @@
|
|
|
1
|
-
import type { AbstractCrdtDoc, CrdtType, InferCrdtJson } from "@pluv/crdt";
|
|
2
|
-
import type { IOLike, Id, InferIOInput, InferIOOutput, JsonObject } from "@pluv/types";
|
|
3
|
-
import type { EventNotifierSubscriptionCallback } from "./EventNotifier";
|
|
4
|
-
import type { OtherNotifierSubscriptionCallback } from "./OtherNotifier";
|
|
5
|
-
import type { MergeEvents, PluvRouterEventConfig } from "./PluvRouter";
|
|
6
|
-
import type { StateNotifierSubjects, SubscriptionCallback } from "./StateNotifier";
|
|
7
|
-
import type { UpdateMyPresenceAction, UserInfo, WebSocketConnection } from "./types";
|
|
8
|
-
|
|
9
|
-
export abstract class AbstractRoom<
|
|
10
|
-
TIO extends IOLike,
|
|
11
|
-
TPresence extends JsonObject = {},
|
|
12
|
-
TStorage extends Record<string, CrdtType<any, any>> = {},
|
|
13
|
-
TEvents extends PluvRouterEventConfig<TIO, TPresence, TStorage> = {},
|
|
14
|
-
> {
|
|
15
|
-
public readonly id: string;
|
|
16
|
-
|
|
17
|
-
constructor(room: string) {
|
|
18
|
-
this.id = room;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
public abstract storageLoaded: boolean;
|
|
22
|
-
|
|
23
|
-
public abstract broadcast<TEvent extends keyof InferIOInput<MergeEvents<TEvents, TIO>>>(
|
|
24
|
-
event: TEvent,
|
|
25
|
-
data: Id<InferIOInput<MergeEvents<TEvents, TIO>>[TEvent]>,
|
|
26
|
-
): void;
|
|
27
|
-
|
|
28
|
-
public abstract canRedo(): boolean;
|
|
29
|
-
|
|
30
|
-
public abstract canUndo(): boolean;
|
|
31
|
-
|
|
32
|
-
public abstract getDoc(): AbstractCrdtDoc<TStorage>;
|
|
33
|
-
|
|
34
|
-
public abstract event<TEvent extends keyof InferIOOutput<MergeEvents<TEvents, TIO>>>(
|
|
35
|
-
event: TEvent,
|
|
36
|
-
callback: EventNotifierSubscriptionCallback<MergeEvents<TEvents, TIO>, TEvent>,
|
|
37
|
-
): () => void;
|
|
38
|
-
|
|
39
|
-
public abstract getConnection(): WebSocketConnection;
|
|
40
|
-
|
|
41
|
-
public abstract getMyPresence(): TPresence;
|
|
42
|
-
|
|
43
|
-
public abstract getMyself(): Id<UserInfo<TIO, TPresence>> | null;
|
|
44
|
-
|
|
45
|
-
public abstract getOther(connectionId: string): Id<UserInfo<TIO, TPresence>> | null;
|
|
46
|
-
|
|
47
|
-
public abstract getOthers(): readonly Id<UserInfo<TIO, TPresence>>[];
|
|
48
|
-
|
|
49
|
-
public abstract getStorage<TKey extends keyof TStorage>(type: TKey): TStorage[TKey] | null;
|
|
50
|
-
|
|
51
|
-
public abstract getStorageJson(): InferCrdtJson<TStorage> | null;
|
|
52
|
-
public abstract getStorageJson<TKey extends keyof TStorage>(
|
|
53
|
-
type: TKey,
|
|
54
|
-
): InferCrdtJson<TStorage[TKey]> | null;
|
|
55
|
-
|
|
56
|
-
public abstract other(
|
|
57
|
-
connectionId: string,
|
|
58
|
-
callback: OtherNotifierSubscriptionCallback<TIO, TPresence>,
|
|
59
|
-
): () => void;
|
|
60
|
-
|
|
61
|
-
public abstract redo(): void;
|
|
62
|
-
|
|
63
|
-
public abstract storage<TKey extends keyof TStorage>(
|
|
64
|
-
key: TKey,
|
|
65
|
-
fn: (value: InferCrdtJson<TStorage[TKey]>) => void,
|
|
66
|
-
): () => void;
|
|
67
|
-
|
|
68
|
-
public abstract storageRoot(
|
|
69
|
-
fn: (value: {
|
|
70
|
-
[P in keyof TStorage]: InferCrdtJson<TStorage[P]>;
|
|
71
|
-
}) => void,
|
|
72
|
-
): () => void;
|
|
73
|
-
|
|
74
|
-
public abstract subscribe<TSubject extends keyof StateNotifierSubjects<TIO, TPresence>>(
|
|
75
|
-
name: TSubject,
|
|
76
|
-
callback: SubscriptionCallback<TIO, TPresence, TSubject>,
|
|
77
|
-
): () => void;
|
|
78
|
-
|
|
79
|
-
public abstract transact(fn: (storage: TStorage) => void, origin?: string): void;
|
|
80
|
-
|
|
81
|
-
public abstract undo(): void;
|
|
82
|
-
|
|
83
|
-
public abstract updateMyPresence(presence: UpdateMyPresenceAction<TPresence>): void;
|
|
84
|
-
}
|
package/src/enums.ts
DELETED