@liveblocks/react
Advanced tools
Comparing version 0.12.0-beta.2 to 0.12.0-beta.3
@@ -1,2 +0,2 @@ | ||
import { Client, Others, Presence, LiveObject, LiveMap, User } from "@liveblocks/client"; | ||
import { Client, Others, Presence, LiveObject, LiveMap, User, LiveList } from "@liveblocks/client"; | ||
import * as React from "react"; | ||
@@ -132,2 +132,4 @@ declare type LiveblocksProviderProps = { | ||
export declare function useMap<TKey extends string, TValue>(key: string): LiveMap<TKey, TValue> | null; | ||
export declare function useList<TValue>(key: string): LiveList<TValue> | null; | ||
export declare function useObject<TData>(key: string): LiveObject<TData> | null; | ||
export {}; |
187
lib/index.js
@@ -98,3 +98,3 @@ Object.defineProperty(exports, '__esModule', { value: true }); | ||
OpType[OpType["CreateObject"] = 4] = "CreateObject"; | ||
OpType[OpType["DeleteObject"] = 5] = "DeleteObject"; | ||
OpType[OpType["DeleteCrdt"] = 5] = "DeleteCrdt"; | ||
OpType[OpType["DeleteObjectKey"] = 6] = "DeleteObjectKey"; | ||
@@ -288,11 +288,5 @@ OpType[OpType["CreateMap"] = 7] = "CreateMap"; | ||
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 { | ||
@@ -306,4 +300,4 @@ createOp.data[key] = value; | ||
const previousValue = this._map.get(key); | ||
if (previousValue instanceof LiveObject) { | ||
previousValue.detach(); | ||
if (isCrdt(previousValue)) { | ||
previousValue[INTERNAL].detach(); | ||
} | ||
@@ -319,4 +313,4 @@ this._map.set(key, child); | ||
} | ||
if (child instanceof LiveObject) { | ||
child.detach(); | ||
if (child) { | ||
child[INTERNAL].detach(); | ||
} | ||
@@ -330,5 +324,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(); | ||
} | ||
@@ -341,4 +335,4 @@ } | ||
const oldValue = this._map.get(key); | ||
if (oldValue instanceof LiveObject) { | ||
oldValue.detach(); | ||
if (isCrdt(oldValue)) { | ||
oldValue[INTERNAL].detach(); | ||
} | ||
@@ -353,4 +347,4 @@ const value = op.data[key]; | ||
const oldValue = this._map.get(key); | ||
if (oldValue instanceof LiveObject) { | ||
oldValue.detach(); | ||
if (isCrdt(oldValue)) { | ||
oldValue[INTERNAL].detach(); | ||
} | ||
@@ -379,4 +373,4 @@ this._map.delete(key); | ||
const item = this._map.get(key); | ||
if (item instanceof LiveObject) { | ||
item.detach(); | ||
if (isCrdt(item)) { | ||
item[INTERNAL].detach(); | ||
} | ||
@@ -401,4 +395,4 @@ this._ctx.doc.dispatch([ | ||
const oldValue = this._map.get(key); | ||
if (oldValue instanceof LiveObject) { | ||
oldValue.detach(); | ||
if (isCrdt(oldValue)) { | ||
oldValue[INTERNAL].detach(); | ||
} | ||
@@ -432,3 +426,2 @@ const value = overrides[key]; | ||
} | ||
// TODO: Consider removing default parameter | ||
class LiveMap { | ||
@@ -470,2 +463,3 @@ constructor(entries) { | ||
attach: this.attach.bind(this), | ||
detach: this.detach.bind(this), | ||
detachChild: this.detachChild.bind(this), | ||
@@ -512,3 +506,3 @@ getParentId: this._getParentId.bind(this), | ||
const previousValue = this._map.get(key); | ||
if (previousValue instanceof LiveObject) { | ||
if (previousValue) { | ||
previousValue[INTERNAL].detach(); | ||
@@ -519,2 +513,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) { | ||
@@ -526,8 +529,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(); | ||
@@ -537,6 +535,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); | ||
} | ||
@@ -547,17 +545,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); | ||
@@ -567,12 +556,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(); | ||
@@ -590,18 +571,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 }]); | ||
} | ||
} | ||
@@ -685,7 +657,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"); | ||
} | ||
@@ -731,3 +705,3 @@ else { | ||
detachChild(crdt) { | ||
throw new Error("Cannot detach CRDT on register"); | ||
throw new Error("Internal error: cannot detach CRDT on register"); | ||
} | ||
@@ -767,3 +741,3 @@ detach() { | ||
this._listeners = []; | ||
// TODO: Find a better data structure | ||
// TODO: Naive array at first, find a better data structure | ||
this._items = []; | ||
@@ -786,3 +760,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); | ||
@@ -840,5 +814,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]); | ||
@@ -851,3 +829,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(); | ||
@@ -956,3 +934,3 @@ } | ||
id: childRecord[INTERNAL].ctx.id, | ||
type: OpType.DeleteObject, | ||
type: OpType.DeleteCrdt, | ||
}, | ||
@@ -965,3 +943,2 @@ ]); | ||
toArray() { | ||
// TODO: typing | ||
return this._items.map((entry) => selfOrRegisterValue(entry[0])); | ||
@@ -1303,2 +1280,50 @@ } | ||
} | ||
function useList(key) { | ||
var _a; | ||
var root = useStorage()[0]; | ||
var _b = React.useState(0), setCount = _b[1]; | ||
React.useEffect(function () { | ||
if (root == null) { | ||
return; | ||
} | ||
var list = root.get(key); | ||
if (list == null) { | ||
list = new LiveList(); | ||
root.set(key, list); | ||
} | ||
function onChange() { | ||
setCount(function (x) { return x + 1; }); | ||
} | ||
list.subscribe(onChange); | ||
setCount(function (x) { return x + 1; }); | ||
return function () { | ||
return list.unsubscribe(onChange); | ||
}; | ||
}, [root]); | ||
return (_a = root === null || root === void 0 ? void 0 : root.get(key)) !== null && _a !== void 0 ? _a : null; | ||
} | ||
function useObject(key) { | ||
var _a; | ||
var root = useStorage()[0]; | ||
var _b = React.useState(0), setCount = _b[1]; | ||
React.useEffect(function () { | ||
if (root == null) { | ||
return; | ||
} | ||
var obj = root.get(key); | ||
if (obj == null) { | ||
obj = new LiveObject(); | ||
root.set(key, obj); | ||
} | ||
function onChange() { | ||
setCount(function (x) { return x + 1; }); | ||
} | ||
obj.subscribe(onChange); | ||
setCount(function (x) { return x + 1; }); | ||
return function () { | ||
return obj.unsubscribe(onChange); | ||
}; | ||
}, [root]); | ||
return (_a = root === null || root === void 0 ? void 0 : root.get(key)) !== null && _a !== void 0 ? _a : null; | ||
} | ||
@@ -1310,4 +1335,6 @@ exports.LiveblocksProvider = LiveblocksProvider; | ||
exports.useEventListener = useEventListener; | ||
exports.useList = useList; | ||
exports.useMap = useMap; | ||
exports.useMyPresence = useMyPresence; | ||
exports.useObject = useObject; | ||
exports.useOthers = useOthers; | ||
@@ -1314,0 +1341,0 @@ exports.useSelf = useSelf; |
{ | ||
"name": "@liveblocks/react", | ||
"version": "0.12.0-beta.2", | ||
"version": "0.12.0-beta.3", | ||
"description": "", | ||
@@ -26,3 +26,3 @@ "main": "./lib/index.js", | ||
"peerDependencies": { | ||
"@liveblocks/client": "0.12.0-beta.2", | ||
"@liveblocks/client": "0.12.0-beta.3", | ||
"react": "^16.14.0 || ^17" | ||
@@ -29,0 +29,0 @@ }, |
Sorry, the diff of this file is not supported yet
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
171678
1436
0