@liveblocks/client 0.15.0-alpha.2 → 0.15.0

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.
Files changed (62) hide show
  1. package/lib/esm/index.js +2991 -5
  2. package/lib/esm/index.mjs +2991 -0
  3. package/lib/esm/internal.js +149 -0
  4. package/lib/esm/internal.mjs +149 -0
  5. package/lib/{esm/types.d.ts → index.d.ts} +255 -50
  6. package/lib/index.js +4237 -0
  7. package/lib/{esm/live.d.ts → internal.d.ts} +46 -34
  8. package/lib/internal.js +193 -0
  9. package/package.json +36 -9
  10. package/lib/cjs/AbstractCrdt.d.ts +0 -67
  11. package/lib/cjs/AbstractCrdt.js +0 -95
  12. package/lib/cjs/LiveList.d.ts +0 -144
  13. package/lib/cjs/LiveList.js +0 -527
  14. package/lib/cjs/LiveMap.d.ts +0 -91
  15. package/lib/cjs/LiveMap.js +0 -322
  16. package/lib/cjs/LiveObject.d.ts +0 -80
  17. package/lib/cjs/LiveObject.js +0 -453
  18. package/lib/cjs/LiveRegister.d.ts +0 -29
  19. package/lib/cjs/LiveRegister.js +0 -88
  20. package/lib/cjs/authentication.d.ts +0 -3
  21. package/lib/cjs/authentication.js +0 -71
  22. package/lib/cjs/client.d.ts +0 -27
  23. package/lib/cjs/client.js +0 -80
  24. package/lib/cjs/immutable.d.ts +0 -9
  25. package/lib/cjs/immutable.js +0 -291
  26. package/lib/cjs/index.d.ts +0 -6
  27. package/lib/cjs/index.js +0 -18
  28. package/lib/cjs/live.d.ts +0 -181
  29. package/lib/cjs/live.js +0 -49
  30. package/lib/cjs/position.d.ts +0 -6
  31. package/lib/cjs/position.js +0 -113
  32. package/lib/cjs/room.d.ts +0 -159
  33. package/lib/cjs/room.js +0 -1094
  34. package/lib/cjs/types.d.ts +0 -489
  35. package/lib/cjs/types.js +0 -2
  36. package/lib/cjs/utils.d.ts +0 -11
  37. package/lib/cjs/utils.js +0 -175
  38. package/lib/esm/AbstractCrdt.d.ts +0 -67
  39. package/lib/esm/AbstractCrdt.js +0 -91
  40. package/lib/esm/LiveList.d.ts +0 -144
  41. package/lib/esm/LiveList.js +0 -523
  42. package/lib/esm/LiveMap.d.ts +0 -91
  43. package/lib/esm/LiveMap.js +0 -318
  44. package/lib/esm/LiveObject.d.ts +0 -80
  45. package/lib/esm/LiveObject.js +0 -449
  46. package/lib/esm/LiveRegister.d.ts +0 -29
  47. package/lib/esm/LiveRegister.js +0 -84
  48. package/lib/esm/authentication.d.ts +0 -3
  49. package/lib/esm/authentication.js +0 -66
  50. package/lib/esm/client.d.ts +0 -27
  51. package/lib/esm/client.js +0 -76
  52. package/lib/esm/immutable.d.ts +0 -9
  53. package/lib/esm/immutable.js +0 -282
  54. package/lib/esm/index.d.ts +0 -6
  55. package/lib/esm/live.js +0 -46
  56. package/lib/esm/position.d.ts +0 -6
  57. package/lib/esm/position.js +0 -106
  58. package/lib/esm/room.d.ts +0 -159
  59. package/lib/esm/room.js +0 -1069
  60. package/lib/esm/types.js +0 -1
  61. package/lib/esm/utils.d.ts +0 -11
  62. package/lib/esm/utils.js +0 -164
