Skip to content

Commit 2f9ad6a

Browse files
committed
fix: 修复同步时如果值是数组时的schema错误问题
1 parent 4107a4d commit 2f9ad6a

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

packages/core/src/schema/manager.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ export class SchemaManager<
7979
},
8080
{
8181
validate: "pass",
82+
silent: true,
8283
},
8384
);
8485
}

packages/syncer/src/__tests__/store.sync.test.ts

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -952,6 +952,81 @@ describe("本地Store同步", () => {
952952
return moduleStore;
953953
});
954954

955+
setTimeout(() => {
956+
resolve();
957+
// expect(toStore.state).toEqual({
958+
// myorder: {
959+
// "order.c": 2,
960+
// },
961+
// });
962+
963+
// expect(toStore.schemas.has(["myorder", "order.c"])).toBeTruthy();
964+
// toStore.schemas.store.on("computed:done", ({ path, value }) => {
965+
// // console.log("computed:done", path, value);
966+
// resolve();
967+
// });
968+
// //
969+
// toStore.schemas.get(["myorder", "order.c"] as never)!.select;
970+
});
971+
});
972+
});
973+
test("多对一同步配置时的初始化事件", () => {
974+
const toStore = new AutoStore(
975+
{},
976+
{
977+
id: "to",
978+
},
979+
);
980+
toStore.watch((operate) => {
981+
if (operate.flags! < 0) return;
982+
if (operate.type === "set") {
983+
console.log("toStore.watch", operate.path, operate.value);
984+
}
985+
});
986+
987+
return new Promise<void>((resolve) => {
988+
// order.a <-> myorder['order.a']
989+
const moduleStores = Array.from({ length: 1 }, (_, i) => {
990+
const id = `module${i + 1}`;
991+
const moduleStore = new AutoStore(
992+
{
993+
// count: configurable(100, {
994+
// label: "数量",
995+
// }),
996+
// price: configurable(100, {
997+
// label: "姓名",
998+
// }),
999+
tags: configurable([], {
1000+
label: "标签",
1001+
}),
1002+
},
1003+
{ id },
1004+
);
1005+
1006+
moduleStore.sync(toStore, {
1007+
remote: id,
1008+
immediate: true,
1009+
filter: (path: string[]) => {
1010+
return moduleStore.schemas.has(path.join(".") as any);
1011+
},
1012+
pathMap: {
1013+
toRemote: (path: string[], value) => {
1014+
// 重点:如果值是对象但使用configurable包装的,则不进行路径转换,否则会导致无法正确同步数据
1015+
if (typeof value !== "object" || moduleStore.schemas.has(path as any)) {
1016+
return [path.join(".")];
1017+
}
1018+
},
1019+
toLocal: (path: string[]) => {
1020+
return path.reduce<string[]>((result, cur) => {
1021+
result.push(...cur.split("."));
1022+
return result;
1023+
}, []);
1024+
},
1025+
},
1026+
});
1027+
return moduleStore;
1028+
});
1029+
9551030
setTimeout(() => {
9561031
resolve();
9571032
// expect(toStore.state).toEqual({

0 commit comments

Comments
 (0)