Skip to content

Commit

Permalink
feat(core): remove initializeLazySyncState
Browse files Browse the repository at this point in the history
  • Loading branch information
janryWang committed Apr 14, 2020
1 parent 4cb37a2 commit 70094be
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 105 deletions.
25 changes: 0 additions & 25 deletions docs/zh-cn/jsx-develop/performance.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,28 +85,3 @@ onFieldValueChange$('aa').subscribe(() => {

- aa 值变化时触发 bb 所有子节点隐藏
- 使用 hostUpdate 包装,可以在当前操作中阻止精确更新策略,在所有字段状态更新完毕之后直接走根组件重渲染策略,从而起到合并渲染的目的

## 表单初始化渲染卡顿问题

因为 Formily 内部维护了一棵状态树,初始化阶段,会频繁同步状态树,用来保证实时幂等,但这样带来的问题就是整体计算压力比较大,如果在一些比较老旧的浏览器,比如 IE11 上,就很难带动起来,节点数量越多,首次渲染会越卡顿,这个没法避免,所以,我们考虑给 Formily 支持一个参数 `initializeLazySyncState`,用于解决首次渲染的惰性同步状态,但是开启之后,肯定会存在一些副作用问题,比如:

```js
onFieldValueChange$().subscribe(() => {
const values = actions.getFormState(state => state.values)
//初始化阶段基于当前表单的某个值去做一些处理
})
```

开启`initializeLazySyncState`

```js
<SchemaForm initializeLazySyncState>
...
</SchemaForm>

or

<Form initializeLazySyncState>
...
</Form>
```
25 changes: 0 additions & 25 deletions docs/zh-cn/schema-develop/performance.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,28 +86,3 @@ onFieldValueChange$('aa').subscribe(() => {

- aa 值变化时触发 bb 所有子节点隐藏
- 使用 hostUpdate 包装,可以在当前操作中阻止精确更新策略,在所有字段状态更新完毕之后直接走根组件重渲染策略,从而起到合并渲染的目的

## 表单初始化渲染卡顿问题

因为 Formily 内部维护了一棵状态树,初始化阶段,会频繁同步状态树,用来保证实时幂等,但这样带来的问题就是整体计算压力比较大,如果在一些比较老旧的浏览器,比如 IE11 上,就很难带动起来,节点数量越多,首次渲染会越卡顿,这个没法避免,所以,我们考虑给 Formily 支持一个参数 `initializeLazySyncState`,用于解决首次渲染的惰性同步状态,但是开启之后,肯定会存在一些副作用问题,比如:

```js
onFieldValueChange$().subscribe(() => {
const values = actions.getFormState(state => state.values)
//初始化阶段基于当前表单的某个值去做一些处理
})
```

开启`initializeLazySyncState`

```js
<SchemaForm initializeLazySyncState>
...
</SchemaForm>

or

<Form initializeLazySyncState>
...
</Form>
```
77 changes: 25 additions & 52 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ import {
FormPathPattern,
BigData,
each,
isObj,
scheduler
isObj
} from '@formily/shared'
import {
FormValidator,
Expand Down Expand Up @@ -264,20 +263,6 @@ export function createForm<FieldProps, VirtualFieldProps>(
const errorsChanged = field.isDirty('errors')
const editableChanged = field.isDirty('editable')

const initializeLazy = (callback: () => void) => {
if (options.initializeLazySyncState) {
if (initialValueChanged) {
scheduler.applyWithIdlePriority(() => {
callback()
})
} else {
callback()
}
} else {
callback()
}
}

if (initializedChanged) {
heart.publish(LifeCycleTypes.ON_FIELD_INIT, field)
const isEmptyValue = !isValid(published.value)
Expand All @@ -301,19 +286,15 @@ export function createForm<FieldProps, VirtualFieldProps>(
published.visible == false || published.unmounted === true
if (valueChanged) {
if (!wasHidden) {
initializeLazy(() => {
setFormValuesIn(path, published.value, true)
notifyTreeFromValues()
})
setFormValuesIn(path, published.value, true)
notifyTreeFromValues()
}
heart.publish(LifeCycleTypes.ON_FIELD_VALUE_CHANGE, field)
}
if (initialValueChanged) {
if (!wasHidden) {
initializeLazy(() => {
setFormInitialValuesIn(path, published.initialValue, true)
notifyTreeFromInitialValues()
})
setFormInitialValuesIn(path, published.initialValue, true)
notifyTreeFromInitialValues()
}
heart.publish(LifeCycleTypes.ON_FIELD_INITIAL_VALUE_CHANGE, field)
}
Expand All @@ -325,22 +306,18 @@ export function createForm<FieldProps, VirtualFieldProps>(
state.visibleCacheValue = published.value
})
}
initializeLazy(() => {
deleteFormValuesIn(path)
notifyTreeFromValues()
})
deleteFormValuesIn(path)
notifyTreeFromValues()
} else {
if (!existFormValuesIn(path)) {
initializeLazy(() => {
setFormValuesIn(
path,
isValid(published.visibleCacheValue)
? published.visibleCacheValue
: published.initialValue,
true
)
notifyTreeFromValues()
})
setFormValuesIn(
path,
isValid(published.visibleCacheValue)
? published.visibleCacheValue
: published.initialValue,
true
)
notifyTreeFromValues()
}
}
}
Expand All @@ -366,22 +343,18 @@ export function createForm<FieldProps, VirtualFieldProps>(
state.visibleCacheValue = published.value
})
}
initializeLazy(() => {
deleteFormValuesIn(path, true)
notifyTreeFromValues()
})
deleteFormValuesIn(path, true)
notifyTreeFromValues()
} else {
if (!existFormValuesIn(path)) {
initializeLazy(() => {
setFormValuesIn(
path,
isValid(published.visibleCacheValue)
? published.visibleCacheValue
: published.initialValue,
true
)
notifyTreeFromValues()
})
setFormValuesIn(
path,
isValid(published.visibleCacheValue)
? published.visibleCacheValue
: published.initialValue,
true
)
notifyTreeFromValues()
}
}
heart.publish(LifeCycleTypes.ON_FIELD_UNMOUNT, field)
Expand Down
1 change: 0 additions & 1 deletion packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,6 @@ export interface IFormStateProps {
values?: {}
lifecycles?: FormLifeCycle[]
useDirty?: boolean
initializeLazySyncState?: boolean
editable?: boolean | ((name: string) => boolean)
validateFirst?: boolean
}
Expand Down
1 change: 0 additions & 1 deletion packages/react-schema-renderer/src/hooks/useSchemaForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ const useInternalSchemaForm = (props: ISchemaFormProps) => {
onSubmit,
onReset,
onValidateFailed,
initializeLazySyncState,
useDirty,
children,
expressionScope,
Expand Down
1 change: 0 additions & 1 deletion packages/react/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ export interface IFormProps<
| ((form: IForm) => React.ReactElement)
useDirty?: boolean
editable?: boolean | ((name: string) => boolean)
initializeLazySyncState?: boolean
validateFirst?: boolean
}

Expand Down

0 comments on commit 70094be

Please sign in to comment.