Created
April 21, 2018 22:29
-
-
Save khanghoang/1131abc4c908ac9ab6f522603c1a6cba to your computer and use it in GitHub Desktop.
grab.signal.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* @flow */ | |
class Signal { | |
value: any; | |
fns: Array<Function> | |
constructor(value: any) { | |
this.value = value; | |
this.fns = []; | |
} | |
update(value: any): void { | |
this.value = value; | |
for (let i = 0; i < this.fns.length; i++) { | |
const currentValue = this.fns[i](this.value); | |
this.value = currentValue; | |
} | |
} | |
next(fn: Function): Signal { | |
this.fns.push(fn); | |
return this; | |
} | |
map(fn: Function): Signal { | |
this.fns.push(arr => arr.map(fn)); | |
return this; | |
} | |
filter(fn: Function): Signal { | |
this.fns.push(arr => arr.filter(fn)); | |
return this; | |
} | |
} | |
const foo = new Signal(); | |
const bar = foo.map(i => i * 2) | |
.filter(a => a % 2 === 0) | |
.next(console.log) | |
bar.update([1, 2]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment