@liveblocks/zustand 0.17.11-debug2 → 0.18.0-beta1
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/.built-by-link-script +1 -1
- package/index.d.ts +1 -1
- package/index.js +5 -5
- package/index.mjs +3 -284
- package/package.json +2 -2
package/.built-by-link-script
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
|
|
1
|
+
316aa44b7e15c044dbf114b7979e2440dee03982
|
package/index.d.ts
CHANGED
|
@@ -25,7 +25,7 @@ declare type LiveblocksState<TState extends ZustandState, TPresence extends Json
|
|
|
25
25
|
/**
|
|
26
26
|
* Other users in the room. Empty no room is currently synced
|
|
27
27
|
*/
|
|
28
|
-
readonly others:
|
|
28
|
+
readonly others: readonly User<TPresence, TUserMeta>[];
|
|
29
29
|
/**
|
|
30
30
|
* Whether or not the room storage is currently loading
|
|
31
31
|
*/
|
package/index.js
CHANGED
|
@@ -113,7 +113,7 @@ function middleware(config, options) {
|
|
|
113
113
|
if (storageRoot) {
|
|
114
114
|
return;
|
|
115
115
|
}
|
|
116
|
-
room = client.enter(roomId);
|
|
116
|
+
room = client.enter(roomId, { initialPresence: {} });
|
|
117
117
|
updateZustandLiveblocksState(set, {
|
|
118
118
|
isStorageLoading: true,
|
|
119
119
|
room
|
|
@@ -121,19 +121,19 @@ function middleware(config, options) {
|
|
|
121
121
|
const state = get();
|
|
122
122
|
broadcastInitialPresence(room, state, presenceMapping);
|
|
123
123
|
unsubscribeCallbacks.push(
|
|
124
|
-
room.subscribe(
|
|
124
|
+
room.events.others.subscribe(({ others }) => {
|
|
125
125
|
updateZustandLiveblocksState(set, { others: others.toArray() });
|
|
126
126
|
})
|
|
127
127
|
);
|
|
128
128
|
unsubscribeCallbacks.push(
|
|
129
|
-
room.subscribe(
|
|
129
|
+
room.events.connection.subscribe(() => {
|
|
130
130
|
updateZustandLiveblocksState(set, {
|
|
131
131
|
connection: room.getConnectionState()
|
|
132
132
|
});
|
|
133
133
|
})
|
|
134
134
|
);
|
|
135
135
|
unsubscribeCallbacks.push(
|
|
136
|
-
room.subscribe(
|
|
136
|
+
room.events.me.subscribe(() => {
|
|
137
137
|
if (isPatching === false) {
|
|
138
138
|
set(
|
|
139
139
|
patchPresenceState(room.getPresence(), presenceMapping)
|
|
@@ -203,7 +203,7 @@ function patchState(state, updates, mapping) {
|
|
|
203
203
|
for (const key in mapping) {
|
|
204
204
|
partialState[key] = state[key];
|
|
205
205
|
}
|
|
206
|
-
const patched = _internal.
|
|
206
|
+
const patched = _internal.legacy_patchImmutableObject.call(void 0, partialState, updates);
|
|
207
207
|
const result = {};
|
|
208
208
|
for (const key in mapping) {
|
|
209
209
|
result[key] = patched[key];
|
package/index.mjs
CHANGED
|
@@ -1,285 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
var __defProps = Object.defineProperties;
|
|
3
|
-
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
4
|
-
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
7
|
-
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
8
|
-
var __spreadValues = (a, b) => {
|
|
9
|
-
for (var prop in b || (b = {}))
|
|
10
|
-
if (__hasOwnProp.call(b, prop))
|
|
11
|
-
__defNormalProp(a, prop, b[prop]);
|
|
12
|
-
if (__getOwnPropSymbols)
|
|
13
|
-
for (var prop of __getOwnPropSymbols(b)) {
|
|
14
|
-
if (__propIsEnum.call(b, prop))
|
|
15
|
-
__defNormalProp(a, prop, b[prop]);
|
|
16
|
-
}
|
|
17
|
-
return a;
|
|
18
|
-
};
|
|
19
|
-
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
1
|
+
import mod from "./index.js";
|
|
20
2
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
lsonToJson,
|
|
24
|
-
patchImmutableObject,
|
|
25
|
-
patchLiveObjectKey
|
|
26
|
-
} from "@liveblocks/client/internal";
|
|
27
|
-
|
|
28
|
-
// src/errors.ts
|
|
29
|
-
var ERROR_PREFIX = "Invalid @liveblocks/zustand middleware config.";
|
|
30
|
-
function missingClient() {
|
|
31
|
-
return new Error(`${ERROR_PREFIX} client is missing`);
|
|
32
|
-
}
|
|
33
|
-
function missingMapping(mappingType) {
|
|
34
|
-
return new Error(`${ERROR_PREFIX} ${mappingType} is missing.`);
|
|
35
|
-
}
|
|
36
|
-
function mappingShouldBeAnObject(mappingType) {
|
|
37
|
-
return new Error(
|
|
38
|
-
`${ERROR_PREFIX} ${mappingType} should be an object where the values are boolean.`
|
|
39
|
-
);
|
|
40
|
-
}
|
|
41
|
-
function mappingValueShouldBeABoolean(mappingType, key) {
|
|
42
|
-
return new Error(
|
|
43
|
-
`${ERROR_PREFIX} ${mappingType}.${key} value should be a boolean`
|
|
44
|
-
);
|
|
45
|
-
}
|
|
46
|
-
function mappingShouldNotHaveTheSameKeys(key) {
|
|
47
|
-
return new Error(
|
|
48
|
-
`${ERROR_PREFIX} "${key}" is mapped on presenceMapping and storageMapping. A key shouldn't exist on both mapping.`
|
|
49
|
-
);
|
|
50
|
-
}
|
|
51
|
-
function mappingToFunctionIsNotAllowed(key) {
|
|
52
|
-
return new Error(
|
|
53
|
-
`${ERROR_PREFIX} mapping.${key} is invalid. Mapping to a function is not allowed.`
|
|
54
|
-
);
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
// src/index.ts
|
|
58
|
-
function isJson(value) {
|
|
59
|
-
return value === null || typeof value === "string" || typeof value === "number" || typeof value === "boolean" || Array.isArray(value) && value.every(isJson) || typeof value === "object" && Object.values(value).every(isJson);
|
|
60
|
-
}
|
|
61
|
-
function middleware(config, options) {
|
|
62
|
-
if (process.env.NODE_ENV !== "production" && options.client == null) {
|
|
63
|
-
throw missingClient();
|
|
64
|
-
}
|
|
65
|
-
const client = options.client;
|
|
66
|
-
const storageMapping = validateMapping(
|
|
67
|
-
options.storageMapping || {},
|
|
68
|
-
"storageMapping"
|
|
69
|
-
);
|
|
70
|
-
const presenceMapping = validateMapping(
|
|
71
|
-
options.presenceMapping || {},
|
|
72
|
-
"presenceMapping"
|
|
73
|
-
);
|
|
74
|
-
if (process.env.NODE_ENV !== "production") {
|
|
75
|
-
validateNoDuplicateKeys(storageMapping, presenceMapping);
|
|
76
|
-
}
|
|
77
|
-
return (set, get, api) => {
|
|
78
|
-
const typedSet = set;
|
|
79
|
-
let room = null;
|
|
80
|
-
let isPatching = false;
|
|
81
|
-
let storageRoot = null;
|
|
82
|
-
let unsubscribeCallbacks = [];
|
|
83
|
-
const store = config(
|
|
84
|
-
(args) => {
|
|
85
|
-
const oldState = get();
|
|
86
|
-
set(args);
|
|
87
|
-
const newState = get();
|
|
88
|
-
if (room) {
|
|
89
|
-
isPatching = true;
|
|
90
|
-
updatePresence(
|
|
91
|
-
room,
|
|
92
|
-
oldState,
|
|
93
|
-
newState,
|
|
94
|
-
presenceMapping
|
|
95
|
-
);
|
|
96
|
-
room.batch(() => {
|
|
97
|
-
if (storageRoot) {
|
|
98
|
-
patchLiveblocksStorage(
|
|
99
|
-
storageRoot,
|
|
100
|
-
oldState,
|
|
101
|
-
newState,
|
|
102
|
-
storageMapping
|
|
103
|
-
);
|
|
104
|
-
}
|
|
105
|
-
});
|
|
106
|
-
isPatching = false;
|
|
107
|
-
}
|
|
108
|
-
},
|
|
109
|
-
get,
|
|
110
|
-
api
|
|
111
|
-
);
|
|
112
|
-
function enterRoom(roomId, initialState) {
|
|
113
|
-
if (storageRoot) {
|
|
114
|
-
return;
|
|
115
|
-
}
|
|
116
|
-
room = client.enter(roomId);
|
|
117
|
-
updateZustandLiveblocksState(set, {
|
|
118
|
-
isStorageLoading: true,
|
|
119
|
-
room
|
|
120
|
-
});
|
|
121
|
-
const state = get();
|
|
122
|
-
broadcastInitialPresence(room, state, presenceMapping);
|
|
123
|
-
unsubscribeCallbacks.push(
|
|
124
|
-
room.subscribe("others", (others) => {
|
|
125
|
-
updateZustandLiveblocksState(set, { others: others.toArray() });
|
|
126
|
-
})
|
|
127
|
-
);
|
|
128
|
-
unsubscribeCallbacks.push(
|
|
129
|
-
room.subscribe("connection", () => {
|
|
130
|
-
updateZustandLiveblocksState(set, {
|
|
131
|
-
connection: room.getConnectionState()
|
|
132
|
-
});
|
|
133
|
-
})
|
|
134
|
-
);
|
|
135
|
-
unsubscribeCallbacks.push(
|
|
136
|
-
room.subscribe("my-presence", () => {
|
|
137
|
-
if (isPatching === false) {
|
|
138
|
-
set(
|
|
139
|
-
patchPresenceState(room.getPresence(), presenceMapping)
|
|
140
|
-
);
|
|
141
|
-
}
|
|
142
|
-
})
|
|
143
|
-
);
|
|
144
|
-
room.getStorage().then(({ root }) => {
|
|
145
|
-
const updates = {};
|
|
146
|
-
room.batch(() => {
|
|
147
|
-
for (const key in storageMapping) {
|
|
148
|
-
const liveblocksStatePart = root.get(key);
|
|
149
|
-
if (liveblocksStatePart == null) {
|
|
150
|
-
updates[key] = initialState[key];
|
|
151
|
-
patchLiveObjectKey(root, key, void 0, initialState[key]);
|
|
152
|
-
} else {
|
|
153
|
-
updates[key] = lsonToJson(liveblocksStatePart);
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
|
-
});
|
|
157
|
-
typedSet(updates);
|
|
158
|
-
storageRoot = root;
|
|
159
|
-
unsubscribeCallbacks.push(
|
|
160
|
-
room.subscribe(
|
|
161
|
-
root,
|
|
162
|
-
(updates2) => {
|
|
163
|
-
if (isPatching === false) {
|
|
164
|
-
set(patchState(get(), updates2, storageMapping));
|
|
165
|
-
}
|
|
166
|
-
},
|
|
167
|
-
{ isDeep: true }
|
|
168
|
-
)
|
|
169
|
-
);
|
|
170
|
-
updateZustandLiveblocksState(set, { isStorageLoading: false });
|
|
171
|
-
});
|
|
172
|
-
}
|
|
173
|
-
function leaveRoom(roomId) {
|
|
174
|
-
for (const unsubscribe of unsubscribeCallbacks) {
|
|
175
|
-
unsubscribe();
|
|
176
|
-
}
|
|
177
|
-
storageRoot = null;
|
|
178
|
-
room = null;
|
|
179
|
-
isPatching = false;
|
|
180
|
-
unsubscribeCallbacks = [];
|
|
181
|
-
client.leave(roomId);
|
|
182
|
-
updateZustandLiveblocksState(set, {
|
|
183
|
-
others: [],
|
|
184
|
-
connection: "closed",
|
|
185
|
-
isStorageLoading: false,
|
|
186
|
-
room: null
|
|
187
|
-
});
|
|
188
|
-
}
|
|
189
|
-
return __spreadProps(__spreadValues({}, store), {
|
|
190
|
-
liveblocks: {
|
|
191
|
-
enterRoom,
|
|
192
|
-
leaveRoom,
|
|
193
|
-
room: null,
|
|
194
|
-
others: [],
|
|
195
|
-
connection: "closed",
|
|
196
|
-
isStorageLoading: false
|
|
197
|
-
}
|
|
198
|
-
});
|
|
199
|
-
};
|
|
200
|
-
}
|
|
201
|
-
function patchState(state, updates, mapping) {
|
|
202
|
-
const partialState = {};
|
|
203
|
-
for (const key in mapping) {
|
|
204
|
-
partialState[key] = state[key];
|
|
205
|
-
}
|
|
206
|
-
const patched = patchImmutableObject(partialState, updates);
|
|
207
|
-
const result = {};
|
|
208
|
-
for (const key in mapping) {
|
|
209
|
-
result[key] = patched[key];
|
|
210
|
-
}
|
|
211
|
-
return result;
|
|
212
|
-
}
|
|
213
|
-
function patchPresenceState(presence, mapping) {
|
|
214
|
-
const partialState = {};
|
|
215
|
-
for (const key in mapping) {
|
|
216
|
-
partialState[key] = presence[key];
|
|
217
|
-
}
|
|
218
|
-
return partialState;
|
|
219
|
-
}
|
|
220
|
-
function updateZustandLiveblocksState(set, partial) {
|
|
221
|
-
set((state) => ({ liveblocks: __spreadValues(__spreadValues({}, state.liveblocks), partial) }));
|
|
222
|
-
}
|
|
223
|
-
function broadcastInitialPresence(room, state, mapping) {
|
|
224
|
-
for (const key in mapping) {
|
|
225
|
-
room == null ? void 0 : room.updatePresence({ [key]: state[key] });
|
|
226
|
-
}
|
|
227
|
-
}
|
|
228
|
-
function updatePresence(room, oldState, newState, presenceMapping) {
|
|
229
|
-
for (const key in presenceMapping) {
|
|
230
|
-
if (typeof newState[key] === "function") {
|
|
231
|
-
throw mappingToFunctionIsNotAllowed("value");
|
|
232
|
-
}
|
|
233
|
-
if (oldState[key] !== newState[key]) {
|
|
234
|
-
const val = newState[key];
|
|
235
|
-
room.updatePresence({ [key]: val });
|
|
236
|
-
}
|
|
237
|
-
}
|
|
238
|
-
}
|
|
239
|
-
function patchLiveblocksStorage(root, oldState, newState, mapping) {
|
|
240
|
-
for (const key in mapping) {
|
|
241
|
-
if (process.env.NODE_ENV !== "production" && typeof newState[key] === "function") {
|
|
242
|
-
throw mappingToFunctionIsNotAllowed("value");
|
|
243
|
-
}
|
|
244
|
-
if (oldState[key] !== newState[key]) {
|
|
245
|
-
const oldVal = oldState[key];
|
|
246
|
-
const newVal = newState[key];
|
|
247
|
-
if ((oldVal === void 0 || isJson(oldVal)) && (newVal === void 0 || isJson(newVal))) {
|
|
248
|
-
patchLiveObjectKey(root, key, oldVal, newVal);
|
|
249
|
-
}
|
|
250
|
-
}
|
|
251
|
-
}
|
|
252
|
-
}
|
|
253
|
-
function isObject(value) {
|
|
254
|
-
return Object.prototype.toString.call(value) === "[object Object]";
|
|
255
|
-
}
|
|
256
|
-
function validateNoDuplicateKeys(storageMapping, presenceMapping) {
|
|
257
|
-
for (const key in storageMapping) {
|
|
258
|
-
if (presenceMapping[key] !== void 0) {
|
|
259
|
-
throw mappingShouldNotHaveTheSameKeys(key);
|
|
260
|
-
}
|
|
261
|
-
}
|
|
262
|
-
}
|
|
263
|
-
function validateMapping(mapping, mappingType) {
|
|
264
|
-
if (process.env.NODE_ENV !== "production") {
|
|
265
|
-
if (mapping == null) {
|
|
266
|
-
throw missingMapping(mappingType);
|
|
267
|
-
}
|
|
268
|
-
if (!isObject(mapping)) {
|
|
269
|
-
throw mappingShouldBeAnObject(mappingType);
|
|
270
|
-
}
|
|
271
|
-
}
|
|
272
|
-
const result = {};
|
|
273
|
-
for (const key in mapping) {
|
|
274
|
-
if (process.env.NODE_ENV !== "production" && typeof mapping[key] !== "boolean") {
|
|
275
|
-
throw mappingValueShouldBeABoolean(mappingType, key);
|
|
276
|
-
}
|
|
277
|
-
if (mapping[key] === true) {
|
|
278
|
-
result[key] = true;
|
|
279
|
-
}
|
|
280
|
-
}
|
|
281
|
-
return result;
|
|
282
|
-
}
|
|
283
|
-
export {
|
|
284
|
-
middleware
|
|
285
|
-
};
|
|
3
|
+
export default mod;
|
|
4
|
+
export const middleware = mod.middleware;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@liveblocks/zustand",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.18.0-beta1",
|
|
4
4
|
"description": "A middleware to integrate Liveblocks into Zustand stores.",
|
|
5
5
|
"main": "./index.js",
|
|
6
6
|
"module": "./index.mjs",
|
|
@@ -27,7 +27,7 @@
|
|
|
27
27
|
"directory": "packages/liveblocks-zustand"
|
|
28
28
|
},
|
|
29
29
|
"peerDependencies": {
|
|
30
|
-
"@liveblocks/client": "0.
|
|
30
|
+
"@liveblocks/client": "0.18.0-beta1",
|
|
31
31
|
"zustand": "^3"
|
|
32
32
|
},
|
|
33
33
|
"sideEffects": false
|