From 78f85490ab9b7085af5d62c0f2cb55c5bd565470 Mon Sep 17 00:00:00 2001 From: DarkSky Date: Tue, 3 Sep 2024 17:58:22 +0800 Subject: [PATCH] fix: get ytype --- y-octo-node/index.ts | 32 +++++++++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/y-octo-node/index.ts b/y-octo-node/index.ts index f88a1fa..ba36485 100644 --- a/y-octo-node/index.ts +++ b/y-octo-node/index.ts @@ -120,7 +120,13 @@ export class Array { return new Array(items); } - constructor(items: ArrayType[], ydoc?: Doc, yarray?: Y.YArray) { + static from_ytype(ytype?: { doc: Doc; array: Y.YArray }) { + const array = new Array(); + array.ytype = ytype; + return array; + } + + constructor(items: ArrayType[] = [], ydoc?: Doc, yarray?: Y.YArray) { this.preliminary = items; if (ydoc) this.integrate(ydoc, yarray); } @@ -242,7 +248,13 @@ export class Map { private ytype?: { doc: Doc; map: Y.YMap }; private preliminary: Record = {}; - constructor(items: Record, ydoc?: Doc, ymap?: Y.YMap) { + static from_ytype(ytype?: { doc: Doc; map: Y.YMap }) { + const map = new Map(); + map.ytype = ytype; + return map; + } + + constructor(items: Record = {}, ydoc?: Doc, ymap?: Y.YMap) { this.preliminary = items; if (ydoc) this.integrate(ydoc, ymap); } @@ -282,12 +294,26 @@ export class Map { } get(key: string): T { - return this.ytype ? this.ytype.map.get(key) : this.preliminary[key]; + if (this.ytype) { + const ret = this.ytype.map.get(key); + if (ret) { + if (ret instanceof Y.YArray) { + return Array.from_ytype({ doc: this.ytype.doc, array: ret }) as T; + } else if (ret instanceof Y.YMap) { + return Map.from_ytype({ doc: this.ytype.doc, map: ret }) as T; + } + } + return ret as T; + } else { + return this.preliminary[key]; + } } set(key: string, value: T) { + console.trace(value, this.ytype); if (this.ytype) { if (value instanceof Array || value instanceof Map) { + console.log("integrate", value); this.ytype.map.set(key, value.integrate(this.ytype.doc)); } else { this.ytype.map.set(key, value);