Skip to content

Commit 09a293f

Browse files
committed
update
1 parent 92ab2dd commit 09a293f

File tree

21 files changed

+4724
-1108
lines changed

21 files changed

+4724
-1108
lines changed

.changeset/config.json

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,24 @@
11
{
2-
"$schema": "https://unpkg.com/@changesets/[email protected]/schema.json",
3-
"changelog": "@changesets/cli/changelog",
4-
"commit": true,
5-
"access": "public",
6-
"fixed": [["autostore", "@autostorejs/react", "@autostorejs/syncer", "@autostorejs/form", "@autostorejs/devtools"]],
7-
"linked": [],
8-
"baseBranch": "master",
9-
"updateInternalDependencies": "patch",
10-
"ignore": ["autostore-docs"]
2+
"$schema": "https://unpkg.com/@changesets/[email protected]/schema.json",
3+
"changelog": "@changesets/cli/changelog",
4+
"commit": true,
5+
"access": "public",
6+
"fixed": [
7+
[
8+
"autostore",
9+
"@autostorejs/react",
10+
"@autostorejs/syncer",
11+
"@autostorejs/form",
12+
"@autostorejs/devtools"
13+
]
14+
],
15+
"linked": [],
16+
"baseBranch": "master",
17+
"updateInternalDependencies": "patch",
18+
"ignore": [
19+
"autostore-docs",
20+
"@autostore-examples/benchmark",
21+
"@autostore-examples/syncer",
22+
"@autostore-examples/storybook"
23+
]
1124
}

docs/zh/store/guide/store/sync.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -538,3 +538,7 @@ syncer.stop();
538538
以下是同步时进行路径映射的示例:
539539

540540
<demo react="store/syncStoreWithPathMap.tsx"/>
541+
542+
:::warning 注意
543+
同步功能无法同步计算属性和监视属性
544+
:::

examples/syncer/tsconfig.json

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"compilerOptions": {
3+
"target": "ES2022",
4+
"experimentalDecorators": true,
5+
"useDefineForClassFields": false,
6+
"module": "ESNext",
7+
"lib": [
8+
"WebWorker",
9+
"ES2020",
10+
"DOM",
11+
"DOM.Iterable",
12+
"Webworker.Iterable",
13+
"WebWorker.AsyncIterable",
14+
"WebWorker.ImportScripts"
15+
],
16+
"skipLibCheck": true,
17+
/* Bundler mode */
18+
"moduleResolution": "bundler",
19+
"allowImportingTsExtensions": true,
20+
"isolatedModules": true,
21+
"moduleDetection": "force",
22+
"noEmit": true,
23+
/* Linting */
24+
"strict": true,
25+
"noUnusedLocals": true,
26+
"noUnusedParameters": true,
27+
"noFallthroughCasesInSwitch": true,
28+
"noUncheckedSideEffectImports": true,
29+
"baseUrl": "./",
30+
"paths": {
31+
"@/*": ["src/*"]
32+
}
33+
},
34+
"include": ["src"]
35+
}

examples/syncer/vite.config.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { defineConfig } from 'vite'
2+
import react from '@vitejs/plugin-react'
3+
4+
// https://vite.dev/config/
5+
export default defineConfig({
6+
plugins: [react()],
7+
})

packages/core/src/schema/manager.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { PATH_DELIMITER } from "../consts";
22
import type { AutoStore } from "../store/store";
33
import type { Dict } from "../types";
4-
import { isSchemaBuilder, markRaw, pathStartsWith, setVal } from "../utils";
4+
import { isPathEq, isSchemaBuilder, markRaw, pathStartsWith, setVal, watchObjectAccess } from "../utils";
55
import { getVal } from "../utils/getVal";
66
import { isFuncDefine } from "../utils/isFuncDefine";
77
import { parseFunc } from "../utils/parseFunc";
@@ -55,8 +55,15 @@ export class SchemaManager<
5555

5656
Object.entries(finalDescriptor).forEach(([key, value]) => {
5757
if (isFuncDefine(value)) {
58-
(finalDescriptor as any)[key] =
59-
key === "onValidate" ? markRaw(parseFunc(value)) : (parseFunc(value) as any);
58+
const func = parseFunc(value);
59+
if (typeof func === "function") {
60+
// if (key === 'onValidate') {
61+
if (key.startsWith("on") || key.startsWith("to")) {
62+
(finalDescriptor as any)[key] = markRaw(func);
63+
} else {
64+
(finalDescriptor as any)[key] = func;
65+
}
66+
}
6067
}
6168
});
6269

