@liveblocks/client 0.12.0-beta.8 → 0.12.2

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/lib/cjs/live.d.ts CHANGED
@@ -114,6 +114,7 @@ export declare enum OpType {
114
114
  }
115
115
  export declare type Op = CreateObjectOp | UpdateObjectOp | DeleteCrdtOp | CreateListOp | SetParentKeyOp | DeleteObjectKeyOp | CreateMapOp | CreateRegisterOp;
116
116
  export declare type UpdateObjectOp = {
117
+ opId?: string;
117
118
  id: string;
118
119
  type: OpType.UpdateObject;
119
120
  data: {
@@ -3,8 +3,4 @@ export declare const max = 126;
3
3
  export declare function makePosition(before?: string, after?: string): string;
4
4
  export declare function posCodes(str: string): number[];
5
5
  export declare function pos(codes: number[]): string;
6
- export declare function compare(itemA: {
7
- position: string;
8
- }, itemB: {
9
- position: string;
10
- }): number;
6
+ export declare function compare(posA: string, posB: string): number;
@@ -94,9 +94,9 @@ function pos(codes) {
94
94
  return String.fromCharCode(...codes);
95
95
  }
96
96
  exports.pos = pos;
97
- function compare(itemA, itemB) {
98
- const aCodes = posCodes(itemA.position);
99
- const bCodes = posCodes(itemB.position);
97
+ function compare(posA, posB) {
98
+ const aCodes = posCodes(posA);
99
+ const bCodes = posCodes(posB);
100
100
  const maxLength = Math.max(aCodes.length, bCodes.length);
101
101
  for (let i = 0; i < maxLength; i++) {
102
102
  const a = aCodes[i] == null ? exports.min : aCodes[i];
@@ -108,6 +108,6 @@ function compare(itemA, itemB) {
108
108
  return a - b;
109
109
  }
110
110
  }
111
- throw new Error(`Impossible to compare similar position "${itemA.position}" and "${itemB.position}"`);
111
+ throw new Error(`Impossible to compare similar position "${posA}" and "${posB}"`);
112
112
  }
113
113
  exports.compare = compare;
package/lib/cjs/room.d.ts CHANGED
@@ -49,6 +49,7 @@ declare type Context = {
49
49
  authEndpoint: AuthEndpoint;
50
50
  liveblocksServer: string;
51
51
  throttleDelay: number;
52
+ publicApiKey?: string;
52
53
  };
53
54
  export declare function makeStateMachine(state: State, context: Context, mockedEffects?: Effects): {
54
55
  onOpen: () => void;
@@ -80,6 +81,8 @@ export declare function makeStateMachine(state: State, context: Context, mockedE
80
81
  };
81
82
  updatePresence: <T_4 extends Presence>(overrides: Partial<T_4>) => void;
82
83
  broadcastEvent: (event: any) => void;
84
+ undo: () => void;
85
+ redo: () => void;
83
86
  getStorage: <TRoot>() => Promise<{
84
87
  root: import("./doc").LiveObject<TRoot>;
85
88
  }>;
package/lib/cjs/room.js CHANGED
@@ -74,7 +74,7 @@ function makeStateMachine(state, context, mockedEffects) {
74
74
  authenticate() {
75
75
  return __awaiter(this, void 0, void 0, function* () {
76
76
  try {
77
- const token = yield (0, authentication_1.default)(context.authEndpoint, context.room);
77
+ const token = yield (0, authentication_1.default)(context.authEndpoint, context.room, context.publicApiKey);
78
78
  const parsedToken = (0, authentication_1.parseToken)(token);
79
79
  const socket = new WebSocket(`${context.liveblocksServer}/?token=${token}`);
80
80
  socket.addEventListener("message", onMessage);
@@ -500,6 +500,12 @@ function makeStateMachine(state, context, mockedEffects) {
500
500
  };
501
501
  });
502
502
  }
503
+ function undo() {
504
+ storage.undo();
505
+ }
506
+ function redo() {
507
+ storage.redo();
508
+ }
503
509
  return {
504
510
  // Internal
505
511
  onOpen,
@@ -518,6 +524,8 @@ function makeStateMachine(state, context, mockedEffects) {
518
524
  // Presence
519
525
  updatePresence,
520
526
  broadcastEvent,
527
+ undo,
528
+ redo,
521
529
  getStorage,
522
530
  selectors: {
523
531
  // Core
@@ -566,14 +574,23 @@ function defaultState(me, defaultStorageRoot) {
566
574
  exports.defaultState = defaultState;
567
575
  function createRoom(name, options) {
568
576
  const throttleDelay = options.throttle || 100;
569
- const liveblocksServer = options.liveblocksServer || "wss://liveblocks.net/v2";
570
- const authEndpoint = options.authEndpoint;
577
+ const liveblocksServer = options.liveblocksServer || "wss://liveblocks.net/v4";
578
+ let authEndpoint;
579
+ if (options.authEndpoint) {
580
+ authEndpoint = options.authEndpoint;
581
+ }
582
+ else {
583
+ const publicAuthorizeEndpoint = options.publicAuthorizeEndpoint ||
584
+ "https://liveblocks.io/api/public/authorize";
585
+ authEndpoint = publicAuthorizeEndpoint;
586
+ }
571
587
  const state = defaultState(options.defaultPresence, options.defaultStorageRoot);
572
588
  const machine = makeStateMachine(state, {
573
589
  throttleDelay,
574
590
  liveblocksServer,
575
591
  authEndpoint,
576
592
  room: name,
593
+ publicApiKey: options.publicApiKey,
577
594
  });
578
595
  const room = {
579
596
  /////////////
@@ -591,6 +608,8 @@ function createRoom(name, options) {
591
608
  getOthers: machine.selectors.getOthers,
592
609
  broadcastEvent: machine.broadcastEvent,
593
610
  getStorage: machine.getStorage,
611
+ undo: machine.undo,
612
+ redo: machine.redo,
594
613
  };
595
614
  return {
596
615
  connect: machine.connect,
@@ -16,4 +16,6 @@ export default class Storage {
16
16
  private createDocFromMessage;
17
17
  getDocument<TRoot>(): Promise<Doc<TRoot>>;
18
18
  onMessage(message: ServerMessage): Promise<void>;
19
+ undo(): void;
20
+ redo(): void;
19
21
  }
@@ -20,11 +20,11 @@ class Storage {
20
20
  this._getInitialStateResolver = null;
21
21
  }
22
22
  createDocFromMessage(message) {
23
- if (message.items.length === 0) {
24
- this._doc = doc_1.Doc.from(this.options.defaultRoot, this.options.getConnectionId(), this.options.dispatch);
25
- }
26
- else {
27
- this._doc = doc_1.Doc.load(message.items, this.options.getConnectionId(), this.options.dispatch);
23
+ this._doc = doc_1.Doc.load(message.items, this.options.getConnectionId(), this.options.dispatch);
24
+ for (const key in this.options.defaultRoot) {
25
+ if (this._doc.root.get(key) == null) {
26
+ this._doc.root.set(key, this.options.defaultRoot[key]);
27
+ }
28
28
  }
29
29
  }
30
30
  getDocument() {
@@ -50,13 +50,19 @@ class Storage {
50
50
  break;
51
51
  }
52
52
  case live_1.ServerMessageType.UpdateStorage: {
53
- for (const op of message.ops) {
54
- (_b = this._doc) === null || _b === void 0 ? void 0 : _b.apply(op);
55
- }
53
+ (_b = this._doc) === null || _b === void 0 ? void 0 : _b.apply(message.ops);
56
54
  break;
57
55
  }
58
56
  }
59
57
  });
60
58
  }
59
+ undo() {
60
+ var _a;
61
+ (_a = this._doc) === null || _a === void 0 ? void 0 : _a.undo();
62
+ }
63
+ redo() {
64
+ var _a;
65
+ (_a = this._doc) === null || _a === void 0 ? void 0 : _a.redo();
66
+ }
61
67
  }
62
68
  exports.default = Storage;
@@ -87,14 +87,19 @@ declare type AuthEndpointCallback = (room: string) => Promise<{
87
87
  token: string;
88
88
  }>;
89
89
  export declare type AuthEndpoint = string | AuthEndpointCallback;
90
+ /**
91
+ * The authentication endpoint that is called to ensure that the current user has access to a room.
92
+ * Can be an url or a callback if you need to add additional headers.
93
+ */
90
94
  export declare type ClientOptions = {
91
- /**
92
- * The authentication endpoint that is called to ensure that the current user has access to a room.
93
- * Can be an url or a callback if you need to add additional headers.
94
- */
95
- authEndpoint: AuthEndpoint;
96
95
  throttle?: number;
97
- };
96
+ } & ({
97
+ publicApiKey: string;
98
+ authEndpoint?: never;
99
+ } | {
100
+ publicApiKey?: never;
101
+ authEndpoint: AuthEndpoint;
102
+ });
98
103
  export declare type AuthorizeResponse = {
99
104
  token: string;
100
105
  };
@@ -261,5 +266,7 @@ export declare type Room = {
261
266
  getStorage: <TRoot>() => Promise<{
262
267
  root: LiveObject<TRoot>;
263
268
  }>;
269
+ undo: () => void;
270
+ redo: () => void;
264
271
  };
265
272
  export {};
@@ -1,3 +1,3 @@
1
1
  import { AuthEndpoint, AuthenticationToken } from "./types";
2
- export default function auth(endpoint: AuthEndpoint, room: string): Promise<string>;
2
+ export default function auth(endpoint: AuthEndpoint, room: string, publicApiKey?: string): Promise<string>;
3
3
  export declare function parseToken(token: string): AuthenticationToken;
@@ -7,7 +7,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
7
7
  step((generator = generator.apply(thisArg, _arguments || [])).next());
8
8
  });
9
9
  };
10
- function fetchAuthorize(endpoint, room) {
10
+ function fetchAuthorize(endpoint, room, publicApiKey) {
11
11
  return __awaiter(this, void 0, void 0, function* () {
12
12
  const res = yield fetch(endpoint, {
13
13
  method: "POST",
@@ -16,6 +16,7 @@ function fetchAuthorize(endpoint, room) {
16
16
  },
17
17
  body: JSON.stringify({
18
18
  room,
19
+ publicApiKey,
19
20
  }),
20
21
  });
21
22
  if (!res.ok) {
@@ -34,10 +35,10 @@ function fetchAuthorize(endpoint, room) {
34
35
  return authResponse.token;
35
36
  });
36
37
  }
37
- export default function auth(endpoint, room) {
38
+ export default function auth(endpoint, room, publicApiKey) {
38
39
  return __awaiter(this, void 0, void 0, function* () {
39
40
  if (typeof endpoint === "string") {
40
- return fetchAuthorize(endpoint, room);
41
+ return fetchAuthorize(endpoint, room, publicApiKey);
41
42
  }
42
43
  if (typeof endpoint === "function") {
43
44
  const { token } = yield endpoint(room);
package/lib/esm/doc.d.ts CHANGED
@@ -9,10 +9,14 @@ export declare class Doc<T extends Record<string, any> = Record<string, any>> {
9
9
  addItem(id: string, item: AbstractCrdt): void;
10
10
  deleteItem(id: string): void;
11
11
  getItem(id: string): AbstractCrdt | undefined;
12
- apply(op: Op): void;
12
+ apply(ops: Op[]): Op[];
13
13
  get root(): LiveObject<T>;
14
+ addToUndoStack(ops: Op[]): void;
15
+ undo(): void;
16
+ redo(): void;
14
17
  count(): number;
15
18
  generateId(): string;
19
+ generateOpId(): string;
16
20
  }
17
21
  declare abstract class AbstractCrdt {
18
22
  #private;
@@ -31,24 +35,49 @@ declare abstract class AbstractCrdt {
31
35
  /**
32
36
  * INTERNAL
33
37
  */
34
- _setParent(parent: AbstractCrdt): void;
38
+ get _parentKey(): string | undefined;
39
+ _apply(op: Op): Op[];
40
+ /**
41
+ * INTERNAL
42
+ */
43
+ _setParentLink(parent: AbstractCrdt, key: string): void;
35
44
  /**
36
45
  * INTERNAL
37
46
  */
38
47
  _attach(id: string, doc: Doc): void;
39
- abstract _attachChild(id: string, key: string, crdt: AbstractCrdt): void;
48
+ abstract _attachChild(id: string, key: string, crdt: AbstractCrdt): Op[];
40
49
  /**
41
50
  * INTERNAL
42
51
  */
43
52
  _detach(): void;
44
53
  abstract _detachChild(crdt: AbstractCrdt): void;
54
+ /**
55
+ * Subscribes to updates.
56
+ */
45
57
  subscribe(listener: () => void): void;
58
+ /**
59
+ * Subscribes to updates and children updates.
60
+ */
46
61
  subscribeDeep(listener: () => void): void;
62
+ /**
63
+ * Unsubscribes to updates.
64
+ */
47
65
  unsubscribe(listener: () => void): void;
66
+ /**
67
+ * Unsubscribes to updates and children updates.
68
+ */
48
69
  unsubscribeDeep(listener: () => void): void;
49
- notify(onlyDeep?: boolean): void;
70
+ /**
71
+ * INTERNAL
72
+ */
73
+ _notify(onlyDeep?: boolean): void;
50
74
  abstract _serialize(parentId: string, parentKey: string): Op[];
51
75
  }
76
+ /**
77
+ * The LiveObject class is similar to a JavaScript object that is synchronized on all clients.
78
+ * Keys should be a string, and values should be serializable to JSON.
79
+ * If multiple clients update the same property simultaneously, the last modification received by the Liveblocks servers is the winner.
80
+ */
52
81
  export declare class LiveObject<T extends Record<string, any> = Record<string, any>> extends AbstractCrdt {
53
82
  #private;
54
83
  constructor(object?: T);
@@ -69,7 +98,7 @@ export declare class LiveObject<T extends Record<string, any> = Record<string, a
69
98
  /**
70
99
  * INTERNAL
71
100
  */
72
- _attachChild(id: string, key: keyof T, child: AbstractCrdt): void;
101
+ _attachChild(id: string, key: keyof T, child: AbstractCrdt): Op[];
73
102
  /**
74
103
  * INTERNAL
75
104
  */
@@ -81,12 +110,33 @@ export declare class LiveObject<T extends Record<string, any> = Record<string, a
81
110
  /**
82
111
  * INTERNAL
83
112
  */
84
- _apply(op: Op): void;
113
+ _apply(op: Op): Op[];
114
+ /**
115
+ * Transform the LiveObject into a javascript object
116
+ */
85
117
  toObject(): T;
118
+ /**
119
+ * Adds or updates a property with a specified key and a value.
120
+ * @param key The key of the property to add
121
+ * @param value The value of the property to add
122
+ */
86
123
  set<TKey extends keyof T>(key: TKey, value: T[TKey]): void;
124
+ /**
125
+ * Returns a specified property from the LiveObject.
126
+ * @param key The key of the property to get
127
+ */
87
128
  get<TKey extends keyof T>(key: TKey): T[TKey];
129
+ /**
130
+ * Adds or updates multiple properties at once with an object.
131
+ * @param overrides The object used to overrides properties
132
+ */
88
133
  update(overrides: Partial<T>): void;
89
134
  }
135
+ /**
136
+ * The LiveMap class is similar to a JavaScript Map that is synchronized on all clients.
137
+ * Keys should be a string, and values should be serializable to JSON.
138
+ * If multiple clients update the same property simultaneously, the last modification received by the Liveblocks servers is the winner.
139
+ */
90
140
  export declare class LiveMap<TKey extends string, TValue> extends AbstractCrdt {
91
141
  #private;
92
142
  constructor(entries?: readonly (readonly [TKey, TValue])[] | null | undefined);
@@ -105,7 +155,7 @@ export declare class LiveMap<TKey extends string, TValue> extends AbstractCrdt {
105
155
  /**
106
156
  * INTERNAL
107
157
  */
108
- _attachChild(id: string, key: TKey, child: AbstractCrdt): void;
158
+ _attachChild(id: string, key: TKey, child: AbstractCrdt): Op[];
109
159
  /**
110
160
  * INTERNAL
111
161
  */
@@ -114,17 +164,58 @@ export declare class LiveMap<TKey extends string, TValue> extends AbstractCrdt {
114
164
  * INTERNAL
115
165
  */
116
166
  _detachChild(child: AbstractCrdt): void;
167
+ /**
168
+ * Returns a specified element from the LiveMap.
169
+ * @param key The key of the element to return.
170
+ * @returns The element associated with the specified key, or undefined if the key can't be found in the LiveMap.
171
+ */
117
172
  get(key: TKey): TValue | undefined;
173
+ /**
174
+ * Adds or updates an element with a specified key and a value.
175
+ * @param key The key of the element to add. Should be a string.
176
+ * @param value The value of the element to add. Should be serializable to JSON.
177
+ */
118
178
  set(key: TKey, value: TValue): void;
179
+ /**
180
+ * Returns the number of elements in the LiveMap.
181
+ */
119
182
  get size(): number;
183
+ /**
184
+ * Returns a boolean indicating whether an element with the specified key exists or not.
185
+ * @param key The key of the element to test for presence.
186
+ */
120
187
  has(key: TKey): boolean;
188
+ /**
189
+ * Removes the specified element by key.
190
+ * @param key The key of the element to remove.
191
+ * @returns true if an element existed and has been removed, or false if the element does not exist.
192
+ */
121
193
  delete(key: TKey): boolean;
194
+ /**
195
+ * Returns a new Iterator object that contains the [key, value] pairs for each element.
196
+ */
122
197
  entries(): IterableIterator<[string, TValue]>;
198
+ /**
199
+ * Same function object as the initial value of the entries method.
200
+ */
123
201
  [Symbol.iterator](): IterableIterator<[string, TValue]>;
202
+ /**
203
+ * Returns a new Iterator object that contains the keys for each element.
204
+ */
124
205
  keys(): IterableIterator<TKey>;
206
+ /**
207
+ * Returns a new Iterator object that contains the values for each element.
208
+ */
125
209
  values(): IterableIterator<TValue>;
210
+ /**
211
+ * Executes a provided function once per each key/value pair in the Map object, in insertion order.
212
+ * @param callback Function to execute for each entry in the map.
213
+ */
126
214
  forEach(callback: (value: TValue, key: TKey, map: LiveMap<TKey, TValue>) => void): void;
127
215
  }
216
+ /**
217
+ * The LiveList class represents an ordered collection of items that is synchorinized across clients.
218
+ */
128
219
  export declare class LiveList<T> extends AbstractCrdt {
129
220
  #private;
130
221
  constructor(items?: T[]);
@@ -147,7 +238,7 @@ export declare class LiveList<T> extends AbstractCrdt {
147
238
  /**
148
239
  * INTERNAL
149
240
  */
150
- _attachChild(id: string, key: string, child: AbstractCrdt): void;
241
+ _attachChild(id: string, key: string, child: AbstractCrdt): Op[];
151
242
  /**
152
243
  * INTERNAL
153
244
  */
@@ -156,20 +247,100 @@ export declare class LiveList<T> extends AbstractCrdt {
156
247
  * INTERNAL
157
248
  */
158
249
  _setChildKey(key: string, child: AbstractCrdt): void;
159
- push(item: T): void;
160
- insert(item: T, index: number): void;
250
+ /**
251
+ * INTERNAL
252
+ */
253
+ _apply(op: Op): Op[];
254
+ /**
255
+ * Returns the number of elements.
256
+ */
257
+ get length(): number;
258
+ /**
259
+ * Adds one element to the end of the LiveList.
260
+ * @param element The element to add to the end of the LiveList.
261
+ */
262
+ push(element: T): void;
263
+ /**
264
+ * Inserts one element at a specified index.
265
+ * @param element The element to insert.
266
+ * @param index The index at which you want to insert the element.
267
+ */
268
+ insert(element: T, index: number): void;
269
+ /**
270
+ * Move one element from one index to another.
271
+ * @param index The index of the element to move
272
+ * @param targetIndex The index where the element should be after moving.
273
+ */
161
274
  move(index: number, targetIndex: number): void;
275
+ /**
276
+ * Deletes an element at the specified index
277
+ * @param index The index of the element to delete
278
+ */
162
279
  delete(index: number): void;
280
+ /**
281
+ * Returns an Array of all the elements in the LiveList.
282
+ */
163
283
  toArray(): T[];
284
+ /**
285
+ * Tests whether all elements pass the test implemented by the provided function.
286
+ * @param predicate Function to test for each element, taking two arguments (the element and its index).
287
+ * @returns true if the predicate function returns a truthy value for every element. Otherwise, false.
288
+ */
164
289
  every(predicate: (value: T, index: number) => unknown): boolean;
290
+ /**
291
+ * Creates an array with all elements that pass the test implemented by the provided function.
292
+ * @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.
293
+ * @returns An array with the elements that pass the test.
294
+ */
165
295
  filter(predicate: (value: T, index: number) => unknown): T[];
296
+ /**
297
+ * Returns the first element that satisfies the provided testing function.
298
+ * @param predicate Function to execute on each value.
299
+ * @returns The value of the first element in the LiveList that satisfies the provided testing function. Otherwise, undefined is returned.
300
+ */
166
301
  find(predicate: (value: T, index: number) => unknown): T | undefined;
302
+ /**
303
+ * Returns the index of the first element in the LiveList that satisfies the provided testing function.
304
+ * @param predicate Function to execute on each value until the function returns true, indicating that the satisfying element was found.
305
+ * @returns The index of the first element in the LiveList that passes the test. Otherwise, -1.
306
+ */
167
307
  findIndex(predicate: (value: T, index: number) => unknown): number;
308
+ /**
309
+ * Executes a provided function once for each element.
310
+ * @param callbackfn Function to execute on each element.
311
+ */
168
312
  forEach(callbackfn: (value: T, index: number) => void): void;
169
- get(index: number): T;
313
+ /**
314
+ * Get the element at the specified index.
315
+ * @param index The index on the element to get.
316
+ * @returns The element at the specified index or undefined.
317
+ */
318
+ get(index: number): T | undefined;
319
+ /**
320
+ * Returns the first index at which a given element can be found in the LiveList, or -1 if it is not present.
321
+ * @param searchElement Element to locate.
322
+ * @param fromIndex The index to start the search at.
323
+ * @returns The first index of the element in the LiveList; -1 if not found.
324
+ */
170
325
  indexOf(searchElement: T, fromIndex?: number): number;
326
+ /**
327
+ * 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.
328
+ * @param searchElement Element to locate.
329
+ * @param fromIndex The index at which to start searching backwards.
330
+ * @returns
331
+ */
171
332
  lastIndexOf(searchElement: T, fromIndex?: number): number;
333
+ /**
334
+ * Creates an array populated with the results of calling a provided function on every element.
335
+ * @param callback Function that is called for every element.
336
+ * @returns An array with each element being the result of the callback function.
337
+ */
172
338
  map<U>(callback: (value: T, index: number) => U): U[];
339
+ /**
340
+ * Tests whether at least one element in the LiveList passes the test implemented by the provided function.
341
+ * @param predicate Function to test for each element.
342
+ * @returns true if the callback function returns a truthy value for at least one element. Otherwise, false.
343
+ */
173
344
  some(predicate: (value: T, index: number) => unknown): boolean;
174
345
  [Symbol.iterator](): IterableIterator<T>;
175
346
  }