Created
October 15, 2015 03:42
-
-
Save antonycourtney/548df5ba7f4975f758c6 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import EventEmitter from 'events'; | |
export class Ref extends EventEmitter { | |
constructor(v) { | |
super(); | |
this._value = v; | |
} | |
getValue() { | |
return this._value; | |
} | |
/** | |
* update contents of this ref cell and notify any listeners | |
*/ | |
setValue(v) { | |
this._value = v; | |
this.emit("change"); | |
} | |
} | |
/** | |
* Given a Ref<A> returns an 'updater' function. | |
* | |
* An updater is a function that takes an ((A) => A) update function (uf), | |
* applies it to the current value in ref and sets ref to the result (which | |
* will notify registered listeners). | |
* | |
* refUpdater: (ref: Ref<A>) => (uf: (A) => A) => void | |
* | |
* We use Currying here so that we can partially apply refUpdater | |
* to obtain an updater function that can passed down to actions. | |
*/ | |
export const refUpdater = (ref) => (uf) => { ref.setValue(uf(ref.getValue())); }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment