Skip to content

Instantly share code, notes, and snippets.

@khanghoang
Created April 21, 2018 22:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save khanghoang/1131abc4c908ac9ab6f522603c1a6cba to your computer and use it in GitHub Desktop.
Save khanghoang/1131abc4c908ac9ab6f522603c1a6cba to your computer and use it in GitHub Desktop.
grab.signal.js
/* @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