@liveblocks/zustand 0.16.4 → 0.16.5
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 +1 -1
- package/index.js +158 -288
- package/index.mjs +119 -192
- package/package.json +9 -5
package/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { StateCreator, SetState, GetState, StoreApi } from 'zustand';
|
|
2
1
|
import { Presence, Room, User, Client } from '@liveblocks/client';
|
|
2
|
+
import { StateCreator, SetState, GetState, StoreApi } from 'zustand';
|
|
3
3
|
|
|
4
4
|
declare type LiveblocksState<TState, TPresence extends Presence = Presence> = TState & {
|
|
5
5
|
/**
|
package/index.js
CHANGED
|
@@ -1,315 +1,185 @@
|
|
|
1
|
-
|
|
1
|
+
"use strict";
|
|
2
2
|
|
|
3
|
-
Object.defineProperty(exports,
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: !0
|
|
5
|
+
});
|
|
4
6
|
|
|
5
|
-
var internal = require(
|
|
7
|
+
var internal = require("@liveblocks/client/internal");
|
|
6
8
|
|
|
7
9
|
function _extends() {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
return target;
|
|
20
|
-
};
|
|
21
|
-
|
|
22
|
-
return _extends.apply(this, arguments);
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
function _unsupportedIterableToArray(o, minLen) {
|
|
26
|
-
if (!o) return;
|
|
27
|
-
if (typeof o === "string") return _arrayLikeToArray(o, minLen);
|
|
28
|
-
var n = Object.prototype.toString.call(o).slice(8, -1);
|
|
29
|
-
if (n === "Object" && o.constructor) n = o.constructor.name;
|
|
30
|
-
if (n === "Map" || n === "Set") return Array.from(o);
|
|
31
|
-
if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
|
|
10
|
+
return _extends = Object.assign || function(target) {
|
|
11
|
+
for (var i = 1; i < arguments.length; i++) {
|
|
12
|
+
var source = arguments[i];
|
|
13
|
+
for (var key in source) Object.prototype.hasOwnProperty.call(source, key) && (target[key] = source[key]);
|
|
14
|
+
}
|
|
15
|
+
return target;
|
|
16
|
+
}, _extends.apply(this, arguments);
|
|
32
17
|
}
|
|
33
18
|
|
|
34
19
|
function _arrayLikeToArray(arr, len) {
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
return arr2;
|
|
20
|
+
(null == len || len > arr.length) && (len = arr.length);
|
|
21
|
+
for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
|
|
22
|
+
return arr2;
|
|
40
23
|
}
|
|
41
24
|
|
|
42
25
|
function _createForOfIteratorHelperLoose(o, allowArrayLike) {
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
if (
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
if (i >= o.length) return {
|
|
51
|
-
done: true
|
|
52
|
-
};
|
|
53
|
-
return {
|
|
54
|
-
done: false,
|
|
55
|
-
value: o[i++]
|
|
56
|
-
};
|
|
57
|
-
};
|
|
26
|
+
var it = "undefined" != typeof Symbol && o[Symbol.iterator] || o["@@iterator"];
|
|
27
|
+
if (it) return (it = it.call(o)).next.bind(it);
|
|
28
|
+
if (Array.isArray(o) || (it = function(o, minLen) {
|
|
29
|
+
if (o) {
|
|
30
|
+
if ("string" == typeof o) return _arrayLikeToArray(o, minLen);
|
|
31
|
+
var n = Object.prototype.toString.call(o).slice(8, -1);
|
|
32
|
+
return "Object" === n && o.constructor && (n = o.constructor.name), "Map" === n || "Set" === n ? Array.from(o) : "Arguments" === n || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n) ? _arrayLikeToArray(o, minLen) : void 0;
|
|
58
33
|
}
|
|
59
|
-
|
|
60
|
-
|
|
34
|
+
}(o)) || allowArrayLike && o && "number" == typeof o.length) {
|
|
35
|
+
it && (o = it);
|
|
36
|
+
var i = 0;
|
|
37
|
+
return function() {
|
|
38
|
+
return i >= o.length ? {
|
|
39
|
+
done: !0
|
|
40
|
+
} : {
|
|
41
|
+
done: !1,
|
|
42
|
+
value: o[i++]
|
|
43
|
+
};
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
|
|
61
47
|
}
|
|
62
48
|
|
|
63
49
|
var ERROR_PREFIX = "Invalid @liveblocks/zustand middleware config.";
|
|
64
|
-
|
|
65
|
-
return new Error(ERROR_PREFIX + " client is missing");
|
|
66
|
-
}
|
|
67
|
-
function missingMapping(mappingType) {
|
|
68
|
-
return new Error(ERROR_PREFIX + " " + mappingType + " is missing.");
|
|
69
|
-
}
|
|
70
|
-
function mappingShouldBeAnObject(mappingType) {
|
|
71
|
-
return new Error(ERROR_PREFIX + " " + mappingType + " should be an object where the values are boolean.");
|
|
72
|
-
}
|
|
50
|
+
|
|
73
51
|
function mappingValueShouldBeABoolean(mappingType, key) {
|
|
74
|
-
|
|
52
|
+
return new Error(ERROR_PREFIX + " " + mappingType + "." + key + " value should be a boolean");
|
|
75
53
|
}
|
|
54
|
+
|
|
76
55
|
function mappingShouldNotHaveTheSameKeys(key) {
|
|
77
|
-
|
|
56
|
+
return new Error(ERROR_PREFIX + ' "' + key + "\" is mapped on presenceMapping and storageMapping. A key shouldn't exist on both mapping.");
|
|
78
57
|
}
|
|
58
|
+
|
|
79
59
|
function mappingToFunctionIsNotAllowed(key) {
|
|
80
|
-
|
|
60
|
+
return new Error(ERROR_PREFIX + " mapping." + key + " is invalid. Mapping to a function is not allowed.");
|
|
81
61
|
}
|
|
82
62
|
|
|
83
|
-
function
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
var presenceMapping = validateMapping(options.presenceMapping || {}, "presenceMapping");
|
|
91
|
-
|
|
92
|
-
if (process.env.NODE_ENV !== "production") {
|
|
93
|
-
validateNoDuplicateKeys(storageMapping, presenceMapping);
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
return function (set, get, api) {
|
|
97
|
-
var typedSet = set;
|
|
98
|
-
var room = null;
|
|
99
|
-
var isPatching = false;
|
|
100
|
-
var storageRoot = null;
|
|
101
|
-
var unsubscribeCallbacks = [];
|
|
102
|
-
var store = config(function (args) {
|
|
103
|
-
var oldState = get();
|
|
104
|
-
set(args);
|
|
105
|
-
var newState = get();
|
|
106
|
-
|
|
107
|
-
if (room) {
|
|
108
|
-
isPatching = true;
|
|
109
|
-
updatePresence(room, oldState, newState, presenceMapping);
|
|
110
|
-
room.batch(function () {
|
|
111
|
-
if (storageRoot) {
|
|
112
|
-
patchLiveblocksStorage(storageRoot, oldState, newState, storageMapping);
|
|
113
|
-
}
|
|
114
|
-
});
|
|
115
|
-
isPatching = false;
|
|
116
|
-
}
|
|
117
|
-
}, get, api);
|
|
118
|
-
|
|
119
|
-
function enterRoom(roomId, initialState) {
|
|
120
|
-
if (storageRoot) {
|
|
121
|
-
return;
|
|
122
|
-
}
|
|
63
|
+
function updateZustandLiveblocksState(set, partial) {
|
|
64
|
+
set((function(state) {
|
|
65
|
+
return {
|
|
66
|
+
liveblocks: _extends({}, state.liveblocks, partial)
|
|
67
|
+
};
|
|
68
|
+
}));
|
|
69
|
+
}
|
|
123
70
|
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
71
|
+
function validateMapping(mapping, mappingType) {
|
|
72
|
+
if ("production" !== process.env.NODE_ENV) {
|
|
73
|
+
if (null == mapping) throw function(mappingType) {
|
|
74
|
+
return new Error(ERROR_PREFIX + " " + mappingType + " is missing.");
|
|
75
|
+
}(mappingType);
|
|
76
|
+
if (value = mapping, "[object Object]" !== Object.prototype.toString.call(value)) throw function(mappingType) {
|
|
77
|
+
return new Error(ERROR_PREFIX + " " + mappingType + " should be an object where the values are boolean.");
|
|
78
|
+
}(mappingType);
|
|
79
|
+
}
|
|
80
|
+
var value, result = {};
|
|
81
|
+
for (var key in mapping) {
|
|
82
|
+
if ("production" !== process.env.NODE_ENV && "boolean" != typeof mapping[key]) throw mappingValueShouldBeABoolean(mappingType, key);
|
|
83
|
+
!0 === mapping[key] && (result[key] = !0);
|
|
84
|
+
}
|
|
85
|
+
return result;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
exports.middleware = function(config, options) {
|
|
89
|
+
if ("production" !== process.env.NODE_ENV && null == options.client) throw new Error(ERROR_PREFIX + " client is missing");
|
|
90
|
+
var client = options.client, storageMapping = validateMapping(options.storageMapping || {}, "storageMapping"), presenceMapping = validateMapping(options.presenceMapping || {}, "presenceMapping");
|
|
91
|
+
return "production" !== process.env.NODE_ENV && function(storageMapping, presenceMapping) {
|
|
92
|
+
for (var key in storageMapping) if (void 0 !== presenceMapping[key]) throw mappingShouldNotHaveTheSameKeys(key);
|
|
93
|
+
}(storageMapping, presenceMapping), function(set, get, api) {
|
|
94
|
+
var typedSet = set, room = null, isPatching = !1, storageRoot = null, unsubscribeCallbacks = [], store = config((function(args) {
|
|
95
|
+
var oldState = get();
|
|
96
|
+
set(args);
|
|
97
|
+
var newState = get();
|
|
98
|
+
room && (isPatching = !0, function(room, oldState, newState, presenceMapping) {
|
|
99
|
+
for (var key in presenceMapping) {
|
|
100
|
+
if ("function" == typeof newState[key]) throw mappingToFunctionIsNotAllowed("value");
|
|
101
|
+
var _room$updatePresence2;
|
|
102
|
+
if (oldState[key] !== newState[key]) room.updatePresence(((_room$updatePresence2 = {})[key] = newState[key],
|
|
103
|
+
_room$updatePresence2));
|
|
104
|
+
}
|
|
105
|
+
}(room, oldState, newState, presenceMapping), room.batch((function() {
|
|
106
|
+
storageRoot && function(root, oldState, newState, mapping) {
|
|
107
|
+
for (var key in mapping) {
|
|
108
|
+
if ("production" !== process.env.NODE_ENV && "function" == typeof newState[key]) throw mappingToFunctionIsNotAllowed("value");
|
|
109
|
+
oldState[key] !== newState[key] && internal.patchLiveObjectKey(root, key, oldState[key], newState[key]);
|
|
110
|
+
}
|
|
111
|
+
}(storageRoot, oldState, newState, storageMapping);
|
|
112
|
+
})), isPatching = !1);
|
|
113
|
+
}), get, api);
|
|
114
|
+
return _extends({}, store, {
|
|
115
|
+
liveblocks: {
|
|
116
|
+
enterRoom: function(roomId, initialState) {
|
|
117
|
+
if (!storageRoot) {
|
|
118
|
+
room = client.enter(roomId), updateZustandLiveblocksState(set, {
|
|
119
|
+
isStorageLoading: !0,
|
|
120
|
+
room: room
|
|
128
121
|
});
|
|
129
122
|
var state = get();
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
unsubscribeCallbacks.push(room.subscribe("
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
}))
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
123
|
+
!function(room, state, mapping) {
|
|
124
|
+
for (var key in mapping) {
|
|
125
|
+
var _room$updatePresence;
|
|
126
|
+
null == room || room.updatePresence(((_room$updatePresence = {})[key] = state[key],
|
|
127
|
+
_room$updatePresence));
|
|
128
|
+
}
|
|
129
|
+
}(room, state, presenceMapping), unsubscribeCallbacks.push(room.subscribe("others", (function(others) {
|
|
130
|
+
updateZustandLiveblocksState(set, {
|
|
131
|
+
others: others.toArray()
|
|
132
|
+
});
|
|
133
|
+
}))), unsubscribeCallbacks.push(room.subscribe("connection", (function() {
|
|
134
|
+
updateZustandLiveblocksState(set, {
|
|
135
|
+
connection: room.getConnectionState()
|
|
136
|
+
});
|
|
137
|
+
}))), unsubscribeCallbacks.push(room.subscribe("my-presence", (function() {
|
|
138
|
+
!1 === isPatching && set(function(presence, mapping) {
|
|
139
|
+
var partialState = {};
|
|
140
|
+
for (var key in mapping) partialState[key] = presence[key];
|
|
141
|
+
return partialState;
|
|
142
|
+
}(room.getPresence(), presenceMapping));
|
|
143
|
+
}))), room.getStorage().then((function(_ref) {
|
|
144
|
+
var root = _ref.root, updates = {};
|
|
145
|
+
room.batch((function() {
|
|
146
|
+
for (var key in storageMapping) {
|
|
147
|
+
var liveblocksStatePart = root.get(key);
|
|
148
|
+
null == liveblocksStatePart ? (updates[key] = initialState[key], internal.patchLiveObjectKey(root, key, void 0, initialState[key])) : updates[key] = internal.lsonToJson(liveblocksStatePart);
|
|
144
149
|
}
|
|
150
|
+
})), typedSet(updates), storageRoot = root, unsubscribeCallbacks.push(room.subscribe(root, (function(updates) {
|
|
151
|
+
!1 === isPatching && set(function(state, updates, mapping) {
|
|
152
|
+
var partialState = {};
|
|
153
|
+
for (var key in mapping) partialState[key] = state[key];
|
|
154
|
+
var patched = internal.patchImmutableObject(partialState, updates), result = {};
|
|
155
|
+
for (var _key in mapping) result[_key] = patched[_key];
|
|
156
|
+
return result;
|
|
157
|
+
}(get(), updates, storageMapping));
|
|
158
|
+
}), {
|
|
159
|
+
isDeep: !0
|
|
160
|
+
})), updateZustandLiveblocksState(set, {
|
|
161
|
+
isStorageLoading: !1
|
|
162
|
+
});
|
|
145
163
|
}));
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
set(patchState(get(), updates, storageMapping));
|
|
166
|
-
}
|
|
167
|
-
}, {
|
|
168
|
-
isDeep: true
|
|
169
|
-
}));
|
|
170
|
-
updateZustandLiveblocksState(set, {
|
|
171
|
-
isStorageLoading: false
|
|
172
|
-
});
|
|
173
|
-
});
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
function leaveRoom(roomId) {
|
|
177
|
-
for (var _iterator = _createForOfIteratorHelperLoose(unsubscribeCallbacks), _step; !(_step = _iterator()).done;) {
|
|
178
|
-
var unsubscribe = _step.value;
|
|
179
|
-
unsubscribe();
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
storageRoot = null;
|
|
183
|
-
room = null;
|
|
184
|
-
isPatching = false;
|
|
185
|
-
unsubscribeCallbacks = [];
|
|
186
|
-
client.leave(roomId);
|
|
187
|
-
updateZustandLiveblocksState(set, {
|
|
188
|
-
others: [],
|
|
189
|
-
connection: "closed",
|
|
190
|
-
isStorageLoading: false,
|
|
191
|
-
room: null
|
|
192
|
-
});
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
return _extends({}, store, {
|
|
196
|
-
liveblocks: {
|
|
197
|
-
enterRoom: enterRoom,
|
|
198
|
-
leaveRoom: leaveRoom,
|
|
199
|
-
room: null,
|
|
200
|
-
others: [],
|
|
201
|
-
connection: "closed",
|
|
202
|
-
isStorageLoading: false
|
|
203
|
-
}
|
|
204
|
-
});
|
|
205
|
-
};
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
function patchState(state, updates, mapping) {
|
|
209
|
-
var partialState = {};
|
|
210
|
-
|
|
211
|
-
for (var key in mapping) {
|
|
212
|
-
partialState[key] = state[key];
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
var patched = internal.patchImmutableObject(partialState, updates);
|
|
216
|
-
var result = {};
|
|
217
|
-
|
|
218
|
-
for (var _key in mapping) {
|
|
219
|
-
result[_key] = patched[_key];
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
return result;
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
function patchPresenceState(presence, mapping) {
|
|
226
|
-
var partialState = {};
|
|
227
|
-
|
|
228
|
-
for (var key in mapping) {
|
|
229
|
-
partialState[key] = presence[key];
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
return partialState;
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
function updateZustandLiveblocksState(set, partial) {
|
|
236
|
-
set(function (state) {
|
|
237
|
-
return {
|
|
238
|
-
liveblocks: _extends({}, state.liveblocks, partial)
|
|
239
|
-
};
|
|
164
|
+
}
|
|
165
|
+
},
|
|
166
|
+
leaveRoom: function(roomId) {
|
|
167
|
+
for (var _step, _iterator = _createForOfIteratorHelperLoose(unsubscribeCallbacks); !(_step = _iterator()).done; ) {
|
|
168
|
+
(0, _step.value)();
|
|
169
|
+
}
|
|
170
|
+
storageRoot = null, room = null, isPatching = !1, unsubscribeCallbacks = [], client.leave(roomId),
|
|
171
|
+
updateZustandLiveblocksState(set, {
|
|
172
|
+
others: [],
|
|
173
|
+
connection: "closed",
|
|
174
|
+
isStorageLoading: !1,
|
|
175
|
+
room: null
|
|
176
|
+
});
|
|
177
|
+
},
|
|
178
|
+
room: null,
|
|
179
|
+
others: [],
|
|
180
|
+
connection: "closed",
|
|
181
|
+
isStorageLoading: !1
|
|
182
|
+
}
|
|
240
183
|
});
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
function broadcastInitialPresence(room, state, mapping) {
|
|
244
|
-
for (var key in mapping) {
|
|
245
|
-
var _room$updatePresence;
|
|
246
|
-
|
|
247
|
-
room == null ? void 0 : room.updatePresence((_room$updatePresence = {}, _room$updatePresence[key] = state[key], _room$updatePresence));
|
|
248
|
-
}
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
function updatePresence(room, oldState, newState, presenceMapping) {
|
|
252
|
-
for (var key in presenceMapping) {
|
|
253
|
-
if (typeof newState[key] === "function") {
|
|
254
|
-
throw mappingToFunctionIsNotAllowed("value");
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
if (oldState[key] !== newState[key]) {
|
|
258
|
-
var _room$updatePresence2;
|
|
259
|
-
|
|
260
|
-
room.updatePresence((_room$updatePresence2 = {}, _room$updatePresence2[key] = newState[key], _room$updatePresence2));
|
|
261
|
-
}
|
|
262
|
-
}
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
function patchLiveblocksStorage(root, oldState, newState, mapping) {
|
|
266
|
-
for (var key in mapping) {
|
|
267
|
-
if (process.env.NODE_ENV !== "production" && typeof newState[key] === "function") {
|
|
268
|
-
throw mappingToFunctionIsNotAllowed("value");
|
|
269
|
-
}
|
|
270
|
-
|
|
271
|
-
if (oldState[key] !== newState[key]) {
|
|
272
|
-
internal.patchLiveObjectKey(root, key, oldState[key], newState[key]);
|
|
273
|
-
}
|
|
274
|
-
}
|
|
275
|
-
}
|
|
276
|
-
|
|
277
|
-
function isObject(value) {
|
|
278
|
-
return Object.prototype.toString.call(value) === "[object Object]";
|
|
279
|
-
}
|
|
280
|
-
|
|
281
|
-
function validateNoDuplicateKeys(storageMapping, presenceMapping) {
|
|
282
|
-
for (var key in storageMapping) {
|
|
283
|
-
if (presenceMapping[key] !== undefined) {
|
|
284
|
-
throw mappingShouldNotHaveTheSameKeys(key);
|
|
285
|
-
}
|
|
286
|
-
}
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
function validateMapping(mapping, mappingType) {
|
|
290
|
-
if (process.env.NODE_ENV !== "production") {
|
|
291
|
-
if (mapping == null) {
|
|
292
|
-
throw missingMapping(mappingType);
|
|
293
|
-
}
|
|
294
|
-
|
|
295
|
-
if (!isObject(mapping)) {
|
|
296
|
-
throw mappingShouldBeAnObject(mappingType);
|
|
297
|
-
}
|
|
298
|
-
}
|
|
299
|
-
|
|
300
|
-
var result = {};
|
|
301
|
-
|
|
302
|
-
for (var key in mapping) {
|
|
303
|
-
if (process.env.NODE_ENV !== "production" && typeof mapping[key] !== "boolean") {
|
|
304
|
-
throw mappingValueShouldBeABoolean(mappingType, key);
|
|
305
|
-
}
|
|
306
|
-
|
|
307
|
-
if (mapping[key] === true) {
|
|
308
|
-
result[key] = true;
|
|
309
|
-
}
|
|
310
|
-
}
|
|
311
|
-
|
|
312
|
-
return result;
|
|
313
|
-
}
|
|
314
|
-
|
|
315
|
-
exports.middleware = middleware;
|
|
184
|
+
};
|
|
185
|
+
};
|
package/index.mjs
CHANGED
|
@@ -1,210 +1,137 @@
|
|
|
1
|
-
import { patchLiveObjectKey, lsonToJson, patchImmutableObject } from
|
|
1
|
+
import { patchLiveObjectKey, lsonToJson, patchImmutableObject } from "@liveblocks/client/internal";
|
|
2
2
|
|
|
3
3
|
const ERROR_PREFIX = "Invalid @liveblocks/zustand middleware config.";
|
|
4
|
-
|
|
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
|
-
}
|
|
4
|
+
|
|
13
5
|
function mappingValueShouldBeABoolean(mappingType, key) {
|
|
14
|
-
|
|
6
|
+
return new Error(`${ERROR_PREFIX} ${mappingType}.${key} value should be a boolean`);
|
|
15
7
|
}
|
|
8
|
+
|
|
16
9
|
function mappingShouldNotHaveTheSameKeys(key) {
|
|
17
|
-
|
|
10
|
+
return new Error(`${ERROR_PREFIX} "${key}" is mapped on presenceMapping and storageMapping. A key shouldn't exist on both mapping.`);
|
|
18
11
|
}
|
|
12
|
+
|
|
19
13
|
function mappingToFunctionIsNotAllowed(key) {
|
|
20
|
-
|
|
14
|
+
return new Error(`${ERROR_PREFIX} mapping.${key} is invalid. Mapping to a function is not allowed.`);
|
|
21
15
|
}
|
|
22
16
|
|
|
23
17
|
function middleware(config, options) {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
let isPatching = false;
|
|
37
|
-
let storageRoot = null;
|
|
38
|
-
let unsubscribeCallbacks = [];
|
|
39
|
-
const store = config((args) => {
|
|
40
|
-
const oldState = get();
|
|
41
|
-
set(args);
|
|
42
|
-
const newState = get();
|
|
43
|
-
if (room) {
|
|
44
|
-
isPatching = true;
|
|
45
|
-
updatePresence(room, oldState, newState, presenceMapping);
|
|
46
|
-
room.batch(() => {
|
|
47
|
-
if (storageRoot) {
|
|
48
|
-
patchLiveblocksStorage(storageRoot, oldState, newState, storageMapping);
|
|
49
|
-
}
|
|
50
|
-
});
|
|
51
|
-
isPatching = false;
|
|
52
|
-
}
|
|
53
|
-
}, get, api);
|
|
54
|
-
function enterRoom(roomId, initialState) {
|
|
55
|
-
if (storageRoot) {
|
|
56
|
-
return;
|
|
57
|
-
}
|
|
58
|
-
room = client.enter(roomId);
|
|
59
|
-
updateZustandLiveblocksState(set, { isStorageLoading: true, room });
|
|
60
|
-
const state = get();
|
|
61
|
-
broadcastInitialPresence(room, state, presenceMapping);
|
|
62
|
-
unsubscribeCallbacks.push(room.subscribe("others", (others) => {
|
|
63
|
-
updateZustandLiveblocksState(set, { others: others.toArray() });
|
|
64
|
-
}));
|
|
65
|
-
unsubscribeCallbacks.push(room.subscribe("connection", () => {
|
|
66
|
-
updateZustandLiveblocksState(set, {
|
|
67
|
-
connection: room.getConnectionState(),
|
|
68
|
-
});
|
|
69
|
-
}));
|
|
70
|
-
unsubscribeCallbacks.push(room.subscribe("my-presence", () => {
|
|
71
|
-
if (isPatching === false) {
|
|
72
|
-
set(patchPresenceState(room.getPresence(), presenceMapping));
|
|
73
|
-
}
|
|
74
|
-
}));
|
|
75
|
-
room.getStorage().then(({ root }) => {
|
|
76
|
-
const updates = {};
|
|
77
|
-
room.batch(() => {
|
|
78
|
-
for (const key in storageMapping) {
|
|
79
|
-
const liveblocksStatePart = root.get(key);
|
|
80
|
-
if (liveblocksStatePart == null) {
|
|
81
|
-
updates[key] = initialState[key];
|
|
82
|
-
patchLiveObjectKey(root, key, undefined, initialState[key]);
|
|
83
|
-
}
|
|
84
|
-
else {
|
|
85
|
-
updates[key] = lsonToJson(liveblocksStatePart);
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
});
|
|
89
|
-
typedSet(updates);
|
|
90
|
-
storageRoot = root;
|
|
91
|
-
unsubscribeCallbacks.push(room.subscribe(root, (updates) => {
|
|
92
|
-
if (isPatching === false) {
|
|
93
|
-
set(patchState(get(), updates, storageMapping));
|
|
94
|
-
}
|
|
95
|
-
}, { isDeep: true }));
|
|
96
|
-
// set isLoading storage to false once storage is loaded
|
|
97
|
-
updateZustandLiveblocksState(set, { isStorageLoading: false });
|
|
98
|
-
});
|
|
99
|
-
}
|
|
100
|
-
function leaveRoom(roomId) {
|
|
101
|
-
for (const unsubscribe of unsubscribeCallbacks) {
|
|
102
|
-
unsubscribe();
|
|
103
|
-
}
|
|
104
|
-
storageRoot = null;
|
|
105
|
-
room = null;
|
|
106
|
-
isPatching = false;
|
|
107
|
-
unsubscribeCallbacks = [];
|
|
108
|
-
client.leave(roomId);
|
|
109
|
-
updateZustandLiveblocksState(set, {
|
|
110
|
-
others: [],
|
|
111
|
-
connection: "closed",
|
|
112
|
-
isStorageLoading: false,
|
|
113
|
-
room: null,
|
|
114
|
-
});
|
|
115
|
-
}
|
|
116
|
-
return Object.assign(Object.assign({}, store), { liveblocks: {
|
|
117
|
-
enterRoom,
|
|
118
|
-
leaveRoom,
|
|
119
|
-
room: null,
|
|
120
|
-
others: [],
|
|
121
|
-
connection: "closed",
|
|
122
|
-
isStorageLoading: false,
|
|
123
|
-
} });
|
|
124
|
-
};
|
|
125
|
-
}
|
|
126
|
-
function patchState(state, updates, // StorageUpdate
|
|
127
|
-
mapping) {
|
|
128
|
-
const partialState = {};
|
|
129
|
-
for (const key in mapping) {
|
|
130
|
-
partialState[key] = state[key];
|
|
131
|
-
}
|
|
132
|
-
const patched = patchImmutableObject(partialState, updates);
|
|
133
|
-
const result = {};
|
|
134
|
-
for (const key in mapping) {
|
|
135
|
-
result[key] = patched[key];
|
|
136
|
-
}
|
|
137
|
-
return result;
|
|
138
|
-
}
|
|
139
|
-
function patchPresenceState(presence, mapping) {
|
|
140
|
-
const partialState = {};
|
|
141
|
-
for (const key in mapping) {
|
|
142
|
-
partialState[key] = presence[key];
|
|
143
|
-
}
|
|
144
|
-
return partialState;
|
|
145
|
-
}
|
|
146
|
-
function updateZustandLiveblocksState(set, partial) {
|
|
147
|
-
set((state) => ({ liveblocks: Object.assign(Object.assign({}, state.liveblocks), partial) }));
|
|
148
|
-
}
|
|
149
|
-
function broadcastInitialPresence(room, state, mapping) {
|
|
150
|
-
for (const key in mapping) {
|
|
151
|
-
room === null || room === void 0 ? void 0 : room.updatePresence({ [key]: state[key] });
|
|
152
|
-
}
|
|
153
|
-
}
|
|
154
|
-
function updatePresence(room, oldState, newState, presenceMapping) {
|
|
18
|
+
if ("production" !== process.env.NODE_ENV && null == options.client) throw new Error(`${ERROR_PREFIX} client is missing`);
|
|
19
|
+
const client = options.client, storageMapping = validateMapping(options.storageMapping || {}, "storageMapping"), presenceMapping = validateMapping(options.presenceMapping || {}, "presenceMapping");
|
|
20
|
+
return "production" !== process.env.NODE_ENV && function(storageMapping, presenceMapping) {
|
|
21
|
+
for (const key in storageMapping) if (void 0 !== presenceMapping[key]) throw mappingShouldNotHaveTheSameKeys(key);
|
|
22
|
+
}(storageMapping, presenceMapping), (set, get, api) => {
|
|
23
|
+
const typedSet = set;
|
|
24
|
+
let room = null, isPatching = !1, storageRoot = null, unsubscribeCallbacks = [];
|
|
25
|
+
const store = config((args => {
|
|
26
|
+
const oldState = get();
|
|
27
|
+
set(args);
|
|
28
|
+
const newState = get();
|
|
29
|
+
room && (isPatching = !0, function(room, oldState, newState, presenceMapping) {
|
|
155
30
|
for (const key in presenceMapping) {
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
room.updatePresence({ [key]: newState[key] });
|
|
161
|
-
}
|
|
31
|
+
if ("function" == typeof newState[key]) throw mappingToFunctionIsNotAllowed("value");
|
|
32
|
+
oldState[key] !== newState[key] && room.updatePresence({
|
|
33
|
+
[key]: newState[key]
|
|
34
|
+
});
|
|
162
35
|
}
|
|
36
|
+
}(room, oldState, newState, presenceMapping), room.batch((() => {
|
|
37
|
+
storageRoot && function(root, oldState, newState, mapping) {
|
|
38
|
+
for (const key in mapping) {
|
|
39
|
+
if ("production" !== process.env.NODE_ENV && "function" == typeof newState[key]) throw mappingToFunctionIsNotAllowed("value");
|
|
40
|
+
oldState[key] !== newState[key] && patchLiveObjectKey(root, key, oldState[key], newState[key]);
|
|
41
|
+
}
|
|
42
|
+
}(storageRoot, oldState, newState, storageMapping);
|
|
43
|
+
})), isPatching = !1);
|
|
44
|
+
}), get, api);
|
|
45
|
+
return Object.assign(Object.assign({}, store), {
|
|
46
|
+
liveblocks: {
|
|
47
|
+
enterRoom: function(roomId, initialState) {
|
|
48
|
+
if (storageRoot) return;
|
|
49
|
+
room = client.enter(roomId), updateZustandLiveblocksState(set, {
|
|
50
|
+
isStorageLoading: !0,
|
|
51
|
+
room: room
|
|
52
|
+
});
|
|
53
|
+
const state = get();
|
|
54
|
+
!function(room, state, mapping) {
|
|
55
|
+
for (const key in mapping) null == room || room.updatePresence({
|
|
56
|
+
[key]: state[key]
|
|
57
|
+
});
|
|
58
|
+
}(room, state, presenceMapping), unsubscribeCallbacks.push(room.subscribe("others", (others => {
|
|
59
|
+
updateZustandLiveblocksState(set, {
|
|
60
|
+
others: others.toArray()
|
|
61
|
+
});
|
|
62
|
+
}))), unsubscribeCallbacks.push(room.subscribe("connection", (() => {
|
|
63
|
+
updateZustandLiveblocksState(set, {
|
|
64
|
+
connection: room.getConnectionState()
|
|
65
|
+
});
|
|
66
|
+
}))), unsubscribeCallbacks.push(room.subscribe("my-presence", (() => {
|
|
67
|
+
!1 === isPatching && set(function(presence, mapping) {
|
|
68
|
+
const partialState = {};
|
|
69
|
+
for (const key in mapping) partialState[key] = presence[key];
|
|
70
|
+
return partialState;
|
|
71
|
+
}(room.getPresence(), presenceMapping));
|
|
72
|
+
}))), room.getStorage().then((({root: root}) => {
|
|
73
|
+
const updates = {};
|
|
74
|
+
room.batch((() => {
|
|
75
|
+
for (const key in storageMapping) {
|
|
76
|
+
const liveblocksStatePart = root.get(key);
|
|
77
|
+
null == liveblocksStatePart ? (updates[key] = initialState[key], patchLiveObjectKey(root, key, void 0, initialState[key])) : updates[key] = lsonToJson(liveblocksStatePart);
|
|
78
|
+
}
|
|
79
|
+
})), typedSet(updates), storageRoot = root, unsubscribeCallbacks.push(room.subscribe(root, (updates => {
|
|
80
|
+
!1 === isPatching && set(function(state, updates, mapping) {
|
|
81
|
+
const partialState = {};
|
|
82
|
+
for (const key in mapping) partialState[key] = state[key];
|
|
83
|
+
const patched = patchImmutableObject(partialState, updates), result = {};
|
|
84
|
+
for (const key in mapping) result[key] = patched[key];
|
|
85
|
+
return result;
|
|
86
|
+
}(get(), updates, storageMapping));
|
|
87
|
+
}), {
|
|
88
|
+
isDeep: !0
|
|
89
|
+
})), updateZustandLiveblocksState(set, {
|
|
90
|
+
isStorageLoading: !1
|
|
91
|
+
});
|
|
92
|
+
}));
|
|
93
|
+
},
|
|
94
|
+
leaveRoom: function(roomId) {
|
|
95
|
+
for (const unsubscribe of unsubscribeCallbacks) unsubscribe();
|
|
96
|
+
storageRoot = null, room = null, isPatching = !1, unsubscribeCallbacks = [], client.leave(roomId),
|
|
97
|
+
updateZustandLiveblocksState(set, {
|
|
98
|
+
others: [],
|
|
99
|
+
connection: "closed",
|
|
100
|
+
isStorageLoading: !1,
|
|
101
|
+
room: null
|
|
102
|
+
});
|
|
103
|
+
},
|
|
104
|
+
room: null,
|
|
105
|
+
others: [],
|
|
106
|
+
connection: "closed",
|
|
107
|
+
isStorageLoading: !1
|
|
108
|
+
}
|
|
109
|
+
});
|
|
110
|
+
};
|
|
163
111
|
}
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
}
|
|
170
|
-
if (oldState[key] !== newState[key]) {
|
|
171
|
-
patchLiveObjectKey(root, key, oldState[key], newState[key]);
|
|
172
|
-
}
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
function isObject(value) {
|
|
176
|
-
return Object.prototype.toString.call(value) === "[object Object]";
|
|
177
|
-
}
|
|
178
|
-
function validateNoDuplicateKeys(storageMapping, presenceMapping) {
|
|
179
|
-
for (const key in storageMapping) {
|
|
180
|
-
if (presenceMapping[key] !== undefined) {
|
|
181
|
-
throw mappingShouldNotHaveTheSameKeys(key);
|
|
182
|
-
}
|
|
183
|
-
}
|
|
112
|
+
|
|
113
|
+
function updateZustandLiveblocksState(set, partial) {
|
|
114
|
+
set((state => ({
|
|
115
|
+
liveblocks: Object.assign(Object.assign({}, state.liveblocks), partial)
|
|
116
|
+
})));
|
|
184
117
|
}
|
|
185
|
-
|
|
186
|
-
* Remove false keys from mapping and generate to a new object to avoid potential mutation from outside the middleware
|
|
187
|
-
*/
|
|
118
|
+
|
|
188
119
|
function validateMapping(mapping, mappingType) {
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
result[key] = true;
|
|
205
|
-
}
|
|
206
|
-
}
|
|
207
|
-
return result;
|
|
120
|
+
if ("production" !== process.env.NODE_ENV) {
|
|
121
|
+
if (null == mapping) throw function(mappingType) {
|
|
122
|
+
return new Error(`${ERROR_PREFIX} ${mappingType} is missing.`);
|
|
123
|
+
}(mappingType);
|
|
124
|
+
if (value = mapping, "[object Object]" !== Object.prototype.toString.call(value)) throw function(mappingType) {
|
|
125
|
+
return new Error(`${ERROR_PREFIX} ${mappingType} should be an object where the values are boolean.`);
|
|
126
|
+
}(mappingType);
|
|
127
|
+
}
|
|
128
|
+
var value;
|
|
129
|
+
const result = {};
|
|
130
|
+
for (const key in mapping) {
|
|
131
|
+
if ("production" !== process.env.NODE_ENV && "boolean" != typeof mapping[key]) throw mappingValueShouldBeABoolean(mappingType, key);
|
|
132
|
+
!0 === mapping[key] && (result[key] = !0);
|
|
133
|
+
}
|
|
134
|
+
return result;
|
|
208
135
|
}
|
|
209
136
|
|
|
210
137
|
export { middleware };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@liveblocks/zustand",
|
|
3
|
-
"version": "0.16.
|
|
3
|
+
"version": "0.16.5",
|
|
4
4
|
"description": "A middleware to integrate Liveblocks into Zustand stores.",
|
|
5
5
|
"main": "./index.js",
|
|
6
6
|
"module": "./index.mjs",
|
|
@@ -18,6 +18,7 @@
|
|
|
18
18
|
],
|
|
19
19
|
"scripts": {
|
|
20
20
|
"build": "rollup -c && cp ./package.json ./README.md ./lib",
|
|
21
|
+
"format": "eslint --fix src/ test/ && prettier --write src/ test/",
|
|
21
22
|
"lint": "eslint src/ test/",
|
|
22
23
|
"test": "jest --watch",
|
|
23
24
|
"test-ci": "jest",
|
|
@@ -30,13 +31,13 @@
|
|
|
30
31
|
"directory": "packages/liveblocks-zustand"
|
|
31
32
|
},
|
|
32
33
|
"peerDependencies": {
|
|
33
|
-
"@liveblocks/client": "0.16.
|
|
34
|
+
"@liveblocks/client": "0.16.5",
|
|
34
35
|
"zustand": "^3"
|
|
35
36
|
},
|
|
36
37
|
"devDependencies": {
|
|
37
|
-
"@babel/core": "^7.
|
|
38
|
-
"@babel/
|
|
39
|
-
"@babel/preset-
|
|
38
|
+
"@babel/core": "^7.17.10",
|
|
39
|
+
"@babel/preset-env": "^7.17.10",
|
|
40
|
+
"@babel/preset-typescript": "^7.16.7",
|
|
40
41
|
"@definitelytyped/dtslint": "^0.0.103",
|
|
41
42
|
"@rollup/plugin-babel": "^5.3.0",
|
|
42
43
|
"@rollup/plugin-node-resolve": "^13.1.3",
|
|
@@ -49,11 +50,14 @@
|
|
|
49
50
|
"@typescript-eslint/eslint-plugin": "^5.18.0",
|
|
50
51
|
"@typescript-eslint/parser": "^5.18.0",
|
|
51
52
|
"eslint": "^8.12.0",
|
|
53
|
+
"eslint-plugin-import": "^2.26.0",
|
|
54
|
+
"eslint-plugin-simple-import-sort": "^7.0.0",
|
|
52
55
|
"jest": "^27.4.7",
|
|
53
56
|
"msw": "^0.36.4",
|
|
54
57
|
"rollup": "^2.64.0",
|
|
55
58
|
"rollup-plugin-command": "^1.1.3",
|
|
56
59
|
"rollup-plugin-dts": "^4.2.1",
|
|
60
|
+
"rollup-plugin-terser": "^7.0.2",
|
|
57
61
|
"whatwg-fetch": "^3.6.2",
|
|
58
62
|
"zustand": "^3.6.9"
|
|
59
63
|
},
|