@@ -7,49 +7,46 @@ class NgModelOptions {
7
7
int _debounceChangeValue = null ;
8
8
int _debounceInputValue = null ;
9
9
10
- static const String _debounceDefaultKey = "default" ;
11
- static const String _debounceBlurKey = "blur" ;
12
- static const String _debounceChangeKey = "change" ;
13
- static const String _debounceInputKey = "input" ;
10
+ static const String DEBOUNCE_DEFAULT_KEY = "default" ;
11
+ static const String DEBOUNCE_BLUR_KEY = "blur" ;
12
+ static const String DEBOUNCE_CHANGE_KEY = "change" ;
13
+ static const String DEBOUNCE_INPUT_KEY = "input" ;
14
14
15
15
NgModelOptions (NodeAttrs attrs) {
16
- print ("options: " + attrs["ng-model-options" ].replaceFirst ("debounce" , "'debounce'" ).replaceAll ("'" , "\" " ));
17
16
Map options = convert.JSON .decode (attrs["ng-model-options" ].replaceFirst ("debounce" , "'debounce'" ).replaceAll ("'" , "\" " ));
18
17
19
- if (options["debounce" ].containsKey (_debounceDefaultKey )) _debounceDefaultValue = options["debounce" ][_debounceDefaultKey ];
20
- if (options[ "debounce" ]. containsKey (_debounceBlurKey)) _debounceBlurValue = options["debounce" ][_debounceBlurKey ];
21
- if (options[ "debounce" ]. containsKey (_debounceChangeKey)) _debounceChangeValue = options["debounce" ][_debounceChangeKey ];
22
- if (options[ "debounce" ]. containsKey (_debounceInputKey)) _debounceInputValue = options["debounce" ][_debounceInputKey ];
18
+ if (options["debounce" ].containsKey (DEBOUNCE_DEFAULT_KEY )) _debounceDefaultValue = options["debounce" ][DEBOUNCE_DEFAULT_KEY ];
19
+ _debounceBlurValue = options["debounce" ][DEBOUNCE_BLUR_KEY ];
20
+ _debounceChangeValue = options["debounce" ][DEBOUNCE_CHANGE_KEY ];
21
+ _debounceInputValue = options["debounce" ][DEBOUNCE_INPUT_KEY ];
23
22
}
24
23
25
24
async .Timer _blurTimer;
26
25
void executeBlurFunc (func ()) {
27
- if (_blurTimer != null && ! _blurTimer.isActive) _blurTimer.cancel ();
28
-
29
26
var delay = _debounceBlurValue == null ? _debounceDefaultValue : _debounceBlurValue;
30
- _runFuncDebounced (delay, func, (timer)=> _blurTimer = timer);
27
+ _runFuncDebounced (delay, func, (timer)=> _blurTimer = timer,_blurTimer );
31
28
}
32
29
33
30
async .Timer _changeTimer;
34
31
void executeChangeFunc (func ()) {
35
- if (_changeTimer != null && ! _changeTimer.isActive) _changeTimer.cancel ();
36
-
37
32
var delay = _debounceChangeValue == null ? _debounceDefaultValue : _debounceChangeValue;
38
- _runFuncDebounced (delay, func, (timer)=> _changeTimer = timer);
33
+ _runFuncDebounced (delay, func, (timer)=> _changeTimer = timer, _changeTimer );
39
34
}
40
35
41
36
async .Timer _inputTimer;
42
37
void executeInputFunc (func ()) {
43
- if (_inputTimer != null && _inputTimer.isActive) _inputTimer.cancel ();
44
-
45
38
var delay = _debounceInputValue == null ? _debounceDefaultValue : _debounceInputValue;
46
- _runFuncDebounced (delay, func, (timer) => _inputTimer = timer);
39
+ _runFuncDebounced (delay, func, (timer) => _inputTimer = timer, _inputTimer );
47
40
}
48
41
49
- void _runFuncDebounced (int delay, func (), setTimer (async .Timer timer)){
50
- if (delay == 0 )
42
+ void _runFuncDebounced (int delay, func (), setTimer (async .Timer timer), async .Timer timer){
43
+ if (timer != null && timer.isActive) timer.cancel ();
44
+
45
+ if (delay == 0 ){
51
46
func ();
52
- else
47
+ }
48
+ else {
53
49
setTimer (new async .Timer (new Duration (milliseconds: delay), func));
50
+ }
54
51
}
55
52
}
0 commit comments