@liveblocks/zustand 0.16.2 → 0.16.4-beta2
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/index.d.ts +8 -6
- package/index.mjs +211 -0
- package/package.json +10 -14
- package/esm/index.js +0 -224
- package/esm/index.mjs +0 -224
package/index.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import { StateCreator, SetState, GetState, StoreApi } from
|
|
2
|
-
import {
|
|
3
|
-
|
|
1
|
+
import { StateCreator, SetState, GetState, StoreApi } from 'zustand';
|
|
2
|
+
import { Presence, Room, User, Client } from '@liveblocks/client';
|
|
3
|
+
|
|
4
|
+
declare type LiveblocksState<TState, TPresence extends Presence = Presence> = TState & {
|
|
4
5
|
/**
|
|
5
6
|
* Liveblocks extra state attached by the middleware
|
|
6
7
|
*/
|
|
@@ -34,7 +35,7 @@ export declare type LiveblocksState<TState, TPresence extends Presence = Presenc
|
|
|
34
35
|
readonly connection: "closed" | "authenticating" | "unavailable" | "failed" | "open" | "connecting";
|
|
35
36
|
};
|
|
36
37
|
};
|
|
37
|
-
|
|
38
|
+
declare type Mapping<T> = Partial<{
|
|
38
39
|
[Property in keyof T]: boolean;
|
|
39
40
|
}>;
|
|
40
41
|
declare type Options<T> = {
|
|
@@ -51,5 +52,6 @@ declare type Options<T> = {
|
|
|
51
52
|
*/
|
|
52
53
|
presenceMapping?: Mapping<T>;
|
|
53
54
|
};
|
|
54
|
-
|
|
55
|
-
|
|
55
|
+
declare function middleware<T extends Record<string, unknown>, TPresence extends Record<string, unknown> = Presence>(config: StateCreator<T, SetState<T>, GetState<LiveblocksState<T>>, StoreApi<T>>, options: Options<T>): StateCreator<LiveblocksState<T, TPresence>, SetState<LiveblocksState<T, TPresence>>, GetState<LiveblocksState<T, TPresence>>, StoreApi<LiveblocksState<T, TPresence>>>;
|
|
56
|
+
|
|
57
|
+
export { LiveblocksState, Mapping, middleware };
|
package/index.mjs
ADDED
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
import { internals } from '@liveblocks/client';
|
|
2
|
+
|
|
3
|
+
const ERROR_PREFIX = "Invalid @liveblocks/zustand middleware config.";
|
|
4
|
+
function missingClient() {
|
|
5
|
+
return new Error(`${ERROR_PREFIX} client is missing`);
|
|
6
|
+
}
|
|
7
|
+
function missingMapping(mappingType) {
|
|
8
|
+
return new Error(`${ERROR_PREFIX} ${mappingType} is missing.`);
|
|
9
|
+
}
|
|
10
|
+
function mappingShouldBeAnObject(mappingType) {
|
|
11
|
+
return new Error(`${ERROR_PREFIX} ${mappingType} should be an object where the values are boolean.`);
|
|
12
|
+
}
|
|
13
|
+
function mappingValueShouldBeABoolean(mappingType, key) {
|
|
14
|
+
return new Error(`${ERROR_PREFIX} ${mappingType}.${key} value should be a boolean`);
|
|
15
|
+
}
|
|
16
|
+
function mappingShouldNotHaveTheSameKeys(key) {
|
|
17
|
+
return new Error(`${ERROR_PREFIX} "${key}" is mapped on presenceMapping and storageMapping. A key shouldn't exist on both mapping.`);
|
|
18
|
+
}
|
|
19
|
+
function mappingToFunctionIsNotAllowed(key) {
|
|
20
|
+
return new Error(`${ERROR_PREFIX} mapping.${key} is invalid. Mapping to a function is not allowed.`);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const { patchLiveObjectKey, patchImmutableObject, lsonToJson } = internals;
|
|
24
|
+
function middleware(config, options) {
|
|
25
|
+
if (process.env.NODE_ENV !== "production" && options.client == null) {
|
|
26
|
+
throw missingClient();
|
|
27
|
+
}
|
|
28
|
+
const client = options.client;
|
|
29
|
+
const storageMapping = validateMapping(options.storageMapping || {}, "storageMapping");
|
|
30
|
+
const presenceMapping = validateMapping(options.presenceMapping || {}, "presenceMapping");
|
|
31
|
+
if (process.env.NODE_ENV !== "production") {
|
|
32
|
+
validateNoDuplicateKeys(storageMapping, presenceMapping);
|
|
33
|
+
}
|
|
34
|
+
return (set, get, api) => {
|
|
35
|
+
const typedSet = set;
|
|
36
|
+
let room = null;
|
|
37
|
+
let isPatching = false;
|
|
38
|
+
let storageRoot = null;
|
|
39
|
+
let unsubscribeCallbacks = [];
|
|
40
|
+
const store = config((args) => {
|
|
41
|
+
const oldState = get();
|
|
42
|
+
set(args);
|
|
43
|
+
const newState = get();
|
|
44
|
+
if (room) {
|
|
45
|
+
isPatching = true;
|
|
46
|
+
updatePresence(room, oldState, newState, presenceMapping);
|
|
47
|
+
room.batch(() => {
|
|
48
|
+
if (storageRoot) {
|
|
49
|
+
patchLiveblocksStorage(storageRoot, oldState, newState, storageMapping);
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
isPatching = false;
|
|
53
|
+
}
|
|
54
|
+
}, get, api);
|
|
55
|
+
function enterRoom(roomId, initialState) {
|
|
56
|
+
if (storageRoot) {
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
room = client.enter(roomId);
|
|
60
|
+
updateZustandLiveblocksState(set, { isStorageLoading: true, room });
|
|
61
|
+
const state = get();
|
|
62
|
+
broadcastInitialPresence(room, state, presenceMapping);
|
|
63
|
+
unsubscribeCallbacks.push(room.subscribe("others", (others) => {
|
|
64
|
+
updateZustandLiveblocksState(set, { others: others.toArray() });
|
|
65
|
+
}));
|
|
66
|
+
unsubscribeCallbacks.push(room.subscribe("connection", () => {
|
|
67
|
+
updateZustandLiveblocksState(set, {
|
|
68
|
+
connection: room.getConnectionState(),
|
|
69
|
+
});
|
|
70
|
+
}));
|
|
71
|
+
unsubscribeCallbacks.push(room.subscribe("my-presence", () => {
|
|
72
|
+
if (isPatching === false) {
|
|
73
|
+
set(patchPresenceState(room.getPresence(), presenceMapping));
|
|
74
|
+
}
|
|
75
|
+
}));
|
|
76
|
+
room.getStorage().then(({ root }) => {
|
|
77
|
+
const updates = {};
|
|
78
|
+
room.batch(() => {
|
|
79
|
+
for (const key in storageMapping) {
|
|
80
|
+
const liveblocksStatePart = root.get(key);
|
|
81
|
+
if (liveblocksStatePart == null) {
|
|
82
|
+
updates[key] = initialState[key];
|
|
83
|
+
patchLiveObjectKey(root, key, undefined, initialState[key]);
|
|
84
|
+
}
|
|
85
|
+
else {
|
|
86
|
+
updates[key] = lsonToJson(liveblocksStatePart);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
});
|
|
90
|
+
typedSet(updates);
|
|
91
|
+
storageRoot = root;
|
|
92
|
+
unsubscribeCallbacks.push(room.subscribe(root, (updates) => {
|
|
93
|
+
if (isPatching === false) {
|
|
94
|
+
set(patchState(get(), updates, storageMapping));
|
|
95
|
+
}
|
|
96
|
+
}, { isDeep: true }));
|
|
97
|
+
// set isLoading storage to false once storage is loaded
|
|
98
|
+
updateZustandLiveblocksState(set, { isStorageLoading: false });
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
function leaveRoom(roomId) {
|
|
102
|
+
for (const unsubscribe of unsubscribeCallbacks) {
|
|
103
|
+
unsubscribe();
|
|
104
|
+
}
|
|
105
|
+
storageRoot = null;
|
|
106
|
+
room = null;
|
|
107
|
+
isPatching = false;
|
|
108
|
+
unsubscribeCallbacks = [];
|
|
109
|
+
client.leave(roomId);
|
|
110
|
+
updateZustandLiveblocksState(set, {
|
|
111
|
+
others: [],
|
|
112
|
+
connection: "closed",
|
|
113
|
+
isStorageLoading: false,
|
|
114
|
+
room: null,
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
return Object.assign(Object.assign({}, store), { liveblocks: {
|
|
118
|
+
enterRoom,
|
|
119
|
+
leaveRoom,
|
|
120
|
+
room: null,
|
|
121
|
+
others: [],
|
|
122
|
+
connection: "closed",
|
|
123
|
+
isStorageLoading: false,
|
|
124
|
+
} });
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
function patchState(state, updates, // StorageUpdate
|
|
128
|
+
mapping) {
|
|
129
|
+
const partialState = {};
|
|
130
|
+
for (const key in mapping) {
|
|
131
|
+
partialState[key] = state[key];
|
|
132
|
+
}
|
|
133
|
+
const patched = patchImmutableObject(partialState, updates);
|
|
134
|
+
const result = {};
|
|
135
|
+
for (const key in mapping) {
|
|
136
|
+
result[key] = patched[key];
|
|
137
|
+
}
|
|
138
|
+
return result;
|
|
139
|
+
}
|
|
140
|
+
function patchPresenceState(presence, mapping) {
|
|
141
|
+
const partialState = {};
|
|
142
|
+
for (const key in mapping) {
|
|
143
|
+
partialState[key] = presence[key];
|
|
144
|
+
}
|
|
145
|
+
return partialState;
|
|
146
|
+
}
|
|
147
|
+
function updateZustandLiveblocksState(set, partial) {
|
|
148
|
+
set((state) => ({ liveblocks: Object.assign(Object.assign({}, state.liveblocks), partial) }));
|
|
149
|
+
}
|
|
150
|
+
function broadcastInitialPresence(room, state, mapping) {
|
|
151
|
+
for (const key in mapping) {
|
|
152
|
+
room === null || room === void 0 ? void 0 : room.updatePresence({ [key]: state[key] });
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
function updatePresence(room, oldState, newState, presenceMapping) {
|
|
156
|
+
for (const key in presenceMapping) {
|
|
157
|
+
if (typeof newState[key] === "function") {
|
|
158
|
+
throw mappingToFunctionIsNotAllowed("value");
|
|
159
|
+
}
|
|
160
|
+
if (oldState[key] !== newState[key]) {
|
|
161
|
+
room.updatePresence({ [key]: newState[key] });
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
function patchLiveblocksStorage(root, oldState, newState, mapping) {
|
|
166
|
+
for (const key in mapping) {
|
|
167
|
+
if (process.env.NODE_ENV !== "production" &&
|
|
168
|
+
typeof newState[key] === "function") {
|
|
169
|
+
throw mappingToFunctionIsNotAllowed("value");
|
|
170
|
+
}
|
|
171
|
+
if (oldState[key] !== newState[key]) {
|
|
172
|
+
patchLiveObjectKey(root, key, oldState[key], newState[key]);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
function isObject(value) {
|
|
177
|
+
return Object.prototype.toString.call(value) === "[object Object]";
|
|
178
|
+
}
|
|
179
|
+
function validateNoDuplicateKeys(storageMapping, presenceMapping) {
|
|
180
|
+
for (const key in storageMapping) {
|
|
181
|
+
if (presenceMapping[key] !== undefined) {
|
|
182
|
+
throw mappingShouldNotHaveTheSameKeys(key);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* Remove false keys from mapping and generate to a new object to avoid potential mutation from outside the middleware
|
|
188
|
+
*/
|
|
189
|
+
function validateMapping(mapping, mappingType) {
|
|
190
|
+
if (process.env.NODE_ENV !== "production") {
|
|
191
|
+
if (mapping == null) {
|
|
192
|
+
throw missingMapping(mappingType);
|
|
193
|
+
}
|
|
194
|
+
if (!isObject(mapping)) {
|
|
195
|
+
throw mappingShouldBeAnObject(mappingType);
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
const result = {};
|
|
199
|
+
for (const key in mapping) {
|
|
200
|
+
if (process.env.NODE_ENV !== "production" &&
|
|
201
|
+
typeof mapping[key] !== "boolean") {
|
|
202
|
+
throw mappingValueShouldBeABoolean(mappingType, key);
|
|
203
|
+
}
|
|
204
|
+
if (mapping[key] === true) {
|
|
205
|
+
result[key] = true;
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
return result;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
export { middleware };
|
package/package.json
CHANGED
|
@@ -1,21 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@liveblocks/zustand",
|
|
3
|
-
"version": "0.16.
|
|
4
|
-
"sideEffects": false,
|
|
3
|
+
"version": "0.16.4-beta2",
|
|
5
4
|
"description": "A middleware to integrate Liveblocks into Zustand stores.",
|
|
6
5
|
"main": "./index.js",
|
|
6
|
+
"module": "./index.mjs",
|
|
7
7
|
"types": "./index.d.ts",
|
|
8
8
|
"files": [
|
|
9
9
|
"**"
|
|
10
10
|
],
|
|
11
|
-
"exports": {
|
|
12
|
-
".": {
|
|
13
|
-
"types": "./index.d.ts",
|
|
14
|
-
"module": "./esm/index.js",
|
|
15
|
-
"import": "./esm/index.mjs",
|
|
16
|
-
"default": "./index.js"
|
|
17
|
-
}
|
|
18
|
-
},
|
|
19
11
|
"keywords": [
|
|
20
12
|
"zustand",
|
|
21
13
|
"react",
|
|
@@ -26,7 +18,9 @@
|
|
|
26
18
|
],
|
|
27
19
|
"scripts": {
|
|
28
20
|
"build": "rollup -c && cp ./package.json ./README.md ./lib",
|
|
21
|
+
"lint": "eslint src/ test/",
|
|
29
22
|
"test": "jest --watch",
|
|
23
|
+
"test-ci": "jest",
|
|
30
24
|
"dtslint": "dtslint --localTs node_modules/typescript/lib --expectOnly types"
|
|
31
25
|
},
|
|
32
26
|
"license": "Apache-2.0",
|
|
@@ -36,7 +30,7 @@
|
|
|
36
30
|
"directory": "packages/liveblocks-zustand"
|
|
37
31
|
},
|
|
38
32
|
"peerDependencies": {
|
|
39
|
-
"@liveblocks/client": "0.16.
|
|
33
|
+
"@liveblocks/client": "0.16.4-beta2",
|
|
40
34
|
"zustand": "^3"
|
|
41
35
|
},
|
|
42
36
|
"devDependencies": {
|
|
@@ -46,6 +40,7 @@
|
|
|
46
40
|
"@definitelytyped/dtslint": "^0.0.103",
|
|
47
41
|
"@rollup/plugin-babel": "^5.3.0",
|
|
48
42
|
"@rollup/plugin-node-resolve": "^13.1.3",
|
|
43
|
+
"@rollup/plugin-replace": "^4.0.0",
|
|
49
44
|
"@rollup/plugin-typescript": "^8.3.0",
|
|
50
45
|
"@testing-library/jest-dom": "^5.16.1",
|
|
51
46
|
"@testing-library/react": "^12.1.2",
|
|
@@ -53,13 +48,14 @@
|
|
|
53
48
|
"@types/jest": "^27.4.0",
|
|
54
49
|
"@typescript-eslint/eslint-plugin": "^5.18.0",
|
|
55
50
|
"@typescript-eslint/parser": "^5.18.0",
|
|
56
|
-
"esbuild": "0.14.11",
|
|
57
51
|
"eslint": "^8.12.0",
|
|
58
52
|
"jest": "^27.4.7",
|
|
59
53
|
"msw": "^0.36.4",
|
|
60
54
|
"rollup": "^2.64.0",
|
|
61
|
-
"rollup-plugin-
|
|
55
|
+
"rollup-plugin-command": "^1.1.3",
|
|
56
|
+
"rollup-plugin-dts": "^4.2.1",
|
|
62
57
|
"whatwg-fetch": "^3.6.2",
|
|
63
58
|
"zustand": "^3.6.9"
|
|
64
|
-
}
|
|
59
|
+
},
|
|
60
|
+
"sideEffects": false
|
|
65
61
|
}
|
package/esm/index.js
DELETED
|
@@ -1,224 +0,0 @@
|
|
|
1
|
-
import { internals } from '@liveblocks/client';
|
|
2
|
-
|
|
3
|
-
const ERROR_PREFIX = "Invalid @liveblocks/zustand middleware config.";
|
|
4
|
-
function missingClient() {
|
|
5
|
-
return new Error(`${ERROR_PREFIX} client is missing`);
|
|
6
|
-
}
|
|
7
|
-
function missingMapping(mappingType) {
|
|
8
|
-
return new Error(`${ERROR_PREFIX} ${mappingType} is missing.`);
|
|
9
|
-
}
|
|
10
|
-
function mappingShouldBeAnObject(mappingType) {
|
|
11
|
-
return new Error(`${ERROR_PREFIX} ${mappingType} should be an object where the values are boolean.`);
|
|
12
|
-
}
|
|
13
|
-
function mappingValueShouldBeABoolean(mappingType, key) {
|
|
14
|
-
return new Error(`${ERROR_PREFIX} ${mappingType}.${key} value should be a boolean`);
|
|
15
|
-
}
|
|
16
|
-
function mappingShouldNotHaveTheSameKeys(key) {
|
|
17
|
-
return new Error(`${ERROR_PREFIX} "${key}" is mapped on presenceMapping and storageMapping. A key shouldn't exist on both mapping.`);
|
|
18
|
-
}
|
|
19
|
-
function mappingToFunctionIsNotAllowed(key) {
|
|
20
|
-
return new Error(`${ERROR_PREFIX} mapping.${key} is invalid. Mapping to a function is not allowed.`);
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
var __defProp = Object.defineProperty;
|
|
24
|
-
var __defProps = Object.defineProperties;
|
|
25
|
-
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
26
|
-
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
27
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
28
|
-
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
29
|
-
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
30
|
-
var __spreadValues = (a, b) => {
|
|
31
|
-
for (var prop in b || (b = {}))
|
|
32
|
-
if (__hasOwnProp.call(b, prop))
|
|
33
|
-
__defNormalProp(a, prop, b[prop]);
|
|
34
|
-
if (__getOwnPropSymbols)
|
|
35
|
-
for (var prop of __getOwnPropSymbols(b)) {
|
|
36
|
-
if (__propIsEnum.call(b, prop))
|
|
37
|
-
__defNormalProp(a, prop, b[prop]);
|
|
38
|
-
}
|
|
39
|
-
return a;
|
|
40
|
-
};
|
|
41
|
-
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
42
|
-
const { patchLiveObjectKey, patchImmutableObject, lsonToJson } = internals;
|
|
43
|
-
function middleware(config, options) {
|
|
44
|
-
if (process.env.NODE_ENV !== "production" && options.client == null) {
|
|
45
|
-
throw missingClient();
|
|
46
|
-
}
|
|
47
|
-
const client = options.client;
|
|
48
|
-
const storageMapping = validateMapping(options.storageMapping || {}, "storageMapping");
|
|
49
|
-
const presenceMapping = validateMapping(options.presenceMapping || {}, "presenceMapping");
|
|
50
|
-
if (process.env.NODE_ENV !== "production") {
|
|
51
|
-
validateNoDuplicateKeys(storageMapping, presenceMapping);
|
|
52
|
-
}
|
|
53
|
-
return (set, get, api) => {
|
|
54
|
-
const typedSet = set;
|
|
55
|
-
let room = null;
|
|
56
|
-
let isPatching = false;
|
|
57
|
-
let storageRoot = null;
|
|
58
|
-
let unsubscribeCallbacks = [];
|
|
59
|
-
const store = config((args) => {
|
|
60
|
-
const oldState = get();
|
|
61
|
-
set(args);
|
|
62
|
-
const newState = get();
|
|
63
|
-
if (room) {
|
|
64
|
-
isPatching = true;
|
|
65
|
-
updatePresence(room, oldState, newState, presenceMapping);
|
|
66
|
-
room.batch(() => {
|
|
67
|
-
if (storageRoot) {
|
|
68
|
-
patchLiveblocksStorage(storageRoot, oldState, newState, storageMapping);
|
|
69
|
-
}
|
|
70
|
-
});
|
|
71
|
-
isPatching = false;
|
|
72
|
-
}
|
|
73
|
-
}, get, api);
|
|
74
|
-
function enterRoom(roomId, initialState) {
|
|
75
|
-
if (storageRoot) {
|
|
76
|
-
return;
|
|
77
|
-
}
|
|
78
|
-
room = client.enter(roomId);
|
|
79
|
-
updateZustandLiveblocksState(set, { isStorageLoading: true, room });
|
|
80
|
-
const state = get();
|
|
81
|
-
broadcastInitialPresence(room, state, presenceMapping);
|
|
82
|
-
unsubscribeCallbacks.push(room.subscribe("others", (others) => {
|
|
83
|
-
updateZustandLiveblocksState(set, { others: others.toArray() });
|
|
84
|
-
}));
|
|
85
|
-
unsubscribeCallbacks.push(room.subscribe("connection", () => {
|
|
86
|
-
updateZustandLiveblocksState(set, {
|
|
87
|
-
connection: room.getConnectionState()
|
|
88
|
-
});
|
|
89
|
-
}));
|
|
90
|
-
unsubscribeCallbacks.push(room.subscribe("my-presence", () => {
|
|
91
|
-
if (isPatching === false) {
|
|
92
|
-
set(patchPresenceState(room.getPresence(), presenceMapping));
|
|
93
|
-
}
|
|
94
|
-
}));
|
|
95
|
-
room.getStorage().then(({ root }) => {
|
|
96
|
-
const updates = {};
|
|
97
|
-
room.batch(() => {
|
|
98
|
-
for (const key in storageMapping) {
|
|
99
|
-
const liveblocksStatePart = root.get(key);
|
|
100
|
-
if (liveblocksStatePart == null) {
|
|
101
|
-
updates[key] = initialState[key];
|
|
102
|
-
patchLiveObjectKey(root, key, void 0, initialState[key]);
|
|
103
|
-
} else {
|
|
104
|
-
updates[key] = lsonToJson(liveblocksStatePart);
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
});
|
|
108
|
-
typedSet(updates);
|
|
109
|
-
storageRoot = root;
|
|
110
|
-
unsubscribeCallbacks.push(room.subscribe(root, (updates2) => {
|
|
111
|
-
if (isPatching === false) {
|
|
112
|
-
set(patchState(get(), updates2, storageMapping));
|
|
113
|
-
}
|
|
114
|
-
}, { isDeep: true }));
|
|
115
|
-
updateZustandLiveblocksState(set, { isStorageLoading: false });
|
|
116
|
-
});
|
|
117
|
-
}
|
|
118
|
-
function leaveRoom(roomId) {
|
|
119
|
-
for (const unsubscribe of unsubscribeCallbacks) {
|
|
120
|
-
unsubscribe();
|
|
121
|
-
}
|
|
122
|
-
storageRoot = null;
|
|
123
|
-
room = null;
|
|
124
|
-
isPatching = false;
|
|
125
|
-
unsubscribeCallbacks = [];
|
|
126
|
-
client.leave(roomId);
|
|
127
|
-
updateZustandLiveblocksState(set, {
|
|
128
|
-
others: [],
|
|
129
|
-
connection: "closed",
|
|
130
|
-
isStorageLoading: false,
|
|
131
|
-
room: null
|
|
132
|
-
});
|
|
133
|
-
}
|
|
134
|
-
return __spreadProps(__spreadValues({}, store), {
|
|
135
|
-
liveblocks: {
|
|
136
|
-
enterRoom,
|
|
137
|
-
leaveRoom,
|
|
138
|
-
room: null,
|
|
139
|
-
others: [],
|
|
140
|
-
connection: "closed",
|
|
141
|
-
isStorageLoading: false
|
|
142
|
-
}
|
|
143
|
-
});
|
|
144
|
-
};
|
|
145
|
-
}
|
|
146
|
-
function patchState(state, updates, mapping) {
|
|
147
|
-
const partialState = {};
|
|
148
|
-
for (const key in mapping) {
|
|
149
|
-
partialState[key] = state[key];
|
|
150
|
-
}
|
|
151
|
-
const patched = patchImmutableObject(partialState, updates);
|
|
152
|
-
const result = {};
|
|
153
|
-
for (const key in mapping) {
|
|
154
|
-
result[key] = patched[key];
|
|
155
|
-
}
|
|
156
|
-
return result;
|
|
157
|
-
}
|
|
158
|
-
function patchPresenceState(presence, mapping) {
|
|
159
|
-
const partialState = {};
|
|
160
|
-
for (const key in mapping) {
|
|
161
|
-
partialState[key] = presence[key];
|
|
162
|
-
}
|
|
163
|
-
return partialState;
|
|
164
|
-
}
|
|
165
|
-
function updateZustandLiveblocksState(set, partial) {
|
|
166
|
-
set((state) => ({ liveblocks: __spreadValues(__spreadValues({}, state.liveblocks), partial) }));
|
|
167
|
-
}
|
|
168
|
-
function broadcastInitialPresence(room, state, mapping) {
|
|
169
|
-
for (const key in mapping) {
|
|
170
|
-
room == null ? void 0 : room.updatePresence({ [key]: state[key] });
|
|
171
|
-
}
|
|
172
|
-
}
|
|
173
|
-
function updatePresence(room, oldState, newState, presenceMapping) {
|
|
174
|
-
for (const key in presenceMapping) {
|
|
175
|
-
if (typeof newState[key] === "function") {
|
|
176
|
-
throw mappingToFunctionIsNotAllowed("value");
|
|
177
|
-
}
|
|
178
|
-
if (oldState[key] !== newState[key]) {
|
|
179
|
-
room.updatePresence({ [key]: newState[key] });
|
|
180
|
-
}
|
|
181
|
-
}
|
|
182
|
-
}
|
|
183
|
-
function patchLiveblocksStorage(root, oldState, newState, mapping) {
|
|
184
|
-
for (const key in mapping) {
|
|
185
|
-
if (process.env.NODE_ENV !== "production" && typeof newState[key] === "function") {
|
|
186
|
-
throw mappingToFunctionIsNotAllowed("value");
|
|
187
|
-
}
|
|
188
|
-
if (oldState[key] !== newState[key]) {
|
|
189
|
-
patchLiveObjectKey(root, key, oldState[key], newState[key]);
|
|
190
|
-
}
|
|
191
|
-
}
|
|
192
|
-
}
|
|
193
|
-
function isObject(value) {
|
|
194
|
-
return Object.prototype.toString.call(value) === "[object Object]";
|
|
195
|
-
}
|
|
196
|
-
function validateNoDuplicateKeys(storageMapping, presenceMapping) {
|
|
197
|
-
for (const key in storageMapping) {
|
|
198
|
-
if (presenceMapping[key] !== void 0) {
|
|
199
|
-
throw mappingShouldNotHaveTheSameKeys(key);
|
|
200
|
-
}
|
|
201
|
-
}
|
|
202
|
-
}
|
|
203
|
-
function validateMapping(mapping, mappingType) {
|
|
204
|
-
if (process.env.NODE_ENV !== "production") {
|
|
205
|
-
if (mapping == null) {
|
|
206
|
-
throw missingMapping(mappingType);
|
|
207
|
-
}
|
|
208
|
-
if (!isObject(mapping)) {
|
|
209
|
-
throw mappingShouldBeAnObject(mappingType);
|
|
210
|
-
}
|
|
211
|
-
}
|
|
212
|
-
const result = {};
|
|
213
|
-
for (const key in mapping) {
|
|
214
|
-
if (process.env.NODE_ENV !== "production" && typeof mapping[key] !== "boolean") {
|
|
215
|
-
throw mappingValueShouldBeABoolean(mappingType, key);
|
|
216
|
-
}
|
|
217
|
-
if (mapping[key] === true) {
|
|
218
|
-
result[key] = true;
|
|
219
|
-
}
|
|
220
|
-
}
|
|
221
|
-
return result;
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
export { middleware };
|
package/esm/index.mjs
DELETED
|
@@ -1,224 +0,0 @@
|
|
|
1
|
-
import { internals } from '@liveblocks/client';
|
|
2
|
-
|
|
3
|
-
const ERROR_PREFIX = "Invalid @liveblocks/zustand middleware config.";
|
|
4
|
-
function missingClient() {
|
|
5
|
-
return new Error(`${ERROR_PREFIX} client is missing`);
|
|
6
|
-
}
|
|
7
|
-
function missingMapping(mappingType) {
|
|
8
|
-
return new Error(`${ERROR_PREFIX} ${mappingType} is missing.`);
|
|
9
|
-
}
|
|
10
|
-
function mappingShouldBeAnObject(mappingType) {
|
|
11
|
-
return new Error(`${ERROR_PREFIX} ${mappingType} should be an object where the values are boolean.`);
|
|
12
|
-
}
|
|
13
|
-
function mappingValueShouldBeABoolean(mappingType, key) {
|
|
14
|
-
return new Error(`${ERROR_PREFIX} ${mappingType}.${key} value should be a boolean`);
|
|
15
|
-
}
|
|
16
|
-
function mappingShouldNotHaveTheSameKeys(key) {
|
|
17
|
-
return new Error(`${ERROR_PREFIX} "${key}" is mapped on presenceMapping and storageMapping. A key shouldn't exist on both mapping.`);
|
|
18
|
-
}
|
|
19
|
-
function mappingToFunctionIsNotAllowed(key) {
|
|
20
|
-
return new Error(`${ERROR_PREFIX} mapping.${key} is invalid. Mapping to a function is not allowed.`);
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
var __defProp = Object.defineProperty;
|
|
24
|
-
var __defProps = Object.defineProperties;
|
|
25
|
-
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
26
|
-
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
27
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
28
|
-
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
29
|
-
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
30
|
-
var __spreadValues = (a, b) => {
|
|
31
|
-
for (var prop in b || (b = {}))
|
|
32
|
-
if (__hasOwnProp.call(b, prop))
|
|
33
|
-
__defNormalProp(a, prop, b[prop]);
|
|
34
|
-
if (__getOwnPropSymbols)
|
|
35
|
-
for (var prop of __getOwnPropSymbols(b)) {
|
|
36
|
-
if (__propIsEnum.call(b, prop))
|
|
37
|
-
__defNormalProp(a, prop, b[prop]);
|
|
38
|
-
}
|
|
39
|
-
return a;
|
|
40
|
-
};
|
|
41
|
-
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
42
|
-
const { patchLiveObjectKey, patchImmutableObject, lsonToJson } = internals;
|
|
43
|
-
function middleware(config, options) {
|
|
44
|
-
if (process.env.NODE_ENV !== "production" && options.client == null) {
|
|
45
|
-
throw missingClient();
|
|
46
|
-
}
|
|
47
|
-
const client = options.client;
|
|
48
|
-
const storageMapping = validateMapping(options.storageMapping || {}, "storageMapping");
|
|
49
|
-
const presenceMapping = validateMapping(options.presenceMapping || {}, "presenceMapping");
|
|
50
|
-
if (process.env.NODE_ENV !== "production") {
|
|
51
|
-
validateNoDuplicateKeys(storageMapping, presenceMapping);
|
|
52
|
-
}
|
|
53
|
-
return (set, get, api) => {
|
|
54
|
-
const typedSet = set;
|
|
55
|
-
let room = null;
|
|
56
|
-
let isPatching = false;
|
|
57
|
-
let storageRoot = null;
|
|
58
|
-
let unsubscribeCallbacks = [];
|
|
59
|
-
const store = config((args) => {
|
|
60
|
-
const oldState = get();
|
|
61
|
-
set(args);
|
|
62
|
-
const newState = get();
|
|
63
|
-
if (room) {
|
|
64
|
-
isPatching = true;
|
|
65
|
-
updatePresence(room, oldState, newState, presenceMapping);
|
|
66
|
-
room.batch(() => {
|
|
67
|
-
if (storageRoot) {
|
|
68
|
-
patchLiveblocksStorage(storageRoot, oldState, newState, storageMapping);
|
|
69
|
-
}
|
|
70
|
-
});
|
|
71
|
-
isPatching = false;
|
|
72
|
-
}
|
|
73
|
-
}, get, api);
|
|
74
|
-
function enterRoom(roomId, initialState) {
|
|
75
|
-
if (storageRoot) {
|
|
76
|
-
return;
|
|
77
|
-
}
|
|
78
|
-
room = client.enter(roomId);
|
|
79
|
-
updateZustandLiveblocksState(set, { isStorageLoading: true, room });
|
|
80
|
-
const state = get();
|
|
81
|
-
broadcastInitialPresence(room, state, presenceMapping);
|
|
82
|
-
unsubscribeCallbacks.push(room.subscribe("others", (others) => {
|
|
83
|
-
updateZustandLiveblocksState(set, { others: others.toArray() });
|
|
84
|
-
}));
|
|
85
|
-
unsubscribeCallbacks.push(room.subscribe("connection", () => {
|
|
86
|
-
updateZustandLiveblocksState(set, {
|
|
87
|
-
connection: room.getConnectionState()
|
|
88
|
-
});
|
|
89
|
-
}));
|
|
90
|
-
unsubscribeCallbacks.push(room.subscribe("my-presence", () => {
|
|
91
|
-
if (isPatching === false) {
|
|
92
|
-
set(patchPresenceState(room.getPresence(), presenceMapping));
|
|
93
|
-
}
|
|
94
|
-
}));
|
|
95
|
-
room.getStorage().then(({ root }) => {
|
|
96
|
-
const updates = {};
|
|
97
|
-
room.batch(() => {
|
|
98
|
-
for (const key in storageMapping) {
|
|
99
|
-
const liveblocksStatePart = root.get(key);
|
|
100
|
-
if (liveblocksStatePart == null) {
|
|
101
|
-
updates[key] = initialState[key];
|
|
102
|
-
patchLiveObjectKey(root, key, void 0, initialState[key]);
|
|
103
|
-
} else {
|
|
104
|
-
updates[key] = lsonToJson(liveblocksStatePart);
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
});
|
|
108
|
-
typedSet(updates);
|
|
109
|
-
storageRoot = root;
|
|
110
|
-
unsubscribeCallbacks.push(room.subscribe(root, (updates2) => {
|
|
111
|
-
if (isPatching === false) {
|
|
112
|
-
set(patchState(get(), updates2, storageMapping));
|
|
113
|
-
}
|
|
114
|
-
}, { isDeep: true }));
|
|
115
|
-
updateZustandLiveblocksState(set, { isStorageLoading: false });
|
|
116
|
-
});
|
|
117
|
-
}
|
|
118
|
-
function leaveRoom(roomId) {
|
|
119
|
-
for (const unsubscribe of unsubscribeCallbacks) {
|
|
120
|
-
unsubscribe();
|
|
121
|
-
}
|
|
122
|
-
storageRoot = null;
|
|
123
|
-
room = null;
|
|
124
|
-
isPatching = false;
|
|
125
|
-
unsubscribeCallbacks = [];
|
|
126
|
-
client.leave(roomId);
|
|
127
|
-
updateZustandLiveblocksState(set, {
|
|
128
|
-
others: [],
|
|
129
|
-
connection: "closed",
|
|
130
|
-
isStorageLoading: false,
|
|
131
|
-
room: null
|
|
132
|
-
});
|
|
133
|
-
}
|
|
134
|
-
return __spreadProps(__spreadValues({}, store), {
|
|
135
|
-
liveblocks: {
|
|
136
|
-
enterRoom,
|
|
137
|
-
leaveRoom,
|
|
138
|
-
room: null,
|
|
139
|
-
others: [],
|
|
140
|
-
connection: "closed",
|
|
141
|
-
isStorageLoading: false
|
|
142
|
-
}
|
|
143
|
-
});
|
|
144
|
-
};
|
|
145
|
-
}
|
|
146
|
-
function patchState(state, updates, mapping) {
|
|
147
|
-
const partialState = {};
|
|
148
|
-
for (const key in mapping) {
|
|
149
|
-
partialState[key] = state[key];
|
|
150
|
-
}
|
|
151
|
-
const patched = patchImmutableObject(partialState, updates);
|
|
152
|
-
const result = {};
|
|
153
|
-
for (const key in mapping) {
|
|
154
|
-
result[key] = patched[key];
|
|
155
|
-
}
|
|
156
|
-
return result;
|
|
157
|
-
}
|
|
158
|
-
function patchPresenceState(presence, mapping) {
|
|
159
|
-
const partialState = {};
|
|
160
|
-
for (const key in mapping) {
|
|
161
|
-
partialState[key] = presence[key];
|
|
162
|
-
}
|
|
163
|
-
return partialState;
|
|
164
|
-
}
|
|
165
|
-
function updateZustandLiveblocksState(set, partial) {
|
|
166
|
-
set((state) => ({ liveblocks: __spreadValues(__spreadValues({}, state.liveblocks), partial) }));
|
|
167
|
-
}
|
|
168
|
-
function broadcastInitialPresence(room, state, mapping) {
|
|
169
|
-
for (const key in mapping) {
|
|
170
|
-
room == null ? void 0 : room.updatePresence({ [key]: state[key] });
|
|
171
|
-
}
|
|
172
|
-
}
|
|
173
|
-
function updatePresence(room, oldState, newState, presenceMapping) {
|
|
174
|
-
for (const key in presenceMapping) {
|
|
175
|
-
if (typeof newState[key] === "function") {
|
|
176
|
-
throw mappingToFunctionIsNotAllowed("value");
|
|
177
|
-
}
|
|
178
|
-
if (oldState[key] !== newState[key]) {
|
|
179
|
-
room.updatePresence({ [key]: newState[key] });
|
|
180
|
-
}
|
|
181
|
-
}
|
|
182
|
-
}
|
|
183
|
-
function patchLiveblocksStorage(root, oldState, newState, mapping) {
|
|
184
|
-
for (const key in mapping) {
|
|
185
|
-
if (process.env.NODE_ENV !== "production" && typeof newState[key] === "function") {
|
|
186
|
-
throw mappingToFunctionIsNotAllowed("value");
|
|
187
|
-
}
|
|
188
|
-
if (oldState[key] !== newState[key]) {
|
|
189
|
-
patchLiveObjectKey(root, key, oldState[key], newState[key]);
|
|
190
|
-
}
|
|
191
|
-
}
|
|
192
|
-
}
|
|
193
|
-
function isObject(value) {
|
|
194
|
-
return Object.prototype.toString.call(value) === "[object Object]";
|
|
195
|
-
}
|
|
196
|
-
function validateNoDuplicateKeys(storageMapping, presenceMapping) {
|
|
197
|
-
for (const key in storageMapping) {
|
|
198
|
-
if (presenceMapping[key] !== void 0) {
|
|
199
|
-
throw mappingShouldNotHaveTheSameKeys(key);
|
|
200
|
-
}
|
|
201
|
-
}
|
|
202
|
-
}
|
|
203
|
-
function validateMapping(mapping, mappingType) {
|
|
204
|
-
if (process.env.NODE_ENV !== "production") {
|
|
205
|
-
if (mapping == null) {
|
|
206
|
-
throw missingMapping(mappingType);
|
|
207
|
-
}
|
|
208
|
-
if (!isObject(mapping)) {
|
|
209
|
-
throw mappingShouldBeAnObject(mappingType);
|
|
210
|
-
}
|
|
211
|
-
}
|
|
212
|
-
const result = {};
|
|
213
|
-
for (const key in mapping) {
|
|
214
|
-
if (process.env.NODE_ENV !== "production" && typeof mapping[key] !== "boolean") {
|
|
215
|
-
throw mappingValueShouldBeABoolean(mappingType, key);
|
|
216
|
-
}
|
|
217
|
-
if (mapping[key] === true) {
|
|
218
|
-
result[key] = true;
|
|
219
|
-
}
|
|
220
|
-
}
|
|
221
|
-
return result;
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
export { middleware };
|