@liveblocks/client
Advanced tools
Comparing version 0.3.1 to 0.4.0
@@ -1,52 +0,3 @@ | ||
export declare type SerializedRecord = { | ||
id: string; | ||
type: "record"; | ||
data: { | ||
[key: string]: SerializedCrdt; | ||
}; | ||
}; | ||
export declare type SerializedList = { | ||
id: string; | ||
type: "list"; | ||
data: { | ||
[position: string]: SerializedCrdt; | ||
}; | ||
}; | ||
export declare type SerializedRegister = { | ||
id?: string; | ||
type: "register"; | ||
data: any; | ||
}; | ||
export declare type SerializedCrdt = SerializedRecord | SerializedList | SerializedRegister; | ||
export declare enum OpType { | ||
ListInsert = 0, | ||
ListMove = 1, | ||
ListRemove = 2, | ||
RecordUpdate = 3 | ||
} | ||
export declare type Op = RecordUpdateOp | ListInsertOp | ListDeleteOp | ListMoveOp; | ||
export declare type RecordUpdateOp = { | ||
id: string; | ||
type: OpType.RecordUpdate; | ||
data: { | ||
[key: string]: SerializedCrdt; | ||
}; | ||
}; | ||
export declare type ListInsertOp = { | ||
id: string; | ||
type: OpType.ListInsert; | ||
position: string; | ||
data: SerializedCrdt; | ||
}; | ||
export declare type ListMoveOp = { | ||
id: string; | ||
type: OpType.ListMove; | ||
itemId: string; | ||
position: string; | ||
}; | ||
export declare type ListDeleteOp = { | ||
id: string; | ||
type: OpType.ListRemove; | ||
itemId: string; | ||
}; | ||
import { Op, SerializedRecord } from "./live"; | ||
import { Serializable, SerializablePrimitive } from "./types"; | ||
declare type Link = { | ||
@@ -65,3 +16,3 @@ parentId: string; | ||
declare const LIST: unique symbol; | ||
export declare function createRecord<T extends Record>(id: string, data: any): T; | ||
export declare function createRecord<T extends RecordData>(id: string, data: T): Record<T>; | ||
export declare function createList<T>(id: string, items?: T[]): List<T>; | ||
@@ -71,10 +22,10 @@ export declare type RecordData = { | ||
}; | ||
declare type RecordValue = string | boolean | null | number | Array<string> | Record | List<any>; | ||
declare type RecordValue = SerializablePrimitive | Array<SerializablePrimitive> | Serializable | Record<any> | List<any>; | ||
export declare type Record<T extends RecordData = RecordData> = { | ||
readonly id: string; | ||
readonly type: typeof RECORD; | ||
} & Omit<T, "id" | "type">; | ||
readonly $$type: typeof RECORD; | ||
} & T; | ||
export declare type List<T> = { | ||
readonly id: string; | ||
readonly type: typeof LIST; | ||
readonly $$type: typeof LIST; | ||
toArray(): Array<T>; | ||
@@ -96,4 +47,4 @@ map<U>(callback: (value: T, index: number) => U): U[]; | ||
private getChild; | ||
updateRecord<T>(id: string, overrides: Partial<T>): Doc<T>; | ||
pushItem<T>(id: string, item: T): Doc<T>; | ||
updateRecord<TRecord>(id: string, overrides: Partial<TRecord>): Doc<T>; | ||
pushItem<TItem>(id: string, item: TItem): Doc<T>; | ||
moveItem(id: string, index: number, targetIndex: number): Doc<T>; | ||
@@ -100,0 +51,0 @@ deleteItem(id: string, index: number): Doc<T>; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.Doc = exports.createList = exports.createRecord = exports.OpType = void 0; | ||
exports.Doc = exports.createList = exports.createRecord = void 0; | ||
const live_1 = require("./live"); | ||
const position_1 = require("./position"); | ||
var OpType; | ||
(function (OpType) { | ||
OpType[OpType["ListInsert"] = 0] = "ListInsert"; | ||
OpType[OpType["ListMove"] = 1] = "ListMove"; | ||
OpType[OpType["ListRemove"] = 2] = "ListRemove"; | ||
OpType[OpType["RecordUpdate"] = 3] = "RecordUpdate"; | ||
})(OpType = exports.OpType || (exports.OpType = {})); | ||
const RECORD = Symbol("liveblocks.record"); | ||
const LIST = Symbol("liveblocks.list"); | ||
function createRecord(id, data) { | ||
return Object.assign({ id, type: RECORD }, data); | ||
return Object.assign({ id, $$type: RECORD }, data); | ||
} | ||
@@ -21,3 +15,3 @@ exports.createRecord = createRecord; | ||
id, | ||
type: LIST, | ||
$$type: LIST, | ||
length: items.length, | ||
@@ -39,3 +33,3 @@ toArray: () => items, | ||
id, | ||
type: RECORD, | ||
$$type: RECORD, | ||
}; | ||
@@ -52,3 +46,3 @@ return new Doc(root, { links: new Map(), listCache: new Map() }, emit); | ||
return doc.dispatch({ | ||
type: OpType.RecordUpdate, | ||
type: live_1.OpType.RecordUpdate, | ||
id: root.id, | ||
@@ -94,3 +88,3 @@ data: root.data, | ||
id: currentRecord.id, | ||
type: OpType.RecordUpdate, | ||
type: live_1.OpType.RecordUpdate, | ||
data, | ||
@@ -105,3 +99,3 @@ }; | ||
} | ||
if (list.type !== LIST) { | ||
if (list.$$type !== LIST) { | ||
throw new Error(`Node with id "${id}" is not a list`); | ||
@@ -115,3 +109,3 @@ } | ||
return this.dispatch({ | ||
type: OpType.ListInsert, | ||
type: live_1.OpType.ListInsert, | ||
id: list.id, | ||
@@ -126,3 +120,3 @@ position: position_1.makePosition(), | ||
const operation = { | ||
type: OpType.ListInsert, | ||
type: live_1.OpType.ListInsert, | ||
id: list.id, | ||
@@ -139,3 +133,3 @@ position, | ||
} | ||
if (list.type !== LIST) { | ||
if (list.$$type !== LIST) { | ||
throw new Error(`Node with id "${id}" is not a list`); | ||
@@ -149,3 +143,3 @@ } | ||
return this.dispatch({ | ||
type: OpType.ListMove, | ||
type: live_1.OpType.ListMove, | ||
id: list.id, | ||
@@ -161,3 +155,3 @@ itemId: item.id, | ||
} | ||
if (list.type !== LIST) { | ||
if (list.$$type !== LIST) { | ||
throw new Error(`Node with id "${id}" is not a list`); | ||
@@ -168,3 +162,3 @@ } | ||
return this.dispatch({ | ||
type: OpType.ListRemove, | ||
type: live_1.OpType.ListRemove, | ||
id: list.id, | ||
@@ -212,3 +206,3 @@ itemId: item.id, | ||
id: serialized.id, | ||
type: RECORD, | ||
$$type: RECORD, | ||
}; | ||
@@ -249,3 +243,3 @@ for (const key in serialized.data) { | ||
switch (op.type) { | ||
case OpType.RecordUpdate: { | ||
case live_1.OpType.RecordUpdate: { | ||
return updateRecord(record, op, cache); | ||
@@ -270,9 +264,9 @@ } | ||
switch (op.type) { | ||
case OpType.ListInsert: { | ||
case live_1.OpType.ListInsert: { | ||
return listInsert(list, op, cache); | ||
} | ||
case OpType.ListMove: { | ||
case live_1.OpType.ListMove: { | ||
return listMove(list, op, cache); | ||
} | ||
case OpType.ListRemove: { | ||
case live_1.OpType.ListRemove: { | ||
return listDelete(list, op, cache); | ||
@@ -298,3 +292,3 @@ } | ||
function dispatch(node, op, cache, links) { | ||
switch (node.type) { | ||
switch (node.$$type) { | ||
case RECORD: | ||
@@ -372,3 +366,3 @@ return dispatchOnRecord(node, op, cache, links); | ||
} | ||
if (currentNode.type === RECORD) { | ||
if (currentNode.$$type === RECORD) { | ||
currentNode = currentNode[link.parentKey]; | ||
@@ -388,6 +382,6 @@ } | ||
function isRecord(value) { | ||
return typeof value === "object" && value.type === RECORD; | ||
return typeof value === "object" && value.$$type === RECORD; | ||
} | ||
function isList(value) { | ||
return typeof value === "object" && value.type === LIST; | ||
return typeof value === "object" && value.$$type === LIST; | ||
} | ||
@@ -400,3 +394,3 @@ function isCrdt(value) { | ||
for (const key in record) { | ||
if (key !== "id" && key !== "type") { | ||
if (key !== "id" && key !== "$$type") { | ||
const value = record[key]; // TODO: Find out why typescript does not like that | ||
@@ -403,0 +397,0 @@ serializedData[key] = serialize(value); |
export type { Record, RecordData, List } from "./doc"; | ||
export { createClient } from "./client"; | ||
export { RoomState } from "./types"; | ||
export type { Others, Presence, Room, InitialStorageFactory, Client, } from "./types"; | ||
export { RoomState, LiveStorageState } from "./types"; | ||
export type { Others, Presence, Room, InitialStorageFactory, Client, LiveStorage, } from "./types"; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.RoomState = exports.createClient = void 0; | ||
exports.LiveStorageState = exports.RoomState = exports.createClient = void 0; | ||
var client_1 = require("./client"); | ||
@@ -8,1 +8,2 @@ Object.defineProperty(exports, "createClient", { enumerable: true, get: function () { return client_1.createClient; } }); | ||
Object.defineProperty(exports, "RoomState", { enumerable: true, get: function () { return types_1.RoomState; } }); | ||
Object.defineProperty(exports, "LiveStorageState", { enumerable: true, get: function () { return types_1.LiveStorageState; } }); |
@@ -48,2 +48,3 @@ "use strict"; | ||
const authentication_1 = __importStar(require("./authentication")); | ||
const live_1 = require("./live"); | ||
const BACKOFF_RETRY_DELAYS = [ | ||
@@ -61,3 +62,3 @@ 250, | ||
]; | ||
const WAIT = 100; | ||
const WAIT = 50; | ||
function isValidRoomEventType(value) { | ||
@@ -87,2 +88,3 @@ return (value === "open" || | ||
let _doc = null; | ||
let _storageState = types_1.LiveStorageState.NotInitialized; | ||
let toFlush = []; | ||
@@ -124,3 +126,3 @@ let _lastEmit = 0; | ||
send({ | ||
type: types_1.ClientMessageType.UpdateDocument, | ||
type: live_1.ClientMessageType.UpdateStorage, | ||
ops: toFlush, | ||
@@ -138,3 +140,3 @@ }); | ||
send({ | ||
type: types_1.ClientMessageType.UpdateDocument, | ||
type: live_1.ClientMessageType.UpdateStorage, | ||
ops: toFlush, | ||
@@ -146,2 +148,27 @@ }); | ||
} | ||
function getStorage() { | ||
if (_storageState === types_1.LiveStorageState.Loaded) { | ||
return { | ||
state: _storageState, | ||
root: _doc.root, | ||
}; | ||
} | ||
return { | ||
state: _storageState, | ||
}; | ||
} | ||
function fetchStorage(initialStorageFactory) { | ||
_initialStorageFactory = initialStorageFactory; | ||
_storageState = types_1.LiveStorageState.Loading; | ||
if (state === types_1.RoomState.Connected) { | ||
send({ type: live_1.ClientMessageType.FetchStorage }); | ||
} | ||
else { | ||
function onConnect() { | ||
send({ type: live_1.ClientMessageType.FetchStorage }); | ||
utils_1.remove(_listeners.open, onConnect); | ||
} | ||
_listeners.open.push(onConnect); | ||
} | ||
} | ||
function updateDoc(doc) { | ||
@@ -151,3 +178,3 @@ _doc = doc; | ||
for (const listener of _listeners.storage) { | ||
listener(doc.root); | ||
listener(getStorage()); | ||
} | ||
@@ -169,2 +196,3 @@ } | ||
function onGetDocument(message) { | ||
_storageState = types_1.LiveStorageState.Loaded; | ||
if (message.root == null) { | ||
@@ -193,3 +221,3 @@ const rootId = makeId(); | ||
if (_me != null) { | ||
send({ type: types_1.ClientMessageType.UpdateUserState, data: _me }); | ||
send({ type: live_1.ClientMessageType.UpdatePresence, data: _me }); | ||
} | ||
@@ -203,14 +231,14 @@ for (const callback of _listeners.open) { | ||
switch (message.type) { | ||
case types_1.ServerMessageType.InitialDocumentState: { | ||
case live_1.ServerMessageType.InitialDocumentState: { | ||
onGetDocument(message); | ||
break; | ||
} | ||
case types_1.ServerMessageType.DocumentOperations: { | ||
case live_1.ServerMessageType.DocumentOperations: { | ||
onDocumentUpdates(message); | ||
break; | ||
} | ||
case types_1.ServerMessageType.UserJoined: { | ||
case live_1.ServerMessageType.UserJoined: { | ||
// Send current presence to new user | ||
send({ | ||
type: types_1.ClientMessageType.UpdateUserState, | ||
type: live_1.ClientMessageType.UpdatePresence, | ||
data: _me, | ||
@@ -221,3 +249,3 @@ targetActor: message.actor, | ||
} | ||
case types_1.ServerMessageType.UpdateUserState: { | ||
case live_1.ServerMessageType.UpdatePresence: { | ||
const currentUser = _users[message.actor]; | ||
@@ -228,3 +256,3 @@ updateUsers(Object.assign(Object.assign({}, _users), { [message.actor]: currentUser | ||
} | ||
case types_1.ServerMessageType.UserLeft: { | ||
case live_1.ServerMessageType.UserLeft: { | ||
const userLeftMessage = message; | ||
@@ -308,3 +336,3 @@ const _a = _users, _b = userLeftMessage.actor, notUsed = _a[_b], rest = __rest(_a, [typeof _b === "symbol" ? _b : _b + ""]); | ||
onSocketOpenCallbacks.push(() => send({ | ||
type: types_1.ClientMessageType.UpdateUserState, | ||
type: live_1.ClientMessageType.UpdatePresence, | ||
data: _me, | ||
@@ -317,3 +345,3 @@ })); | ||
send({ | ||
type: types_1.ClientMessageType.UpdateUserState, | ||
type: live_1.ClientMessageType.UpdatePresence, | ||
data: Object.assign(Object.assign({}, _toSend), overrides), | ||
@@ -332,3 +360,3 @@ }); | ||
send({ | ||
type: types_1.ClientMessageType.UpdateUserState, | ||
type: live_1.ClientMessageType.UpdatePresence, | ||
data: _toSend, | ||
@@ -373,18 +401,4 @@ }); | ||
///////////// | ||
getStorage: () => { | ||
return _doc == null ? null : _doc.root; | ||
}, | ||
fetchStorageOrInit(initialStorageFactory) { | ||
_initialStorageFactory = initialStorageFactory; | ||
if (state === types_1.RoomState.Connected) { | ||
send({ type: types_1.ClientMessageType.GetDocument }); | ||
} | ||
else { | ||
function onConnect() { | ||
send({ type: types_1.ClientMessageType.GetDocument }); | ||
utils_1.remove(_listeners.open, onConnect); | ||
} | ||
_listeners.open.push(onConnect); | ||
} | ||
}, | ||
getStorage, | ||
fetchStorage, | ||
createRecord, | ||
@@ -391,0 +405,0 @@ createList, |
@@ -22,2 +22,13 @@ import { RecordData, Record, List } from "./doc"; | ||
}; | ||
export declare enum LiveStorageState { | ||
NotInitialized = 0, | ||
Loading = 1, | ||
Loaded = 2 | ||
} | ||
export declare type LiveStorage<T extends RecordData = RecordData> = { | ||
state: LiveStorageState.Loading | LiveStorageState.NotInitialized; | ||
} | { | ||
state: LiveStorageState.Loaded; | ||
root: Record<T>; | ||
}; | ||
export declare type Room = { | ||
@@ -42,10 +53,10 @@ connect(): void; | ||
}; | ||
getStorage: () => Record<any> | null; | ||
fetchStorageOrInit(initialStorageFactory: InitialStorageFactory): void; | ||
getStorage: () => LiveStorage; | ||
fetchStorage(initialStorageFactory: InitialStorageFactory): void; | ||
createRecord: <T extends RecordData>(data: T) => Record<T>; | ||
createList: <T extends Record>() => List<T>; | ||
createList: <T extends RecordData>() => List<Record<T>>; | ||
updateRecord<T extends RecordData>(record: Record<T>, overrides: Partial<T>): void; | ||
pushItem<T extends Record>(list: List<T>, item: T): void; | ||
deleteItem<T extends Record>(list: List<T>, index: number): void; | ||
moveItem<T extends Record>(list: List<T>, index: number, targetIndex: number): void; | ||
pushItem<T extends RecordData>(list: List<Record<T>>, item: Record<T>): void; | ||
deleteItem<T extends RecordData>(list: List<Record<T>>, index: number): void; | ||
moveItem<T extends RecordData>(list: List<Record<T>>, index: number, targetIndex: number): void; | ||
getPresence: <T extends Presence>() => T | null; | ||
@@ -55,3 +66,3 @@ getOthers: <T extends Presence>() => Others<T>; | ||
}; | ||
export declare type StorageEventCallback<T extends RecordData = RecordData> = (root: Record<T> | null) => void; | ||
export declare type StorageEventCallback<T extends RecordData = RecordData> = (storage: LiveStorage<T>) => void; | ||
export declare type MyPresenceEventCallback<T extends Presence = Presence> = (me: T) => void; | ||
@@ -89,103 +100,2 @@ export declare type OthersPresenceEventCallback<T extends Presence = Presence> = (others: Others<T>) => void; | ||
}; | ||
/** | ||
* Internals | ||
*/ | ||
export declare type ServerMessage = UpdateUserStateMessage | UserJoinMessage | UserLeftMessage | InitialDocumentStateMessage | DocumentOperationsMessage; | ||
export declare enum ServerMessageType { | ||
UpdateUserState = 100, | ||
UserJoined = 101, | ||
UserLeft = 102, | ||
InitialDocumentState = 200, | ||
DocumentOperations = 201 | ||
} | ||
export declare type UpdateUserStateMessage = { | ||
type: ServerMessageType.UpdateUserState; | ||
actor: number; | ||
data: Partial<Presence>; | ||
}; | ||
export declare type UserJoinMessage = { | ||
type: ServerMessageType.UserJoined; | ||
actor: number; | ||
}; | ||
export declare type UserLeftMessage = { | ||
type: ServerMessageType.UserLeft; | ||
actor: number; | ||
}; | ||
export declare type InitialDocumentStateMessage = { | ||
type: ServerMessageType.InitialDocumentState; | ||
root: SerializedRecord | null; | ||
}; | ||
export declare type DocumentOperationsMessage = { | ||
type: ServerMessageType.DocumentOperations; | ||
ops: Op[]; | ||
}; | ||
export declare type ClientMessage = UpdatePresenceClientMessage | UpdateDocumentClientMessage | GetDocumentClientMessage; | ||
export declare enum ClientMessageType { | ||
UpdateUserState = 100, | ||
GetDocument = 200, | ||
UpdateDocument = 201 | ||
} | ||
export declare type UpdatePresenceClientMessage = { | ||
type: ClientMessageType.UpdateUserState; | ||
data: Partial<Presence>; | ||
targetActor?: number; | ||
}; | ||
export declare type UpdateDocumentClientMessage = { | ||
type: ClientMessageType.UpdateDocument; | ||
ops: Op[]; | ||
}; | ||
export declare type GetDocumentClientMessage = { | ||
type: ClientMessageType.GetDocument; | ||
}; | ||
export declare type SerializedRecord = { | ||
id: string; | ||
type: "record"; | ||
data: { | ||
[key: string]: SerializedCrdt; | ||
}; | ||
}; | ||
export declare type SerializedList = { | ||
id: string; | ||
type: "list"; | ||
data: { | ||
[position: string]: SerializedCrdt; | ||
}; | ||
}; | ||
export declare type SerializedRegister = { | ||
id?: string; | ||
type: "register"; | ||
data: any; | ||
}; | ||
export declare type SerializedCrdt = SerializedRecord | SerializedList | SerializedRegister; | ||
export declare enum OpType { | ||
ListInsert = 0, | ||
ListMove = 1, | ||
ListRemove = 2, | ||
RecordUpdate = 3 | ||
} | ||
export declare type Op = RecordUpdateOp | ListInsertOp | ListDeleteOp | ListMoveOp; | ||
export declare type RecordUpdateOp = { | ||
id: string; | ||
type: OpType.RecordUpdate; | ||
data: { | ||
[key: string]: SerializedCrdt; | ||
}; | ||
}; | ||
export declare type ListInsertOp = { | ||
id: string; | ||
type: OpType.ListInsert; | ||
position: string; | ||
data: SerializedCrdt; | ||
}; | ||
export declare type ListMoveOp = { | ||
id: string; | ||
type: OpType.ListMove; | ||
itemId: string; | ||
position: string; | ||
}; | ||
export declare type ListDeleteOp = { | ||
id: string; | ||
type: OpType.ListRemove; | ||
itemId: string; | ||
}; | ||
export {}; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.OpType = exports.ClientMessageType = exports.ServerMessageType = exports.RoomState = void 0; | ||
exports.RoomState = exports.LiveStorageState = void 0; | ||
var LiveStorageState; | ||
(function (LiveStorageState) { | ||
LiveStorageState[LiveStorageState["NotInitialized"] = 0] = "NotInitialized"; | ||
LiveStorageState[LiveStorageState["Loading"] = 1] = "Loading"; | ||
LiveStorageState[LiveStorageState["Loaded"] = 2] = "Loaded"; | ||
})(LiveStorageState = exports.LiveStorageState || (exports.LiveStorageState = {})); | ||
var RoomState; | ||
@@ -11,22 +17,1 @@ (function (RoomState) { | ||
})(RoomState = exports.RoomState || (exports.RoomState = {})); | ||
var ServerMessageType; | ||
(function (ServerMessageType) { | ||
ServerMessageType[ServerMessageType["UpdateUserState"] = 100] = "UpdateUserState"; | ||
ServerMessageType[ServerMessageType["UserJoined"] = 101] = "UserJoined"; | ||
ServerMessageType[ServerMessageType["UserLeft"] = 102] = "UserLeft"; | ||
ServerMessageType[ServerMessageType["InitialDocumentState"] = 200] = "InitialDocumentState"; | ||
ServerMessageType[ServerMessageType["DocumentOperations"] = 201] = "DocumentOperations"; | ||
})(ServerMessageType = exports.ServerMessageType || (exports.ServerMessageType = {})); | ||
var ClientMessageType; | ||
(function (ClientMessageType) { | ||
ClientMessageType[ClientMessageType["UpdateUserState"] = 100] = "UpdateUserState"; | ||
ClientMessageType[ClientMessageType["GetDocument"] = 200] = "GetDocument"; | ||
ClientMessageType[ClientMessageType["UpdateDocument"] = 201] = "UpdateDocument"; | ||
})(ClientMessageType = exports.ClientMessageType || (exports.ClientMessageType = {})); | ||
var OpType; | ||
(function (OpType) { | ||
OpType[OpType["ListInsert"] = 0] = "ListInsert"; | ||
OpType[OpType["ListMove"] = 1] = "ListMove"; | ||
OpType[OpType["ListRemove"] = 2] = "ListRemove"; | ||
OpType[OpType["RecordUpdate"] = 3] = "RecordUpdate"; | ||
})(OpType = exports.OpType || (exports.OpType = {})); |
@@ -1,52 +0,3 @@ | ||
export declare type SerializedRecord = { | ||
id: string; | ||
type: "record"; | ||
data: { | ||
[key: string]: SerializedCrdt; | ||
}; | ||
}; | ||
export declare type SerializedList = { | ||
id: string; | ||
type: "list"; | ||
data: { | ||
[position: string]: SerializedCrdt; | ||
}; | ||
}; | ||
export declare type SerializedRegister = { | ||
id?: string; | ||
type: "register"; | ||
data: any; | ||
}; | ||
export declare type SerializedCrdt = SerializedRecord | SerializedList | SerializedRegister; | ||
export declare enum OpType { | ||
ListInsert = 0, | ||
ListMove = 1, | ||
ListRemove = 2, | ||
RecordUpdate = 3 | ||
} | ||
export declare type Op = RecordUpdateOp | ListInsertOp | ListDeleteOp | ListMoveOp; | ||
export declare type RecordUpdateOp = { | ||
id: string; | ||
type: OpType.RecordUpdate; | ||
data: { | ||
[key: string]: SerializedCrdt; | ||
}; | ||
}; | ||
export declare type ListInsertOp = { | ||
id: string; | ||
type: OpType.ListInsert; | ||
position: string; | ||
data: SerializedCrdt; | ||
}; | ||
export declare type ListMoveOp = { | ||
id: string; | ||
type: OpType.ListMove; | ||
itemId: string; | ||
position: string; | ||
}; | ||
export declare type ListDeleteOp = { | ||
id: string; | ||
type: OpType.ListRemove; | ||
itemId: string; | ||
}; | ||
import { Op, SerializedRecord } from "./live"; | ||
import { Serializable, SerializablePrimitive } from "./types"; | ||
declare type Link = { | ||
@@ -65,3 +16,3 @@ parentId: string; | ||
declare const LIST: unique symbol; | ||
export declare function createRecord<T extends Record>(id: string, data: any): T; | ||
export declare function createRecord<T extends RecordData>(id: string, data: T): Record<T>; | ||
export declare function createList<T>(id: string, items?: T[]): List<T>; | ||
@@ -71,10 +22,10 @@ export declare type RecordData = { | ||
}; | ||
declare type RecordValue = string | boolean | null | number | Array<string> | Record | List<any>; | ||
declare type RecordValue = SerializablePrimitive | Array<SerializablePrimitive> | Serializable | Record<any> | List<any>; | ||
export declare type Record<T extends RecordData = RecordData> = { | ||
readonly id: string; | ||
readonly type: typeof RECORD; | ||
} & Omit<T, "id" | "type">; | ||
readonly $$type: typeof RECORD; | ||
} & T; | ||
export declare type List<T> = { | ||
readonly id: string; | ||
readonly type: typeof LIST; | ||
readonly $$type: typeof LIST; | ||
toArray(): Array<T>; | ||
@@ -96,4 +47,4 @@ map<U>(callback: (value: T, index: number) => U): U[]; | ||
private getChild; | ||
updateRecord<T>(id: string, overrides: Partial<T>): Doc<T>; | ||
pushItem<T>(id: string, item: T): Doc<T>; | ||
updateRecord<TRecord>(id: string, overrides: Partial<TRecord>): Doc<T>; | ||
pushItem<TItem>(id: string, item: TItem): Doc<T>; | ||
moveItem(id: string, index: number, targetIndex: number): Doc<T>; | ||
@@ -100,0 +51,0 @@ deleteItem(id: string, index: number): Doc<T>; |
@@ -0,13 +1,7 @@ | ||
import { OpType, } from "./live"; | ||
import { compare, makePosition } from "./position"; | ||
export var OpType; | ||
(function (OpType) { | ||
OpType[OpType["ListInsert"] = 0] = "ListInsert"; | ||
OpType[OpType["ListMove"] = 1] = "ListMove"; | ||
OpType[OpType["ListRemove"] = 2] = "ListRemove"; | ||
OpType[OpType["RecordUpdate"] = 3] = "RecordUpdate"; | ||
})(OpType || (OpType = {})); | ||
const RECORD = Symbol("liveblocks.record"); | ||
const LIST = Symbol("liveblocks.list"); | ||
export function createRecord(id, data) { | ||
return Object.assign({ id, type: RECORD }, data); | ||
return Object.assign({ id, $$type: RECORD }, data); | ||
} | ||
@@ -17,3 +11,3 @@ export function createList(id, items = []) { | ||
id, | ||
type: LIST, | ||
$$type: LIST, | ||
length: items.length, | ||
@@ -34,3 +28,3 @@ toArray: () => items, | ||
id, | ||
type: RECORD, | ||
$$type: RECORD, | ||
}; | ||
@@ -98,3 +92,3 @@ return new Doc(root, { links: new Map(), listCache: new Map() }, emit); | ||
} | ||
if (list.type !== LIST) { | ||
if (list.$$type !== LIST) { | ||
throw new Error(`Node with id "${id}" is not a list`); | ||
@@ -130,3 +124,3 @@ } | ||
} | ||
if (list.type !== LIST) { | ||
if (list.$$type !== LIST) { | ||
throw new Error(`Node with id "${id}" is not a list`); | ||
@@ -151,3 +145,3 @@ } | ||
} | ||
if (list.type !== LIST) { | ||
if (list.$$type !== LIST) { | ||
throw new Error(`Node with id "${id}" is not a list`); | ||
@@ -200,3 +194,3 @@ } | ||
id: serialized.id, | ||
type: RECORD, | ||
$$type: RECORD, | ||
}; | ||
@@ -284,3 +278,3 @@ for (const key in serialized.data) { | ||
function dispatch(node, op, cache, links) { | ||
switch (node.type) { | ||
switch (node.$$type) { | ||
case RECORD: | ||
@@ -358,3 +352,3 @@ return dispatchOnRecord(node, op, cache, links); | ||
} | ||
if (currentNode.type === RECORD) { | ||
if (currentNode.$$type === RECORD) { | ||
currentNode = currentNode[link.parentKey]; | ||
@@ -374,6 +368,6 @@ } | ||
function isRecord(value) { | ||
return typeof value === "object" && value.type === RECORD; | ||
return typeof value === "object" && value.$$type === RECORD; | ||
} | ||
function isList(value) { | ||
return typeof value === "object" && value.type === LIST; | ||
return typeof value === "object" && value.$$type === LIST; | ||
} | ||
@@ -386,3 +380,3 @@ function isCrdt(value) { | ||
for (const key in record) { | ||
if (key !== "id" && key !== "type") { | ||
if (key !== "id" && key !== "$$type") { | ||
const value = record[key]; // TODO: Find out why typescript does not like that | ||
@@ -389,0 +383,0 @@ serializedData[key] = serialize(value); |
export type { Record, RecordData, List } from "./doc"; | ||
export { createClient } from "./client"; | ||
export { RoomState } from "./types"; | ||
export type { Others, Presence, Room, InitialStorageFactory, Client, } from "./types"; | ||
export { RoomState, LiveStorageState } from "./types"; | ||
export type { Others, Presence, Room, InitialStorageFactory, Client, LiveStorage, } from "./types"; |
export { createClient } from "./client"; | ||
export { RoomState } from "./types"; | ||
export { RoomState, LiveStorageState } from "./types"; |
@@ -22,6 +22,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
import { Doc } from "./doc"; | ||
import { ClientMessageType, ServerMessageType, RoomState, } from "./types"; | ||
import { RoomState, LiveStorageState, } from "./types"; | ||
import { createRecord as innerCreateRecord, createList as innerCreateList, } from "./doc"; | ||
import { remove } from "./utils"; | ||
import auth, { parseToken } from "./authentication"; | ||
import { ClientMessageType, ServerMessageType, } from "./live"; | ||
const BACKOFF_RETRY_DELAYS = [ | ||
@@ -39,3 +40,3 @@ 250, | ||
]; | ||
const WAIT = 100; | ||
const WAIT = 50; | ||
function isValidRoomEventType(value) { | ||
@@ -65,2 +66,3 @@ return (value === "open" || | ||
let _doc = null; | ||
let _storageState = LiveStorageState.NotInitialized; | ||
let toFlush = []; | ||
@@ -102,3 +104,3 @@ let _lastEmit = 0; | ||
send({ | ||
type: ClientMessageType.UpdateDocument, | ||
type: ClientMessageType.UpdateStorage, | ||
ops: toFlush, | ||
@@ -116,3 +118,3 @@ }); | ||
send({ | ||
type: ClientMessageType.UpdateDocument, | ||
type: ClientMessageType.UpdateStorage, | ||
ops: toFlush, | ||
@@ -124,2 +126,27 @@ }); | ||
} | ||
function getStorage() { | ||
if (_storageState === LiveStorageState.Loaded) { | ||
return { | ||
state: _storageState, | ||
root: _doc.root, | ||
}; | ||
} | ||
return { | ||
state: _storageState, | ||
}; | ||
} | ||
function fetchStorage(initialStorageFactory) { | ||
_initialStorageFactory = initialStorageFactory; | ||
_storageState = LiveStorageState.Loading; | ||
if (state === RoomState.Connected) { | ||
send({ type: ClientMessageType.FetchStorage }); | ||
} | ||
else { | ||
function onConnect() { | ||
send({ type: ClientMessageType.FetchStorage }); | ||
remove(_listeners.open, onConnect); | ||
} | ||
_listeners.open.push(onConnect); | ||
} | ||
} | ||
function updateDoc(doc) { | ||
@@ -129,3 +156,3 @@ _doc = doc; | ||
for (const listener of _listeners.storage) { | ||
listener(doc.root); | ||
listener(getStorage()); | ||
} | ||
@@ -147,2 +174,3 @@ } | ||
function onGetDocument(message) { | ||
_storageState = LiveStorageState.Loaded; | ||
if (message.root == null) { | ||
@@ -171,3 +199,3 @@ const rootId = makeId(); | ||
if (_me != null) { | ||
send({ type: ClientMessageType.UpdateUserState, data: _me }); | ||
send({ type: ClientMessageType.UpdatePresence, data: _me }); | ||
} | ||
@@ -192,3 +220,3 @@ for (const callback of _listeners.open) { | ||
send({ | ||
type: ClientMessageType.UpdateUserState, | ||
type: ClientMessageType.UpdatePresence, | ||
data: _me, | ||
@@ -199,3 +227,3 @@ targetActor: message.actor, | ||
} | ||
case ServerMessageType.UpdateUserState: { | ||
case ServerMessageType.UpdatePresence: { | ||
const currentUser = _users[message.actor]; | ||
@@ -285,3 +313,3 @@ updateUsers(Object.assign(Object.assign({}, _users), { [message.actor]: currentUser | ||
onSocketOpenCallbacks.push(() => send({ | ||
type: ClientMessageType.UpdateUserState, | ||
type: ClientMessageType.UpdatePresence, | ||
data: _me, | ||
@@ -294,3 +322,3 @@ })); | ||
send({ | ||
type: ClientMessageType.UpdateUserState, | ||
type: ClientMessageType.UpdatePresence, | ||
data: Object.assign(Object.assign({}, _toSend), overrides), | ||
@@ -309,3 +337,3 @@ }); | ||
send({ | ||
type: ClientMessageType.UpdateUserState, | ||
type: ClientMessageType.UpdatePresence, | ||
data: _toSend, | ||
@@ -350,18 +378,4 @@ }); | ||
///////////// | ||
getStorage: () => { | ||
return _doc == null ? null : _doc.root; | ||
}, | ||
fetchStorageOrInit(initialStorageFactory) { | ||
_initialStorageFactory = initialStorageFactory; | ||
if (state === RoomState.Connected) { | ||
send({ type: ClientMessageType.GetDocument }); | ||
} | ||
else { | ||
function onConnect() { | ||
send({ type: ClientMessageType.GetDocument }); | ||
remove(_listeners.open, onConnect); | ||
} | ||
_listeners.open.push(onConnect); | ||
} | ||
}, | ||
getStorage, | ||
fetchStorage, | ||
createRecord, | ||
@@ -368,0 +382,0 @@ createList, |
@@ -22,2 +22,13 @@ import { RecordData, Record, List } from "./doc"; | ||
}; | ||
export declare enum LiveStorageState { | ||
NotInitialized = 0, | ||
Loading = 1, | ||
Loaded = 2 | ||
} | ||
export declare type LiveStorage<T extends RecordData = RecordData> = { | ||
state: LiveStorageState.Loading | LiveStorageState.NotInitialized; | ||
} | { | ||
state: LiveStorageState.Loaded; | ||
root: Record<T>; | ||
}; | ||
export declare type Room = { | ||
@@ -42,10 +53,10 @@ connect(): void; | ||
}; | ||
getStorage: () => Record<any> | null; | ||
fetchStorageOrInit(initialStorageFactory: InitialStorageFactory): void; | ||
getStorage: () => LiveStorage; | ||
fetchStorage(initialStorageFactory: InitialStorageFactory): void; | ||
createRecord: <T extends RecordData>(data: T) => Record<T>; | ||
createList: <T extends Record>() => List<T>; | ||
createList: <T extends RecordData>() => List<Record<T>>; | ||
updateRecord<T extends RecordData>(record: Record<T>, overrides: Partial<T>): void; | ||
pushItem<T extends Record>(list: List<T>, item: T): void; | ||
deleteItem<T extends Record>(list: List<T>, index: number): void; | ||
moveItem<T extends Record>(list: List<T>, index: number, targetIndex: number): void; | ||
pushItem<T extends RecordData>(list: List<Record<T>>, item: Record<T>): void; | ||
deleteItem<T extends RecordData>(list: List<Record<T>>, index: number): void; | ||
moveItem<T extends RecordData>(list: List<Record<T>>, index: number, targetIndex: number): void; | ||
getPresence: <T extends Presence>() => T | null; | ||
@@ -55,3 +66,3 @@ getOthers: <T extends Presence>() => Others<T>; | ||
}; | ||
export declare type StorageEventCallback<T extends RecordData = RecordData> = (root: Record<T> | null) => void; | ||
export declare type StorageEventCallback<T extends RecordData = RecordData> = (storage: LiveStorage<T>) => void; | ||
export declare type MyPresenceEventCallback<T extends Presence = Presence> = (me: T) => void; | ||
@@ -89,103 +100,2 @@ export declare type OthersPresenceEventCallback<T extends Presence = Presence> = (others: Others<T>) => void; | ||
}; | ||
/** | ||
* Internals | ||
*/ | ||
export declare type ServerMessage = UpdateUserStateMessage | UserJoinMessage | UserLeftMessage | InitialDocumentStateMessage | DocumentOperationsMessage; | ||
export declare enum ServerMessageType { | ||
UpdateUserState = 100, | ||
UserJoined = 101, | ||
UserLeft = 102, | ||
InitialDocumentState = 200, | ||
DocumentOperations = 201 | ||
} | ||
export declare type UpdateUserStateMessage = { | ||
type: ServerMessageType.UpdateUserState; | ||
actor: number; | ||
data: Partial<Presence>; | ||
}; | ||
export declare type UserJoinMessage = { | ||
type: ServerMessageType.UserJoined; | ||
actor: number; | ||
}; | ||
export declare type UserLeftMessage = { | ||
type: ServerMessageType.UserLeft; | ||
actor: number; | ||
}; | ||
export declare type InitialDocumentStateMessage = { | ||
type: ServerMessageType.InitialDocumentState; | ||
root: SerializedRecord | null; | ||
}; | ||
export declare type DocumentOperationsMessage = { | ||
type: ServerMessageType.DocumentOperations; | ||
ops: Op[]; | ||
}; | ||
export declare type ClientMessage = UpdatePresenceClientMessage | UpdateDocumentClientMessage | GetDocumentClientMessage; | ||
export declare enum ClientMessageType { | ||
UpdateUserState = 100, | ||
GetDocument = 200, | ||
UpdateDocument = 201 | ||
} | ||
export declare type UpdatePresenceClientMessage = { | ||
type: ClientMessageType.UpdateUserState; | ||
data: Partial<Presence>; | ||
targetActor?: number; | ||
}; | ||
export declare type UpdateDocumentClientMessage = { | ||
type: ClientMessageType.UpdateDocument; | ||
ops: Op[]; | ||
}; | ||
export declare type GetDocumentClientMessage = { | ||
type: ClientMessageType.GetDocument; | ||
}; | ||
export declare type SerializedRecord = { | ||
id: string; | ||
type: "record"; | ||
data: { | ||
[key: string]: SerializedCrdt; | ||
}; | ||
}; | ||
export declare type SerializedList = { | ||
id: string; | ||
type: "list"; | ||
data: { | ||
[position: string]: SerializedCrdt; | ||
}; | ||
}; | ||
export declare type SerializedRegister = { | ||
id?: string; | ||
type: "register"; | ||
data: any; | ||
}; | ||
export declare type SerializedCrdt = SerializedRecord | SerializedList | SerializedRegister; | ||
export declare enum OpType { | ||
ListInsert = 0, | ||
ListMove = 1, | ||
ListRemove = 2, | ||
RecordUpdate = 3 | ||
} | ||
export declare type Op = RecordUpdateOp | ListInsertOp | ListDeleteOp | ListMoveOp; | ||
export declare type RecordUpdateOp = { | ||
id: string; | ||
type: OpType.RecordUpdate; | ||
data: { | ||
[key: string]: SerializedCrdt; | ||
}; | ||
}; | ||
export declare type ListInsertOp = { | ||
id: string; | ||
type: OpType.ListInsert; | ||
position: string; | ||
data: SerializedCrdt; | ||
}; | ||
export declare type ListMoveOp = { | ||
id: string; | ||
type: OpType.ListMove; | ||
itemId: string; | ||
position: string; | ||
}; | ||
export declare type ListDeleteOp = { | ||
id: string; | ||
type: OpType.ListRemove; | ||
itemId: string; | ||
}; | ||
export {}; |
@@ -0,1 +1,7 @@ | ||
export var LiveStorageState; | ||
(function (LiveStorageState) { | ||
LiveStorageState[LiveStorageState["NotInitialized"] = 0] = "NotInitialized"; | ||
LiveStorageState[LiveStorageState["Loading"] = 1] = "Loading"; | ||
LiveStorageState[LiveStorageState["Loaded"] = 2] = "Loaded"; | ||
})(LiveStorageState || (LiveStorageState = {})); | ||
export var RoomState; | ||
@@ -8,22 +14,1 @@ (function (RoomState) { | ||
})(RoomState || (RoomState = {})); | ||
export var ServerMessageType; | ||
(function (ServerMessageType) { | ||
ServerMessageType[ServerMessageType["UpdateUserState"] = 100] = "UpdateUserState"; | ||
ServerMessageType[ServerMessageType["UserJoined"] = 101] = "UserJoined"; | ||
ServerMessageType[ServerMessageType["UserLeft"] = 102] = "UserLeft"; | ||
ServerMessageType[ServerMessageType["InitialDocumentState"] = 200] = "InitialDocumentState"; | ||
ServerMessageType[ServerMessageType["DocumentOperations"] = 201] = "DocumentOperations"; | ||
})(ServerMessageType || (ServerMessageType = {})); | ||
export var ClientMessageType; | ||
(function (ClientMessageType) { | ||
ClientMessageType[ClientMessageType["UpdateUserState"] = 100] = "UpdateUserState"; | ||
ClientMessageType[ClientMessageType["GetDocument"] = 200] = "GetDocument"; | ||
ClientMessageType[ClientMessageType["UpdateDocument"] = 201] = "UpdateDocument"; | ||
})(ClientMessageType || (ClientMessageType = {})); | ||
export var OpType; | ||
(function (OpType) { | ||
OpType[OpType["ListInsert"] = 0] = "ListInsert"; | ||
OpType[OpType["ListMove"] = 1] = "ListMove"; | ||
OpType[OpType["ListRemove"] = 2] = "ListRemove"; | ||
OpType[OpType["RecordUpdate"] = 3] = "RecordUpdate"; | ||
})(OpType || (OpType = {})); |
{ | ||
"name": "@liveblocks/client", | ||
"version": "0.3.1", | ||
"version": "0.4.0", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "./lib/cjs/index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
94751
37
2716