@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
@@ -1,41 +1,189 @@
1
- import { AbstractCrdt } from "./AbstractCrdt";
2
- import type { LiveList } from "./LiveList";
3
- import type { LiveMap } from "./LiveMap";
4
- import type { LiveObject } from "./LiveObject";
5
- export declare type MyPresenceCallback<T extends Presence = Presence> = (me: T) => void;
6
- export declare type OthersEventCallback<T extends Presence = Presence> = (others: Others<T>, event: OthersEvent<T>) => void;
7
- export declare type EventCallback = ({ connectionId, event, }: {
1
+ /**
2
+ * The LiveList class represents an ordered collection of items that is synchorinized across clients.
3
+ */
4
+ declare class LiveList<T> extends AbstractCrdt {
5
+ #private;
6
+ constructor(items?: T[]);
7
+ /**
8
+ * Returns the number of elements.
9
+ */
10
+ get length(): number;
11
+ /**
12
+ * Adds one element to the end of the LiveList.
13
+ * @param element The element to add to the end of the LiveList.
14
+ */
15
+ push(element: T): void;
16
+ /**
17
+ * Inserts one element at a specified index.
18
+ * @param element The element to insert.
19
+ * @param index The index at which you want to insert the element.
20
+ */
21
+ insert(element: T, index: number): void;
22
+ /**
23
+ * Move one element from one index to another.
24
+ * @param index The index of the element to move
25
+ * @param targetIndex The index where the element should be after moving.
26
+ */
27
+ move(index: number, targetIndex: number): void;
28
+ /**
29
+ * Deletes an element at the specified index
30
+ * @param index The index of the element to delete
31
+ */
32
+ delete(index: number): void;
33
+ clear(): void;
34
+ /**
35
+ * Returns an Array of all the elements in the LiveList.
36
+ */
37
+ toArray(): T[];
38
+ /**
39
+ * Tests whether all elements pass the test implemented by the provided function.
40
+ * @param predicate Function to test for each element, taking two arguments (the element and its index).
41
+ * @returns true if the predicate function returns a truthy value for every element. Otherwise, false.
42
+ */
43
+ every(predicate: (value: T, index: number) => unknown): boolean;
44
+ /**
45
+ * Creates an array with all elements that pass the test implemented by the provided function.
46
+ * @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.
47
+ * @returns An array with the elements that pass the test.
48
+ */
49
+ filter(predicate: (value: T, index: number) => unknown): T[];
50
+ /**
51
+ * Returns the first element that satisfies the provided testing function.
52
+ * @param predicate Function to execute on each value.
53
+ * @returns The value of the first element in the LiveList that satisfies the provided testing function. Otherwise, undefined is returned.
54
+ */
55
+ find(predicate: (value: T, index: number) => unknown): T | undefined;
56
+ /**
57
+ * Returns the index of the first element in the LiveList that satisfies the provided testing function.
58
+ * @param predicate Function to execute on each value until the function returns true, indicating that the satisfying element was found.
59
+ * @returns The index of the first element in the LiveList that passes the test. Otherwise, -1.
60
+ */
61
+ findIndex(predicate: (value: T, index: number) => unknown): number;
62
+ /**
63
+ * Executes a provided function once for each element.
64
+ * @param callbackfn Function to execute on each element.
65
+ */
66
+ forEach(callbackfn: (value: T, index: number) => void): void;
67
+ /**
68
+ * Get the element at the specified index.
69
+ * @param index The index on the element to get.
70
+ * @returns The element at the specified index or undefined.
71
+ */
72
+ get(index: number): T | undefined;
73
+ /**
74
+ * Returns the first index at which a given element can be found in the LiveList, or -1 if it is not present.
75
+ * @param searchElement Element to locate.
76
+ * @param fromIndex The index to start the search at.
77
+ * @returns The first index of the element in the LiveList; -1 if not found.
78
+ */
79
+ indexOf(searchElement: T, fromIndex?: number): number;
80
+ /**
81
+ * 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.
82
+ * @param searchElement Element to locate.
83
+ * @param fromIndex The index at which to start searching backwards.
84
+ * @returns
85
+ */
86
+ lastIndexOf(searchElement: T, fromIndex?: number): number;
87
+ /**
88
+ * Creates an array populated with the results of calling a provided function on every element.
89
+ * @param callback Function that is called for every element.
90
+ * @returns An array with each element being the result of the callback function.
91
+ */
92
+ map<U>(callback: (value: T, index: number) => U): U[];
93
+ /**
94
+ * Tests whether at least one element in the LiveList passes the test implemented by the provided function.
95
+ * @param predicate Function to test for each element.
96
+ * @returns true if the callback function returns a truthy value for at least one element. Otherwise, false.
97
+ */
98
+ some(predicate: (value: T, index: number) => unknown): boolean;
99
+ [Symbol.iterator](): IterableIterator<T>;
100
+ }
101
+
102
+ /**
103
+ * The LiveMap class is similar to a JavaScript Map that is synchronized on all clients.
104
+ * Keys should be a string, and values should be serializable to JSON.
105
+ * If multiple clients update the same property simultaneously, the last modification received by the Liveblocks servers is the winner.
106
+ */
107
+ declare class LiveMap<TKey extends string, TValue> extends AbstractCrdt {
108
+ #private;
109
+ constructor(entries?: readonly (readonly [TKey, TValue])[] | null | undefined);
110
+ /**
111
+ * Returns a specified element from the LiveMap.
112
+ * @param key The key of the element to return.
113
+ * @returns The element associated with the specified key, or undefined if the key can't be found in the LiveMap.
114
+ */
115
+ get(key: TKey): TValue | undefined;
116
+ /**
117
+ * Adds or updates an element with a specified key and a value.
118
+ * @param key The key of the element to add. Should be a string.
119
+ * @param value The value of the element to add. Should be serializable to JSON.
120
+ */
121
+ set(key: TKey, value: TValue): void;
122
+ /**
123
+ * Returns the number of elements in the LiveMap.
124
+ */
125
+ get size(): number;
126
+ /**
127
+ * Returns a boolean indicating whether an element with the specified key exists or not.
128
+ * @param key The key of the element to test for presence.
129
+ */
130
+ has(key: TKey): boolean;
131
+ /**
132
+ * Removes the specified element by key.
133
+ * @param key The key of the element to remove.
134
+ * @returns true if an element existed and has been removed, or false if the element does not exist.
135
+ */
136
+ delete(key: TKey): boolean;
137
+ /**
138
+ * Returns a new Iterator object that contains the [key, value] pairs for each element.
139
+ */
140
+ entries(): IterableIterator<[string, TValue]>;
141
+ /**
142
+ * Same function object as the initial value of the entries method.
143
+ */
144
+ [Symbol.iterator](): IterableIterator<[string, TValue]>;
145
+ /**
146
+ * Returns a new Iterator object that contains the keys for each element.
147
+ */
148
+ keys(): IterableIterator<TKey>;
149
+ /**
150
+ * Returns a new Iterator object that contains the values for each element.
151
+ */
152
+ values(): IterableIterator<TValue>;
153
+ /**
154
+ * Executes a provided function once per each key/value pair in the Map object, in insertion order.
155
+ * @param callback Function to execute for each entry in the map.
156
+ */
157
+ forEach(callback: (value: TValue, key: TKey, map: LiveMap<TKey, TValue>) => void): void;
158
+ }
159
+
160
+ declare type MyPresenceCallback<T extends Presence = Presence> = (me: T) => void;
161
+ declare type OthersEventCallback<T extends Presence = Presence> = (others: Others<T>, event: OthersEvent<T>) => void;
162
+ declare type EventCallback = ({ connectionId, event, }: {
8
163
  connectionId: number;
9
164
  event: any;
10
165
  }) => void;
11
- export declare type ErrorCallback = (error: Error) => void;
12
- export declare type ConnectionCallback = (state: ConnectionState) => void;
13
- export declare type RoomEventCallbackMap = {
14
- "my-presence": MyPresenceCallback;
15
- others: OthersEventCallback;
16
- event: EventCallback;
17
- error: ErrorCallback;
18
- connection: ConnectionCallback;
19
- };
20
- export declare type UpdateDelta = {
166
+ declare type ErrorCallback = (error: Error) => void;
167
+ declare type ConnectionCallback = (state: ConnectionState) => void;
168
+ declare type UpdateDelta = {
21
169
  type: "update";
22
170
  } | {
23
171
  type: "delete";
24
172
  };
25
- export declare type LiveMapUpdates<TKey extends string = string, TValue = any> = {
173
+ declare type LiveMapUpdates<TKey extends string = string, TValue = any> = {
26
174
  type: "LiveMap";
27
175
  node: LiveMap<TKey, TValue>;
28
176
  updates: Record<TKey, UpdateDelta>;
29
177
  };
30
- export declare type LiveObjectUpdateDelta<T> = Partial<{
178
+ declare type LiveObjectUpdateDelta<T> = Partial<{
31
179
  [Property in keyof T]: UpdateDelta;
32
180
  }>;
33
- export declare type LiveObjectUpdates<TData = any> = {
181
+ declare type LiveObjectUpdates<TData = any> = {
34
182
  type: "LiveObject";
35
183
  node: LiveObject<TData>;
36
184
  updates: LiveObjectUpdateDelta<TData>;
37
185
  };
38
- export declare type LiveListUpdateDelta = {
186
+ declare type LiveListUpdateDelta = {
39
187
  index: number;
40
188
  item: AbstractCrdt;
41
189
  type: "insert";
@@ -48,12 +196,12 @@ export declare type LiveListUpdateDelta = {
48
196
  item: AbstractCrdt;
49
197
  type: "move";
50
198
  };
51
- export declare type LiveListUpdates<TItem = any> = {
199
+ declare type LiveListUpdates<TItem = any> = {
52
200
  type: "LiveList";
53
201
  node: LiveList<TItem>;
54
202
  updates: LiveListUpdateDelta[];
55
203
  };
56
- export declare type BroadcastOptions = {
204
+ declare type BroadcastOptions = {
57
205
  /**
58
206
  * Whether or not event is queued if the connection is currently closed.
59
207
  *
@@ -61,9 +209,8 @@ export declare type BroadcastOptions = {
61
209
  */
62
210
  shouldQueueEventIfNotReady: boolean;
63
211
  };
64
- export declare type StorageUpdate = LiveMapUpdates | LiveObjectUpdates | LiveListUpdates;
65
- export declare type StorageCallback = (updates: StorageUpdate[]) => void;
66
- export declare type Client = {
212
+ declare type StorageUpdate = LiveMapUpdates | LiveObjectUpdates | LiveListUpdates;
213
+ declare type Client = {
67
214
  /**
68
215
  * Gets a room. Returns null if {@link Client.enter} has not been called previously.
69
216
  *
@@ -85,15 +232,10 @@ export declare type Client = {
85
232
  */
86
233
  leave(roomId: string): void;
87
234
  };
88
- export declare type AuthenticationToken = {
89
- actor: number;
90
- id?: string;
91
- info?: any;
92
- };
93
235
  /**
94
236
  * Represents all the other users connected in the room. Treated as immutable.
95
237
  */
96
- export interface Others<TPresence extends Presence = Presence> {
238
+ interface Others<TPresence extends Presence = Presence> {
97
239
  /**
98
240
  * Number of other users in the room.
99
241
  */
@@ -114,7 +256,7 @@ export interface Others<TPresence extends Presence = Presence> {
114
256
  /**
115
257
  * Represents a user connected in a room. Treated as immutable.
116
258
  */
117
- export declare type User<TPresence extends Presence = Presence> = {
259
+ declare type User<TPresence extends Presence = Presence> = {
118
260
  /**
119
261
  * The connection id of the user. It is unique and increment at every new connection.
120
262
  */
@@ -133,18 +275,18 @@ export declare type User<TPresence extends Presence = Presence> = {
133
275
  */
134
276
  readonly presence?: TPresence;
135
277
  };
136
- export declare type Presence = {
278
+ declare type Presence = {
137
279
  [key: string]: any;
138
280
  };
139
281
  declare type AuthEndpointCallback = (room: string) => Promise<{
140
282
  token: string;
141
283
  }>;
142
- export declare type AuthEndpoint = string | AuthEndpointCallback;
284
+ declare type AuthEndpoint = string | AuthEndpointCallback;
143
285
  /**
144
286
  * The authentication endpoint that is called to ensure that the current user has access to a room.
145
287
  * Can be an url or a callback if you need to add additional headers.
146
288
  */
147
- export declare type ClientOptions = {
289
+ declare type ClientOptions = {
148
290
  throttle?: number;
149
291
  fetchPolyfill?: any;
150
292
  WebSocketPolyfill?: any;
@@ -155,30 +297,8 @@ export declare type ClientOptions = {
155
297
  publicApiKey?: never;
156
298
  authEndpoint: AuthEndpoint;
157
299
  });
158
- export declare type AuthorizeResponse = {
159
- token: string;
160
- };
161
- export declare type Authentication = {
162
- type: "public";
163
- publicApiKey: string;
164
- url: string;
165
- } | {
166
- type: "private";
167
- url: string;
168
- } | {
169
- type: "custom";
170
- callback: (room: string) => Promise<AuthorizeResponse>;
171
- };
172
300
  declare type ConnectionState = "closed" | "authenticating" | "unavailable" | "failed" | "open" | "connecting";
173
- export declare type Connection = {
174
- state: "closed" | "authenticating" | "unavailable" | "failed";
175
- } | {
176
- state: "open" | "connecting";
177
- id: number;
178
- userId?: string;
179
- userInfo?: any;
180
- };
181
- export declare type OthersEvent<T extends Presence = Presence> = {
301
+ declare type OthersEvent<T extends Presence = Presence> = {
182
302
  type: "leave";
183
303
  user: User<T>;
184
304
  } | {
@@ -191,7 +311,11 @@ export declare type OthersEvent<T extends Presence = Presence> = {
191
311
  } | {
192
312
  type: "reset";
193
313
  };
194
- export declare type Room = {
314
+ declare type Room = {
315
+ /**
316
+ * The id of the room.
317
+ */
318
+ readonly id: string;
195
319
  getConnectionState(): ConnectionState;
196
320
  subscribe: {
197
321
  /**
@@ -499,4 +623,72 @@ export declare type Room = {
499
623
  */
500
624
  batch: (fn: () => void) => void;
501
625
  };
502
- export {};
626
+
627
+ declare abstract class AbstractCrdt {
628
+ #private;
629
+ get roomId(): string | null;
630
+ }
631
+
632
+ /**
633
+ * The LiveObject class is similar to a JavaScript object that is synchronized on all clients.
634
+ * Keys should be a string, and values should be serializable to JSON.
635
+ * If multiple clients update the same property simultaneously, the last modification received by the Liveblocks servers is the winner.
636
+ */
637
+ declare class LiveObject<T extends Record<string, any> = Record<string, any>> extends AbstractCrdt {
638
+ #private;
639
+ constructor(object?: T);
640
+ /**
641
+ * Transform the LiveObject into a javascript object
642
+ */
643
+ toObject(): T;
644
+ /**
645
+ * Adds or updates a property with a specified key and a value.
646
+ * @param key The key of the property to add
647
+ * @param value The value of the property to add
648
+ */
649
+ set<TKey extends keyof T>(key: TKey, value: T[TKey]): void;
650
+ /**
651
+ * Returns a specified property from the LiveObject.
652
+ * @param key The key of the property to get
653
+ */
654
+ get<TKey extends keyof T>(key: TKey): T[TKey];
655
+ /**
656
+ * Deletes a key from the LiveObject
657
+ * @param key The key of the property to delete
658
+ */
659
+ delete(key: keyof T): void;
660
+ /**
661
+ * Adds or updates multiple properties at once with an object.
662
+ * @param overrides The object used to overrides properties
663
+ */
664
+ update(overrides: Partial<T>): void;
665
+ }
666
+
667
+ /**
668
+ * Create a client that will be responsible to communicate with liveblocks servers.
669
+ *
670
+ * @example
671
+ * const client = createClient({
672
+ * authEndpoint: "/api/auth"
673
+ * });
674
+ *
675
+ * // It's also possible to use a function to call your authentication endpoint.
676
+ * // Useful to add additional headers or use an API wrapper (like Firebase functions)
677
+ * const client = createClient({
678
+ * authEndpoint: async (room) => {
679
+ * const response = await fetch("/api/auth", {
680
+ * method: "POST",
681
+ * headers: {
682
+ * Authentication: "token",
683
+ * "Content-Type": "application/json"
684
+ * },
685
+ * body: JSON.stringify({ room })
686
+ * });
687
+ *
688
+ * return await response.json();
689
+ * }
690
+ * });
691
+ */
692
+ declare function createClient(options: ClientOptions): Client;
693
+
694
+ export { BroadcastOptions, Client, LiveList, LiveMap, LiveObject, Others, Presence, Room, StorageUpdate, User, createClient };