@liveblocks/client
Advanced tools
Comparing version 0.12.0-beta.2 to 0.12.0-beta.3
@@ -10,2 +10,3 @@ import { Op, SerializedCrdtWithId, SerializedList } from "./live"; | ||
attachChild(key: any, child: ICrdt): void; | ||
detach(): void; | ||
detachChild(child: ICrdt): void; | ||
@@ -31,2 +32,3 @@ }; | ||
private applyCreateMap; | ||
private applyCreateList; | ||
private applyCreateObject; | ||
@@ -77,3 +79,3 @@ private applyDeleteRecord; | ||
} | ||
export declare class LiveMap<TKey extends string = string, TValue = any> implements ICrdt { | ||
export declare class LiveMap<TKey extends string, TValue> implements ICrdt { | ||
private _listeners; | ||
@@ -83,3 +85,3 @@ private _map; | ||
constructor(entries?: readonly (readonly [TKey, TValue])[] | null | undefined); | ||
static deserialize([id, item]: SerializedCrdtWithId, parentToChildren: Map<string, SerializedCrdtWithId[]>, doc: Doc): LiveMap<string, any>; | ||
static deserialize([id, item]: SerializedCrdtWithId, parentToChildren: Map<string, SerializedCrdtWithId[]>, doc: Doc): LiveMap<string, unknown>; | ||
get [INTERNAL](): { | ||
@@ -94,2 +96,3 @@ ctx: { | ||
attach: (id: string, doc: Doc<Record<string, any>>, parentId: string, parentKey: string) => Op[]; | ||
detach: () => void; | ||
detachChild: (child: ICrdt) => void; | ||
@@ -104,2 +107,3 @@ getParentId: () => string | undefined; | ||
private attachChild; | ||
private detach; | ||
private detachChild; | ||
@@ -106,0 +110,0 @@ get(key: TKey): TValue | undefined; |
@@ -75,3 +75,7 @@ "use strict"; | ||
} | ||
case live_1.OpType.DeleteObject: { | ||
case live_1.OpType.CreateList: { | ||
this.applyCreateList(op); | ||
break; | ||
} | ||
case live_1.OpType.DeleteCrdt: { | ||
this.applyDeleteRecord(op); | ||
@@ -119,11 +123,18 @@ break; | ||
applyCreateMap(op) { | ||
const parent = this._items.get(op.parentId); | ||
if (parent == null) { | ||
return; | ||
} | ||
const newMap = new LiveMap(); | ||
newMap[INTERNAL].attach(op.id, this, op.parentId, op.parentKey); | ||
if (op.parentId && op.parentKey) { | ||
const parent = this._items.get(op.parentId); | ||
if (parent == null) { | ||
throw new Error("Parent is missing"); | ||
} | ||
parent[INTERNAL].attachChild(op.parentKey, newMap); | ||
parent[INTERNAL].attachChild(op.parentKey, newMap); | ||
} | ||
applyCreateList(op) { | ||
const parent = this._items.get(op.parentId); | ||
if (parent == null) { | ||
return; | ||
} | ||
const newMap = new LiveList(); | ||
newMap[INTERNAL].attach(op.id, this, op.parentId, op.parentKey); | ||
parent[INTERNAL].attachChild(op.parentKey, newMap); | ||
} | ||
@@ -136,3 +147,3 @@ applyCreateObject(op) { | ||
if (parent == null) { | ||
throw new Error("Parent is missing"); | ||
return; | ||
} | ||
@@ -246,11 +257,5 @@ parent[INTERNAL].attachChild(op.parentKey, newObj); | ||
for (const [key, value] of this._map) { | ||
if (value instanceof LiveObject) { | ||
ops.push(...value.attach(doc.generateId(), doc, this._ctx.id, key)); | ||
} | ||
else if (value instanceof LiveMap) { | ||
if (isCrdt(value)) { | ||
ops.push(...value[INTERNAL].attach(doc.generateId(), doc, this._ctx.id, key)); | ||
} | ||
else if (value instanceof LiveList) { | ||
ops.push(...value[INTERNAL].attach(doc.generateId(), doc, this._ctx.id, key)); | ||
} | ||
else { | ||
@@ -264,4 +269,4 @@ createOp.data[key] = value; | ||
const previousValue = this._map.get(key); | ||
if (previousValue instanceof LiveObject) { | ||
previousValue.detach(); | ||
if (isCrdt(previousValue)) { | ||
previousValue[INTERNAL].detach(); | ||
} | ||
@@ -277,4 +282,4 @@ this._map.set(key, child); | ||
} | ||
if (child instanceof LiveObject) { | ||
child.detach(); | ||
if (child) { | ||
child[INTERNAL].detach(); | ||
} | ||
@@ -288,5 +293,5 @@ this.notify(); | ||
this._ctx.doc.deleteItem(this._ctx.id); | ||
for (const [, value] of this._map) { | ||
if (value instanceof LiveObject) { | ||
value.detach(); | ||
for (const value of this._map.values()) { | ||
if (isCrdt(value)) { | ||
value[INTERNAL].detach(); | ||
} | ||
@@ -299,4 +304,4 @@ } | ||
const oldValue = this._map.get(key); | ||
if (oldValue instanceof LiveObject) { | ||
oldValue.detach(); | ||
if (isCrdt(oldValue)) { | ||
oldValue[INTERNAL].detach(); | ||
} | ||
@@ -311,4 +316,4 @@ const value = op.data[key]; | ||
const oldValue = this._map.get(key); | ||
if (oldValue instanceof LiveObject) { | ||
oldValue.detach(); | ||
if (isCrdt(oldValue)) { | ||
oldValue[INTERNAL].detach(); | ||
} | ||
@@ -338,4 +343,4 @@ this._map.delete(key); | ||
const item = this._map.get(key); | ||
if (item instanceof LiveObject) { | ||
item.detach(); | ||
if (isCrdt(item)) { | ||
item[INTERNAL].detach(); | ||
} | ||
@@ -360,4 +365,4 @@ this._ctx.doc.dispatch([ | ||
const oldValue = this._map.get(key); | ||
if (oldValue instanceof LiveObject) { | ||
oldValue.detach(); | ||
if (isCrdt(oldValue)) { | ||
oldValue[INTERNAL].detach(); | ||
} | ||
@@ -392,3 +397,2 @@ const value = overrides[key]; | ||
exports.LiveObject = LiveObject; | ||
// TODO: Consider removing default parameter | ||
class LiveMap { | ||
@@ -430,2 +434,3 @@ constructor(entries) { | ||
attach: this.attach.bind(this), | ||
detach: this.detach.bind(this), | ||
detachChild: this.detachChild.bind(this), | ||
@@ -472,3 +477,3 @@ getParentId: this._getParentId.bind(this), | ||
const previousValue = this._map.get(key); | ||
if (previousValue instanceof LiveObject) { | ||
if (previousValue) { | ||
previousValue[INTERNAL].detach(); | ||
@@ -479,2 +484,11 @@ } | ||
} | ||
detach() { | ||
if (this._ctx == null) { | ||
return; | ||
} | ||
for (const item of this._map.values()) { | ||
item[INTERNAL].detach(); | ||
} | ||
this._ctx.doc.deleteItem(this._ctx.id); | ||
} | ||
detachChild(child) { | ||
@@ -486,8 +500,3 @@ for (const [key, value] of this._map) { | ||
} | ||
if (child instanceof LiveObject) { | ||
child[INTERNAL].detach(); | ||
} | ||
else if (child instanceof LiveRegister) { | ||
child[INTERNAL].detach(); | ||
} | ||
child[INTERNAL].detach(); | ||
this.notify(); | ||
@@ -497,6 +506,6 @@ } | ||
const value = this._map.get(key); | ||
if (value instanceof LiveRegister) { | ||
return value.data; | ||
if (value == undefined) { | ||
return undefined; | ||
} | ||
return this._map.get(key); | ||
return selfOrRegisterValue(value); | ||
} | ||
@@ -507,17 +516,8 @@ set(key, value) { | ||
const oldValue = this._map.get(key); | ||
if (oldValue instanceof LiveObject) { | ||
if (oldValue) { | ||
oldValue[INTERNAL].detach(); | ||
} | ||
if (value instanceof LiveObject) { | ||
ops.push(...value[INTERNAL].attach(this._ctx.doc.generateId(), this._ctx.doc, this._ctx.id, key)); | ||
this._map.set(key, value); | ||
} | ||
else if (value instanceof LiveMap || value instanceof LiveList) { | ||
throw new Error("Adding a map or a list inside map is not yet supported"); | ||
} | ||
else { | ||
const register = new LiveRegister(value); | ||
ops.push(...register[INTERNAL].attach(this._ctx.doc.generateId(), this._ctx.doc, this._ctx.id, key)); | ||
this._map.set(key, register); | ||
} | ||
const item = selfOrRegister(value); | ||
ops.push(...item[INTERNAL].attach(this._ctx.doc.generateId(), this._ctx.doc, this._ctx.id, key)); | ||
this._map.set(key, item); | ||
this._ctx.doc.dispatch(ops); | ||
@@ -527,12 +527,4 @@ this.notify(); | ||
else { | ||
if (value instanceof LiveObject) { | ||
this._map.set(key, value); | ||
} | ||
else if (value instanceof LiveMap || value instanceof LiveList) { | ||
throw new Error("Adding a map or a list inside map is not yet supported"); | ||
} | ||
else { | ||
const register = new LiveRegister(value); | ||
this._map.set(key, register); | ||
} | ||
const item = selfOrRegister(value); | ||
this._map.set(key, item); | ||
this.notify(); | ||
@@ -550,18 +542,9 @@ } | ||
const item = this._map.get(key); | ||
if (item instanceof LiveObject) { | ||
const itemCtx = item[INTERNAL].ctx; | ||
if (itemCtx == null) { | ||
throw new Error("Tried to detach a CRDT that is not yet attached"); | ||
if (item) { | ||
const itemId = item[INTERNAL].getId(); | ||
if (itemId != null) { | ||
item[INTERNAL].detach(); | ||
this._ctx.doc.dispatch([{ type: live_1.OpType.DeleteCrdt, id: itemId }]); | ||
} | ||
item[INTERNAL].detach(); | ||
this._ctx.doc.dispatch([{ type: live_1.OpType.DeleteObject, id: itemCtx.id }]); | ||
} | ||
else if (item instanceof LiveRegister) { | ||
const itemCtx = item[INTERNAL].ctx; | ||
if (itemCtx == null) { | ||
throw new Error("Tried to detach a CRDT that is not yet attached"); | ||
} | ||
item[INTERNAL].detach(); | ||
this._ctx.doc.dispatch([{ type: live_1.OpType.DeleteObject, id: itemCtx.id }]); | ||
} | ||
} | ||
@@ -646,7 +629,9 @@ const isDeleted = this._map.delete(key); | ||
function selfOrRegister(obj) { | ||
if (obj instanceof LiveObject) { | ||
if (obj instanceof LiveObject || | ||
obj instanceof LiveMap || | ||
obj instanceof LiveList) { | ||
return obj; | ||
} | ||
else if (obj instanceof LiveMap || obj instanceof LiveList) { | ||
throw new Error("Nested map and list are not yet supported inside a map"); | ||
else if (obj instanceof LiveRegister) { | ||
throw new Error("Internal error. LiveRegister should not be created from LiveRegister"); | ||
} | ||
@@ -692,3 +677,3 @@ else { | ||
detachChild(crdt) { | ||
throw new Error("Cannot detach CRDT on register"); | ||
throw new Error("Internal error: cannot detach CRDT on register"); | ||
} | ||
@@ -728,3 +713,3 @@ detach() { | ||
this._listeners = []; | ||
// TODO: Find a better data structure | ||
// TODO: Naive array at first, find a better data structure | ||
this._items = []; | ||
@@ -747,3 +732,3 @@ let position = undefined; | ||
for (const entry of children) { | ||
const child = LiveObject.deserialize(entry, parentToChildren, doc); | ||
const child = deserialize(entry, parentToChildren, doc); | ||
list.attachChild(entry[1].parentKey, child); | ||
@@ -801,5 +786,9 @@ } | ||
} | ||
for (const [value] of this._items) { | ||
value[INTERNAL].detach(); | ||
} | ||
this._ctx.doc.deleteItem(this._ctx.id); | ||
} | ||
attachChild(key, child) { | ||
// TODO: Handle list conflict | ||
this._items.push([child, key]); | ||
@@ -812,3 +801,3 @@ this._items.sort((itemA, itemB) => position_1.compare({ position: itemA[1] }, { position: itemB[1] })); | ||
this._items.splice(indexToDelete); | ||
if (child instanceof LiveObject) { | ||
if (child) { | ||
child[INTERNAL].detach(); | ||
@@ -917,3 +906,3 @@ } | ||
id: childRecord[INTERNAL].ctx.id, | ||
type: live_1.OpType.DeleteObject, | ||
type: live_1.OpType.DeleteCrdt, | ||
}, | ||
@@ -926,3 +915,2 @@ ]); | ||
toArray() { | ||
// TODO: typing | ||
return this._items.map((entry) => selfOrRegisterValue(entry[0])); | ||
@@ -929,0 +917,0 @@ } |
@@ -110,3 +110,3 @@ import { Presence } from "./types"; | ||
CreateObject = 4, | ||
DeleteObject = 5, | ||
DeleteCrdt = 5, | ||
DeleteObjectKey = 6, | ||
@@ -116,3 +116,3 @@ CreateMap = 7, | ||
} | ||
export declare type Op = CreateObjectOp | UpdateObjectOp | DeleteObjectOp | CreateListOp | SetParentKeyOp | DeleteObjectKeyOp | CreateMapOp | CreateRegisterOp; | ||
export declare type Op = CreateObjectOp | UpdateObjectOp | DeleteCrdtOp | CreateListOp | SetParentKeyOp | DeleteObjectKeyOp | CreateMapOp | CreateRegisterOp; | ||
export declare type UpdateObjectOp = { | ||
@@ -153,5 +153,5 @@ id: string; | ||
}; | ||
export declare type DeleteObjectOp = { | ||
export declare type DeleteCrdtOp = { | ||
id: string; | ||
type: OpType.DeleteObject; | ||
type: OpType.DeleteCrdt; | ||
}; | ||
@@ -158,0 +158,0 @@ export declare type SetParentKeyOp = { |
@@ -35,3 +35,3 @@ "use strict"; | ||
OpType[OpType["CreateObject"] = 4] = "CreateObject"; | ||
OpType[OpType["DeleteObject"] = 5] = "DeleteObject"; | ||
OpType[OpType["DeleteCrdt"] = 5] = "DeleteCrdt"; | ||
OpType[OpType["DeleteObjectKey"] = 6] = "DeleteObjectKey"; | ||
@@ -38,0 +38,0 @@ OpType[OpType["CreateMap"] = 7] = "CreateMap"; |
@@ -10,2 +10,3 @@ import { Op, SerializedCrdtWithId, SerializedList } from "./live"; | ||
attachChild(key: any, child: ICrdt): void; | ||
detach(): void; | ||
detachChild(child: ICrdt): void; | ||
@@ -31,2 +32,3 @@ }; | ||
private applyCreateMap; | ||
private applyCreateList; | ||
private applyCreateObject; | ||
@@ -77,3 +79,3 @@ private applyDeleteRecord; | ||
} | ||
export declare class LiveMap<TKey extends string = string, TValue = any> implements ICrdt { | ||
export declare class LiveMap<TKey extends string, TValue> implements ICrdt { | ||
private _listeners; | ||
@@ -83,3 +85,3 @@ private _map; | ||
constructor(entries?: readonly (readonly [TKey, TValue])[] | null | undefined); | ||
static deserialize([id, item]: SerializedCrdtWithId, parentToChildren: Map<string, SerializedCrdtWithId[]>, doc: Doc): LiveMap<string, any>; | ||
static deserialize([id, item]: SerializedCrdtWithId, parentToChildren: Map<string, SerializedCrdtWithId[]>, doc: Doc): LiveMap<string, unknown>; | ||
get [INTERNAL](): { | ||
@@ -94,2 +96,3 @@ ctx: { | ||
attach: (id: string, doc: Doc<Record<string, any>>, parentId: string, parentKey: string) => Op[]; | ||
detach: () => void; | ||
detachChild: (child: ICrdt) => void; | ||
@@ -104,2 +107,3 @@ getParentId: () => string | undefined; | ||
private attachChild; | ||
private detach; | ||
private detachChild; | ||
@@ -106,0 +110,0 @@ get(key: TKey): TValue | undefined; |
@@ -72,3 +72,7 @@ import { remove } from "./utils"; | ||
} | ||
case OpType.DeleteObject: { | ||
case OpType.CreateList: { | ||
this.applyCreateList(op); | ||
break; | ||
} | ||
case OpType.DeleteCrdt: { | ||
this.applyDeleteRecord(op); | ||
@@ -116,11 +120,18 @@ break; | ||
applyCreateMap(op) { | ||
const parent = this._items.get(op.parentId); | ||
if (parent == null) { | ||
return; | ||
} | ||
const newMap = new LiveMap(); | ||
newMap[INTERNAL].attach(op.id, this, op.parentId, op.parentKey); | ||
if (op.parentId && op.parentKey) { | ||
const parent = this._items.get(op.parentId); | ||
if (parent == null) { | ||
throw new Error("Parent is missing"); | ||
} | ||
parent[INTERNAL].attachChild(op.parentKey, newMap); | ||
parent[INTERNAL].attachChild(op.parentKey, newMap); | ||
} | ||
applyCreateList(op) { | ||
const parent = this._items.get(op.parentId); | ||
if (parent == null) { | ||
return; | ||
} | ||
const newMap = new LiveList(); | ||
newMap[INTERNAL].attach(op.id, this, op.parentId, op.parentKey); | ||
parent[INTERNAL].attachChild(op.parentKey, newMap); | ||
} | ||
@@ -133,3 +144,3 @@ applyCreateObject(op) { | ||
if (parent == null) { | ||
throw new Error("Parent is missing"); | ||
return; | ||
} | ||
@@ -242,11 +253,5 @@ parent[INTERNAL].attachChild(op.parentKey, newObj); | ||
for (const [key, value] of this._map) { | ||
if (value instanceof LiveObject) { | ||
ops.push(...value.attach(doc.generateId(), doc, this._ctx.id, key)); | ||
} | ||
else if (value instanceof LiveMap) { | ||
if (isCrdt(value)) { | ||
ops.push(...value[INTERNAL].attach(doc.generateId(), doc, this._ctx.id, key)); | ||
} | ||
else if (value instanceof LiveList) { | ||
ops.push(...value[INTERNAL].attach(doc.generateId(), doc, this._ctx.id, key)); | ||
} | ||
else { | ||
@@ -260,4 +265,4 @@ createOp.data[key] = value; | ||
const previousValue = this._map.get(key); | ||
if (previousValue instanceof LiveObject) { | ||
previousValue.detach(); | ||
if (isCrdt(previousValue)) { | ||
previousValue[INTERNAL].detach(); | ||
} | ||
@@ -273,4 +278,4 @@ this._map.set(key, child); | ||
} | ||
if (child instanceof LiveObject) { | ||
child.detach(); | ||
if (child) { | ||
child[INTERNAL].detach(); | ||
} | ||
@@ -284,5 +289,5 @@ this.notify(); | ||
this._ctx.doc.deleteItem(this._ctx.id); | ||
for (const [, value] of this._map) { | ||
if (value instanceof LiveObject) { | ||
value.detach(); | ||
for (const value of this._map.values()) { | ||
if (isCrdt(value)) { | ||
value[INTERNAL].detach(); | ||
} | ||
@@ -295,4 +300,4 @@ } | ||
const oldValue = this._map.get(key); | ||
if (oldValue instanceof LiveObject) { | ||
oldValue.detach(); | ||
if (isCrdt(oldValue)) { | ||
oldValue[INTERNAL].detach(); | ||
} | ||
@@ -307,4 +312,4 @@ const value = op.data[key]; | ||
const oldValue = this._map.get(key); | ||
if (oldValue instanceof LiveObject) { | ||
oldValue.detach(); | ||
if (isCrdt(oldValue)) { | ||
oldValue[INTERNAL].detach(); | ||
} | ||
@@ -334,4 +339,4 @@ this._map.delete(key); | ||
const item = this._map.get(key); | ||
if (item instanceof LiveObject) { | ||
item.detach(); | ||
if (isCrdt(item)) { | ||
item[INTERNAL].detach(); | ||
} | ||
@@ -356,4 +361,4 @@ this._ctx.doc.dispatch([ | ||
const oldValue = this._map.get(key); | ||
if (oldValue instanceof LiveObject) { | ||
oldValue.detach(); | ||
if (isCrdt(oldValue)) { | ||
oldValue[INTERNAL].detach(); | ||
} | ||
@@ -387,3 +392,2 @@ const value = overrides[key]; | ||
} | ||
// TODO: Consider removing default parameter | ||
export class LiveMap { | ||
@@ -425,2 +429,3 @@ constructor(entries) { | ||
attach: this.attach.bind(this), | ||
detach: this.detach.bind(this), | ||
detachChild: this.detachChild.bind(this), | ||
@@ -467,3 +472,3 @@ getParentId: this._getParentId.bind(this), | ||
const previousValue = this._map.get(key); | ||
if (previousValue instanceof LiveObject) { | ||
if (previousValue) { | ||
previousValue[INTERNAL].detach(); | ||
@@ -474,2 +479,11 @@ } | ||
} | ||
detach() { | ||
if (this._ctx == null) { | ||
return; | ||
} | ||
for (const item of this._map.values()) { | ||
item[INTERNAL].detach(); | ||
} | ||
this._ctx.doc.deleteItem(this._ctx.id); | ||
} | ||
detachChild(child) { | ||
@@ -481,8 +495,3 @@ for (const [key, value] of this._map) { | ||
} | ||
if (child instanceof LiveObject) { | ||
child[INTERNAL].detach(); | ||
} | ||
else if (child instanceof LiveRegister) { | ||
child[INTERNAL].detach(); | ||
} | ||
child[INTERNAL].detach(); | ||
this.notify(); | ||
@@ -492,6 +501,6 @@ } | ||
const value = this._map.get(key); | ||
if (value instanceof LiveRegister) { | ||
return value.data; | ||
if (value == undefined) { | ||
return undefined; | ||
} | ||
return this._map.get(key); | ||
return selfOrRegisterValue(value); | ||
} | ||
@@ -502,17 +511,8 @@ set(key, value) { | ||
const oldValue = this._map.get(key); | ||
if (oldValue instanceof LiveObject) { | ||
if (oldValue) { | ||
oldValue[INTERNAL].detach(); | ||
} | ||
if (value instanceof LiveObject) { | ||
ops.push(...value[INTERNAL].attach(this._ctx.doc.generateId(), this._ctx.doc, this._ctx.id, key)); | ||
this._map.set(key, value); | ||
} | ||
else if (value instanceof LiveMap || value instanceof LiveList) { | ||
throw new Error("Adding a map or a list inside map is not yet supported"); | ||
} | ||
else { | ||
const register = new LiveRegister(value); | ||
ops.push(...register[INTERNAL].attach(this._ctx.doc.generateId(), this._ctx.doc, this._ctx.id, key)); | ||
this._map.set(key, register); | ||
} | ||
const item = selfOrRegister(value); | ||
ops.push(...item[INTERNAL].attach(this._ctx.doc.generateId(), this._ctx.doc, this._ctx.id, key)); | ||
this._map.set(key, item); | ||
this._ctx.doc.dispatch(ops); | ||
@@ -522,12 +522,4 @@ this.notify(); | ||
else { | ||
if (value instanceof LiveObject) { | ||
this._map.set(key, value); | ||
} | ||
else if (value instanceof LiveMap || value instanceof LiveList) { | ||
throw new Error("Adding a map or a list inside map is not yet supported"); | ||
} | ||
else { | ||
const register = new LiveRegister(value); | ||
this._map.set(key, register); | ||
} | ||
const item = selfOrRegister(value); | ||
this._map.set(key, item); | ||
this.notify(); | ||
@@ -545,18 +537,9 @@ } | ||
const item = this._map.get(key); | ||
if (item instanceof LiveObject) { | ||
const itemCtx = item[INTERNAL].ctx; | ||
if (itemCtx == null) { | ||
throw new Error("Tried to detach a CRDT that is not yet attached"); | ||
if (item) { | ||
const itemId = item[INTERNAL].getId(); | ||
if (itemId != null) { | ||
item[INTERNAL].detach(); | ||
this._ctx.doc.dispatch([{ type: OpType.DeleteCrdt, id: itemId }]); | ||
} | ||
item[INTERNAL].detach(); | ||
this._ctx.doc.dispatch([{ type: OpType.DeleteObject, id: itemCtx.id }]); | ||
} | ||
else if (item instanceof LiveRegister) { | ||
const itemCtx = item[INTERNAL].ctx; | ||
if (itemCtx == null) { | ||
throw new Error("Tried to detach a CRDT that is not yet attached"); | ||
} | ||
item[INTERNAL].detach(); | ||
this._ctx.doc.dispatch([{ type: OpType.DeleteObject, id: itemCtx.id }]); | ||
} | ||
} | ||
@@ -640,7 +623,9 @@ const isDeleted = this._map.delete(key); | ||
function selfOrRegister(obj) { | ||
if (obj instanceof LiveObject) { | ||
if (obj instanceof LiveObject || | ||
obj instanceof LiveMap || | ||
obj instanceof LiveList) { | ||
return obj; | ||
} | ||
else if (obj instanceof LiveMap || obj instanceof LiveList) { | ||
throw new Error("Nested map and list are not yet supported inside a map"); | ||
else if (obj instanceof LiveRegister) { | ||
throw new Error("Internal error. LiveRegister should not be created from LiveRegister"); | ||
} | ||
@@ -686,3 +671,3 @@ else { | ||
detachChild(crdt) { | ||
throw new Error("Cannot detach CRDT on register"); | ||
throw new Error("Internal error: cannot detach CRDT on register"); | ||
} | ||
@@ -722,3 +707,3 @@ detach() { | ||
this._listeners = []; | ||
// TODO: Find a better data structure | ||
// TODO: Naive array at first, find a better data structure | ||
this._items = []; | ||
@@ -741,3 +726,3 @@ let position = undefined; | ||
for (const entry of children) { | ||
const child = LiveObject.deserialize(entry, parentToChildren, doc); | ||
const child = deserialize(entry, parentToChildren, doc); | ||
list.attachChild(entry[1].parentKey, child); | ||
@@ -795,5 +780,9 @@ } | ||
} | ||
for (const [value] of this._items) { | ||
value[INTERNAL].detach(); | ||
} | ||
this._ctx.doc.deleteItem(this._ctx.id); | ||
} | ||
attachChild(key, child) { | ||
// TODO: Handle list conflict | ||
this._items.push([child, key]); | ||
@@ -806,3 +795,3 @@ this._items.sort((itemA, itemB) => compare({ position: itemA[1] }, { position: itemB[1] })); | ||
this._items.splice(indexToDelete); | ||
if (child instanceof LiveObject) { | ||
if (child) { | ||
child[INTERNAL].detach(); | ||
@@ -911,3 +900,3 @@ } | ||
id: childRecord[INTERNAL].ctx.id, | ||
type: OpType.DeleteObject, | ||
type: OpType.DeleteCrdt, | ||
}, | ||
@@ -920,3 +909,2 @@ ]); | ||
toArray() { | ||
// TODO: typing | ||
return this._items.map((entry) => selfOrRegisterValue(entry[0])); | ||
@@ -923,0 +911,0 @@ } |
@@ -110,3 +110,3 @@ import { Presence } from "./types"; | ||
CreateObject = 4, | ||
DeleteObject = 5, | ||
DeleteCrdt = 5, | ||
DeleteObjectKey = 6, | ||
@@ -116,3 +116,3 @@ CreateMap = 7, | ||
} | ||
export declare type Op = CreateObjectOp | UpdateObjectOp | DeleteObjectOp | CreateListOp | SetParentKeyOp | DeleteObjectKeyOp | CreateMapOp | CreateRegisterOp; | ||
export declare type Op = CreateObjectOp | UpdateObjectOp | DeleteCrdtOp | CreateListOp | SetParentKeyOp | DeleteObjectKeyOp | CreateMapOp | CreateRegisterOp; | ||
export declare type UpdateObjectOp = { | ||
@@ -153,5 +153,5 @@ id: string; | ||
}; | ||
export declare type DeleteObjectOp = { | ||
export declare type DeleteCrdtOp = { | ||
id: string; | ||
type: OpType.DeleteObject; | ||
type: OpType.DeleteCrdt; | ||
}; | ||
@@ -158,0 +158,0 @@ export declare type SetParentKeyOp = { |
@@ -32,3 +32,3 @@ export var ServerMessageType; | ||
OpType[OpType["CreateObject"] = 4] = "CreateObject"; | ||
OpType[OpType["DeleteObject"] = 5] = "DeleteObject"; | ||
OpType[OpType["DeleteCrdt"] = 5] = "DeleteCrdt"; | ||
OpType[OpType["DeleteObjectKey"] = 6] = "DeleteObjectKey"; | ||
@@ -35,0 +35,0 @@ OpType[OpType["CreateMap"] = 7] = "CreateMap"; |
{ | ||
"name": "@liveblocks/client", | ||
"version": "0.12.0-beta.2", | ||
"version": "0.12.0-beta.3", | ||
"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
328434
62
9531