@liveblocks/client 0.15.0-alpha.3 → 0.15.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.
Files changed (59) hide show
  1. package/README.md +5 -10
  2. package/lib/esm/index.js +2991 -5
  3. package/lib/esm/index.mjs +2991 -0
  4. package/lib/esm/internal.js +149 -0
  5. package/lib/esm/internal.mjs +149 -0
  6. package/lib/{esm/types.d.ts → index.d.ts} +253 -61
  7. package/lib/index.js +4237 -0
  8. package/lib/{esm/live.d.ts → internal.d.ts} +46 -34
  9. package/lib/internal.js +193 -0
  10. package/package.json +32 -10
  11. package/lib/cjs/AbstractCrdt.d.ts +0 -68
  12. package/lib/cjs/AbstractCrdt.js +0 -95
  13. package/lib/cjs/LiveList.d.ts +0 -144
  14. package/lib/cjs/LiveList.js +0 -530
  15. package/lib/cjs/LiveMap.d.ts +0 -91
  16. package/lib/cjs/LiveMap.js +0 -325
  17. package/lib/cjs/LiveObject.d.ts +0 -80
  18. package/lib/cjs/LiveObject.js +0 -485
  19. package/lib/cjs/LiveRegister.d.ts +0 -29
  20. package/lib/cjs/LiveRegister.js +0 -88
  21. package/lib/cjs/client.d.ts +0 -27
  22. package/lib/cjs/client.js +0 -123
  23. package/lib/cjs/immutable.d.ts +0 -9
  24. package/lib/cjs/immutable.js +0 -299
  25. package/lib/cjs/index.d.ts +0 -6
  26. package/lib/cjs/index.js +0 -18
  27. package/lib/cjs/live.d.ts +0 -181
  28. package/lib/cjs/live.js +0 -49
  29. package/lib/cjs/position.d.ts +0 -6
  30. package/lib/cjs/position.js +0 -113
  31. package/lib/cjs/room.d.ts +0 -159
  32. package/lib/cjs/room.js +0 -1129
  33. package/lib/cjs/types.d.ts +0 -502
  34. package/lib/cjs/types.js +0 -2
  35. package/lib/cjs/utils.d.ts +0 -15
  36. package/lib/cjs/utils.js +0 -225
  37. package/lib/esm/AbstractCrdt.d.ts +0 -68
  38. package/lib/esm/AbstractCrdt.js +0 -91
  39. package/lib/esm/LiveList.d.ts +0 -144
  40. package/lib/esm/LiveList.js +0 -526
  41. package/lib/esm/LiveMap.d.ts +0 -91
  42. package/lib/esm/LiveMap.js +0 -321
  43. package/lib/esm/LiveObject.d.ts +0 -80
  44. package/lib/esm/LiveObject.js +0 -481
  45. package/lib/esm/LiveRegister.d.ts +0 -29
  46. package/lib/esm/LiveRegister.js +0 -84
  47. package/lib/esm/client.d.ts +0 -27
  48. package/lib/esm/client.js +0 -119
  49. package/lib/esm/immutable.d.ts +0 -9
  50. package/lib/esm/immutable.js +0 -290
  51. package/lib/esm/index.d.ts +0 -6
  52. package/lib/esm/live.js +0 -46
  53. package/lib/esm/position.d.ts +0 -6
  54. package/lib/esm/position.js +0 -106
  55. package/lib/esm/room.d.ts +0 -159
  56. package/lib/esm/room.js +0 -1123
  57. package/lib/esm/types.js +0 -1
  58. package/lib/esm/utils.d.ts +0 -15
  59. package/lib/esm/utils.js +0 -213
