@liveblocks/client 0.16.3 → 0.16.4
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 +3 -727
- package/index.js +85 -2861
- package/index.mjs +1324 -0
- package/internal.d.ts +18 -56
- package/internal.js +321 -148
- package/internal.mjs +320 -0
- package/package.json +17 -16
- package/shared.d.ts +771 -0
- package/shared.js +2482 -0
- package/shared.mjs +1947 -0
- package/esm/index.js +0 -3068
- package/esm/index.mjs +0 -3068
- package/esm/internal.js +0 -150
- package/esm/internal.mjs +0 -150
package/internal.d.ts
CHANGED
|
@@ -1,21 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
* four types that make up the Json family:
|
|
4
|
-
*
|
|
5
|
-
* - Json any legal JSON value
|
|
6
|
-
* - JsonScalar any legal JSON leaf value (no lists or objects)
|
|
7
|
-
* - JsonArray a JSON value whose outer type is an array
|
|
8
|
-
* - JsonObject a JSON value whose outer type is an object
|
|
9
|
-
*
|
|
10
|
-
*/
|
|
11
|
-
declare type Json = JsonScalar | JsonArray | JsonObject;
|
|
12
|
-
declare type JsonScalar = string | number | boolean | null;
|
|
13
|
-
declare type JsonArray = Json[];
|
|
14
|
-
declare type JsonObject = {
|
|
15
|
-
[key: string]: Json | undefined;
|
|
16
|
-
};
|
|
17
|
-
|
|
18
|
-
declare type Presence = Record<string, unknown>;
|
|
1
|
+
import { J as Json, P as Presence, d as JsonObject, e as Lson, A as AbstractCrdt, f as LsonObject, L as LiveObject, S as StorageUpdate } from './shared';
|
|
2
|
+
export { g as Resolve, h as RoomInitializers } from './shared';
|
|
19
3
|
|
|
20
4
|
/**
|
|
21
5
|
* Messages that can be sent from the server to the client.
|
|
@@ -137,32 +121,12 @@ declare type UpdateStorageMessage = {
|
|
|
137
121
|
type: ServerMessageType.UpdateStorage;
|
|
138
122
|
ops: Op[];
|
|
139
123
|
};
|
|
140
|
-
/**
|
|
141
|
-
* Messages that can be sent from the client to the server.
|
|
142
|
-
*/
|
|
143
|
-
declare type ClientMessage = ClientEventMessage | UpdatePresenceClientMessage | UpdateStorageClientMessage | FetchStorageClientMessage;
|
|
144
124
|
declare enum ClientMessageType {
|
|
145
125
|
UpdatePresence = 100,
|
|
146
126
|
ClientEvent = 103,
|
|
147
127
|
FetchStorage = 200,
|
|
148
128
|
UpdateStorage = 201
|
|
149
129
|
}
|
|
150
|
-
declare type ClientEventMessage = {
|
|
151
|
-
type: ClientMessageType.ClientEvent;
|
|
152
|
-
event: Json;
|
|
153
|
-
};
|
|
154
|
-
declare type UpdatePresenceClientMessage = {
|
|
155
|
-
type: ClientMessageType.UpdatePresence;
|
|
156
|
-
data: Presence;
|
|
157
|
-
targetActor?: number;
|
|
158
|
-
};
|
|
159
|
-
declare type UpdateStorageClientMessage = {
|
|
160
|
-
type: ClientMessageType.UpdateStorage;
|
|
161
|
-
ops: Op[];
|
|
162
|
-
};
|
|
163
|
-
declare type FetchStorageClientMessage = {
|
|
164
|
-
type: ClientMessageType.FetchStorage;
|
|
165
|
-
};
|
|
166
130
|
declare enum CrdtType {
|
|
167
131
|
Object = 0,
|
|
168
132
|
List = 1,
|
|
@@ -208,7 +172,6 @@ declare enum OpType {
|
|
|
208
172
|
* only.
|
|
209
173
|
*/
|
|
210
174
|
declare type Op = CreateObjectOp | UpdateObjectOp | DeleteCrdtOp | CreateListOp | SetParentKeyOp | DeleteObjectKeyOp | CreateMapOp | CreateRegisterOp;
|
|
211
|
-
declare type CreateOp = CreateObjectOp | CreateRegisterOp | CreateMapOp | CreateListOp;
|
|
212
175
|
declare type UpdateObjectOp = {
|
|
213
176
|
opId?: string;
|
|
214
177
|
id: string;
|
|
@@ -266,22 +229,21 @@ declare type DeleteObjectKeyOp = {
|
|
|
266
229
|
type: OpType.DeleteObjectKey;
|
|
267
230
|
key: string;
|
|
268
231
|
};
|
|
269
|
-
declare enum WebsocketCloseCodes {
|
|
270
|
-
CLOSE_ABNORMAL = 1006,
|
|
271
|
-
INVALID_MESSAGE_FORMAT = 4000,
|
|
272
|
-
NOT_ALLOWED = 4001,
|
|
273
|
-
MAX_NUMBER_OF_MESSAGES_PER_SECONDS = 4002,
|
|
274
|
-
MAX_NUMBER_OF_CONCURRENT_CONNECTIONS = 4003,
|
|
275
|
-
MAX_NUMBER_OF_MESSAGES_PER_DAY_PER_APP = 4004,
|
|
276
|
-
MAX_NUMBER_OF_CONCURRENT_CONNECTIONS_PER_ROOM = 4005,
|
|
277
|
-
CLOSE_WITHOUT_RETRY = 4999
|
|
278
|
-
}
|
|
279
232
|
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
declare function
|
|
285
|
-
|
|
233
|
+
/**
|
|
234
|
+
* Displays a deprecation warning in the dev console. Only in dev mode, and
|
|
235
|
+
* only once per message/key. In production, this is a no-op.
|
|
236
|
+
*/
|
|
237
|
+
declare function deprecate(message: string, key?: string): void;
|
|
238
|
+
/**
|
|
239
|
+
* Conditionally displays a deprecation warning in the dev
|
|
240
|
+
* console if the first argument is truthy. Only in dev mode, and
|
|
241
|
+
* only once per message/key. In production, this is a no-op.
|
|
242
|
+
*/
|
|
243
|
+
declare function deprecateIf(condition: unknown, message: string, key?: string): void;
|
|
244
|
+
|
|
245
|
+
declare function lsonToJson(value: Lson | AbstractCrdt): Json;
|
|
246
|
+
declare function patchLiveObjectKey<O extends LsonObject>(liveObject: LiveObject<O>, key: keyof O, prev: unknown, next: unknown): void;
|
|
247
|
+
declare function patchImmutableObject<T>(state: T, updates: StorageUpdate[]): T;
|
|
286
248
|
|
|
287
|
-
export {
|
|
249
|
+
export { ClientMessageType, CrdtType, OpType, SerializedCrdtWithId, ServerMessage, ServerMessageType, deprecate, deprecateIf, lsonToJson, patchImmutableObject, patchLiveObjectKey };
|
package/internal.js
CHANGED
|
@@ -2,193 +2,366 @@
|
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
(function (ClientMessageType) {
|
|
20
|
-
ClientMessageType[ClientMessageType["UpdatePresence"] = 100] = "UpdatePresence";
|
|
21
|
-
ClientMessageType[ClientMessageType["ClientEvent"] = 103] = "ClientEvent";
|
|
22
|
-
ClientMessageType[ClientMessageType["FetchStorage"] = 200] = "FetchStorage";
|
|
23
|
-
ClientMessageType[ClientMessageType["UpdateStorage"] = 201] = "UpdateStorage";
|
|
24
|
-
})(exports.ClientMessageType || (exports.ClientMessageType = {}));
|
|
25
|
-
|
|
26
|
-
exports.CrdtType = void 0;
|
|
27
|
-
|
|
28
|
-
(function (CrdtType) {
|
|
29
|
-
CrdtType[CrdtType["Object"] = 0] = "Object";
|
|
30
|
-
CrdtType[CrdtType["List"] = 1] = "List";
|
|
31
|
-
CrdtType[CrdtType["Map"] = 2] = "Map";
|
|
32
|
-
CrdtType[CrdtType["Register"] = 3] = "Register";
|
|
33
|
-
})(exports.CrdtType || (exports.CrdtType = {}));
|
|
34
|
-
|
|
35
|
-
exports.OpType = void 0;
|
|
36
|
-
|
|
37
|
-
(function (OpType) {
|
|
38
|
-
OpType[OpType["Init"] = 0] = "Init";
|
|
39
|
-
OpType[OpType["SetParentKey"] = 1] = "SetParentKey";
|
|
40
|
-
OpType[OpType["CreateList"] = 2] = "CreateList";
|
|
41
|
-
OpType[OpType["UpdateObject"] = 3] = "UpdateObject";
|
|
42
|
-
OpType[OpType["CreateObject"] = 4] = "CreateObject";
|
|
43
|
-
OpType[OpType["DeleteCrdt"] = 5] = "DeleteCrdt";
|
|
44
|
-
OpType[OpType["DeleteObjectKey"] = 6] = "DeleteObjectKey";
|
|
45
|
-
OpType[OpType["CreateMap"] = 7] = "CreateMap";
|
|
46
|
-
OpType[OpType["CreateRegister"] = 8] = "CreateRegister";
|
|
47
|
-
})(exports.OpType || (exports.OpType = {}));
|
|
48
|
-
|
|
49
|
-
exports.WebsocketCloseCodes = void 0;
|
|
50
|
-
|
|
51
|
-
(function (WebsocketCloseCodes) {
|
|
52
|
-
WebsocketCloseCodes[WebsocketCloseCodes["CLOSE_ABNORMAL"] = 1006] = "CLOSE_ABNORMAL";
|
|
53
|
-
WebsocketCloseCodes[WebsocketCloseCodes["INVALID_MESSAGE_FORMAT"] = 4000] = "INVALID_MESSAGE_FORMAT";
|
|
54
|
-
WebsocketCloseCodes[WebsocketCloseCodes["NOT_ALLOWED"] = 4001] = "NOT_ALLOWED";
|
|
55
|
-
WebsocketCloseCodes[WebsocketCloseCodes["MAX_NUMBER_OF_MESSAGES_PER_SECONDS"] = 4002] = "MAX_NUMBER_OF_MESSAGES_PER_SECONDS";
|
|
56
|
-
WebsocketCloseCodes[WebsocketCloseCodes["MAX_NUMBER_OF_CONCURRENT_CONNECTIONS"] = 4003] = "MAX_NUMBER_OF_CONCURRENT_CONNECTIONS";
|
|
57
|
-
WebsocketCloseCodes[WebsocketCloseCodes["MAX_NUMBER_OF_MESSAGES_PER_DAY_PER_APP"] = 4004] = "MAX_NUMBER_OF_MESSAGES_PER_DAY_PER_APP";
|
|
58
|
-
WebsocketCloseCodes[WebsocketCloseCodes["MAX_NUMBER_OF_CONCURRENT_CONNECTIONS_PER_ROOM"] = 4005] = "MAX_NUMBER_OF_CONCURRENT_CONNECTIONS_PER_ROOM";
|
|
59
|
-
WebsocketCloseCodes[WebsocketCloseCodes["CLOSE_WITHOUT_RETRY"] = 4999] = "CLOSE_WITHOUT_RETRY";
|
|
60
|
-
})(exports.WebsocketCloseCodes || (exports.WebsocketCloseCodes = {}));
|
|
61
|
-
|
|
62
|
-
var min = 32;
|
|
63
|
-
var max = 126;
|
|
64
|
-
function makePosition(before, after) {
|
|
65
|
-
if (before == null && after == null) {
|
|
66
|
-
return pos([min + 1]);
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
if (before != null && after == null) {
|
|
70
|
-
return getNextPosition(before);
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
if (before == null && after != null) {
|
|
74
|
-
return getPreviousPosition(after);
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
return pos(makePositionFromCodes(posCodes(before), posCodes(after)));
|
|
5
|
+
var LiveObject = require('./shared.js');
|
|
6
|
+
|
|
7
|
+
function lsonObjectToJson(obj) {
|
|
8
|
+
var result = {};
|
|
9
|
+
|
|
10
|
+
for (var _key in obj) {
|
|
11
|
+
var val = obj[_key];
|
|
12
|
+
|
|
13
|
+
if (val !== undefined) {
|
|
14
|
+
result[_key] = lsonToJson(val);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
return result;
|
|
78
19
|
}
|
|
79
20
|
|
|
80
|
-
function
|
|
81
|
-
|
|
82
|
-
|
|
21
|
+
function liveObjectToJson(liveObject) {
|
|
22
|
+
return lsonObjectToJson(liveObject.toObject());
|
|
23
|
+
}
|
|
83
24
|
|
|
84
|
-
|
|
85
|
-
|
|
25
|
+
function liveMapToJson(map) {
|
|
26
|
+
var result = {};
|
|
86
27
|
|
|
87
|
-
|
|
88
|
-
|
|
28
|
+
for (var _iterator = LiveObject._createForOfIteratorHelperLoose(map.entries()), _step; !(_step = _iterator()).done;) {
|
|
29
|
+
var _step$value = _step.value,
|
|
30
|
+
_key2 = _step$value[0],
|
|
31
|
+
value = _step$value[1];
|
|
32
|
+
result[_key2] = lsonToJson(value);
|
|
33
|
+
}
|
|
89
34
|
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
35
|
+
return result;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function lsonListToJson(value) {
|
|
39
|
+
return value.map(lsonToJson);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function liveListToJson(value) {
|
|
43
|
+
return lsonListToJson(value.toArray());
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function lsonToJson(value) {
|
|
47
|
+
if (value instanceof LiveObject.LiveObject) {
|
|
48
|
+
return liveObjectToJson(value);
|
|
49
|
+
} else if (value instanceof LiveObject.LiveList) {
|
|
50
|
+
return liveListToJson(value);
|
|
51
|
+
} else if (value instanceof LiveObject.LiveMap) {
|
|
52
|
+
return liveMapToJson(value);
|
|
53
|
+
} else if (value instanceof LiveObject.LiveRegister) {
|
|
54
|
+
return value.data;
|
|
55
|
+
} else if (value instanceof LiveObject.AbstractCrdt) {
|
|
56
|
+
throw new Error("Unhandled subclass of AbstractCrdt encountered");
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
if (Array.isArray(value)) {
|
|
60
|
+
return lsonListToJson(value);
|
|
61
|
+
} else if (isPlainObject(value)) {
|
|
62
|
+
return lsonObjectToJson(value);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
return value;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function isPlainObject(obj) {
|
|
69
|
+
return obj !== null && Object.prototype.toString.call(obj) === "[object Object]";
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function anyToCrdt(obj) {
|
|
73
|
+
if (obj == null) {
|
|
74
|
+
return obj;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
if (Array.isArray(obj)) {
|
|
78
|
+
return new LiveObject.LiveList(obj.map(anyToCrdt));
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
if (isPlainObject(obj)) {
|
|
82
|
+
var init = {};
|
|
83
|
+
|
|
84
|
+
for (var _key3 in obj) {
|
|
85
|
+
init[_key3] = anyToCrdt(obj[_key3]);
|
|
97
86
|
}
|
|
87
|
+
|
|
88
|
+
return new LiveObject.LiveObject(init);
|
|
98
89
|
}
|
|
99
90
|
|
|
100
|
-
return
|
|
91
|
+
return obj;
|
|
101
92
|
}
|
|
102
93
|
|
|
103
|
-
function
|
|
104
|
-
var
|
|
105
|
-
var
|
|
94
|
+
function patchLiveList(liveList, prev, next) {
|
|
95
|
+
var i = 0;
|
|
96
|
+
var prevEnd = prev.length - 1;
|
|
97
|
+
var nextEnd = next.length - 1;
|
|
98
|
+
var prevNode = prev[0];
|
|
99
|
+
var nextNode = next[0];
|
|
106
100
|
|
|
107
|
-
|
|
108
|
-
|
|
101
|
+
outer: {
|
|
102
|
+
while (prevNode === nextNode) {
|
|
103
|
+
++i;
|
|
109
104
|
|
|
110
|
-
|
|
111
|
-
|
|
105
|
+
if (i > prevEnd || i > nextEnd) {
|
|
106
|
+
break outer;
|
|
107
|
+
}
|
|
112
108
|
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
109
|
+
prevNode = prev[i];
|
|
110
|
+
nextNode = next[i];
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
prevNode = prev[prevEnd];
|
|
114
|
+
nextNode = next[nextEnd];
|
|
115
|
+
|
|
116
|
+
while (prevNode === nextNode) {
|
|
117
|
+
prevEnd--;
|
|
118
|
+
nextEnd--;
|
|
119
|
+
|
|
120
|
+
if (i > prevEnd || i > nextEnd) {
|
|
121
|
+
break outer;
|
|
116
122
|
}
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
123
|
+
|
|
124
|
+
prevNode = prev[prevEnd];
|
|
125
|
+
nextNode = next[nextEnd];
|
|
120
126
|
}
|
|
121
127
|
}
|
|
122
128
|
|
|
123
|
-
|
|
124
|
-
|
|
129
|
+
if (i > prevEnd) {
|
|
130
|
+
if (i <= nextEnd) {
|
|
131
|
+
while (i <= nextEnd) {
|
|
132
|
+
liveList.insert(anyToCrdt(next[i]), i);
|
|
133
|
+
i++;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
} else if (i > nextEnd) {
|
|
137
|
+
var localI = i;
|
|
125
138
|
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
139
|
+
while (localI <= prevEnd) {
|
|
140
|
+
liveList.delete(i);
|
|
141
|
+
localI++;
|
|
142
|
+
}
|
|
143
|
+
} else {
|
|
144
|
+
while (i <= prevEnd && i <= nextEnd) {
|
|
145
|
+
prevNode = prev[i];
|
|
146
|
+
nextNode = next[i];
|
|
147
|
+
var liveListNode = liveList.get(i);
|
|
148
|
+
|
|
149
|
+
if (liveListNode instanceof LiveObject.LiveObject && isPlainObject(prevNode) && isPlainObject(nextNode)) {
|
|
150
|
+
patchLiveObject(liveListNode, prevNode, nextNode);
|
|
151
|
+
} else {
|
|
152
|
+
liveList.set(i, anyToCrdt(nextNode));
|
|
153
|
+
}
|
|
129
154
|
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
var afterDigit = after[index] || max;
|
|
155
|
+
i++;
|
|
156
|
+
}
|
|
133
157
|
|
|
134
|
-
|
|
135
|
-
|
|
158
|
+
while (i <= nextEnd) {
|
|
159
|
+
liveList.insert(anyToCrdt(next[i]), i);
|
|
160
|
+
i++;
|
|
136
161
|
}
|
|
137
162
|
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
163
|
+
var _localI = i;
|
|
164
|
+
|
|
165
|
+
while (_localI <= prevEnd) {
|
|
166
|
+
liveList.delete(i);
|
|
167
|
+
_localI++;
|
|
142
168
|
}
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
function patchLiveObjectKey(liveObject, key, prev, next) {
|
|
172
|
+
if (process.env.NODE_ENV !== "production") {
|
|
173
|
+
var nonSerializableValue = LiveObject.findNonSerializableValue(next);
|
|
143
174
|
|
|
144
|
-
if (
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
break;
|
|
175
|
+
if (nonSerializableValue) {
|
|
176
|
+
console.error("New state path: '" + nonSerializableValue.path + "' value: '" + nonSerializableValue.value + "' is not serializable.\nOnly serializable value can be synced with Liveblocks.");
|
|
177
|
+
return;
|
|
148
178
|
}
|
|
179
|
+
}
|
|
149
180
|
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
181
|
+
var value = liveObject.get(key);
|
|
182
|
+
|
|
183
|
+
if (next === undefined) {
|
|
184
|
+
liveObject.delete(key);
|
|
185
|
+
} else if (value === undefined) {
|
|
186
|
+
liveObject.set(key, anyToCrdt(next));
|
|
187
|
+
} else if (prev === next) {
|
|
188
|
+
return;
|
|
189
|
+
} else if (value instanceof LiveObject.LiveList && Array.isArray(prev) && Array.isArray(next)) {
|
|
190
|
+
patchLiveList(value, prev, next);
|
|
191
|
+
} else if (value instanceof LiveObject.LiveObject && isPlainObject(prev) && isPlainObject(next)) {
|
|
192
|
+
patchLiveObject(value, prev, next);
|
|
193
|
+
} else {
|
|
194
|
+
liveObject.set(key, anyToCrdt(next));
|
|
153
195
|
}
|
|
196
|
+
}
|
|
197
|
+
function patchLiveObject(root, prev, next) {
|
|
198
|
+
var updates = {};
|
|
154
199
|
|
|
155
|
-
|
|
200
|
+
for (var _key4 in next) {
|
|
201
|
+
patchLiveObjectKey(root, _key4, prev[_key4], next[_key4]);
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
for (var _key5 in prev) {
|
|
205
|
+
if (next[_key5] === undefined) {
|
|
206
|
+
root.delete(_key5);
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
if (Object.keys(updates).length > 0) {
|
|
211
|
+
root.update(updates);
|
|
212
|
+
}
|
|
156
213
|
}
|
|
157
214
|
|
|
158
|
-
function
|
|
159
|
-
var
|
|
215
|
+
function getParentsPath(node) {
|
|
216
|
+
var path = [];
|
|
160
217
|
|
|
161
|
-
|
|
162
|
-
|
|
218
|
+
while (node._parentKey != null && node._parent != null) {
|
|
219
|
+
if (node._parent instanceof LiveObject.LiveList) {
|
|
220
|
+
path.push(node._parent._indexOfPosition(node._parentKey));
|
|
221
|
+
} else {
|
|
222
|
+
path.push(node._parentKey);
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
node = node._parent;
|
|
163
226
|
}
|
|
164
227
|
|
|
165
|
-
return
|
|
228
|
+
return path;
|
|
166
229
|
}
|
|
167
|
-
|
|
168
|
-
|
|
230
|
+
|
|
231
|
+
function patchImmutableObject(state, updates) {
|
|
232
|
+
return updates.reduce(function (state, update) {
|
|
233
|
+
return patchImmutableObjectWithUpdate(state, update);
|
|
234
|
+
}, state);
|
|
169
235
|
}
|
|
170
|
-
function compare(posA, posB) {
|
|
171
|
-
var aCodes = posCodes(posA);
|
|
172
|
-
var bCodes = posCodes(posB);
|
|
173
|
-
var maxLength = Math.max(aCodes.length, bCodes.length);
|
|
174
236
|
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
237
|
+
function patchImmutableObjectWithUpdate(state, update) {
|
|
238
|
+
var path = getParentsPath(update.node);
|
|
239
|
+
return patchImmutableNode(state, path, update);
|
|
240
|
+
}
|
|
178
241
|
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
242
|
+
function patchImmutableNode(state, path, update) {
|
|
243
|
+
var pathItem = path.pop();
|
|
244
|
+
|
|
245
|
+
if (pathItem === undefined) {
|
|
246
|
+
switch (update.type) {
|
|
247
|
+
case "LiveObject":
|
|
248
|
+
{
|
|
249
|
+
if (typeof state !== "object") {
|
|
250
|
+
throw new Error("Internal: received update on LiveObject but state was not an object");
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
var newState = Object.assign({}, state);
|
|
254
|
+
|
|
255
|
+
for (var _key6 in update.updates) {
|
|
256
|
+
var _update$updates$_key, _update$updates$_key2;
|
|
257
|
+
|
|
258
|
+
if (((_update$updates$_key = update.updates[_key6]) == null ? void 0 : _update$updates$_key.type) === "update") {
|
|
259
|
+
var val = update.node.get(_key6);
|
|
260
|
+
|
|
261
|
+
if (val !== undefined) {
|
|
262
|
+
newState[_key6] = lsonToJson(val);
|
|
263
|
+
}
|
|
264
|
+
} else if (((_update$updates$_key2 = update.updates[_key6]) == null ? void 0 : _update$updates$_key2.type) === "delete") {
|
|
265
|
+
delete newState[_key6];
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
return newState;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
case "LiveList":
|
|
273
|
+
{
|
|
274
|
+
if (Array.isArray(state) === false) {
|
|
275
|
+
throw new Error("Internal: received update on LiveList but state was not an array");
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
var _newState = state.map(function (x) {
|
|
279
|
+
return x;
|
|
280
|
+
});
|
|
281
|
+
|
|
282
|
+
var _loop = function _loop() {
|
|
283
|
+
var listUpdate = _step2.value;
|
|
284
|
+
|
|
285
|
+
if (listUpdate.type === "set") {
|
|
286
|
+
_newState = _newState.map(function (item, index) {
|
|
287
|
+
return index === listUpdate.index ? listUpdate.item : item;
|
|
288
|
+
});
|
|
289
|
+
} else if (listUpdate.type === "insert") {
|
|
290
|
+
if (listUpdate.index === _newState.length) {
|
|
291
|
+
_newState.push(lsonToJson(listUpdate.item));
|
|
292
|
+
} else {
|
|
293
|
+
_newState = [].concat(_newState.slice(0, listUpdate.index), [lsonToJson(listUpdate.item)], _newState.slice(listUpdate.index));
|
|
294
|
+
}
|
|
295
|
+
} else if (listUpdate.type === "delete") {
|
|
296
|
+
_newState.splice(listUpdate.index, 1);
|
|
297
|
+
} else if (listUpdate.type === "move") {
|
|
298
|
+
if (listUpdate.previousIndex > listUpdate.index) {
|
|
299
|
+
_newState = [].concat(_newState.slice(0, listUpdate.index), [lsonToJson(listUpdate.item)], _newState.slice(listUpdate.index, listUpdate.previousIndex), _newState.slice(listUpdate.previousIndex + 1));
|
|
300
|
+
} else {
|
|
301
|
+
_newState = [].concat(_newState.slice(0, listUpdate.previousIndex), _newState.slice(listUpdate.previousIndex + 1, listUpdate.index + 1), [lsonToJson(listUpdate.item)], _newState.slice(listUpdate.index + 1));
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
};
|
|
305
|
+
|
|
306
|
+
for (var _iterator2 = LiveObject._createForOfIteratorHelperLoose(update.updates), _step2; !(_step2 = _iterator2()).done;) {
|
|
307
|
+
_loop();
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
return _newState;
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
case "LiveMap":
|
|
314
|
+
{
|
|
315
|
+
if (typeof state !== "object") {
|
|
316
|
+
throw new Error("Internal: received update on LiveMap but state was not an object");
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
var _newState2 = Object.assign({}, state);
|
|
320
|
+
|
|
321
|
+
for (var _key7 in update.updates) {
|
|
322
|
+
var _update$updates$_key3, _update$updates$_key4;
|
|
323
|
+
|
|
324
|
+
if (((_update$updates$_key3 = update.updates[_key7]) == null ? void 0 : _update$updates$_key3.type) === "update") {
|
|
325
|
+
_newState2[_key7] = lsonToJson(update.node.get(_key7));
|
|
326
|
+
} else if (((_update$updates$_key4 = update.updates[_key7]) == null ? void 0 : _update$updates$_key4.type) === "delete") {
|
|
327
|
+
delete _newState2[_key7];
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
return _newState2;
|
|
332
|
+
}
|
|
183
333
|
}
|
|
184
334
|
}
|
|
185
335
|
|
|
186
|
-
|
|
336
|
+
if (Array.isArray(state)) {
|
|
337
|
+
var newArray = [].concat(state);
|
|
338
|
+
newArray[pathItem] = patchImmutableNode(state[pathItem], path, update);
|
|
339
|
+
return newArray;
|
|
340
|
+
} else {
|
|
341
|
+
var _extends2;
|
|
342
|
+
|
|
343
|
+
return LiveObject._extends({}, state, (_extends2 = {}, _extends2[pathItem] = patchImmutableNode(state[pathItem], path, update), _extends2));
|
|
344
|
+
}
|
|
187
345
|
}
|
|
188
346
|
|
|
189
|
-
exports
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
exports
|
|
194
|
-
|
|
347
|
+
Object.defineProperty(exports, 'ClientMessageType', {
|
|
348
|
+
enumerable: true,
|
|
349
|
+
get: function () { return LiveObject.ClientMessageType; }
|
|
350
|
+
});
|
|
351
|
+
Object.defineProperty(exports, 'CrdtType', {
|
|
352
|
+
enumerable: true,
|
|
353
|
+
get: function () { return LiveObject.CrdtType; }
|
|
354
|
+
});
|
|
355
|
+
Object.defineProperty(exports, 'OpType', {
|
|
356
|
+
enumerable: true,
|
|
357
|
+
get: function () { return LiveObject.OpType; }
|
|
358
|
+
});
|
|
359
|
+
Object.defineProperty(exports, 'ServerMessageType', {
|
|
360
|
+
enumerable: true,
|
|
361
|
+
get: function () { return LiveObject.ServerMessageType; }
|
|
362
|
+
});
|
|
363
|
+
exports.deprecate = LiveObject.deprecate;
|
|
364
|
+
exports.deprecateIf = LiveObject.deprecateIf;
|
|
365
|
+
exports.lsonToJson = lsonToJson;
|
|
366
|
+
exports.patchImmutableObject = patchImmutableObject;
|
|
367
|
+
exports.patchLiveObjectKey = patchLiveObjectKey;
|