Skip to content

Instantly share code, notes, and snippets.

@antonycourtney
Created October 15, 2015 03:42
Show Gist options
  • Save antonycourtney/548df5ba7f4975f758c6 to your computer and use it in GitHub Desktop.
Save antonycourtney/548df5ba7f4975f758c6 to your computer and use it in GitHub Desktop.
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