Skip to content

Commit

Permalink
fix: get ytype
Browse files Browse the repository at this point in the history
  • Loading branch information
darkskygit committed Sep 3, 2024
1 parent 34e8cd2 commit 78f8549
Showing 1 changed file with 29 additions and 3 deletions.
32 changes: 29 additions & 3 deletions y-octo-node/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -242,7 +248,13 @@ export class Map {
private ytype?: { doc: Doc; map: Y.YMap };
private preliminary: Record<string, any> = {};

constructor(items: Record<string, any>, 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<string, any> = {}, ydoc?: Doc, ymap?: Y.YMap) {
this.preliminary = items;
if (ydoc) this.integrate(ydoc, ymap);
}
Expand Down Expand Up @@ -282,12 +294,26 @@ export class Map {
}

get<T = unknown>(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<T = ListItem>(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);
Expand Down

0 comments on commit 78f8549

Please sign in to comment.