-
Notifications
You must be signed in to change notification settings - Fork 473
fix: 优化全局状态处理和资源加载逻辑 #1207
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
fix: 优化全局状态处理和资源加载逻辑 #1207
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,14 +13,43 @@ export function useGlobalState() { | |
| watchEffect(() => { | ||
| reset(stores) | ||
| globalState.value.forEach(({ id, state = {}, getters = {} }) => { | ||
| const computedGetters = Object.keys(getters).reduce( | ||
| (acc, key) => ({ | ||
| ...acc, | ||
| [key]: new Func('return ' + getters[key])().call(acc, state) // parseData(getters[key], null, acc)?.call?.(acc, state) //理论上不应该走parseData, unibuy代码遗留 | ||
| }), | ||
| {} | ||
| ) | ||
| stores[id] = { ...state, ...computedGetters } | ||
| const hasGetters = Object.keys(getters).length > 0 | ||
|
|
||
| if (Array.isArray(state)) { | ||
| if (!hasGetters) { | ||
| stores[id] = [...state] | ||
| } else { | ||
| const computedGetters = {} | ||
| Object.keys(getters).forEach((key) => { | ||
| try { | ||
| computedGetters[key] = new Func('return ' + getters[key])().call(computedGetters, state) | ||
| } catch (error) { | ||
| computedGetters[key] = undefined | ||
| } | ||
| }) | ||
|
|
||
| const arrayWithGetters = [...state] | ||
| Object.assign(arrayWithGetters, computedGetters) | ||
| stores[id] = arrayWithGetters | ||
| } | ||
| } else if (typeof state !== 'object' || state === null) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 我们将全局状态的 state 限制为 plain object 会不会好一点? |
||
| stores[id] = state | ||
| } else { | ||
| if (!hasGetters) { | ||
| stores[id] = { ...state } | ||
| } else { | ||
| const computedGetters = {} | ||
| Object.keys(getters).forEach((key) => { | ||
| try { | ||
| computedGetters[key] = new Func('return ' + getters[key])().call(computedGetters, state) | ||
| } catch (error) { | ||
| computedGetters[key] = undefined | ||
| } | ||
| }) | ||
|
|
||
| stores[id] = Object.assign({}, state, computedGetters) | ||
| } | ||
| } | ||
| }) | ||
| }) | ||
| return { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -503,14 +503,8 @@ export default { | |
| state.variables = {} | ||
|
|
||
| const stores = useResource().appSchemaState.globalState | ||
| stores.forEach(({ id, state: storeState = {}, getters = {} }) => { | ||
| const loadProp = (prop) => { | ||
| const propBinding = `${id}.${prop}` | ||
| state.variables[propBinding] = propBinding | ||
| } | ||
|
|
||
| Object.keys(storeState).forEach(loadProp) | ||
| Object.keys(getters).forEach(loadProp) | ||
| stores.forEach(({ id, state: _storeState = {}, _getters = {} }) => { | ||
| state.variables[id] = id | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| }) | ||
| } else if (item.id === 'loop') { | ||
| state.bindPrefix = '' | ||
|
|
||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 原代码中,直接赋值为 store(),出码后,变量的值变成了 store 实例,而不是所需的变量值 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,7 +4,7 @@ const useStores = () => { | |
| const stores = {} | ||
|
|
||
| Object.values({ ...useDefinedStores }).forEach((store) => { | ||
| stores[store.$id] = store() | ||
| stores[store.$id] = store().$state | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 请问这里改成 这里应该不仅仅只赋值 store 的 state。
|
||
| }) | ||
|
|
||
| return stores | ||
|
|
||






There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
可以给一下相关的场景用例数据嘛?我们结合来评估一下,麻烦啦~
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
原来的代码,新建一个应用状态,如: testList: ["One", "Two", "Three", "Four"],变量列表中会展示四个变量: testList.0、testList.1、testList.2、testList.3
如果是字符串,如 demo: "Hello",变量列表会得到 demo.0/demo.1/……