packages/core/src/schema/types/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,7 @@ export type SchemaDescriptor<Value = any, Options = Dict> = {
263263
* 比如在进行syncer时,传入falgs==SYNC_INIT_FLAG
264264
*/
265265
flags?: number;
266+
pathMap?: Record<string, string>;
266267
};
267268

268269
export interface SchemaDescriptorBuilder<Value = any, State = Dict> {

packages/core/src/utils/index.ts

Lines changed: 42 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,42 @@
1-
export * from './isRaw';
2-
export * from './isEq';
3-
export * from './isMap';
4-
export * from './isAbsolutePath';
5-
export * from './isPathEq';
6-
export * from './isRaw';
7-
export * from './isPathMatched';
8-
export * from './isPlainObject';
9-
export * from './isAsyncComputedValue';
10-
export * from './isPromise';
11-
export * from './isObserverDescriptor';
12-
export * from './isObserverDescriptorBuilder';
13-
export * from './isProxy';
14-
export * from './getVal';
15-
export * from './getMapVal';
16-
export * from './markRaw';
17-
export * from './setVal';
18-
export * from './joinValuePath';
19-
export * from './normalizeDeps';
20-
export * from './getId';
21-
export * from './calcDependPaths';
22-
export * from './getComputedId';
23-
export * from './updateObjectVal';
24-
export * from './forEachObject';
25-
export * from './getSnap';
26-
export * from './delay';
27-
export * from './noRepeat';
28-
export * from './pathStartsWith';
29-
export * from './getDepends';
30-
export * from './pathIsExists';
31-
export * from './getSnapshot';
32-
export * from './isPrimitive';
33-
export * from './defineExtend';
34-
export * from './isFunction';
35-
export * from './createObserverObject';
36-
export * from './isComputedDescriptorParameter';
37-
export * from './getAsyncVal';
38-
export * from './createAsyncComptuedValue';
39-
export * from './isValueSchema';
40-
export * from './isRelPath';
41-
export * from './isSchemaBuilder';
1+
export * from "./isRaw";
2+
export * from "./isEq";
3+
export * from "./isMap";
4+
export * from "./isAbsolutePath";
5+
export * from "./isPathEq";
6+
export * from "./isRaw";
7+
export * from "./isPathMatched";
8+
export * from "./isPlainObject";
9+
export * from "./isAsyncComputedValue";
10+
export * from "./isPromise";
11+
export * from "./isObserverDescriptor";
12+
export * from "./isObserverDescriptorBuilder";
13+
export * from "./isProxy";
14+
export * from "./getVal";
15+
export * from "./getMapVal";
16+
export * from "./markRaw";
17+
export * from "./setVal";
18+
export * from "./joinValuePath";
19+
export * from "./normalizeDeps";
20+
export * from "./getId";
21+
export * from "./calcDependPaths";
22+
export * from "./getComputedId";
23+
export * from "./updateObjectVal";
24+
export * from "./forEachObject";
25+
export * from "./getSnap";
26+
export * from "./delay";
27+
export * from "./noRepeat";
28+
export * from "./pathStartsWith";
29+
export * from "./getDepends";
30+
export * from "./pathIsExists";
31+
export * from "./getSnapshot";
32+
export * from "./isPrimitive";
33+
export * from "./defineExtend";
34+
export * from "./isFunction";
35+
export * from "./createObserverObject";
36+
export * from "./isComputedDescriptorParameter";
37+
export * from "./getAsyncVal";
38+
export * from "./createAsyncComptuedValue";
39+
export * from "./isValueSchema";
40+
export * from "./isRelPath";
41+
export * from "./isSchemaBuilder";
42+
export * from "./watchObjectAccess";
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/**
2+
*
3+
* m
4+
*/
5+
export type WatchObjectAccessOptions = {
6+
get?: (path: string[]) => any;
7+
set?: (path: string[], value: any) => any;
8+
};
9+
10+
/**
11+
* 创建一个代理对象,用于追踪对对象的访问路径
12+
* @param {WatchObjectAccessOptions} [options] - 配置选项
13+
* @param {Function} [options.get] - 当访问属性时调用的回调函数,接收访问路径作为参数
14+
* @param {Function} [options.set] - 当设置属性时调用的回调函数,接收访问路径和设置的值作为参数
15+
* @returns {Proxy} 返回一个代理对象,可以通过访问其属性来构建访问路径
16+
* @example
17+
* const obj = watchObjectAccess({
18+
* get: (path) => console.log('访问路径:', path),
19+
* set: (path, value) => console.log('设置路径:', path, '值为:', value)
20+
* });
21+
* obj.a.b.c; // 输出: 访问路径: ['a', 'b', 'c']
22+
* obj.x = 1; // 输出: 设置路径: ['x'] 值为: 1
23+
*/
24+
export function watchObjectAccess(options?: WatchObjectAccessOptions) {
25+
const { get, set } = options || {};
26+
27+
// 使用WeakMap来存储路径,避免污染对象
28+
const pathMap = new WeakMap<object, string[]>();
29+
30+
const handler: ProxyHandler<any> = {
31+
get(target, prop: string, receiver) {
32+
const currentPath = pathMap.get(target) || [];
33+
34+
// 处理特殊属性,避免被代理
35+
if (prop === "__path") {
36+
return currentPath;
37+
}
38+
// 如果访问的是Symbol或内置方法,直接返回
39+
if (typeof prop === "symbol" || prop in Object.prototype) {
40+
return Reflect.get(target, prop, receiver);
41+
}
42+
43+
const newPath = [...currentPath, prop];
44+
45+
// 如果用户提供了get回调,在这里调用
46+
if (get) {
47+
const customResult = get(newPath);
48+
// 如果回调返回了特定值,就使用该值
49+
if (customResult !== undefined) {
50+
return customResult;
51+
}
52+
}
53+
54+
// 创建新的代理对象来继续路径追踪
55+
const newTarget = {};
56+
pathMap.set(newTarget, newPath);
57+
58+
return new Proxy(newTarget, handler);
59+
},
60+
61+
set(target, prop: string, value: any) {
62+
const currentPath = pathMap.get(target) || [];
63+
const setPath = [...currentPath, prop];
64+
if (set) {
65+
set(setPath, value);
66+
}
67+
return true;
68+
},
69+
};
70+
71+
// 创建根代理对象
72+
const rootTarget = {};
73+
pathMap.set(rootTarget, []);
74+
return new Proxy(rootTarget, handler);
75+
}
76+
// 测试代码
77+
const proxyState = watchObjectAccess({
78+
get: (path) => {
79+
console.log("GET回调 - 完整路径:", path);
80+
// 当访问到 a.b.c 时返回特定值
81+
if (path.join(".") === "a.b.c") {
82+
return "最终的值:" + path.join(" → ");
83+
}
84+
// 返回 undefined 表示继续代理
85+
return undefined;
86+
},
87+
set: (path, value) => {
88+
console.log("SET回调 - 设置路径:", path, "值:", value);
89+
return true;
90+
},
91+
});
92+
93+
console.log("=== 测试1: 连续属性访问 ===");
94+
const result1 = proxyState.a.b.c;
95+
console.log("结果:", result1);
96+
97+
console.log("\n=== 测试2: 属性赋值 ===");
98+
proxyState.x.y = 123;
99+
100+
console.log("\n=== 测试3: 检查最终对象 ===");
101+
console.log("最终对象类型:", typeof proxyState.a.b.c);

packages/form/examples/index.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
import "./general";
2-
import "./usepath";
3-
import "./transform";
4-
import "./group-tabs";
5-
import "./group-tabs2";
6-
import "./group-collapse";
7-
import "./lazy-select";
8-
import "./addContact";
1+
// import './general';
2+
// import './usepath';
3+
// import './transform';
4+
// import './group-tabs';
5+
// import './group-tabs2';
6+
// import './group-collapse';
7+
// import './lazy-select';
8+
// import './addContact';
99
import "./ipConfig";
10+
import "./ipSyncConfig";
1011
export * from "./debuger";
11-
export * from "./store";
12+
// export * from './store';

0 commit comments

Comments
 (0)