loro-crdt
Advanced tools
Comparing version 1.2.1 to 1.2.2
@@ -154,4 +154,51 @@ import { LoroDoc, AwarenessWasm } from "./loro_wasm"; | ||
} | ||
LoroDoc.prototype.toJsonWithReplacer = function (replacer) { | ||
const doc = this; | ||
const m = (key, value) => { | ||
if (typeof value === "string") { | ||
if (isContainerId(value)) { | ||
const container = doc.getContainerById(value); | ||
if (container == null) { | ||
throw new Error(`ContainerID not found: ${value}`); | ||
} | ||
const ans = replacer(key, container); | ||
if (ans === container) { | ||
const ans = container.getShallowValue(); | ||
if (typeof ans === "object") { | ||
return run(ans); | ||
} | ||
return ans; | ||
} | ||
if (isContainer(ans)) { | ||
throw new Error("Using new container is not allowed in toJsonWithReplacer"); | ||
} | ||
return ans; | ||
} | ||
} | ||
const ans = replacer(key, value); | ||
if (isContainer(ans)) { | ||
throw new Error("Using new container is not allowed in toJsonWithReplacer"); | ||
} | ||
return ans; | ||
}; | ||
const run = (layer) => { | ||
if (Array.isArray(layer)) { | ||
return layer.map((item, index) => { | ||
return m(index, item); | ||
}).filter((item) => item !== undefined); | ||
} | ||
const result = {}; | ||
for (const [key, value] of Object.entries(layer)) { | ||
const ans = m(key, value); | ||
if (ans !== undefined) { | ||
result[key] = ans; | ||
} | ||
} | ||
return result; | ||
}; | ||
const layer = this.getShallowValue(); | ||
return run(layer); | ||
}; | ||
export { Awareness, Loro, getType, isContainer, isContainerId, newContainerID, newRootContainerID }; | ||
//# sourceMappingURL=index.js.map |
@@ -13,2 +13,3 @@ /* tslint:disable */ | ||
export function lorocounter_getAttached(a: number): number; | ||
export function lorocounter_getShallowValue(a: number): number; | ||
export function __wbg_awarenesswasm_free(a: number): void; | ||
@@ -80,2 +81,3 @@ export function awarenesswasm_new(a: number, b: number): number; | ||
export function lorodoc_importUpdateBatch(a: number, b: number, c: number): void; | ||
export function lorodoc_importBatch(a: number, b: number, c: number): void; | ||
export function lorodoc_getShallowValue(a: number, b: number): void; | ||
@@ -124,2 +126,3 @@ export function lorodoc_toJSON(a: number, b: number): void; | ||
export function lorotext_isDeleted(a: number): number; | ||
export function lorotext_getShallowValue(a: number, b: number): void; | ||
export function __wbg_loromap_free(a: number): void; | ||
@@ -145,2 +148,3 @@ export function loromap_new(): number; | ||
export function loromap_isDeleted(a: number): number; | ||
export function loromap_getShallowValue(a: number): number; | ||
export function __wbg_lorolist_free(a: number): void; | ||
@@ -168,2 +172,3 @@ export function lorolist_new(): number; | ||
export function lorolist_isDeleted(a: number): number; | ||
export function lorolist_getShallowValue(a: number): number; | ||
export function loromovablelist_new(): number; | ||
@@ -193,2 +198,3 @@ export function loromovablelist_kind(a: number): number; | ||
export function loromovablelist_isDeleted(a: number): number; | ||
export function loromovablelist_getShallowValue(a: number): number; | ||
export function __wbg_lorotree_free(a: number): void; | ||
@@ -234,2 +240,3 @@ export function lorotreenode___getClassname(a: number, b: number): void; | ||
export function lorotree_isDeleted(a: number): number; | ||
export function lorotree_getShallowValue(a: number): number; | ||
export function __wbg_cursor_free(a: number): void; | ||
@@ -236,0 +243,0 @@ export function cursor_containerId(a: number): number; |
# Changelog | ||
## 1.2.2 | ||
### Patch Changes | ||
- 3b7a738: Add getShallowValue and toJsonWIthReplacer | ||
- Add getShallowValue for each container (#581) | ||
- Implement toJsonWithReplacer method for LoroDoc to customize JSON serialization (#582) | ||
- Rename importUpdateBatch into importBatch & refine type (#580) | ||
## 1.2.1 | ||
@@ -4,0 +14,0 @@ |
@@ -155,2 +155,49 @@ 'use strict'; | ||
} | ||
loroWasm.LoroDoc.prototype.toJsonWithReplacer = function (replacer) { | ||
const doc = this; | ||
const m = (key, value) => { | ||
if (typeof value === "string") { | ||
if (isContainerId(value)) { | ||
const container = doc.getContainerById(value); | ||
if (container == null) { | ||
throw new Error(`ContainerID not found: ${value}`); | ||
} | ||
const ans = replacer(key, container); | ||
if (ans === container) { | ||
const ans = container.getShallowValue(); | ||
if (typeof ans === "object") { | ||
return run(ans); | ||
} | ||
return ans; | ||
} | ||
if (isContainer(ans)) { | ||
throw new Error("Using new container is not allowed in toJsonWithReplacer"); | ||
} | ||
return ans; | ||
} | ||
} | ||
const ans = replacer(key, value); | ||
if (isContainer(ans)) { | ||
throw new Error("Using new container is not allowed in toJsonWithReplacer"); | ||
} | ||
return ans; | ||
}; | ||
const run = (layer) => { | ||
if (Array.isArray(layer)) { | ||
return layer.map((item, index) => { | ||
return m(index, item); | ||
}).filter((item) => item !== undefined); | ||
} | ||
const result = {}; | ||
for (const [key, value] of Object.entries(layer)) { | ||
const ans = m(key, value); | ||
if (ans !== undefined) { | ||
result[key] = ans; | ||
} | ||
} | ||
return result; | ||
}; | ||
const layer = this.getShallowValue(); | ||
return run(layer); | ||
}; | ||
@@ -157,0 +204,0 @@ exports.Awareness = Awareness; |
@@ -13,2 +13,3 @@ /* tslint:disable */ | ||
export function lorocounter_getAttached(a: number): number; | ||
export function lorocounter_getShallowValue(a: number): number; | ||
export function __wbg_awarenesswasm_free(a: number): void; | ||
@@ -80,2 +81,3 @@ export function awarenesswasm_new(a: number, b: number): number; | ||
export function lorodoc_importUpdateBatch(a: number, b: number, c: number): void; | ||
export function lorodoc_importBatch(a: number, b: number, c: number): void; | ||
export function lorodoc_getShallowValue(a: number, b: number): void; | ||
@@ -124,2 +126,3 @@ export function lorodoc_toJSON(a: number, b: number): void; | ||
export function lorotext_isDeleted(a: number): number; | ||
export function lorotext_getShallowValue(a: number, b: number): void; | ||
export function __wbg_loromap_free(a: number): void; | ||
@@ -145,2 +148,3 @@ export function loromap_new(): number; | ||
export function loromap_isDeleted(a: number): number; | ||
export function loromap_getShallowValue(a: number): number; | ||
export function __wbg_lorolist_free(a: number): void; | ||
@@ -168,2 +172,3 @@ export function lorolist_new(): number; | ||
export function lorolist_isDeleted(a: number): number; | ||
export function lorolist_getShallowValue(a: number): number; | ||
export function loromovablelist_new(): number; | ||
@@ -193,2 +198,3 @@ export function loromovablelist_kind(a: number): number; | ||
export function loromovablelist_isDeleted(a: number): number; | ||
export function loromovablelist_getShallowValue(a: number): number; | ||
export function __wbg_lorotree_free(a: number): void; | ||
@@ -234,2 +240,3 @@ export function lorotreenode___getClassname(a: number, b: number): void; | ||
export function lorotree_isDeleted(a: number): number; | ||
export function lorotree_getShallowValue(a: number): number; | ||
export function __wbg_cursor_free(a: number): void; | ||
@@ -236,0 +243,0 @@ export function cursor_containerId(a: number): number; |
{ | ||
"name": "loro-crdt", | ||
"version": "1.2.1", | ||
"version": "1.2.2", | ||
"description": "Loro CRDTs is a high-performance CRDT framework that makes your app state synchronized, collaborative and maintainable effortlessly.", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -155,4 +155,51 @@ export { default } from "./loro_wasm.js"; | ||
} | ||
LoroDoc.prototype.toJsonWithReplacer = function (replacer) { | ||
const doc = this; | ||
const m = (key, value) => { | ||
if (typeof value === "string") { | ||
if (isContainerId(value)) { | ||
const container = doc.getContainerById(value); | ||
if (container == null) { | ||
throw new Error(`ContainerID not found: ${value}`); | ||
} | ||
const ans = replacer(key, container); | ||
if (ans === container) { | ||
const ans = container.getShallowValue(); | ||
if (typeof ans === "object") { | ||
return run(ans); | ||
} | ||
return ans; | ||
} | ||
if (isContainer(ans)) { | ||
throw new Error("Using new container is not allowed in toJsonWithReplacer"); | ||
} | ||
return ans; | ||
} | ||
} | ||
const ans = replacer(key, value); | ||
if (isContainer(ans)) { | ||
throw new Error("Using new container is not allowed in toJsonWithReplacer"); | ||
} | ||
return ans; | ||
}; | ||
const run = (layer) => { | ||
if (Array.isArray(layer)) { | ||
return layer.map((item, index) => { | ||
return m(index, item); | ||
}).filter((item) => item !== undefined); | ||
} | ||
const result = {}; | ||
for (const [key, value] of Object.entries(layer)) { | ||
const ans = m(key, value); | ||
if (ans !== undefined) { | ||
result[key] = ans; | ||
} | ||
} | ||
return result; | ||
}; | ||
const layer = this.getShallowValue(); | ||
return run(layer); | ||
}; | ||
export { Awareness, Loro, getType, isContainer, isContainerId, newContainerID, newRootContainerID }; | ||
//# sourceMappingURL=index.js.map |
@@ -13,2 +13,3 @@ /* tslint:disable */ | ||
export function lorocounter_getAttached(a: number): number; | ||
export function lorocounter_getShallowValue(a: number): number; | ||
export function __wbg_awarenesswasm_free(a: number): void; | ||
@@ -80,2 +81,3 @@ export function awarenesswasm_new(a: number, b: number): number; | ||
export function lorodoc_importUpdateBatch(a: number, b: number, c: number): void; | ||
export function lorodoc_importBatch(a: number, b: number, c: number): void; | ||
export function lorodoc_getShallowValue(a: number, b: number): void; | ||
@@ -124,2 +126,3 @@ export function lorodoc_toJSON(a: number, b: number): void; | ||
export function lorotext_isDeleted(a: number): number; | ||
export function lorotext_getShallowValue(a: number, b: number): void; | ||
export function __wbg_loromap_free(a: number): void; | ||
@@ -145,2 +148,3 @@ export function loromap_new(): number; | ||
export function loromap_isDeleted(a: number): number; | ||
export function loromap_getShallowValue(a: number): number; | ||
export function __wbg_lorolist_free(a: number): void; | ||
@@ -168,2 +172,3 @@ export function lorolist_new(): number; | ||
export function lorolist_isDeleted(a: number): number; | ||
export function lorolist_getShallowValue(a: number): number; | ||
export function loromovablelist_new(): number; | ||
@@ -193,2 +198,3 @@ export function loromovablelist_kind(a: number): number; | ||
export function loromovablelist_isDeleted(a: number): number; | ||
export function loromovablelist_getShallowValue(a: number): number; | ||
export function __wbg_lorotree_free(a: number): void; | ||
@@ -234,2 +240,3 @@ export function lorotreenode___getClassname(a: number, b: number): void; | ||
export function lorotree_isDeleted(a: number): number; | ||
export function lorotree_getShallowValue(a: number): number; | ||
export function __wbg_cursor_free(a: number): void; | ||
@@ -236,0 +243,0 @@ export function cursor_containerId(a: number): number; |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
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
14270853
56482