fix: use Object.getPrototypeOf instead of __proto__

This commit is contained in:
Zixuan Chen 2023-12-05 14:51:31 +08:00
parent aa87a96286
commit 14c66edcd4
No known key found for this signature in database
4 changed files with 31 additions and 7 deletions

View file

@ -128,7 +128,7 @@ export function isContainer(value: any): value is Container {
return false; return false;
} }
const p = value.__proto__; const p = Object.getPrototypeOf(value);
if (p == null || typeof p !== "object" || typeof p["kind"] !== "function") { if (p == null || typeof p !== "object" || typeof p["kind"] !== "function") {
return false; return false;
} }

View file

@ -8,6 +8,7 @@ import {
MapDiff, MapDiff,
TextDiff, TextDiff,
setPanicHook, setPanicHook,
getType,
} from "../src"; } from "../src";
setPanicHook(); setPanicHook();
@ -302,9 +303,9 @@ describe("event", () => {
const list = loro.getList("list"); const list = loro.getList("list");
let first = true; let first = true;
loro.subscribe((e) => { loro.subscribe((e) => {
if(first){ if (first) {
const diff = e.diff.diff; const diff = (e.diff as ListDiff).diff;
const text = diff[0].insert[0] as LoroText; const text = diff[0].insert![0] as LoroText;
text.insert(0, "abc"); text.insert(0, "abc");
first = false; first = false;
} }
@ -315,6 +316,32 @@ describe("event", () => {
expect(loro.toJson().list[0]).toBe('abc'); 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<void> { function oneMs(): Promise<void> {

View file

@ -305,7 +305,6 @@ describe("type", () => {
it("works for list type", () => { it("works for list type", () => {
const loro = new Loro<{ list: LoroList<[string, number]> }>(); const loro = new Loro<{ list: LoroList<[string, number]> }>();
const list = loro.getTypedList("list"); const list = loro.getTypedList("list");
console.dir((list as any).__proto__);
list.insertTyped(0, "123"); list.insertTyped(0, "123");
list.insertTyped(1, 123); list.insertTyped(1, 123);
const v0 = list.getTyped(loro, 0); const v0 = list.getTyped(loro, 0);
@ -331,7 +330,6 @@ describe("tree", () => {
it("create move", () => { it("create move", () => {
const id = tree.create(); const id = tree.create();
const childID = tree.create(id); const childID = tree.create(id);
console.log(typeof id);
assertEquals(tree.parent(childID), id); assertEquals(tree.parent(childID), id);
}); });

View file

@ -68,6 +68,5 @@ describe("Version", () => {
it("get ops inside changes", () => { it("get ops inside changes", () => {
const change = a.getOpsInChange({ peer: 0n, counter: 2 }); const change = a.getOpsInChange({ peer: 0n, counter: 2 });
expect(change.length).toBe(1); expect(change.length).toBe(1);
console.dir(change, { depth: 100 });
}); });
}); });