From 14c66edcd4295575725e56856cddb3fad831b6e6 Mon Sep 17 00:00:00 2001 From: Zixuan Chen Date: Tue, 5 Dec 2023 14:51:31 +0800 Subject: [PATCH] fix: use Object.getPrototypeOf instead of __proto__ --- loro-js/src/index.ts | 2 +- loro-js/tests/event.test.ts | 33 ++++++++++++++++++++++++++++++--- loro-js/tests/misc.test.ts | 2 -- loro-js/tests/version.test.ts | 1 - 4 files changed, 31 insertions(+), 7 deletions(-) diff --git a/loro-js/src/index.ts b/loro-js/src/index.ts index 85fecd19..e359869f 100644 --- a/loro-js/src/index.ts +++ b/loro-js/src/index.ts @@ -128,7 +128,7 @@ export function isContainer(value: any): value is Container { return false; } - const p = value.__proto__; + const p = Object.getPrototypeOf(value); if (p == null || typeof p !== "object" || typeof p["kind"] !== "function") { return false; } diff --git a/loro-js/tests/event.test.ts b/loro-js/tests/event.test.ts index 34d3de91..1d0071f4 100644 --- a/loro-js/tests/event.test.ts +++ b/loro-js/tests/event.test.ts @@ -8,6 +8,7 @@ import { MapDiff, TextDiff, setPanicHook, + getType, } from "../src"; setPanicHook(); @@ -302,9 +303,9 @@ describe("event", () => { const list = loro.getList("list"); let first = true; loro.subscribe((e) => { - if(first){ - const diff = e.diff.diff; - const text = diff[0].insert[0] as LoroText; + if (first) { + const diff = (e.diff as ListDiff).diff; + const text = diff[0].insert![0] as LoroText; text.insert(0, "abc"); first = false; } @@ -315,6 +316,32 @@ describe("event", () => { expect(loro.toJson().list[0]).toBe('abc'); }); }); + + it("diff can contain containers", async () => { + const doc = new Loro(); + const list = doc.getList("list"); + let ran = false; + doc.subscribe(event => { + if (event.diff.type === "list") { + for (const item of event.diff.diff) { + const t = item.insert![0] as LoroText; + expect(t.toString()).toBe("Hello") + expect(item.insert?.length).toBe(2); + expect(getType(item.insert![0])).toBe("Text") + expect(getType(item.insert![1])).toBe("Map") + } + ran = true; + } + }) + + list.insertContainer(0, "Map"); + const t = list.insertContainer(0, "Text"); + t.insert(0, "He"); + t.insert(2, "llo"); + doc.commit(); + await new Promise(resolve => setTimeout(resolve, 1)); + expect(ran).toBeTruthy() + }) }); function oneMs(): Promise { diff --git a/loro-js/tests/misc.test.ts b/loro-js/tests/misc.test.ts index 98f3ed6d..77621540 100644 --- a/loro-js/tests/misc.test.ts +++ b/loro-js/tests/misc.test.ts @@ -305,7 +305,6 @@ describe("type", () => { it("works for list type", () => { const loro = new Loro<{ list: LoroList<[string, number]> }>(); const list = loro.getTypedList("list"); - console.dir((list as any).__proto__); list.insertTyped(0, "123"); list.insertTyped(1, 123); const v0 = list.getTyped(loro, 0); @@ -331,7 +330,6 @@ describe("tree", () => { it("create move", () => { const id = tree.create(); const childID = tree.create(id); - console.log(typeof id); assertEquals(tree.parent(childID), id); }); diff --git a/loro-js/tests/version.test.ts b/loro-js/tests/version.test.ts index 7c2feb5e..1c98b7a5 100644 --- a/loro-js/tests/version.test.ts +++ b/loro-js/tests/version.test.ts @@ -68,6 +68,5 @@ describe("Version", () => { it("get ops inside changes", () => { const change = a.getOpsInChange({ peer: 0n, counter: 2 }); expect(change.length).toBe(1); - console.dir(change, { depth: 100 }); }); });