Skip to content

Instantly share code, notes, and snippets.

Created December 18, 2012 14:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/4328616 to your computer and use it in GitHub Desktop.
Save anonymous/4328616 to your computer and use it in GitHub Desktop.
Functional Reactive Programming (FRP)をjsで手書きするとこんな感じになるか。
require('allthemagicalstuff');
//event object constructor;
function event (target, eventName) {
return {
target: target
, eventName: eventName
};
};
/*
* SIGNALS
*/
var MouseE , mouseS;
MouseE.clicks = event(document, 'click');
MouseS.position = function (e) {
return {x:e.pageX, y:pageY};
};
//under the hood
function sampleOn (ev, signal) {
var samples = [];
var signal = extend(function () {
return samples;
}, EventEmitter);
ev.target.addEventListener(ev.eventName, function (e) {
samples.push(signal(e));
signal.fire('change');
});
return signal;
}
var clickLocations = sampleOn(MouseE.clicks, MouseS.position); //is a compound signal
//capture the scene
function scene (locs) {
return map(drawPoint, locs);
};
function drawPoint (pt) {
return canvas.point(pt.x, pt.y);
};
function lift (f, sig) {
sig.addEventListener('change');
f(sig());
}
scene(clickLocations);
lift(scene, clickLocations);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment