@liveblocks/zustand 0.15.0-alpha.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +0 -0
- package/lib/esm/index.js.js +185 -0
- package/lib/esm/index.js.mjs +185 -0
- package/lib/index.d.ts +20 -0
- package/lib/index.js +272 -0
- package/package.json +61 -0
package/README.md
ADDED
|
File without changes
|
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
import { patchLiveObjectKey, liveNodeToJson, patchImmutableObject } from '@liveblocks/client';
|
|
2
|
+
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __defProps = Object.defineProperties;
|
|
5
|
+
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
6
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
9
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
10
|
+
var __spreadValues = (a, b) => {
|
|
11
|
+
for (var prop in b || (b = {}))
|
|
12
|
+
if (__hasOwnProp.call(b, prop))
|
|
13
|
+
__defNormalProp(a, prop, b[prop]);
|
|
14
|
+
if (__getOwnPropSymbols)
|
|
15
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
16
|
+
if (__propIsEnum.call(b, prop))
|
|
17
|
+
__defNormalProp(a, prop, b[prop]);
|
|
18
|
+
}
|
|
19
|
+
return a;
|
|
20
|
+
};
|
|
21
|
+
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
22
|
+
const middleware = (config, {
|
|
23
|
+
client,
|
|
24
|
+
storageMapping: unvalidatedMapping,
|
|
25
|
+
presenceMapping: unvalidatedPresenceMapping = {}
|
|
26
|
+
}) => {
|
|
27
|
+
if (client == null) {
|
|
28
|
+
throw new Error(`${ERROR_PREFIX} client is missing`);
|
|
29
|
+
}
|
|
30
|
+
const mapping = validateMapping(unvalidatedMapping, "storageMapping");
|
|
31
|
+
const presenceMapping = validateMapping(unvalidatedPresenceMapping, "presenceMapping");
|
|
32
|
+
validateNoDuplicateKeys(mapping, presenceMapping);
|
|
33
|
+
return (set, get, api) => {
|
|
34
|
+
const typedSet = set;
|
|
35
|
+
let room = null;
|
|
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, mapping);
|
|
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
|
+
room.getStorage().then(({ root }) => {
|
|
71
|
+
const updates = {};
|
|
72
|
+
room.batch(() => {
|
|
73
|
+
for (const key in mapping) {
|
|
74
|
+
const liveblocksStatePart = root.get(key);
|
|
75
|
+
if (liveblocksStatePart == null) {
|
|
76
|
+
updates[key] = initialState[key];
|
|
77
|
+
patchLiveObjectKey(root, key, void 0, initialState[key]);
|
|
78
|
+
} else {
|
|
79
|
+
updates[key] = liveNodeToJson(liveblocksStatePart);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
});
|
|
83
|
+
typedSet(updates);
|
|
84
|
+
storageRoot = root;
|
|
85
|
+
unsubscribeCallbacks.push(room.subscribe(root, (updates2) => {
|
|
86
|
+
if (isPatching === false) {
|
|
87
|
+
set(patchState(get(), updates2, mapping));
|
|
88
|
+
}
|
|
89
|
+
}, { isDeep: true }));
|
|
90
|
+
updateZustandLiveblocksState(set, { isStorageLoading: false });
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
function leaveRoom(roomId) {
|
|
94
|
+
for (const unsubscribe of unsubscribeCallbacks) {
|
|
95
|
+
unsubscribe();
|
|
96
|
+
}
|
|
97
|
+
storageRoot = null;
|
|
98
|
+
room = null;
|
|
99
|
+
isPatching = false;
|
|
100
|
+
unsubscribeCallbacks = [];
|
|
101
|
+
client.leave(roomId);
|
|
102
|
+
updateZustandLiveblocksState(set, {
|
|
103
|
+
others: [],
|
|
104
|
+
connection: "closed",
|
|
105
|
+
isStorageLoading: false,
|
|
106
|
+
room: null
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
return __spreadProps(__spreadValues({}, store), {
|
|
110
|
+
liveblocks: {
|
|
111
|
+
enterRoom,
|
|
112
|
+
leaveRoom,
|
|
113
|
+
room: null,
|
|
114
|
+
others: [],
|
|
115
|
+
connection: "closed",
|
|
116
|
+
isStorageLoading: false
|
|
117
|
+
}
|
|
118
|
+
});
|
|
119
|
+
};
|
|
120
|
+
};
|
|
121
|
+
function patchState(state, updates, mapping) {
|
|
122
|
+
const partialState = {};
|
|
123
|
+
for (const key in mapping) {
|
|
124
|
+
partialState[key] = state[key];
|
|
125
|
+
}
|
|
126
|
+
const patched = patchImmutableObject(partialState, updates);
|
|
127
|
+
const result = {};
|
|
128
|
+
for (const key in mapping) {
|
|
129
|
+
result[key] = patched[key];
|
|
130
|
+
}
|
|
131
|
+
return result;
|
|
132
|
+
}
|
|
133
|
+
function updateZustandLiveblocksState(set, partial) {
|
|
134
|
+
set((state) => ({ liveblocks: __spreadValues(__spreadValues({}, state.liveblocks), partial) }));
|
|
135
|
+
}
|
|
136
|
+
function broadcastInitialPresence(room, state, mapping) {
|
|
137
|
+
for (const key in mapping) {
|
|
138
|
+
room == null ? void 0 : room.updatePresence({ [key]: state[key] });
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
function updatePresence(room, oldState, newState, presenceMapping) {
|
|
142
|
+
for (const key in presenceMapping) {
|
|
143
|
+
if (oldState[key] !== newState[key]) {
|
|
144
|
+
room.updatePresence({ [key]: newState[key] });
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
function patchLiveblocksStorage(root, oldState, newState, mapping) {
|
|
149
|
+
for (const key in mapping) {
|
|
150
|
+
if (oldState[key] !== newState[key]) {
|
|
151
|
+
patchLiveObjectKey(root, key, oldState[key], newState[key]);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
function isObject(value) {
|
|
156
|
+
return Object.prototype.toString.call(value) === "[object Object]";
|
|
157
|
+
}
|
|
158
|
+
function validateNoDuplicateKeys(storageMapping, presenceMapping) {
|
|
159
|
+
for (const key in storageMapping) {
|
|
160
|
+
if (presenceMapping[key] !== void 0) {
|
|
161
|
+
throw new Error(`${ERROR_PREFIX} "${key}" is mapped on presenceMapping and storageMapping. A key shouldn't exist on both mapping.`);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
function validateMapping(mapping, mappingType) {
|
|
166
|
+
if (mapping == null) {
|
|
167
|
+
throw new Error(`${ERROR_PREFIX} ${mappingType} is missing.`);
|
|
168
|
+
}
|
|
169
|
+
if (!isObject(mapping)) {
|
|
170
|
+
throw new Error(`${ERROR_PREFIX} ${mappingType} should be an object where the values are boolean.`);
|
|
171
|
+
}
|
|
172
|
+
const result = {};
|
|
173
|
+
for (const key in mapping) {
|
|
174
|
+
if (typeof mapping[key] !== "boolean") {
|
|
175
|
+
throw new Error(`${ERROR_PREFIX} ${mappingType}.${key} value should be a boolean`);
|
|
176
|
+
}
|
|
177
|
+
if (mapping[key] === true) {
|
|
178
|
+
result[key] = true;
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
return result;
|
|
182
|
+
}
|
|
183
|
+
const ERROR_PREFIX = "Invalid @liveblocks/zustand middleware config.";
|
|
184
|
+
|
|
185
|
+
export { middleware };
|
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
import { patchLiveObjectKey, liveNodeToJson, patchImmutableObject } from '@liveblocks/client';
|
|
2
|
+
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __defProps = Object.defineProperties;
|
|
5
|
+
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
6
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
9
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
10
|
+
var __spreadValues = (a, b) => {
|
|
11
|
+
for (var prop in b || (b = {}))
|
|
12
|
+
if (__hasOwnProp.call(b, prop))
|
|
13
|
+
__defNormalProp(a, prop, b[prop]);
|
|
14
|
+
if (__getOwnPropSymbols)
|
|
15
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
16
|
+
if (__propIsEnum.call(b, prop))
|
|
17
|
+
__defNormalProp(a, prop, b[prop]);
|
|
18
|
+
}
|
|
19
|
+
return a;
|
|
20
|
+
};
|
|
21
|
+
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
22
|
+
const middleware = (config, {
|
|
23
|
+
client,
|
|
24
|
+
storageMapping: unvalidatedMapping,
|
|
25
|
+
presenceMapping: unvalidatedPresenceMapping = {}
|
|
26
|
+
}) => {
|
|
27
|
+
if (client == null) {
|
|
28
|
+
throw new Error(`${ERROR_PREFIX} client is missing`);
|
|
29
|
+
}
|
|
30
|
+
const mapping = validateMapping(unvalidatedMapping, "storageMapping");
|
|
31
|
+
const presenceMapping = validateMapping(unvalidatedPresenceMapping, "presenceMapping");
|
|
32
|
+
validateNoDuplicateKeys(mapping, presenceMapping);
|
|
33
|
+
return (set, get, api) => {
|
|
34
|
+
const typedSet = set;
|
|
35
|
+
let room = null;
|
|
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, mapping);
|
|
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
|
+
room.getStorage().then(({ root }) => {
|
|
71
|
+
const updates = {};
|
|
72
|
+
room.batch(() => {
|
|
73
|
+
for (const key in mapping) {
|
|
74
|
+
const liveblocksStatePart = root.get(key);
|
|
75
|
+
if (liveblocksStatePart == null) {
|
|
76
|
+
updates[key] = initialState[key];
|
|
77
|
+
patchLiveObjectKey(root, key, void 0, initialState[key]);
|
|
78
|
+
} else {
|
|
79
|
+
updates[key] = liveNodeToJson(liveblocksStatePart);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
});
|
|
83
|
+
typedSet(updates);
|
|
84
|
+
storageRoot = root;
|
|
85
|
+
unsubscribeCallbacks.push(room.subscribe(root, (updates2) => {
|
|
86
|
+
if (isPatching === false) {
|
|
87
|
+
set(patchState(get(), updates2, mapping));
|
|
88
|
+
}
|
|
89
|
+
}, { isDeep: true }));
|
|
90
|
+
updateZustandLiveblocksState(set, { isStorageLoading: false });
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
function leaveRoom(roomId) {
|
|
94
|
+
for (const unsubscribe of unsubscribeCallbacks) {
|
|
95
|
+
unsubscribe();
|
|
96
|
+
}
|
|
97
|
+
storageRoot = null;
|
|
98
|
+
room = null;
|
|
99
|
+
isPatching = false;
|
|
100
|
+
unsubscribeCallbacks = [];
|
|
101
|
+
client.leave(roomId);
|
|
102
|
+
updateZustandLiveblocksState(set, {
|
|
103
|
+
others: [],
|
|
104
|
+
connection: "closed",
|
|
105
|
+
isStorageLoading: false,
|
|
106
|
+
room: null
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
return __spreadProps(__spreadValues({}, store), {
|
|
110
|
+
liveblocks: {
|
|
111
|
+
enterRoom,
|
|
112
|
+
leaveRoom,
|
|
113
|
+
room: null,
|
|
114
|
+
others: [],
|
|
115
|
+
connection: "closed",
|
|
116
|
+
isStorageLoading: false
|
|
117
|
+
}
|
|
118
|
+
});
|
|
119
|
+
};
|
|
120
|
+
};
|
|
121
|
+
function patchState(state, updates, mapping) {
|
|
122
|
+
const partialState = {};
|
|
123
|
+
for (const key in mapping) {
|
|
124
|
+
partialState[key] = state[key];
|
|
125
|
+
}
|
|
126
|
+
const patched = patchImmutableObject(partialState, updates);
|
|
127
|
+
const result = {};
|
|
128
|
+
for (const key in mapping) {
|
|
129
|
+
result[key] = patched[key];
|
|
130
|
+
}
|
|
131
|
+
return result;
|
|
132
|
+
}
|
|
133
|
+
function updateZustandLiveblocksState(set, partial) {
|
|
134
|
+
set((state) => ({ liveblocks: __spreadValues(__spreadValues({}, state.liveblocks), partial) }));
|
|
135
|
+
}
|
|
136
|
+
function broadcastInitialPresence(room, state, mapping) {
|
|
137
|
+
for (const key in mapping) {
|
|
138
|
+
room == null ? void 0 : room.updatePresence({ [key]: state[key] });
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
function updatePresence(room, oldState, newState, presenceMapping) {
|
|
142
|
+
for (const key in presenceMapping) {
|
|
143
|
+
if (oldState[key] !== newState[key]) {
|
|
144
|
+
room.updatePresence({ [key]: newState[key] });
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
function patchLiveblocksStorage(root, oldState, newState, mapping) {
|
|
149
|
+
for (const key in mapping) {
|
|
150
|
+
if (oldState[key] !== newState[key]) {
|
|
151
|
+
patchLiveObjectKey(root, key, oldState[key], newState[key]);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
function isObject(value) {
|
|
156
|
+
return Object.prototype.toString.call(value) === "[object Object]";
|
|
157
|
+
}
|
|
158
|
+
function validateNoDuplicateKeys(storageMapping, presenceMapping) {
|
|
159
|
+
for (const key in storageMapping) {
|
|
160
|
+
if (presenceMapping[key] !== void 0) {
|
|
161
|
+
throw new Error(`${ERROR_PREFIX} "${key}" is mapped on presenceMapping and storageMapping. A key shouldn't exist on both mapping.`);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
function validateMapping(mapping, mappingType) {
|
|
166
|
+
if (mapping == null) {
|
|
167
|
+
throw new Error(`${ERROR_PREFIX} ${mappingType} is missing.`);
|
|
168
|
+
}
|
|
169
|
+
if (!isObject(mapping)) {
|
|
170
|
+
throw new Error(`${ERROR_PREFIX} ${mappingType} should be an object where the values are boolean.`);
|
|
171
|
+
}
|
|
172
|
+
const result = {};
|
|
173
|
+
for (const key in mapping) {
|
|
174
|
+
if (typeof mapping[key] !== "boolean") {
|
|
175
|
+
throw new Error(`${ERROR_PREFIX} ${mappingType}.${key} value should be a boolean`);
|
|
176
|
+
}
|
|
177
|
+
if (mapping[key] === true) {
|
|
178
|
+
result[key] = true;
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
return result;
|
|
182
|
+
}
|
|
183
|
+
const ERROR_PREFIX = "Invalid @liveblocks/zustand middleware config.";
|
|
184
|
+
|
|
185
|
+
export { middleware };
|
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { StateCreator, SetState, GetState, StoreApi } from "zustand";
|
|
2
|
+
import { Client, User, Room } from "@liveblocks/client";
|
|
3
|
+
export declare type LiveblocksState<TState, TPresence = any> = TState & {
|
|
4
|
+
readonly liveblocks: {
|
|
5
|
+
readonly enterRoom: (room: string, initialState: Partial<TState>) => void;
|
|
6
|
+
readonly leaveRoom: (room: string) => void;
|
|
7
|
+
readonly room: Room | null;
|
|
8
|
+
readonly others: Array<User<TPresence>>;
|
|
9
|
+
readonly isStorageLoading: boolean;
|
|
10
|
+
readonly connection: "closed" | "authenticating" | "unavailable" | "failed" | "open" | "connecting";
|
|
11
|
+
};
|
|
12
|
+
};
|
|
13
|
+
export declare type Mapping<T> = Partial<{
|
|
14
|
+
[Property in keyof T]: boolean;
|
|
15
|
+
}>;
|
|
16
|
+
export declare const middleware: <T extends Object, TPresence extends Object = any>(config: StateCreator<T, SetState<T>, GetState<LiveblocksState<T>>, StoreApi<T>>, options: {
|
|
17
|
+
client: Client;
|
|
18
|
+
storageMapping: Mapping<T>;
|
|
19
|
+
presenceMapping?: Mapping<T>;
|
|
20
|
+
}) => StateCreator<LiveblocksState<T, TPresence>, SetState<LiveblocksState<T, TPresence>>, GetState<LiveblocksState<T, TPresence>>, StoreApi<LiveblocksState<T, TPresence>>>;
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,272 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var client = require('@liveblocks/client');
|
|
6
|
+
|
|
7
|
+
function _extends() {
|
|
8
|
+
_extends = Object.assign || function (target) {
|
|
9
|
+
for (var i = 1; i < arguments.length; i++) {
|
|
10
|
+
var source = arguments[i];
|
|
11
|
+
|
|
12
|
+
for (var key in source) {
|
|
13
|
+
if (Object.prototype.hasOwnProperty.call(source, key)) {
|
|
14
|
+
target[key] = source[key];
|
|
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);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function _arrayLikeToArray(arr, len) {
|
|
35
|
+
if (len == null || len > arr.length) len = arr.length;
|
|
36
|
+
|
|
37
|
+
for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
|
|
38
|
+
|
|
39
|
+
return arr2;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function _createForOfIteratorHelperLoose(o, allowArrayLike) {
|
|
43
|
+
var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"];
|
|
44
|
+
if (it) return (it = it.call(o)).next.bind(it);
|
|
45
|
+
|
|
46
|
+
if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") {
|
|
47
|
+
if (it) o = it;
|
|
48
|
+
var i = 0;
|
|
49
|
+
return function () {
|
|
50
|
+
if (i >= o.length) return {
|
|
51
|
+
done: true
|
|
52
|
+
};
|
|
53
|
+
return {
|
|
54
|
+
done: false,
|
|
55
|
+
value: o[i++]
|
|
56
|
+
};
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
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
|
+
}
|
|
62
|
+
|
|
63
|
+
var middleware = function middleware(config, _ref) {
|
|
64
|
+
var client$1 = _ref.client,
|
|
65
|
+
unvalidatedMapping = _ref.storageMapping,
|
|
66
|
+
_ref$presenceMapping = _ref.presenceMapping,
|
|
67
|
+
unvalidatedPresenceMapping = _ref$presenceMapping === void 0 ? {} : _ref$presenceMapping;
|
|
68
|
+
|
|
69
|
+
if (client$1 == null) {
|
|
70
|
+
throw new Error(ERROR_PREFIX + " client is missing");
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
var mapping = validateMapping(unvalidatedMapping, "storageMapping");
|
|
74
|
+
var presenceMapping = validateMapping(unvalidatedPresenceMapping, "presenceMapping");
|
|
75
|
+
validateNoDuplicateKeys(mapping, presenceMapping);
|
|
76
|
+
return function (set, get, api) {
|
|
77
|
+
var typedSet = set;
|
|
78
|
+
var room = null;
|
|
79
|
+
var isPatching = false;
|
|
80
|
+
var storageRoot = null;
|
|
81
|
+
var unsubscribeCallbacks = [];
|
|
82
|
+
var store = config(function (args) {
|
|
83
|
+
var oldState = get();
|
|
84
|
+
set(args);
|
|
85
|
+
var newState = get();
|
|
86
|
+
|
|
87
|
+
if (room) {
|
|
88
|
+
isPatching = true;
|
|
89
|
+
updatePresence(room, oldState, newState, presenceMapping);
|
|
90
|
+
room.batch(function () {
|
|
91
|
+
if (storageRoot) {
|
|
92
|
+
patchLiveblocksStorage(storageRoot, oldState, newState, mapping);
|
|
93
|
+
}
|
|
94
|
+
});
|
|
95
|
+
isPatching = false;
|
|
96
|
+
}
|
|
97
|
+
}, get, api);
|
|
98
|
+
|
|
99
|
+
function enterRoom(roomId, initialState) {
|
|
100
|
+
if (storageRoot) {
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
room = client$1.enter(roomId);
|
|
105
|
+
updateZustandLiveblocksState(set, {
|
|
106
|
+
isStorageLoading: true,
|
|
107
|
+
room: room
|
|
108
|
+
});
|
|
109
|
+
var state = get();
|
|
110
|
+
broadcastInitialPresence(room, state, presenceMapping);
|
|
111
|
+
unsubscribeCallbacks.push(room.subscribe("others", function (others) {
|
|
112
|
+
updateZustandLiveblocksState(set, {
|
|
113
|
+
others: others.toArray()
|
|
114
|
+
});
|
|
115
|
+
}));
|
|
116
|
+
unsubscribeCallbacks.push(room.subscribe("connection", function () {
|
|
117
|
+
updateZustandLiveblocksState(set, {
|
|
118
|
+
connection: room.getConnectionState()
|
|
119
|
+
});
|
|
120
|
+
}));
|
|
121
|
+
room.getStorage().then(function (_ref2) {
|
|
122
|
+
var root = _ref2.root;
|
|
123
|
+
var updates = {};
|
|
124
|
+
room.batch(function () {
|
|
125
|
+
for (var key in mapping) {
|
|
126
|
+
var liveblocksStatePart = root.get(key);
|
|
127
|
+
|
|
128
|
+
if (liveblocksStatePart == null) {
|
|
129
|
+
updates[key] = initialState[key];
|
|
130
|
+
client.patchLiveObjectKey(root, key, undefined, initialState[key]);
|
|
131
|
+
} else {
|
|
132
|
+
updates[key] = client.liveNodeToJson(liveblocksStatePart);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
});
|
|
136
|
+
typedSet(updates);
|
|
137
|
+
storageRoot = root;
|
|
138
|
+
unsubscribeCallbacks.push(room.subscribe(root, function (updates) {
|
|
139
|
+
if (isPatching === false) {
|
|
140
|
+
set(patchState(get(), updates, mapping));
|
|
141
|
+
}
|
|
142
|
+
}, {
|
|
143
|
+
isDeep: true
|
|
144
|
+
}));
|
|
145
|
+
updateZustandLiveblocksState(set, {
|
|
146
|
+
isStorageLoading: false
|
|
147
|
+
});
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
function leaveRoom(roomId) {
|
|
152
|
+
for (var _iterator = _createForOfIteratorHelperLoose(unsubscribeCallbacks), _step; !(_step = _iterator()).done;) {
|
|
153
|
+
var unsubscribe = _step.value;
|
|
154
|
+
unsubscribe();
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
storageRoot = null;
|
|
158
|
+
room = null;
|
|
159
|
+
isPatching = false;
|
|
160
|
+
unsubscribeCallbacks = [];
|
|
161
|
+
client$1.leave(roomId);
|
|
162
|
+
updateZustandLiveblocksState(set, {
|
|
163
|
+
others: [],
|
|
164
|
+
connection: "closed",
|
|
165
|
+
isStorageLoading: false,
|
|
166
|
+
room: null
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
return _extends({}, store, {
|
|
171
|
+
liveblocks: {
|
|
172
|
+
enterRoom: enterRoom,
|
|
173
|
+
leaveRoom: leaveRoom,
|
|
174
|
+
room: null,
|
|
175
|
+
others: [],
|
|
176
|
+
connection: "closed",
|
|
177
|
+
isStorageLoading: false
|
|
178
|
+
}
|
|
179
|
+
});
|
|
180
|
+
};
|
|
181
|
+
};
|
|
182
|
+
|
|
183
|
+
function patchState(state, updates, mapping) {
|
|
184
|
+
var partialState = {};
|
|
185
|
+
|
|
186
|
+
for (var key in mapping) {
|
|
187
|
+
partialState[key] = state[key];
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
var patched = client.patchImmutableObject(partialState, updates);
|
|
191
|
+
var result = {};
|
|
192
|
+
|
|
193
|
+
for (var _key in mapping) {
|
|
194
|
+
result[_key] = patched[_key];
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
return result;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
function updateZustandLiveblocksState(set, partial) {
|
|
201
|
+
set(function (state) {
|
|
202
|
+
return {
|
|
203
|
+
liveblocks: _extends({}, state.liveblocks, partial)
|
|
204
|
+
};
|
|
205
|
+
});
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
function broadcastInitialPresence(room, state, mapping) {
|
|
209
|
+
for (var key in mapping) {
|
|
210
|
+
var _room$updatePresence;
|
|
211
|
+
|
|
212
|
+
room == null ? void 0 : room.updatePresence((_room$updatePresence = {}, _room$updatePresence[key] = state[key], _room$updatePresence));
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
function updatePresence(room, oldState, newState, presenceMapping) {
|
|
217
|
+
for (var key in presenceMapping) {
|
|
218
|
+
if (oldState[key] !== newState[key]) {
|
|
219
|
+
var _room$updatePresence2;
|
|
220
|
+
|
|
221
|
+
room.updatePresence((_room$updatePresence2 = {}, _room$updatePresence2[key] = newState[key], _room$updatePresence2));
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
function patchLiveblocksStorage(root, oldState, newState, mapping) {
|
|
227
|
+
for (var key in mapping) {
|
|
228
|
+
if (oldState[key] !== newState[key]) {
|
|
229
|
+
client.patchLiveObjectKey(root, key, oldState[key], newState[key]);
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
function isObject(value) {
|
|
235
|
+
return Object.prototype.toString.call(value) === "[object Object]";
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
function validateNoDuplicateKeys(storageMapping, presenceMapping) {
|
|
239
|
+
for (var key in storageMapping) {
|
|
240
|
+
if (presenceMapping[key] !== undefined) {
|
|
241
|
+
throw new Error(ERROR_PREFIX + " \"" + key + "\" is mapped on presenceMapping and storageMapping. A key shouldn't exist on both mapping.");
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
function validateMapping(mapping, mappingType) {
|
|
247
|
+
if (mapping == null) {
|
|
248
|
+
throw new Error(ERROR_PREFIX + " " + mappingType + " is missing.");
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
if (!isObject(mapping)) {
|
|
252
|
+
throw new Error(ERROR_PREFIX + " " + mappingType + " should be an object where the values are boolean.");
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
var result = {};
|
|
256
|
+
|
|
257
|
+
for (var key in mapping) {
|
|
258
|
+
if (typeof mapping[key] !== "boolean") {
|
|
259
|
+
throw new Error(ERROR_PREFIX + " " + mappingType + "." + key + " value should be a boolean");
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
if (mapping[key] === true) {
|
|
263
|
+
result[key] = true;
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
return result;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
var ERROR_PREFIX = "Invalid @liveblocks/zustand middleware config.";
|
|
271
|
+
|
|
272
|
+
exports.middleware = middleware;
|
package/package.json
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@liveblocks/zustand",
|
|
3
|
+
"version": "0.15.0-alpha.1",
|
|
4
|
+
"description": "",
|
|
5
|
+
"main": "./lib/index.js",
|
|
6
|
+
"types": "./lib/index.d.ts",
|
|
7
|
+
"files": [
|
|
8
|
+
"lib/"
|
|
9
|
+
],
|
|
10
|
+
"exports": {
|
|
11
|
+
".": {
|
|
12
|
+
"types": "./lib/index.d.ts",
|
|
13
|
+
"module": "./lib/esm/index.js",
|
|
14
|
+
"import": "./lib/esm/index.mjs",
|
|
15
|
+
"default": "./lib/index.js"
|
|
16
|
+
}
|
|
17
|
+
},
|
|
18
|
+
"keywords": [
|
|
19
|
+
"zustand",
|
|
20
|
+
"react",
|
|
21
|
+
"liveblocks",
|
|
22
|
+
"multiplayer",
|
|
23
|
+
"live-cursors",
|
|
24
|
+
"collaborative"
|
|
25
|
+
],
|
|
26
|
+
"scripts": {
|
|
27
|
+
"build": "rollup -c",
|
|
28
|
+
"test": "jest --watch",
|
|
29
|
+
"dtslint": "dtslint --localTs node_modules/typescript/lib --expectOnly types"
|
|
30
|
+
},
|
|
31
|
+
"license": "Apache-2.0",
|
|
32
|
+
"repository": {
|
|
33
|
+
"type": "git",
|
|
34
|
+
"url": "https://github.com/liveblocks/liveblocks.git",
|
|
35
|
+
"directory": "packages/liveblocks-zustand"
|
|
36
|
+
},
|
|
37
|
+
"peerDependencies": {
|
|
38
|
+
"@liveblocks/client": "0.15.0-alpha.1",
|
|
39
|
+
"zustand": "^3"
|
|
40
|
+
},
|
|
41
|
+
"devDependencies": {
|
|
42
|
+
"@babel/core": "^7.16.7",
|
|
43
|
+
"@babel/plugin-transform-typescript": "^7.16.8",
|
|
44
|
+
"@babel/preset-env": "^7.16.8",
|
|
45
|
+
"@definitelytyped/dtslint": "^0.0.103",
|
|
46
|
+
"@rollup/plugin-babel": "^5.3.0",
|
|
47
|
+
"@rollup/plugin-node-resolve": "^13.1.3",
|
|
48
|
+
"@rollup/plugin-typescript": "^8.3.0",
|
|
49
|
+
"@testing-library/jest-dom": "^5.16.1",
|
|
50
|
+
"@testing-library/react": "^12.1.2",
|
|
51
|
+
"@testing-library/react-hooks": "^7.0.2",
|
|
52
|
+
"@types/jest": "^27.4.0",
|
|
53
|
+
"esbuild": "0.14.11",
|
|
54
|
+
"jest": "^27.4.7",
|
|
55
|
+
"msw": "^0.36.4",
|
|
56
|
+
"rollup": "^2.64.0",
|
|
57
|
+
"rollup-plugin-esbuild": "^4.8.2",
|
|
58
|
+
"whatwg-fetch": "^3.6.2",
|
|
59
|
+
"zustand": "^3.6.9"
|
|
60
|
+
}
|
|
61
|
+
}
|