Skip to content

Commit

Permalink
fix(core): fix memo leak of onFieldReact/onFieldChange (#3231)
Browse files Browse the repository at this point in the history
* fix(core): fix memo leak of onFieldReact/onFieldChange

* test: add some tests
  • Loading branch information
janryWang authored Jun 26, 2022
1 parent cc1f5d4 commit d1c4451
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 35 deletions.
24 changes: 24 additions & 0 deletions packages/core/src/__tests__/field.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2253,3 +2253,27 @@ test('field destroyed or display none should not be assign value from patch init
expect(aa.value).toBe('123')
expect(form.values).toEqual({ aa: '123' })
})

test('onFieldReact with field destroyed', () => {
const fn = jest.fn()
const obs = observable<any>({ value: 123 })
const form = attach(
createForm({
effects() {
onFieldReact('aa', () => {
fn(obs.value)
})
},
})
)
const aa = attach(
form.createField({
name: 'aa',
})
)
obs.value = '321'
expect(fn).toBeCalledTimes(2)
aa.destroy()
obs.value = '111'
expect(fn).toBeCalledTimes(2)
})
37 changes: 11 additions & 26 deletions packages/core/src/effects/onFieldEffects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
IFieldState,
} from '../types'
import { createEffectHook, useEffectForm } from '../shared/effective'
import { onFormUnmount } from './onFormEffects'

function createFieldEffect<Result extends GeneralField = GeneralField>(
type: LifeCycleTypes
Expand Down Expand Up @@ -110,19 +109,13 @@ export function onFieldReact(
pattern: FormPathPattern,
callback?: (field: GeneralField, form: Form) => void
) {
const disposers = []
onFieldInit(pattern, (field, form) => {
disposers.push(
field.disposers.push(
autorun(() => {
if (isFn(callback)) callback(field, form)
})
)
})
onFormUnmount(() => {
disposers.forEach((dispose) => {
dispose()
})
})
}
export function onFieldChange(
pattern: FormPathPattern,
Expand All @@ -144,26 +137,18 @@ export function onFieldChange(
} else {
watches = watches || ['value']
}
const disposers = []
onFieldInit(pattern, (field, form) => {
if (isFn(callback)) callback(field, form)
disposers.push(
reaction(
() => {
return toArr(watches).map((key) => {
return field[key]
})
},
() => {
if (isFn(callback)) callback(field, form)
}
)
const dispose = reaction(
() => {
return toArr(watches).map((key) => {
return field[key]
})
},
() => {
if (isFn(callback)) callback(field, form)
}
)
})

onFormUnmount(() => {
disposers.forEach((dispose) => {
dispose()
})
field.disposers.push(dispose)
})
}
16 changes: 8 additions & 8 deletions packages/core/src/models/Form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ export class Form<ValueType extends object = any> {
Component extends JSXComponent
>(
props: IFieldFactoryProps<Decorator, Component>
) => {
): Field<Decorator, Component> => {
const address = FormPath.parse(props.basePath).concat(props.name)
const identifier = address.toString()
if (!identifier) return
Expand All @@ -304,15 +304,15 @@ export class Form<ValueType extends object = any> {
})
this.notify(LifeCycleTypes.ON_FORM_GRAPH_CHANGE)
}
return this.fields[identifier] as Field<Decorator, Component>
return this.fields[identifier] as any
}

createArrayField = <
Decorator extends JSXComponent,
Component extends JSXComponent
>(
props: IFieldFactoryProps<Decorator, Component>
) => {
): ArrayField<Decorator, Component> => {
const address = FormPath.parse(props.basePath).concat(props.name)
const identifier = address.toString()
if (!identifier) return
Expand All @@ -330,15 +330,15 @@ export class Form<ValueType extends object = any> {
})
this.notify(LifeCycleTypes.ON_FORM_GRAPH_CHANGE)
}
return this.fields[identifier] as ArrayField<Decorator, Component>
return this.fields[identifier] as any
}

createObjectField = <
Decorator extends JSXComponent,
Component extends JSXComponent
>(
props: IFieldFactoryProps<Decorator, Component>
) => {
): ObjectField<Decorator, Component> => {
const address = FormPath.parse(props.basePath).concat(props.name)
const identifier = address.toString()
if (!identifier) return
Expand All @@ -356,15 +356,15 @@ export class Form<ValueType extends object = any> {
})
this.notify(LifeCycleTypes.ON_FORM_GRAPH_CHANGE)
}
return this.fields[identifier] as ObjectField<Decorator, Component>
return this.fields[identifier] as any
}

createVoidField = <
Decorator extends JSXComponent,
Component extends JSXComponent
>(
props: IVoidFieldFactoryProps<Decorator, Component>
) => {
): VoidField<Decorator, Component> => {
const address = FormPath.parse(props.basePath).concat(props.name)
const identifier = address.toString()
if (!identifier) return
Expand All @@ -374,7 +374,7 @@ export class Form<ValueType extends object = any> {
})
this.notify(LifeCycleTypes.ON_FORM_GRAPH_CHANGE)
}
return this.fields[identifier] as VoidField<Decorator, Component>
return this.fields[identifier] as any
}

/** 状态操作模型 **/
Expand Down
2 changes: 1 addition & 1 deletion packages/reactive/src/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export class ArraySet<T> {

forEach(callback: (value: T) => void) {
if (this.value.length === 0) return
for (let index = 0, len = this.value.length; index < len; index++) {
for (let index = 0; index < this.value.length; index++) {
callback(this.value[index])
}
}
Expand Down

0 comments on commit d1c4451

Please sign in to comment.