package/lib/cjs/utils.js DELETED
@@ -1,225 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.findNonSerializableValue = exports.mergeStorageUpdates = exports.getTreesDiffOperations = exports.selfOrRegister = exports.selfOrRegisterValue = exports.isCrdt = exports.deserialize = exports.isSameNodeOrChildOf = exports.remove = void 0;
4
- const live_1 = require("./live");
5
- const LiveList_1 = require("./LiveList");
6
- const LiveMap_1 = require("./LiveMap");
7
- const LiveObject_1 = require("./LiveObject");
8
- const LiveRegister_1 = require("./LiveRegister");
9
- function remove(array, item) {
10
- for (let i = 0; i < array.length; i++) {
11
- if (array[i] === item) {
12
- array.splice(i, 1);
13
- break;
14
- }
15
- }
16
- }
17
- exports.remove = remove;
18
- function isSameNodeOrChildOf(node, parent) {
19
- if (node === parent) {
20
- return true;
21
- }
22
- if (node._parent) {
23
- return isSameNodeOrChildOf(node._parent, parent);
24
- }
25
- return false;
26
- }
27
- exports.isSameNodeOrChildOf = isSameNodeOrChildOf;
28
- function deserialize(entry, parentToChildren, doc) {
29
- switch (entry[1].type) {
30
- case live_1.CrdtType.Object: {
31
- return LiveObject_1.LiveObject._deserialize(entry, parentToChildren, doc);
32
- }
33
- case live_1.CrdtType.List: {
34
- return LiveList_1.LiveList._deserialize(entry, parentToChildren, doc);
35
- }
36
- case live_1.CrdtType.Map: {
37
- return LiveMap_1.LiveMap._deserialize(entry, parentToChildren, doc);
38
- }
39
- case live_1.CrdtType.Register: {
40
- return LiveRegister_1.LiveRegister._deserialize(entry, parentToChildren, doc);
41
- }
42
- default: {
43
- throw new Error("Unexpected CRDT type");
44
- }
45
- }
46
- }
47
- exports.deserialize = deserialize;
48
- function isCrdt(obj) {
49
- return (obj instanceof LiveObject_1.LiveObject ||
50
- obj instanceof LiveMap_1.LiveMap ||
51
- obj instanceof LiveList_1.LiveList ||
52
- obj instanceof LiveRegister_1.LiveRegister);
53
- }
54
- exports.isCrdt = isCrdt;
55
- function selfOrRegisterValue(obj) {
56
- if (obj instanceof LiveRegister_1.LiveRegister) {
57
- return obj.data;
58
- }
59
- return obj;
60
- }
61
- exports.selfOrRegisterValue = selfOrRegisterValue;
62
- function selfOrRegister(obj) {
63
- if (obj instanceof LiveObject_1.LiveObject ||
64
- obj instanceof LiveMap_1.LiveMap ||
65
- obj instanceof LiveList_1.LiveList) {
66
- return obj;
67
- }
68
- else if (obj instanceof LiveRegister_1.LiveRegister) {
69
- throw new Error("Internal error. LiveRegister should not be created from selfOrRegister");
70
- }
71
- else {
72
- return new LiveRegister_1.LiveRegister(obj);
73
- }
74
- }
75
- exports.selfOrRegister = selfOrRegister;
76
- function getTreesDiffOperations(currentItems, newItems) {
77
- const ops = [];
78
- currentItems.forEach((_, id) => {
79
- if (!newItems.get(id)) {
80
- // Delete crdt
81
- ops.push({
82
- type: live_1.OpType.DeleteCrdt,
83
- id: id,
84
- });
85
- }
86
- });
87
- newItems.forEach((crdt, id) => {
88
- const currentCrdt = currentItems.get(id);
89
- if (currentCrdt) {
90
- if (crdt.type === live_1.CrdtType.Object) {
91
- if (JSON.stringify(crdt.data) !==
92
- JSON.stringify(currentCrdt.data)) {
93
- ops.push({
94
- type: live_1.OpType.UpdateObject,
95
- id: id,
96
- data: crdt.data,
97
- });
98
- }
99
- }
100
- if (crdt.parentKey !== currentCrdt.parentKey) {
101
- ops.push({
102
- type: live_1.OpType.SetParentKey,
103
- id: id,
104
- parentKey: crdt.parentKey,
105
- });
106
- }
107
- }
108
- else {
109
- // new Crdt
110
- switch (crdt.type) {
111
- case live_1.CrdtType.Register:
112
- ops.push({
113
- type: live_1.OpType.CreateRegister,
114
- id: id,
115
- parentId: crdt.parentId,
116
- parentKey: crdt.parentKey,
117
- data: crdt.data,
118
- });
119
- break;
120
- case live_1.CrdtType.List:
121
- ops.push({
122
- type: live_1.OpType.CreateList,
123
- id: id,
124
- parentId: crdt.parentId,
125
- parentKey: crdt.parentKey,
126
- });
127
- break;
128
- case live_1.CrdtType.Object:
129
- ops.push({
130
- type: live_1.OpType.CreateObject,
131
- id: id,
132
- parentId: crdt.parentId,
133
- parentKey: crdt.parentKey,
134
- data: crdt.data,
135
- });
136
- break;
137
- case live_1.CrdtType.Map:
138
- ops.push({
139
- type: live_1.OpType.CreateMap,
140
- id: id,
141
- parentId: crdt.parentId,
142
- parentKey: crdt.parentKey,
143
- });
144
- break;
145
- }
146
- }
147
- });
148
- return ops;
149
- }
150
- exports.getTreesDiffOperations = getTreesDiffOperations;
151
- function mergeStorageUpdates(first, second) {
152
- if (!first) {
153
- return second;
154
- }
155
- if (second.type === "LiveObject") {
156
- const updates = first.updates;
157
- for (const [key, value] of Object.entries(second.updates)) {
158
- updates[key] = value;
159
- }
160
- return Object.assign(Object.assign({}, second), { updates: updates });
161
- }
162
- else if (second.type === "LiveMap") {
163
- const updates = first.updates;
164
- for (const [key, value] of Object.entries(second.updates)) {
165
- updates[key] = value;
166
- }
167
- return Object.assign(Object.assign({}, second), { updates: updates });
168
- }
169
- else if (second.type === "LiveList") {
170
- const updates = first.updates;
171
- return Object.assign(Object.assign({}, second), { updates: updates.concat(second.updates) });
172
- }
173
- return second;
174
- }
175
- exports.mergeStorageUpdates = mergeStorageUpdates;
176
- function isPlain(value) {
177
- const type = typeof value;
178
- return (type === "undefined" ||
179
- value === null ||
180
- type === "string" ||
181
- type === "boolean" ||
182
- type === "number" ||
183
- Array.isArray(value) ||
184
- isPlainObject(value));
185
- }
186
- function isPlainObject(value) {
187
- if (typeof value !== "object" || value === null)
188
- return false;
189
- let proto = Object.getPrototypeOf(value);
190
- if (proto === null)
191
- return true;
192
- let baseProto = proto;
193
- while (Object.getPrototypeOf(baseProto) !== null) {
194
- baseProto = Object.getPrototypeOf(baseProto);
195
- }
196
- return proto === baseProto;
197
- }
198
- function findNonSerializableValue(value, path = "") {
199
- if (!isPlain) {
200
- return {
201
- path: path || "root",
202
- value: value,
203
- };
204
- }
205
- if (typeof value !== "object" || value === null) {
206
- return false;
207
- }
208
- for (const [key, nestedValue] of Object.entries(value)) {
209
- const nestedPath = path ? path + "." + key : key;
210
- if (!isPlain(nestedValue)) {
211
- return {
212
- path: nestedPath,
213
- value: nestedValue,
214
- };
215
- }
216
- if (typeof nestedValue === "object") {
217
- const nonSerializableNestedValue = findNonSerializableValue(nestedValue, nestedPath);
218
- if (nonSerializableNestedValue) {
219
- return nonSerializableNestedValue;
220
- }
221
- }
222
- }
223
- return false;
224
- }
225
- exports.findNonSerializableValue = findNonSerializableValue;
@@ -1,68 +0,0 @@
1
- import { Op, SerializedCrdt } from "./live";
2
- import { StorageUpdate } from "./types";
3
- export declare type ApplyResult = {
4
- reverse: Op[];
5
- modified: StorageUpdate;
6
- } | {
7
- modified: false;
8
- };
9
- export interface Doc {
10
- generateId: () => string;
11
- generateOpId: () => string;
12
- getItem: (id: string) => AbstractCrdt | undefined;
13
- addItem: (id: string, item: AbstractCrdt) => void;
14
- deleteItem: (id: string) => void;
15
- dispatch: (ops: Op[], reverseOps: Op[], storageUpdates: Map<string, StorageUpdate>) => void;
16
- }
17
- export declare abstract class AbstractCrdt {
18
- #private;
19
- /**
20
- * INTERNAL
21
- */
22
- protected get _doc(): Doc | undefined;
23
- /**
24
- * INTERNAL
25
- */
26
- get _id(): string | undefined;
27
- /**
28
- * INTERNAL
29
- */
30
- get _parent(): AbstractCrdt | undefined;
31
- /**
32
- * INTERNAL
33
- */
34
- get _parentKey(): string | undefined;
35
- /**
36
- * INTERNAL
37
- */
38
- _apply(op: Op, isLocal: boolean): ApplyResult;
39
- /**
40
- * INTERNAL
41
- */
42
- _setParentLink(parent: AbstractCrdt, key: string): void;
43
- /**
44
- * INTERNAL
45
- */
46
- _attach(id: string, doc: Doc): void;
47
- /**
48
- * INTERNAL
49
- */
50
- abstract _attachChild(id: string, key: string, crdt: AbstractCrdt, opId: string, isLocal: boolean): ApplyResult;
51
- /**
52
- * INTERNAL
53
- */
54
- _detach(): void;
55
- /**
56
- * INTERNAL
57
- */
58
- abstract _detachChild(crdt: AbstractCrdt): ApplyResult;
59
- /**
60
- * INTERNAL
61
- */
62
- abstract _serialize(parentId: string, parentKey: string, doc?: Doc): Op[];
63
- /**
64
- * INTERNAL
65
- */
66
- abstract _toSerializedCrdt(): SerializedCrdt;
67
- abstract _getType(): string;
68
- }
@@ -1,91 +0,0 @@
1
- var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
2
- if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
3
- if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
4
- return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
5
- };
6
- var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
7
- if (kind === "m") throw new TypeError("Private method is not writable");
8
- if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
9
- if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
10
- return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
11
- };
12
- var _AbstractCrdt_parent, _AbstractCrdt_doc, _AbstractCrdt_id, _AbstractCrdt_parentKey;
13
- import { OpType } from "./live";
14
- export class AbstractCrdt {
15
- constructor() {
16
- _AbstractCrdt_parent.set(this, void 0);
17
- _AbstractCrdt_doc.set(this, void 0);
18
- _AbstractCrdt_id.set(this, void 0);
19
- _AbstractCrdt_parentKey.set(this, void 0);
20
- }
21
- /**
22
- * INTERNAL
23
- */
24
- get _doc() {
25
- return __classPrivateFieldGet(this, _AbstractCrdt_doc, "f");
26
- }
27
- /**
28
- * INTERNAL
29
- */
30
- get _id() {
31
- return __classPrivateFieldGet(this, _AbstractCrdt_id, "f");
32
- }
33
- /**
34
- * INTERNAL
35
- */
36
- get _parent() {
37
- return __classPrivateFieldGet(this, _AbstractCrdt_parent, "f");
38
- }
39
- /**
40
- * INTERNAL
41
- */
42
- get _parentKey() {
43
- return __classPrivateFieldGet(this, _AbstractCrdt_parentKey, "f");
44
- }
45
- /**
46
- * INTERNAL
47
- */
48
- _apply(op, isLocal) {
49
- switch (op.type) {
50
- case OpType.DeleteCrdt: {
51
- if (this._parent != null && this._parentKey != null) {
52
- return this._parent._detachChild(this);
53
- }
54
- return { modified: false };
55
- }
56
- }
57
- return { modified: false };
58
- }
59
- /**
60
- * INTERNAL
61
- */
62
- _setParentLink(parent, key) {
63
- if (__classPrivateFieldGet(this, _AbstractCrdt_parent, "f") != null && __classPrivateFieldGet(this, _AbstractCrdt_parent, "f") !== parent) {
64
- throw new Error("Cannot attach parent if it already exist");
65
- }
66
- __classPrivateFieldSet(this, _AbstractCrdt_parentKey, key, "f");
67
- __classPrivateFieldSet(this, _AbstractCrdt_parent, parent, "f");
68
- }
69
- /**
70
- * INTERNAL
71
- */
72
- _attach(id, doc) {
73
- if (__classPrivateFieldGet(this, _AbstractCrdt_id, "f") || __classPrivateFieldGet(this, _AbstractCrdt_doc, "f")) {
74
- throw new Error("Cannot attach if CRDT is already attached");
75
- }
76
- doc.addItem(id, this);
77
- __classPrivateFieldSet(this, _AbstractCrdt_id, id, "f");
78
- __classPrivateFieldSet(this, _AbstractCrdt_doc, doc, "f");
79
- }
80
- /**
81
- * INTERNAL
82
- */
83
- _detach() {
84
- if (__classPrivateFieldGet(this, _AbstractCrdt_doc, "f") && __classPrivateFieldGet(this, _AbstractCrdt_id, "f")) {
85
- __classPrivateFieldGet(this, _AbstractCrdt_doc, "f").deleteItem(__classPrivateFieldGet(this, _AbstractCrdt_id, "f"));
86
- }
87
- __classPrivateFieldSet(this, _AbstractCrdt_parent, undefined, "f");
88
- __classPrivateFieldSet(this, _AbstractCrdt_doc, undefined, "f");
89
- }
90
- }
91
- _AbstractCrdt_parent = new WeakMap(), _AbstractCrdt_doc = new WeakMap(), _AbstractCrdt_id = new WeakMap(), _AbstractCrdt_parentKey = new WeakMap();
@@ -1,144 +0,0 @@
1
- import { AbstractCrdt, Doc, ApplyResult } from "./AbstractCrdt";
2
- import { SerializedList, SerializedCrdtWithId, Op, SerializedCrdt } from "./live";
3
- /**
4
- * The LiveList class represents an ordered collection of items that is synchorinized across clients.
5
- */
6
- export declare class LiveList<T> extends AbstractCrdt {
7
- #private;
8
- constructor(items?: T[]);
9
- /**
10
- * INTERNAL
11
- */
12
- static _deserialize([id, item]: [id: string, item: SerializedList], parentToChildren: Map<string, SerializedCrdtWithId[]>, doc: Doc): LiveList<never>;
13
- /**
14
- * INTERNAL
15
- */
16
- _serialize(parentId?: string, parentKey?: string, doc?: Doc): Op[];
17
- _indexOfPosition(position: string): number;
18
- /**
19
- * INTERNAL
20
- */
21
- _attach(id: string, doc: Doc): void;
22
- /**
23
- * INTERNAL
24
- */
25
- _detach(): void;
26
- /**
27
- * INTERNAL
28
- */
29
- _attachChild(id: string, key: string, child: AbstractCrdt, opId: string, isLocal: boolean): ApplyResult;
30
- /**
31
- * INTERNAL
32
- */
33
- _detachChild(child: AbstractCrdt): ApplyResult;
34
- /**
35
- * INTERNAL
36
- */
37
- _setChildKey(key: string, child: AbstractCrdt, previousKey: string): ApplyResult;
38
- /**
39
- * INTERNAL
40
- */
41
- _apply(op: Op, isLocal: boolean): ApplyResult;
42
- /**
43
- * INTERNAL
44
- */
45
- _getType(): string;
46
- /**
47
- * INTERNAL
48
- */
49
- _toSerializedCrdt(): SerializedCrdt;
50
- /**
51
- * Returns the number of elements.
52
- */
53
- get length(): number;
54
- /**
55
- * Adds one element to the end of the LiveList.
56
- * @param element The element to add to the end of the LiveList.
57
- */
58
- push(element: T): void;
59
- /**
60
- * Inserts one element at a specified index.
61
- * @param element The element to insert.
62
- * @param index The index at which you want to insert the element.
63
- */
64
- insert(element: T, index: number): void;
65
- /**
66
- * Move one element from one index to another.
67
- * @param index The index of the element to move
68
- * @param targetIndex The index where the element should be after moving.
69
- */
70
- move(index: number, targetIndex: number): void;
71
- /**
72
- * Deletes an element at the specified index
73
- * @param index The index of the element to delete
74
- */
75
- delete(index: number): void;
76
- clear(): void;
77
- /**
78
- * Returns an Array of all the elements in the LiveList.
79
- */
80
- toArray(): T[];
81
- toCrdtArray(): AbstractCrdt[];
82
- /**
83
- * Tests whether all elements pass the test implemented by the provided function.
84
- * @param predicate Function to test for each element, taking two arguments (the element and its index).
85
- * @returns true if the predicate function returns a truthy value for every element. Otherwise, false.
86
- */
87
- every(predicate: (value: T, index: number) => unknown): boolean;
88
- /**
89
- * Creates an array with all elements that pass the test implemented by the provided function.
90
- * @param predicate Function to test each element of the LiveList. Return a value that coerces to true to keep the element, or to false otherwise.
91
- * @returns An array with the elements that pass the test.
92
- */
93
- filter(predicate: (value: T, index: number) => unknown): T[];
94
- /**
95
- * Returns the first element that satisfies the provided testing function.
96
- * @param predicate Function to execute on each value.
97
- * @returns The value of the first element in the LiveList that satisfies the provided testing function. Otherwise, undefined is returned.
98
- */
99
- find(predicate: (value: T, index: number) => unknown): T | undefined;
100
- /**
101
- * Returns the index of the first element in the LiveList that satisfies the provided testing function.
102
- * @param predicate Function to execute on each value until the function returns true, indicating that the satisfying element was found.
103
- * @returns The index of the first element in the LiveList that passes the test. Otherwise, -1.
104
- */
105
- findIndex(predicate: (value: T, index: number) => unknown): number;
106
- /**
107
- * Executes a provided function once for each element.
108
- * @param callbackfn Function to execute on each element.
109
- */
110
- forEach(callbackfn: (value: T, index: number) => void): void;
111
- /**
112
- * Get the element at the specified index.
113
- * @param index The index on the element to get.
114
- * @returns The element at the specified index or undefined.
115
- */
116
- get(index: number): T | undefined;
117
- /**
118
- * Returns the first index at which a given element can be found in the LiveList, or -1 if it is not present.
119
- * @param searchElement Element to locate.
120
- * @param fromIndex The index to start the search at.
121
- * @returns The first index of the element in the LiveList; -1 if not found.
122
- */
123
- indexOf(searchElement: T, fromIndex?: number): number;
124
- /**
125
- * Returns the last index at which a given element can be found in the LiveList, or -1 if it is not present. The LiveLsit is searched backwards, starting at fromIndex.
126
- * @param searchElement Element to locate.
127
- * @param fromIndex The index at which to start searching backwards.
128
- * @returns
129
- */
130
- lastIndexOf(searchElement: T, fromIndex?: number): number;
131
- /**
132
- * Creates an array populated with the results of calling a provided function on every element.
133
- * @param callback Function that is called for every element.
134
- * @returns An array with each element being the result of the callback function.
135
- */
136
- map<U>(callback: (value: T, index: number) => U): U[];
137
- /**
138
- * Tests whether at least one element in the LiveList passes the test implemented by the provided function.
139
- * @param predicate Function to test for each element.
140
- * @returns true if the callback function returns a truthy value for at least one element. Otherwise, false.
141
- */
142
- some(predicate: (value: T, index: number) => unknown): boolean;
143
- [Symbol.iterator](): IterableIterator<T>;
144
- }