@pluv/io 0.5.0 → 0.7.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/.eslintrc.js +2 -2
- package/.turbo/turbo-build.log +8 -8
- package/CHANGELOG.md +82 -0
- package/dist/index.d.ts +8 -4
- package/dist/index.js +14 -12
- package/dist/index.mjs +14 -12
- package/package.json +10 -8
- package/src/PluvIO.ts +28 -19
- package/src/createIO.ts +4 -4
- package/src/index.ts +6 -1
package/.eslintrc.js
CHANGED
package/.turbo/turbo-build.log
CHANGED
|
@@ -1,17 +1,17 @@
|
|
|
1
1
|
|
|
2
|
-
> @pluv/io@0.
|
|
2
|
+
> @pluv/io@0.7.0 build /home/runner/work/pluv/pluv/packages/io
|
|
3
3
|
> tsup src/index.ts --format esm,cjs --dts
|
|
4
4
|
|
|
5
5
|
[34mCLI[39m Building entry: src/index.ts
|
|
6
6
|
[34mCLI[39m Using tsconfig: tsconfig.json
|
|
7
|
-
[34mCLI[39m tsup
|
|
7
|
+
[34mCLI[39m tsup v7.0.0
|
|
8
8
|
[34mCLI[39m Target: es6
|
|
9
9
|
[34mESM[39m Build start
|
|
10
10
|
[34mCJS[39m Build start
|
|
11
|
-
[32mCJS[39m [1mdist/index.js [22m[
|
|
12
|
-
[32mCJS[39m ⚡️ Build success in
|
|
13
|
-
[32mESM[39m [1mdist/index.mjs [22m[32m28.
|
|
14
|
-
[32mESM[39m ⚡️ Build success in
|
|
11
|
+
[32mCJS[39m [1mdist/index.js [22m[32m31.05 KB[39m
|
|
12
|
+
[32mCJS[39m ⚡️ Build success in 81ms
|
|
13
|
+
[32mESM[39m [1mdist/index.mjs [22m[32m28.99 KB[39m
|
|
14
|
+
[32mESM[39m ⚡️ Build success in 84ms
|
|
15
15
|
[34mDTS[39m Build start
|
|
16
|
-
[32mDTS[39m ⚡️ Build success in
|
|
17
|
-
[32mDTS[39m [1mdist/index.d.ts [22m[
|
|
16
|
+
[32mDTS[39m ⚡️ Build success in 4546ms
|
|
17
|
+
[32mDTS[39m [1mdist/index.d.ts [22m[32m17.03 KB[39m
|
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,87 @@
|
|
|
1
1
|
# @pluv/io
|
|
2
2
|
|
|
3
|
+
## 0.7.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- 829d31b: Added support for defining persistant frontend storage for rooms via a new `addons` option on rooms.
|
|
8
|
+
|
|
9
|
+
This also introduces the first new addon `@pluv/addon-indexeddb`, which is more-or-less the equivalent to `y-indexeddb` which you can install like so:
|
|
10
|
+
|
|
11
|
+
```
|
|
12
|
+
npm install @pluv/addon-indexeddb
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
To use this new addon, simply pass it to options when creating a room:
|
|
16
|
+
|
|
17
|
+
```ts
|
|
18
|
+
import { addonIndexedDB } from "@pluv/addon-indexeddb";
|
|
19
|
+
import { createClient } from "@pluv/client";
|
|
20
|
+
|
|
21
|
+
const client = createClient({
|
|
22
|
+
// ...
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
const room = client.createRoom("my-new-room", {
|
|
26
|
+
addons: [
|
|
27
|
+
// Define your addons in an array like so
|
|
28
|
+
addonIndexedDB(),
|
|
29
|
+
],
|
|
30
|
+
});
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
Or when using `@pluv/react`:
|
|
34
|
+
|
|
35
|
+
```ts
|
|
36
|
+
const PluvRoom = createRoomBundle({
|
|
37
|
+
// ...
|
|
38
|
+
addons: [
|
|
39
|
+
// Define your addons in an array like so
|
|
40
|
+
addonIndexedDB(),
|
|
41
|
+
],
|
|
42
|
+
});
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
### Patch Changes
|
|
46
|
+
|
|
47
|
+
- 8d11672: bumped dependencies to latest
|
|
48
|
+
- Updated dependencies [8d11672]
|
|
49
|
+
- Updated dependencies [829d31b]
|
|
50
|
+
- @pluv/crdt-yjs@0.4.0
|
|
51
|
+
- @pluv/types@0.2.1
|
|
52
|
+
|
|
53
|
+
## 0.6.0
|
|
54
|
+
|
|
55
|
+
### Minor Changes
|
|
56
|
+
|
|
57
|
+
- 2e7cbfa: Updated createIO config option `initialStorage` to be renamed to `getInitialStorage` and updated the params to use an object that includes platform parameters.
|
|
58
|
+
|
|
59
|
+
Previously:
|
|
60
|
+
|
|
61
|
+
```ts
|
|
62
|
+
createIO({
|
|
63
|
+
initialStorage(room: string): Promise<string> => {
|
|
64
|
+
// ...
|
|
65
|
+
},
|
|
66
|
+
});
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
Now:
|
|
70
|
+
|
|
71
|
+
```ts
|
|
72
|
+
createIO({
|
|
73
|
+
platform: platformCloudflare<Env>(),
|
|
74
|
+
getInitialStorage({
|
|
75
|
+
// If you're using Cloudflare, this is the Cloudflare env parameter
|
|
76
|
+
env,
|
|
77
|
+
// The name of the room
|
|
78
|
+
room,
|
|
79
|
+
}): Promise<string> => {
|
|
80
|
+
// ...
|
|
81
|
+
},
|
|
82
|
+
});
|
|
83
|
+
```
|
|
84
|
+
|
|
3
85
|
## 0.5.0
|
|
4
86
|
|
|
5
87
|
### Minor Changes
|
package/dist/index.d.ts
CHANGED
|
@@ -238,6 +238,10 @@ interface AuthorizeModule {
|
|
|
238
238
|
type InferIOContext<TIO extends PluvIO<any, any, any, any, any, any, any>> = TIO extends PluvIO<any, any, infer IContext, any, any, any, any> ? IContext : never;
|
|
239
239
|
type InferIOPlatform<TIO extends PluvIO<any, any, any, any, any, any, any>> = TIO extends PluvIO<infer IPlatform, any, any, any, any, any, any> ? IPlatform : never;
|
|
240
240
|
type InferIORoom<TIO extends PluvIO<any, any, any, any, any, any, any>> = TIO extends PluvIO<infer IPlatform, infer IAuthorize, infer IContext, infer IInput, infer IOutputBroadcast, infer IOutputSelf, infer IOutputSync> ? IORoom<IPlatform, IAuthorize, IContext, IInput, IOutputBroadcast, IOutputSelf, IOutputSync> : never;
|
|
241
|
+
type GetInitialStorageEvent<TPlatform extends AbstractPlatform> = {
|
|
242
|
+
room: string;
|
|
243
|
+
} & InferPlatformRoomContextType<TPlatform>;
|
|
244
|
+
type GetInitialStorageFn<TPlatform extends AbstractPlatform> = (event: GetInitialStorageEvent<TPlatform>) => MaybePromise<Maybe<string>>;
|
|
241
245
|
interface PluvIOListeners<TPlatform extends AbstractPlatform> {
|
|
242
246
|
onRoomDeleted: (event: IORoomListenerEvent<TPlatform>) => void;
|
|
243
247
|
onStorageUpdated: (event: IORoomListenerEvent<TPlatform>) => void;
|
|
@@ -247,7 +251,7 @@ type PluvIOConfig<TPlatform extends AbstractPlatform<any> = AbstractPlatform<any
|
|
|
247
251
|
context?: TContext;
|
|
248
252
|
debug?: boolean;
|
|
249
253
|
events?: InferEventConfig<TPlatform, TContext, TInput, TOutputBroadcast, TOutputSelf, TOutputSync>;
|
|
250
|
-
|
|
254
|
+
getInitialStorage?: GetInitialStorageFn<TPlatform>;
|
|
251
255
|
platform: TPlatform;
|
|
252
256
|
};
|
|
253
257
|
type GetRoomOptions<TPlatform extends AbstractPlatform> = {
|
|
@@ -255,7 +259,7 @@ type GetRoomOptions<TPlatform extends AbstractPlatform> = {
|
|
|
255
259
|
} & InferPlatformRoomContextType<TPlatform>;
|
|
256
260
|
declare class PluvIO<TPlatform extends AbstractPlatform<any> = AbstractPlatform<any>, TAuthorize extends IOAuthorize<any, any> = BaseIOAuthorize, TContext extends JsonObject = {}, TInput extends EventRecord<string, any> = {}, TOutputBroadcast extends EventRecord<string, any> = {}, TOutputSelf extends EventRecord<string, any> = {}, TOutputSync extends EventRecord<string, any> = {}> implements IOLike<TAuthorize, TInput, TOutputBroadcast, TOutputSelf, TOutputSync> {
|
|
257
261
|
private _context;
|
|
258
|
-
private
|
|
262
|
+
private _getInitialStorage;
|
|
259
263
|
private _rooms;
|
|
260
264
|
readonly _authorize: TAuthorize | null;
|
|
261
265
|
readonly _debug: boolean;
|
|
@@ -275,9 +279,9 @@ type CreateIOParams<TPlatform extends AbstractPlatform<any> = AbstractPlatform<a
|
|
|
275
279
|
authorize?: IOAuthorize<TAuthorizeUser, TAuthorizeRequired>;
|
|
276
280
|
context?: TContext;
|
|
277
281
|
debug?: boolean;
|
|
278
|
-
|
|
282
|
+
getInitialStorage?: GetInitialStorageFn<TPlatform>;
|
|
279
283
|
platform: TPlatform;
|
|
280
284
|
};
|
|
281
285
|
declare const createIO: <TPlatform extends AbstractPlatform<any, {}, {}> = AbstractPlatform<any, {}, {}>, TContext extends JsonObject = {}, TAuthorizeUser extends BaseUser = BaseUser, TAuthorizeRequired extends boolean = false>(params: CreateIOParams<TPlatform, TContext, TAuthorizeUser, TAuthorizeRequired>) => PluvIO<TPlatform, IOAuthorize<TAuthorizeUser, TAuthorizeRequired>, TContext, BaseClientEventRecord, BaseIOEventRecord<IOAuthorize<TAuthorizeUser, TAuthorizeRequired>>, {}, {}>;
|
|
282
286
|
|
|
283
|
-
export { AbstractCloseEvent, AbstractErrorEvent, AbstractEvent, AbstractEventMap, AbstractListener, AbstractMessageEvent, AbstractPersistance, AbstractPlatform, AbstractPubSub, AbstractWebSocket, AbstractWebSocketConfig, AbstractWebSocketHandleErrorParams, AuthorizeModule, AuthorizeParams, ConvertWebSocketConfig, CreateIOParams, GetRoomOptions, IOPubSubEventMessage, IORoom, InferIORoom, InferPlatformEventContextType, InferPlatformRoomContextType, InferPlatformWebSocketType, JWT, JWTEncodeParams, PluvIO, createIO };
|
|
287
|
+
export { AbstractCloseEvent, AbstractErrorEvent, AbstractEvent, AbstractEventMap, AbstractListener, AbstractMessageEvent, AbstractPersistance, AbstractPlatform, AbstractPubSub, AbstractWebSocket, AbstractWebSocketConfig, AbstractWebSocketHandleErrorParams, AuthorizeModule, AuthorizeParams, ConvertWebSocketConfig, CreateIOParams, GetRoomOptions, IOPubSubEventMessage, IORoom, InferIORoom, InferPlatformEventContextType, InferPlatformRoomContextType, InferPlatformWebSocketType, JWT, JWTEncodeParams, PluvIO, PluvIOConfig, createIO };
|
package/dist/index.js
CHANGED
|
@@ -727,10 +727,10 @@ var IORoom = class {
|
|
|
727
727
|
};
|
|
728
728
|
|
|
729
729
|
// src/PluvIO.ts
|
|
730
|
-
var PluvIO = class {
|
|
730
|
+
var PluvIO = class _PluvIO {
|
|
731
731
|
constructor(options) {
|
|
732
732
|
this._context = {};
|
|
733
|
-
this.
|
|
733
|
+
this._getInitialStorage = null;
|
|
734
734
|
this._rooms = /* @__PURE__ */ new Map();
|
|
735
735
|
this._authorize = null;
|
|
736
736
|
this._events = {
|
|
@@ -770,7 +770,9 @@ var PluvIO = class {
|
|
|
770
770
|
var _a, _b;
|
|
771
771
|
const oldState = yield this._platform.persistance.getStorageState(room);
|
|
772
772
|
const updates = [
|
|
773
|
-
oldState ? null : yield (_a = this.
|
|
773
|
+
oldState ? null : yield (_a = this._getInitialStorage) == null ? void 0 : _a.call(this, __spreadProps(__spreadValues({}, context), {
|
|
774
|
+
room
|
|
775
|
+
})),
|
|
774
776
|
update
|
|
775
777
|
];
|
|
776
778
|
if (updates.some((_update) => !!_update)) {
|
|
@@ -816,7 +818,7 @@ var PluvIO = class {
|
|
|
816
818
|
},
|
|
817
819
|
$UPDATE_STORAGE: {
|
|
818
820
|
resolver: ({ origin, update }, { context, doc: doc2, room }) => {
|
|
819
|
-
if (origin === "$
|
|
821
|
+
if (origin === "$INITIALIZED" && Object.keys(doc2.toJSON()).length) {
|
|
820
822
|
return;
|
|
821
823
|
}
|
|
822
824
|
const updated = doc2.applyUpdate(update);
|
|
@@ -836,7 +838,7 @@ var PluvIO = class {
|
|
|
836
838
|
context,
|
|
837
839
|
debug = false,
|
|
838
840
|
events,
|
|
839
|
-
|
|
841
|
+
getInitialStorage,
|
|
840
842
|
onRoomDeleted,
|
|
841
843
|
onStorageUpdated,
|
|
842
844
|
platform
|
|
@@ -849,8 +851,8 @@ var PluvIO = class {
|
|
|
849
851
|
this._context = context;
|
|
850
852
|
if (events)
|
|
851
853
|
this._events = events;
|
|
852
|
-
if (
|
|
853
|
-
this.
|
|
854
|
+
if (getInitialStorage)
|
|
855
|
+
this._getInitialStorage = getInitialStorage;
|
|
854
856
|
this._listeners = {
|
|
855
857
|
onRoomDeleted: (event) => onRoomDeleted == null ? void 0 : onRoomDeleted(event),
|
|
856
858
|
onStorageUpdated: (event) => onStorageUpdated == null ? void 0 : onStorageUpdated(event)
|
|
@@ -876,9 +878,9 @@ var PluvIO = class {
|
|
|
876
878
|
const newEvent = {
|
|
877
879
|
[event]: config
|
|
878
880
|
};
|
|
879
|
-
return
|
|
881
|
+
return _PluvIO.merge(
|
|
880
882
|
this,
|
|
881
|
-
new
|
|
883
|
+
new _PluvIO({
|
|
882
884
|
events: newEvent,
|
|
883
885
|
platform: this._platform
|
|
884
886
|
})
|
|
@@ -940,7 +942,7 @@ var PluvIO = class {
|
|
|
940
942
|
const events2 = second._events;
|
|
941
943
|
const { onRoomDeleted, onStorageUpdated } = first._listeners;
|
|
942
944
|
const events = Object.assign(/* @__PURE__ */ Object.create(null), events1, events2);
|
|
943
|
-
return new
|
|
945
|
+
return new _PluvIO({
|
|
944
946
|
authorize: authorize2,
|
|
945
947
|
debug,
|
|
946
948
|
events,
|
|
@@ -968,7 +970,7 @@ var createIO = (params) => {
|
|
|
968
970
|
authorize: authorize2,
|
|
969
971
|
context,
|
|
970
972
|
debug,
|
|
971
|
-
|
|
973
|
+
getInitialStorage,
|
|
972
974
|
onRoomDeleted,
|
|
973
975
|
onStorageUpdated,
|
|
974
976
|
platform
|
|
@@ -977,7 +979,7 @@ var createIO = (params) => {
|
|
|
977
979
|
authorize: authorize2,
|
|
978
980
|
context,
|
|
979
981
|
debug,
|
|
980
|
-
|
|
982
|
+
getInitialStorage,
|
|
981
983
|
onRoomDeleted,
|
|
982
984
|
onStorageUpdated,
|
|
983
985
|
platform
|
package/dist/index.mjs
CHANGED
|
@@ -690,10 +690,10 @@ var IORoom = class {
|
|
|
690
690
|
};
|
|
691
691
|
|
|
692
692
|
// src/PluvIO.ts
|
|
693
|
-
var PluvIO = class {
|
|
693
|
+
var PluvIO = class _PluvIO {
|
|
694
694
|
constructor(options) {
|
|
695
695
|
this._context = {};
|
|
696
|
-
this.
|
|
696
|
+
this._getInitialStorage = null;
|
|
697
697
|
this._rooms = /* @__PURE__ */ new Map();
|
|
698
698
|
this._authorize = null;
|
|
699
699
|
this._events = {
|
|
@@ -733,7 +733,9 @@ var PluvIO = class {
|
|
|
733
733
|
var _a, _b;
|
|
734
734
|
const oldState = yield this._platform.persistance.getStorageState(room);
|
|
735
735
|
const updates = [
|
|
736
|
-
oldState ? null : yield (_a = this.
|
|
736
|
+
oldState ? null : yield (_a = this._getInitialStorage) == null ? void 0 : _a.call(this, __spreadProps(__spreadValues({}, context), {
|
|
737
|
+
room
|
|
738
|
+
})),
|
|
737
739
|
update
|
|
738
740
|
];
|
|
739
741
|
if (updates.some((_update) => !!_update)) {
|
|
@@ -779,7 +781,7 @@ var PluvIO = class {
|
|
|
779
781
|
},
|
|
780
782
|
$UPDATE_STORAGE: {
|
|
781
783
|
resolver: ({ origin, update }, { context, doc: doc2, room }) => {
|
|
782
|
-
if (origin === "$
|
|
784
|
+
if (origin === "$INITIALIZED" && Object.keys(doc2.toJSON()).length) {
|
|
783
785
|
return;
|
|
784
786
|
}
|
|
785
787
|
const updated = doc2.applyUpdate(update);
|
|
@@ -799,7 +801,7 @@ var PluvIO = class {
|
|
|
799
801
|
context,
|
|
800
802
|
debug = false,
|
|
801
803
|
events,
|
|
802
|
-
|
|
804
|
+
getInitialStorage,
|
|
803
805
|
onRoomDeleted,
|
|
804
806
|
onStorageUpdated,
|
|
805
807
|
platform
|
|
@@ -812,8 +814,8 @@ var PluvIO = class {
|
|
|
812
814
|
this._context = context;
|
|
813
815
|
if (events)
|
|
814
816
|
this._events = events;
|
|
815
|
-
if (
|
|
816
|
-
this.
|
|
817
|
+
if (getInitialStorage)
|
|
818
|
+
this._getInitialStorage = getInitialStorage;
|
|
817
819
|
this._listeners = {
|
|
818
820
|
onRoomDeleted: (event) => onRoomDeleted == null ? void 0 : onRoomDeleted(event),
|
|
819
821
|
onStorageUpdated: (event) => onStorageUpdated == null ? void 0 : onStorageUpdated(event)
|
|
@@ -839,9 +841,9 @@ var PluvIO = class {
|
|
|
839
841
|
const newEvent = {
|
|
840
842
|
[event]: config
|
|
841
843
|
};
|
|
842
|
-
return
|
|
844
|
+
return _PluvIO.merge(
|
|
843
845
|
this,
|
|
844
|
-
new
|
|
846
|
+
new _PluvIO({
|
|
845
847
|
events: newEvent,
|
|
846
848
|
platform: this._platform
|
|
847
849
|
})
|
|
@@ -903,7 +905,7 @@ var PluvIO = class {
|
|
|
903
905
|
const events2 = second._events;
|
|
904
906
|
const { onRoomDeleted, onStorageUpdated } = first._listeners;
|
|
905
907
|
const events = Object.assign(/* @__PURE__ */ Object.create(null), events1, events2);
|
|
906
|
-
return new
|
|
908
|
+
return new _PluvIO({
|
|
907
909
|
authorize: authorize2,
|
|
908
910
|
debug,
|
|
909
911
|
events,
|
|
@@ -931,7 +933,7 @@ var createIO = (params) => {
|
|
|
931
933
|
authorize: authorize2,
|
|
932
934
|
context,
|
|
933
935
|
debug,
|
|
934
|
-
|
|
936
|
+
getInitialStorage,
|
|
935
937
|
onRoomDeleted,
|
|
936
938
|
onStorageUpdated,
|
|
937
939
|
platform
|
|
@@ -940,7 +942,7 @@ var createIO = (params) => {
|
|
|
940
942
|
authorize: authorize2,
|
|
941
943
|
context,
|
|
942
944
|
debug,
|
|
943
|
-
|
|
945
|
+
getInitialStorage,
|
|
944
946
|
onRoomDeleted,
|
|
945
947
|
onStorageUpdated,
|
|
946
948
|
platform
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pluv/io",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.7.0",
|
|
4
4
|
"description": "@pluv/io server-side websocket manager",
|
|
5
5
|
"author": "leedavidcs",
|
|
6
6
|
"license": "MIT",
|
|
@@ -18,21 +18,23 @@
|
|
|
18
18
|
},
|
|
19
19
|
"dependencies": {
|
|
20
20
|
"@panva/hkdf": "^1.1.1",
|
|
21
|
+
"@playwright/test": "^1.35.1",
|
|
22
|
+
"@types/react": "^18.2.13",
|
|
21
23
|
"jose": "^4.14.4",
|
|
22
24
|
"kleur": "^4.1.5",
|
|
23
25
|
"wonka": "^6.3.2",
|
|
24
|
-
"@pluv/crdt-yjs": "^0.
|
|
25
|
-
"@pluv/types": "^0.2.
|
|
26
|
+
"@pluv/crdt-yjs": "^0.4.0",
|
|
27
|
+
"@pluv/types": "^0.2.1"
|
|
26
28
|
},
|
|
27
29
|
"peerDependencies": {
|
|
28
30
|
"yjs": "^13.0.0"
|
|
29
31
|
},
|
|
30
32
|
"devDependencies": {
|
|
31
|
-
"tsup": "^
|
|
32
|
-
"typescript": "^5.
|
|
33
|
-
"yjs": "^13.6.
|
|
34
|
-
"@pluv/tsconfig": "^0.1.
|
|
35
|
-
"eslint-config-pluv": "^0.1.
|
|
33
|
+
"tsup": "^7.0.0",
|
|
34
|
+
"typescript": "^5.1.3",
|
|
35
|
+
"yjs": "^13.6.4",
|
|
36
|
+
"@pluv/tsconfig": "^0.1.7",
|
|
37
|
+
"eslint-config-pluv": "^0.1.13"
|
|
36
38
|
},
|
|
37
39
|
"scripts": {
|
|
38
40
|
"build": "tsup src/index.ts --format esm,cjs --dts",
|
package/src/PluvIO.ts
CHANGED
|
@@ -24,17 +24,15 @@ import type { JWTEncodeParams } from "./authorize";
|
|
|
24
24
|
import { authorize } from "./authorize";
|
|
25
25
|
import type { EventConfig, InferEventConfig } from "./types";
|
|
26
26
|
|
|
27
|
-
|
|
28
|
-
TIO extends PluvIO<any, any,
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
? IPlatform
|
|
37
|
-
: never;
|
|
27
|
+
type InferIOContext<TIO extends PluvIO<any, any, any, any, any, any, any>> =
|
|
28
|
+
TIO extends PluvIO<any, any, infer IContext, any, any, any, any>
|
|
29
|
+
? IContext
|
|
30
|
+
: never;
|
|
31
|
+
|
|
32
|
+
type InferIOPlatform<TIO extends PluvIO<any, any, any, any, any, any, any>> =
|
|
33
|
+
TIO extends PluvIO<infer IPlatform, any, any, any, any, any, any>
|
|
34
|
+
? IPlatform
|
|
35
|
+
: never;
|
|
38
36
|
|
|
39
37
|
export type InferIORoom<TIO extends PluvIO<any, any, any, any, any, any, any>> =
|
|
40
38
|
TIO extends PluvIO<
|
|
@@ -57,6 +55,14 @@ export type InferIORoom<TIO extends PluvIO<any, any, any, any, any, any, any>> =
|
|
|
57
55
|
>
|
|
58
56
|
: never;
|
|
59
57
|
|
|
58
|
+
type GetInitialStorageEvent<TPlatform extends AbstractPlatform> = {
|
|
59
|
+
room: string;
|
|
60
|
+
} & InferPlatformRoomContextType<TPlatform>;
|
|
61
|
+
|
|
62
|
+
export type GetInitialStorageFn<TPlatform extends AbstractPlatform> = (
|
|
63
|
+
event: GetInitialStorageEvent<TPlatform>
|
|
64
|
+
) => MaybePromise<Maybe<string>>;
|
|
65
|
+
|
|
60
66
|
export interface PluvIOListeners<TPlatform extends AbstractPlatform> {
|
|
61
67
|
onRoomDeleted: (event: IORoomListenerEvent<TPlatform>) => void;
|
|
62
68
|
onStorageUpdated: (event: IORoomListenerEvent<TPlatform>) => void;
|
|
@@ -82,7 +88,7 @@ export type PluvIOConfig<
|
|
|
82
88
|
TOutputSelf,
|
|
83
89
|
TOutputSync
|
|
84
90
|
>;
|
|
85
|
-
|
|
91
|
+
getInitialStorage?: GetInitialStorageFn<TPlatform>;
|
|
86
92
|
platform: TPlatform;
|
|
87
93
|
};
|
|
88
94
|
|
|
@@ -102,9 +108,7 @@ export class PluvIO<
|
|
|
102
108
|
IOLike<TAuthorize, TInput, TOutputBroadcast, TOutputSelf, TOutputSync>
|
|
103
109
|
{
|
|
104
110
|
private _context: TContext = {} as TContext;
|
|
105
|
-
private
|
|
106
|
-
| ((room: string) => MaybePromise<Maybe<string>>)
|
|
107
|
-
| null = null;
|
|
111
|
+
private _getInitialStorage: GetInitialStorageFn<TPlatform> | null = null;
|
|
108
112
|
private _rooms = new Map<
|
|
109
113
|
string,
|
|
110
114
|
IORoom<
|
|
@@ -176,7 +180,12 @@ export class PluvIO<
|
|
|
176
180
|
await this._platform.persistance.getStorageState(room);
|
|
177
181
|
|
|
178
182
|
const updates: readonly Maybe<string>[] = [
|
|
179
|
-
oldState
|
|
183
|
+
oldState
|
|
184
|
+
? null
|
|
185
|
+
: await this._getInitialStorage?.({
|
|
186
|
+
...context,
|
|
187
|
+
room,
|
|
188
|
+
}),
|
|
180
189
|
update,
|
|
181
190
|
];
|
|
182
191
|
|
|
@@ -235,7 +244,7 @@ export class PluvIO<
|
|
|
235
244
|
$UPDATE_STORAGE: {
|
|
236
245
|
resolver: ({ origin, update }, { context, doc, room }) => {
|
|
237
246
|
if (
|
|
238
|
-
origin === "$
|
|
247
|
+
origin === "$INITIALIZED" &&
|
|
239
248
|
Object.keys(doc.toJSON()).length
|
|
240
249
|
) {
|
|
241
250
|
return;
|
|
@@ -284,7 +293,7 @@ export class PluvIO<
|
|
|
284
293
|
context,
|
|
285
294
|
debug = false,
|
|
286
295
|
events,
|
|
287
|
-
|
|
296
|
+
getInitialStorage,
|
|
288
297
|
onRoomDeleted,
|
|
289
298
|
onStorageUpdated,
|
|
290
299
|
platform,
|
|
@@ -296,7 +305,7 @@ export class PluvIO<
|
|
|
296
305
|
if (authorize) this._authorize = authorize;
|
|
297
306
|
if (context) this._context = context;
|
|
298
307
|
if (events) this._events = events;
|
|
299
|
-
if (
|
|
308
|
+
if (getInitialStorage) this._getInitialStorage = getInitialStorage;
|
|
300
309
|
|
|
301
310
|
this._listeners = {
|
|
302
311
|
onRoomDeleted: (event) => onRoomDeleted?.(event),
|
package/src/createIO.ts
CHANGED
|
@@ -8,7 +8,7 @@ import type {
|
|
|
8
8
|
MaybePromise,
|
|
9
9
|
} from "@pluv/types";
|
|
10
10
|
import type { AbstractPlatform } from "./AbstractPlatform";
|
|
11
|
-
import type { PluvIOListeners } from "./PluvIO";
|
|
11
|
+
import type { GetInitialStorageFn, PluvIOListeners } from "./PluvIO";
|
|
12
12
|
import { PluvIO } from "./PluvIO";
|
|
13
13
|
|
|
14
14
|
export type CreateIOParams<
|
|
@@ -20,7 +20,7 @@ export type CreateIOParams<
|
|
|
20
20
|
authorize?: IOAuthorize<TAuthorizeUser, TAuthorizeRequired>;
|
|
21
21
|
context?: TContext;
|
|
22
22
|
debug?: boolean;
|
|
23
|
-
|
|
23
|
+
getInitialStorage?: GetInitialStorageFn<TPlatform>;
|
|
24
24
|
platform: TPlatform;
|
|
25
25
|
};
|
|
26
26
|
|
|
@@ -47,7 +47,7 @@ export const createIO = <
|
|
|
47
47
|
authorize,
|
|
48
48
|
context,
|
|
49
49
|
debug,
|
|
50
|
-
|
|
50
|
+
getInitialStorage,
|
|
51
51
|
onRoomDeleted,
|
|
52
52
|
onStorageUpdated,
|
|
53
53
|
platform,
|
|
@@ -63,7 +63,7 @@ export const createIO = <
|
|
|
63
63
|
authorize,
|
|
64
64
|
context,
|
|
65
65
|
debug,
|
|
66
|
-
|
|
66
|
+
getInitialStorage,
|
|
67
67
|
onRoomDeleted,
|
|
68
68
|
onStorageUpdated,
|
|
69
69
|
platform,
|
package/src/index.ts
CHANGED
|
@@ -26,7 +26,12 @@ export type {
|
|
|
26
26
|
AbstractWebSocketHandleErrorParams,
|
|
27
27
|
} from "./AbstractWebSocket";
|
|
28
28
|
export type { IORoom } from "./IORoom";
|
|
29
|
-
export type {
|
|
29
|
+
export type {
|
|
30
|
+
GetRoomOptions,
|
|
31
|
+
InferIORoom,
|
|
32
|
+
PluvIO,
|
|
33
|
+
PluvIOConfig,
|
|
34
|
+
} from "./PluvIO";
|
|
30
35
|
export type {
|
|
31
36
|
AuthorizeModule,
|
|
32
37
|
AuthorizeParams,
|