Skip to content

Commit

Permalink
debug debounce of FieldState (#13)
Browse files Browse the repository at this point in the history
* debug debounce of FieldState

* v1.0.8
  • Loading branch information
nighca authored Feb 14, 2020
1 parent 9a05a63 commit 16b11a4
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "formstate-x",
"version": "1.0.7",
"version": "1.0.8",
"description": "Extended alternative for formstate",
"repository": {
"type": "git",
Expand Down
25 changes: 25 additions & 0 deletions src/fieldState.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -375,4 +375,29 @@ describe('FieldState validation', () => {

state.dispose()
})

it('should work well with delay', async () => {
const state = new FieldState('0', 1000)

state.onChange('1')
expect(state.value).toBe('0')
await delay(250)
expect(state.value).toBe('0')

state.onChange('2')
expect(state.value).toBe('0')
await delay(500)
expect(state.value).toBe('0')

state.onChange('3')
expect(state.value).toBe('0')
await delay(750)
expect(state.value).toBe('0')

state.onChange('4')
expect(state.value).toBe('0')
await delay(1250)
expect(state.value).toBe('4')

})
})
11 changes: 7 additions & 4 deletions src/fieldState.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { observable, computed, action, reaction, autorun, runInAction, when } from 'mobx'
import { ComposibleValidatable, Validator, Validated, ValidationResponse, ValidateStatus } from './types'
import { applyValidators } from './utils'
import { applyValidators, debounce } from './utils'
import Disposable from './disposable'

/**
Expand Down Expand Up @@ -233,14 +233,17 @@ export default class FieldState<TValue> extends Disposable implements Composible
// debounced reaction to `_value` change
this.addDisposer(reaction(
() => this._value,
() => {
// use debounce instead of reactionOptions.delay
// cause the later do throttle in fact, not debounce
// see https://github.com/mobxjs/mobx/issues/1956
debounce(() => {
if (this.value !== this._value) {
this.value = this._value
this._validateStatus = ValidateStatus.NotValidated
this._activated = true
}
},
{ delay, name: 'reaction-when-_value-change' }
}, delay),
{ name: 'reaction-when-_value-change' }
))

// auto sync when validate ok: this.value -> this.$
Expand Down
10 changes: 10 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,13 @@ export function applyValidators<TValue>(value: TValue, validators: Validator<TVa

return asyncResponsesAnd(asyncResponses)
}

export function debounce(fn: () => void, delay: number) {
let timeout: any = null
return () => {
if (timeout) {
clearTimeout(timeout)
}
timeout = setTimeout(fn, delay)
}
}

0 comments on commit 16b11a4

Please sign in to comment.