A javascript class that allows you to watch a value. This is a simple version which means there are some restriction when using.
Please pass in a function with returning a boolean value. When you change
value, this callback
will be called. And it's return value will be translate to current status of the Whenever instance. (true for SimpleWhenever.status
= 'resolve' and false for SimpleWhenever.status
= 'reject').This call back will be SimpleWhenever.judge
add callback
to whenever, and will be execute whenever the SimpleWhenever.judge
return true. If the SimpleWhenever.status == 'resolve'
, callback
will execute right away.If once
equals true, this callback
will execute only once.
similar to yes
but will execute when SimpleWhenever.judge
return false or SimpleWhenever.status == 'reject'
.
change Whenever value.This will call SimpleWhenever.judge
, and execute yes functions or no functions according to the return value.
var w = new SimpleWhenever(function(value){
return value == 1;
});
w.yes(function(){
console.log('yes once');
},true);
w.yes(function(){
console.log('yesyes');
});
w.no(function(){
console.log('no once');
},true);
w.no(function(){
console.log('nono');
});
w.change(1);
//output: yesyes
//output: yes once
//explain: once functions will execute later
w.yes(function(){
console.log('yesyesyes once');
},true);
//output: yesyesyes once
//explain: any functions add to yes will execute right away if matches value.
w.change(2);
//output: nono
//output: no once
//explain: once functions will execute later than forever one
w.change(1);
//output: yesyes
//explain: once functions will remove after execution
w.change(2);
//output: nono
w.no(function(){
console.log('nonono once');
},true);
//output: nonono once
//explain: any functions add to no will execute right away if do not matches value.
w.change(1);
//output: yesyes
w.change(2);
//output: nono