package/lib/esm/client.js DELETED
@@ -1,76 +0,0 @@
1
- import { createRoom } from "./room";
2
- /**
3
- * Create a client that will be responsible to communicate with liveblocks servers.
4
- *
5
- * @example
6
- * const client = createClient({
7
- * authEndpoint: "/api/auth"
8
- * });
9
- *
10
- * // It's also possible to use a function to call your authentication endpoint.
11
- * // Useful to add additional headers or use an API wrapper (like Firebase functions)
12
- * const client = createClient({
13
- * authEndpoint: async (room) => {
14
- * const response = await fetch("/api/auth", {
15
- * method: "POST",
16
- * headers: {
17
- * Authentication: "token",
18
- * "Content-Type": "application/json"
19
- * },
20
- * body: JSON.stringify({ room })
21
- * });
22
- *
23
- * return await response.json();
24
- * }
25
- * });
26
- */
27
- export function createClient(options) {
28
- const clientOptions = options;
29
- if (typeof clientOptions.throttle === "number") {
30
- if (clientOptions.throttle < 80 || clientOptions.throttle > 1000) {
31
- throw new Error("Liveblocks client throttle should be between 80 and 1000 ms");
32
- }
33
- }
34
- const rooms = new Map();
35
- function getRoom(roomId) {
36
- const internalRoom = rooms.get(roomId);
37
- return internalRoom ? internalRoom.room : null;
38
- }
39
- function enter(roomId, options = {}) {
40
- let internalRoom = rooms.get(roomId);
41
- if (internalRoom) {
42
- return internalRoom.room;
43
- }
44
- internalRoom = createRoom(roomId, Object.assign(Object.assign({}, clientOptions), options));
45
- rooms.set(roomId, internalRoom);
46
- internalRoom.connect();
47
- return internalRoom.room;
48
- }
49
- function leave(roomId) {
50
- let room = rooms.get(roomId);
51
- if (room) {
52
- room.disconnect();
53
- rooms.delete(roomId);
54
- }
55
- }
56
- if (typeof window !== "undefined") {
57
- // TODO: Expose a way to clear these
58
- window.addEventListener("online", () => {
59
- for (const [, room] of rooms) {
60
- room.onNavigatorOnline();
61
- }
62
- });
63
- }
64
- if (typeof document !== "undefined") {
65
- document.addEventListener("visibilitychange", () => {
66
- for (const [, room] of rooms) {
67
- room.onVisibilityChange(document.visibilityState);
68
- }
69
- });
70
- }
71
- return {
72
- getRoom,
73
- enter,
74
- leave,
75
- };
76
- }
@@ -1,9 +0,0 @@
1
- import { LiveList } from "./LiveList";
2
- import { LiveObject } from "./LiveObject";
3
- import { StorageUpdate } from "./types";
4
- export declare function liveObjectToJson(liveObject: LiveObject<any>): any;
5
- export declare function liveNodeToJson(value: any): any;
6
- export declare function patchLiveList<T>(liveList: LiveList<T>, prev: Array<T>, next: Array<T>): void;
7
- export declare function patchLiveObjectKey<T>(liveObject: LiveObject<T>, key: keyof T, prev: any, next: any): void;
8
- export declare function patchLiveObject<T extends Record<string, any>>(root: LiveObject<T>, prev: T, next: T): void;
9
- export declare function patchImmutableObject<T>(state: T, updates: StorageUpdate[]): T;
@@ -1,282 +0,0 @@
1
- import { LiveList } from "./LiveList";
2
- import { LiveMap } from "./LiveMap";
3
- import { LiveObject } from "./LiveObject";
4
- import { LiveRegister } from "./LiveRegister";
5
- export function liveObjectToJson(liveObject) {
6
- const result = {};
7
- const obj = liveObject.toObject();
8
- for (const key in obj) {
9
- result[key] = liveNodeToJson(obj[key]);
10
- }
11
- return result;
12
- }
13
- function liveMapToJson(map) {
14
- const result = {};
15
- const obj = Object.fromEntries(map);
16
- for (const key in obj) {
17
- result[key] = liveNodeToJson(obj[key]);
18
- }
19
- return result;
20
- }
21
- function liveListToJson(value) {
22
- return value.toArray().map(liveNodeToJson);
23
- }
24
- export function liveNodeToJson(value) {
25
- if (value instanceof LiveObject) {
26
- return liveObjectToJson(value);
27
- }
28
- else if (value instanceof LiveList) {
29
- return liveListToJson(value);
30
- }
31
- else if (value instanceof LiveMap) {
32
- return liveMapToJson(value);
33
- }
34
- else if (value instanceof LiveRegister) {
35
- return value.data;
36
- }
37
- return value;
38
- }
39
- function isPlainObject(obj) {
40
- return Object.prototype.toString.call(obj) === "[object Object]";
41
- }
42
- function anyToCrdt(obj) {
43
- if (obj == null) {
44
- return obj;
45
- }
46
- if (Array.isArray(obj)) {
47
- return new LiveList(obj.map(anyToCrdt));
48
- }
49
- if (isPlainObject(obj)) {
50
- const init = {};
51
- for (const key in obj) {
52
- init[key] = anyToCrdt(obj[key]);
53
- }
54
- return new LiveObject(init);
55
- }
56
- return obj;
57
- }
58
- export function patchLiveList(liveList, prev, next) {
59
- let i = 0;
60
- let prevEnd = prev.length - 1;
61
- let nextEnd = next.length - 1;
62
- let prevNode = prev[0];
63
- let nextNode = next[0];
64
- /**
65
- * For A,B,C => A,B,C,D
66
- * i = 3, prevEnd = 2, nextEnd = 3
67
- *
68
- * For A,B,C => B,C
69
- * i = 2, prevEnd = 2, nextEnd = 1
70
- *
71
- * For B,C => A,B,C
72
- * i = 0, pre
73
- */
74
- outer: {
75
- while (prevNode === nextNode) {
76
- ++i;
77
- if (i > prevEnd || i > nextEnd) {
78
- break outer;
79
- }
80
- prevNode = prev[i];
81
- nextNode = next[i];
82
- }
83
- prevNode = prev[prevEnd];
84
- nextNode = next[nextEnd];
85
- while (prevNode === nextNode) {
86
- prevEnd--;
87
- nextEnd--;
88
- if (i > prevEnd || i > nextEnd) {
89
- break outer;
90
- }
91
- prevNode = prev[prevEnd];
92
- nextNode = next[nextEnd];
93
- }
94
- }
95
- if (i > prevEnd) {
96
- if (i <= nextEnd) {
97
- while (i <= nextEnd) {
98
- liveList.insert(anyToCrdt(next[i]), i);
99
- i++;
100
- }
101
- }
102
- }
103
- else if (i > nextEnd) {
104
- let localI = i;
105
- while (localI <= prevEnd) {
106
- liveList.delete(i);
107
- localI++;
108
- }
109
- }
110
- else {
111
- while (i <= prevEnd && i <= nextEnd) {
112
- prevNode = prev[i];
113
- nextNode = next[i];
114
- const liveListNode = liveList.get(i);
115
- if (liveListNode instanceof LiveObject &&
116
- isPlainObject(prevNode) &&
117
- isPlainObject(nextNode)) {
118
- patchLiveObject(liveListNode, prevNode, nextNode);
119
- }
120
- else {
121
- liveList.delete(i);
122
- liveList.insert(anyToCrdt(nextNode), i);
123
- }
124
- i++;
125
- }
126
- while (i <= nextEnd) {
127
- liveList.insert(anyToCrdt(next[i]), i);
128
- i++;
129
- }
130
- while (i <= prevEnd) {
131
- liveList.delete(i);
132
- i++;
133
- }
134
- }
135
- }
136
- export function patchLiveObjectKey(liveObject, key, prev, next) {
137
- const value = liveObject.get(key);
138
- if (next === undefined) {
139
- liveObject.delete(key);
140
- }
141
- else if (value === undefined) {
142
- liveObject.set(key, anyToCrdt(next));
143
- }
144
- else if (prev === next) {
145
- return;
146
- }
147
- else if (value instanceof LiveList &&
148
- Array.isArray(prev) &&
149
- Array.isArray(next)) {
150
- patchLiveList(value, prev, next);
151
- }
152
- else if (value instanceof LiveObject &&
153
- isPlainObject(prev) &&
154
- isPlainObject(next)) {
155
- patchLiveObject(value, prev, next);
156
- }
157
- else {
158
- liveObject.set(key, anyToCrdt(next));
159
- }
160
- }
161
- export function patchLiveObject(root, prev, next) {
162
- const updates = {};
163
- for (const key in next) {
164
- patchLiveObjectKey(root, key, prev[key], next[key]);
165
- }
166
- for (const key in prev) {
167
- if (next[key] === undefined) {
168
- root.delete(key);
169
- }
170
- }
171
- if (Object.keys(updates).length > 0) {
172
- root.update(updates);
173
- }
174
- }
175
- function getParentsPath(node) {
176
- const path = [];
177
- while (node._parentKey != null && node._parent != null) {
178
- if (node._parent instanceof LiveList) {
179
- path.push(node._parent._indexOfPosition(node._parentKey));
180
- }
181
- else {
182
- path.push(node._parentKey);
183
- }
184
- node = node._parent;
185
- }
186
- return path;
187
- }
188
- export function patchImmutableObject(state, updates) {
189
- return updates.reduce((state, update) => patchImmutableObjectWithUpdate(state, update), state);
190
- }
191
- function patchImmutableObjectWithUpdate(state, update) {
192
- const path = getParentsPath(update.node);
193
- return patchImmutableNode(state, path, update);
194
- }
195
- function patchImmutableNode(state, path, update) {
196
- var _a, _b, _c, _d;
197
- const pathItem = path.pop();
198
- if (pathItem === undefined) {
199
- switch (update.type) {
200
- case "LiveObject": {
201
- if (typeof state !== "object") {
202
- throw new Error("Internal: received update on LiveObject but state was not an object");
203
- }
204
- let newState = Object.assign({}, state);
205
- for (const key in update.updates) {
206
- if (((_a = update.updates[key]) === null || _a === void 0 ? void 0 : _a.type) === "update") {
207
- newState[key] = liveNodeToJson(update.node.get(key));
208
- }
209
- else if (((_b = update.updates[key]) === null || _b === void 0 ? void 0 : _b.type) === "delete") {
210
- delete newState[key];
211
- }
212
- }
213
- return newState;
214
- }
215
- case "LiveList": {
216
- if (Array.isArray(state) === false) {
217
- throw new Error("Internal: received update on LiveList but state was not an array");
218
- }
219
- let newState = state.map((x) => x);
220
- for (const listUpdate of update.updates) {
221
- if (listUpdate.type === "insert") {
222
- if (listUpdate.index === newState.length) {
223
- newState.push(liveNodeToJson(listUpdate.item));
224
- }
225
- else {
226
- newState = [
227
- ...newState.slice(0, listUpdate.index),
228
- liveNodeToJson(listUpdate.item),
229
- ...newState.slice(listUpdate.index),
230
- ];
231
- }
232
- }
233
- else if (listUpdate.type === "delete") {
234
- newState.splice(listUpdate.index, 1);
235
- }
236
- else if (listUpdate.type === "move") {
237
- if (listUpdate.previousIndex > listUpdate.index) {
238
- newState = [
239
- ...newState.slice(0, listUpdate.index),
240
- liveNodeToJson(listUpdate.item),
241
- ...newState.slice(listUpdate.index, listUpdate.previousIndex),
242
- ...newState.slice(listUpdate.previousIndex + 1),
243
- ];
244
- }
245
- else {
246
- newState = [
247
- ...newState.slice(0, listUpdate.previousIndex),
248
- ...newState.slice(listUpdate.previousIndex + 1, listUpdate.index + 1),
249
- liveNodeToJson(listUpdate.item),
250
- ...newState.slice(listUpdate.index + 1),
251
- ];
252
- }
253
- }
254
- }
255
- return newState;
256
- }
257
- case "LiveMap": {
258
- if (typeof state !== "object") {
259
- throw new Error("Internal: received update on LiveMap but state was not an object");
260
- }
261
- let newState = Object.assign({}, state);
262
- for (const key in update.updates) {
263
- if (((_c = update.updates[key]) === null || _c === void 0 ? void 0 : _c.type) === "update") {
264
- newState[key] = liveNodeToJson(update.node.get(key));
265
- }
266
- else if (((_d = update.updates[key]) === null || _d === void 0 ? void 0 : _d.type) === "delete") {
267
- delete newState[key];
268
- }
269
- }
270
- return newState;
271
- }
272
- }
273
- }
274
- if (Array.isArray(state)) {
275
- const newArray = [...state];
276
- newArray[pathItem] = patchImmutableNode(state[pathItem], path, update);
277
- return newArray;
278
- }
279
- else {
280
- return Object.assign(Object.assign({}, state), { [pathItem]: patchImmutableNode(state[pathItem], path, update) });
281
- }
282
- }
@@ -1,6 +0,0 @@
1
- export { LiveObject } from "./LiveObject";
2
- export { LiveMap } from "./LiveMap";
3
- export { LiveList } from "./LiveList";
4
- export type { Others, Presence, Room, Client, User, BroadcastOptions, } from "./types";
5
- export { createClient } from "./client";
6
- export { liveObjectToJson, liveNodeToJson, patchLiveList, patchImmutableObject, patchLiveObject, patchLiveObjectKey, } from "./immutable";
package/lib/esm/live.js DELETED
@@ -1,46 +0,0 @@
1
- export var ServerMessageType;
2
- (function (ServerMessageType) {
3
- ServerMessageType[ServerMessageType["UpdatePresence"] = 100] = "UpdatePresence";
4
- ServerMessageType[ServerMessageType["UserJoined"] = 101] = "UserJoined";
5
- ServerMessageType[ServerMessageType["UserLeft"] = 102] = "UserLeft";
6
- ServerMessageType[ServerMessageType["Event"] = 103] = "Event";
7
- ServerMessageType[ServerMessageType["RoomState"] = 104] = "RoomState";
8
- ServerMessageType[ServerMessageType["InitialStorageState"] = 200] = "InitialStorageState";
9
- ServerMessageType[ServerMessageType["UpdateStorage"] = 201] = "UpdateStorage";
10
- })(ServerMessageType || (ServerMessageType = {}));
11
- export var ClientMessageType;
12
- (function (ClientMessageType) {
13
- ClientMessageType[ClientMessageType["UpdatePresence"] = 100] = "UpdatePresence";
14
- ClientMessageType[ClientMessageType["ClientEvent"] = 103] = "ClientEvent";
15
- ClientMessageType[ClientMessageType["FetchStorage"] = 200] = "FetchStorage";
16
- ClientMessageType[ClientMessageType["UpdateStorage"] = 201] = "UpdateStorage";
17
- })(ClientMessageType || (ClientMessageType = {}));
18
- export var CrdtType;
19
- (function (CrdtType) {
20
- CrdtType[CrdtType["Object"] = 0] = "Object";
21
- CrdtType[CrdtType["List"] = 1] = "List";
22
- CrdtType[CrdtType["Map"] = 2] = "Map";
23
- CrdtType[CrdtType["Register"] = 3] = "Register";
24
- })(CrdtType || (CrdtType = {}));
25
- export var OpType;
26
- (function (OpType) {
27
- OpType[OpType["Init"] = 0] = "Init";
28
- OpType[OpType["SetParentKey"] = 1] = "SetParentKey";
29
- OpType[OpType["CreateList"] = 2] = "CreateList";
30
- OpType[OpType["UpdateObject"] = 3] = "UpdateObject";
31
- OpType[OpType["CreateObject"] = 4] = "CreateObject";
32
- OpType[OpType["DeleteCrdt"] = 5] = "DeleteCrdt";
33
- OpType[OpType["DeleteObjectKey"] = 6] = "DeleteObjectKey";
34
- OpType[OpType["CreateMap"] = 7] = "CreateMap";
35
- OpType[OpType["CreateRegister"] = 8] = "CreateRegister";
36
- })(OpType || (OpType = {}));
37
- export var WebsocketCloseCodes;
38
- (function (WebsocketCloseCodes) {
39
- WebsocketCloseCodes[WebsocketCloseCodes["CLOSE_ABNORMAL"] = 1006] = "CLOSE_ABNORMAL";
40
- WebsocketCloseCodes[WebsocketCloseCodes["INVALID_MESSAGE_FORMAT"] = 4000] = "INVALID_MESSAGE_FORMAT";
41
- WebsocketCloseCodes[WebsocketCloseCodes["NOT_ALLOWED"] = 4001] = "NOT_ALLOWED";
42
- WebsocketCloseCodes[WebsocketCloseCodes["MAX_NUMBER_OF_MESSAGES_PER_SECONDS"] = 4002] = "MAX_NUMBER_OF_MESSAGES_PER_SECONDS";
43
- WebsocketCloseCodes[WebsocketCloseCodes["MAX_NUMBER_OF_CONCURRENT_CONNECTIONS"] = 4003] = "MAX_NUMBER_OF_CONCURRENT_CONNECTIONS";
44
- WebsocketCloseCodes[WebsocketCloseCodes["MAX_NUMBER_OF_MESSAGES_PER_DAY_PER_APP"] = 4004] = "MAX_NUMBER_OF_MESSAGES_PER_DAY_PER_APP";
45
- WebsocketCloseCodes[WebsocketCloseCodes["MAX_NUMBER_OF_CONCURRENT_CONNECTIONS_PER_ROOM"] = 4005] = "MAX_NUMBER_OF_CONCURRENT_CONNECTIONS_PER_ROOM";
46
- })(WebsocketCloseCodes || (WebsocketCloseCodes = {}));
@@ -1,6 +0,0 @@
1
- export declare const min = 32;
2
- export declare const max = 126;
3
- export declare function makePosition(before?: string, after?: string): string;
4
- export declare function posCodes(str: string): number[];
5
- export declare function pos(codes: number[]): string;
6
- export declare function compare(posA: string, posB: string): number;
@@ -1,106 +0,0 @@
1
- export const min = 32;
2
- export const max = 126;
3
- export function makePosition(before, after) {
4
- // No children
5
- if (before == null && after == null) {
6
- return pos([min + 1]);
7
- }
8
- // Insert at the end
9
- if (before != null && after == null) {
10
- return getNextPosition(before);
11
- }
12
- // Insert at the start
13
- if (before == null && after != null) {
14
- return getPreviousPosition(after);
15
- }
16
- return pos(makePositionFromCodes(posCodes(before), posCodes(after)));
17
- }
18
- function getPreviousPosition(after) {
19
- const result = [];
20
- const afterCodes = posCodes(after);
21
- for (let i = 0; i < afterCodes.length; i++) {
22
- const code = afterCodes[i];
23
- if (code <= min + 1) {
24
- result.push(min);
25
- if (afterCodes.length - 1 === i) {
26
- result.push(max);
27
- break;
28
- }
29
- }
30
- else {
31
- result.push(code - 1);
32
- break;
33
- }
34
- }
35
- return pos(result);
36
- }
37
- function getNextPosition(before) {
38
- const result = [];
39
- const beforeCodes = posCodes(before);
40
- for (let i = 0; i < beforeCodes.length; i++) {
41
- const code = beforeCodes[i];
42
- if (code === max) {
43
- result.push(code);
44
- if (beforeCodes.length - 1 === i) {
45
- result.push(min + 1);
46
- break;
47
- }
48
- }
49
- else {
50
- result.push(code + 1);
51
- break;
52
- }
53
- }
54
- return pos(result);
55
- }
56
- function makePositionFromCodes(before, after) {
57
- let index = 0;
58
- const result = [];
59
- while (true) {
60
- const beforeDigit = before[index] || min;
61
- const afterDigit = after[index] || max;
62
- if (beforeDigit > afterDigit) {
63
- throw new Error(`Impossible to generate position between ${before} and ${after}`);
64
- }
65
- if (beforeDigit === afterDigit) {
66
- result.push(beforeDigit);
67
- index++;
68
- continue;
69
- }
70
- if (afterDigit - beforeDigit === 1) {
71
- result.push(beforeDigit);
72
- result.push(...makePositionFromCodes(before.slice(index + 1), []));
73
- break;
74
- }
75
- const mid = (afterDigit + beforeDigit) >> 1;
76
- result.push(mid);
77
- break;
78
- }
79
- return result;
80
- }
81
- export function posCodes(str) {
82
- const codes = [];
83
- for (let i = 0; i < str.length; i++) {
84
- codes.push(str.charCodeAt(i));
85
- }
86
- return codes;
87
- }
88
- export function pos(codes) {
89
- return String.fromCharCode(...codes);
90
- }
91
- export function compare(posA, posB) {
92
- const aCodes = posCodes(posA);
93
- const bCodes = posCodes(posB);
94
- const maxLength = Math.max(aCodes.length, bCodes.length);
95
- for (let i = 0; i < maxLength; i++) {
96
- const a = aCodes[i] == null ? min : aCodes[i];
97
- const b = bCodes[i] == null ? min : bCodes[i];
98
- if (a === b) {
99
- continue;
100
- }
101
- else {
102
- return a - b;
103
- }
104
- }
105
- throw new Error(`Impossible to compare similar position "${posA}" and "${posB}"`);
106
- }
package/lib/esm/room.d.ts DELETED
@@ -1,159 +0,0 @@
1
- import { Others, Presence, ClientOptions, Room, MyPresenceCallback, OthersEventCallback, AuthEndpoint, EventCallback, User, Connection, ErrorCallback, AuthenticationToken, ConnectionCallback, StorageCallback, StorageUpdate, BroadcastOptions } from "./types";
2
- import { ClientMessage, Op } from "./live";
3
- import { LiveMap } from "./LiveMap";
4
- import { LiveObject } from "./LiveObject";
5
- import { LiveList } from "./LiveList";
6
- import { AbstractCrdt } from "./AbstractCrdt";
7
- declare type HistoryItem = Array<Op | {
8
- type: "presence";
9
- data: Presence;
10
- }>;
11
- declare type IdFactory = () => string;
12
- export declare type State = {
13
- connection: Connection;
14
- lastConnectionId: number | null;
15
- socket: WebSocket | null;
16
- lastFlushTime: number;
17
- buffer: {
18
- presence: Presence | null;
19
- messages: ClientMessage[];
20
- storageOperations: Op[];
21
- };
22
- timeoutHandles: {
23
- flush: number | null;
24
- reconnect: number;
25
- pongTimeout: number;
26
- };
27
- intervalHandles: {
28
- heartbeat: number;
29
- };
30
- listeners: {
31
- event: EventCallback[];
32
- others: OthersEventCallback[];
33
- "my-presence": MyPresenceCallback[];
34
- error: ErrorCallback[];
35
- connection: ConnectionCallback[];
36
- storage: StorageCallback[];
37
- };
38
- me: Presence;
39
- others: Others;
40
- users: {
41
- [connectionId: number]: User;
42
- };
43
- idFactory: IdFactory | null;
44
- numberOfRetry: number;
45
- defaultStorageRoot?: {
46
- [key: string]: any;
47
- };
48
- clock: number;
49
- opClock: number;
50
- items: Map<string, AbstractCrdt>;
51
- root: LiveObject | undefined;
52
- undoStack: HistoryItem[];
53
- redoStack: HistoryItem[];
54
- isHistoryPaused: boolean;
55
- pausedHistory: HistoryItem;
56
- isBatching: boolean;
57
- batch: {
58
- ops: Op[];
59
- reverseOps: HistoryItem;
60
- updates: {
61
- others: [];
62
- presence: boolean;
63
- storageUpdates: Map<string, StorageUpdate>;
64
- };
65
- };
66
- offlineOperations: Map<string, Op>;
67
- };
68
- export declare type Effects = {
69
- authenticate(): void;
70
- send(messages: ClientMessage[]): void;
71
- delayFlush(delay: number): number;
72
- startHeartbeatInterval(): number;
73
- schedulePongTimeout(): number;
74
- scheduleReconnect(delay: number): number;
75
- };
76
- declare type Context = {
77
- room: string;
78
- authEndpoint: AuthEndpoint;
79
- liveblocksServer: string;
80
- throttleDelay: number;
81
- publicApiKey?: string;
82
- };
83
- export declare function makeStateMachine(state: State, context: Context, mockedEffects?: Effects): {
84
- onOpen: () => void;
85
- onClose: (event: {
86
- code: number;
87
- wasClean: boolean;
88
- reason: any;
89
- }) => void;
90
- onMessage: (event: MessageEvent) => void;
91
- authenticationSuccess: (token: AuthenticationToken, socket: WebSocket) => void;
92
- heartbeat: () => void;
93
- onNavigatorOnline: () => void;
94
- simulateSocketClose: () => void;
95
- simulateSendCloseEvent: (event: {
96
- code: number;
97
- wasClean: boolean;
98
- reason: any;
99
- }) => void;
100
- onVisibilityChange: (visibilityState: VisibilityState) => void;
101
- getUndoStack: () => HistoryItem[];
102
- getItemsCount: () => number;
103
- connect: () => null | undefined;
104
- disconnect: () => void;
105
- subscribe: {
106
- (callback: (updates: StorageUpdate) => void): () => void;
107
- <TKey extends string, TValue>(liveMap: LiveMap<TKey, TValue>, callback: (liveMap: LiveMap<TKey, TValue>) => void): () => void;
108
- <TData>(liveObject: LiveObject<TData>, callback: (liveObject: LiveObject<TData>) => void): () => void;
109
- <TItem>(liveList: LiveList<TItem>, callback: (liveList: LiveList<TItem>) => void): () => void;
110
- <TItem_1 extends AbstractCrdt>(node: TItem_1, callback: (updates: StorageUpdate[]) => void, options: {
111
- isDeep: true;
112
- }): () => void;
113
- <T extends Presence>(type: "my-presence", listener: MyPresenceCallback<T>): () => void;
114
- <T_1 extends Presence>(type: "others", listener: OthersEventCallback<T_1>): () => void;
115
- (type: "event", listener: EventCallback): () => void;
116
- (type: "error", listener: ErrorCallback): () => void;
117
- (type: "connection", listener: ConnectionCallback): () => void;
118
- };
119
- unsubscribe: {
120
- <T_2 extends Presence>(type: "my-presence", listener: MyPresenceCallback<T_2>): void;
121
- <T_3 extends Presence>(type: "others", listener: OthersEventCallback<T_3>): void;
122
- (type: "event", listener: EventCallback): void;
123
- (type: "error", listener: ErrorCallback): void;
124
- (type: "connection", listener: ConnectionCallback): void;
125
- };
126
- updatePresence: <T_4 extends Presence>(overrides: Partial<T_4>, options?: {
127
- addToHistory: boolean;
128
- } | undefined) => void;
129
- broadcastEvent: (event: any, options?: BroadcastOptions) => void;
130
- batch: (callback: () => void) => void;
131
- undo: () => void;
132
- redo: () => void;
133
- pauseHistory: () => void;
134
- resumeHistory: () => void;
135
- getStorage: <TRoot>() => Promise<{
136
- root: LiveObject<TRoot>;
137
- }>;
138
- selectors: {
139
- getConnectionState: () => "failed" | "closed" | "connecting" | "open" | "authenticating" | "unavailable";
140
- getSelf: <TPresence extends Presence = Presence>() => User<TPresence> | null;
141
- getPresence: <T_5 extends Presence>() => T_5;
142
- getOthers: <T_6 extends Presence>() => Others<T_6>;
143
- };
144
- };
145
- export declare function defaultState(me?: Presence, defaultStorageRoot?: {
146
- [key: string]: any;
147
- }): State;
148
- export declare type InternalRoom = {
149
- room: Room;
150
- connect: () => void;
151
- disconnect: () => void;
152
- onNavigatorOnline: () => void;
153
- onVisibilityChange: (visibilityState: VisibilityState) => void;
154
- };
155
- export declare function createRoom(name: string, options: ClientOptions & {
156
- defaultPresence?: Presence;
157
- defaultStorageRoot?: Record<string, any>;
158
- }): InternalRoom;
159
- export {};