Skip to content

Instantly share code, notes, and snippets.

@ympbyc
Last active December 9, 2015 23:39
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 ympbyc/4345466 to your computer and use it in GitHub Desktop.
Save ympbyc/4345466 to your computer and use it in GitHub Desktop.
Looking for a way to program GUI as purely functional as possible
var SB = require('./StateBeaver');
//Simple app that have a window and a button.
//Pressing the button will change the color of the window
/*
* domPatcher
* Create patches to the DOM according to the current app state
*/
var domPatcher = function (state) {
if (state.color === 'green')
return {
button1: SB.ui(Ti.UI.createButton, {title:state.buttonText}, 'win', SB.signal('click', 'to-red' ))
, button2: SB.ui(Ti.UI.createButton, {title:'to-red', bottom:10}, 'win', SB.signal('click', 'to-red' ))
, win : SB.style({backgroundColor:'green'})}
if (state.color === 'red')
return {
button1: SB.ui(Ti.UI.createButton, {title:state.buttonText}, 'win', SB.signal('click', 'to-blue' ))
, win: SB.style({backgroundColor:'red'})}
if (state.color === 'blue')
return {
button1: SB.ui(Ti.UI.createButton, {title:state.buttonText}, 'win', SB.signal('click', 'to-green'))
, win: SB.style({backgroundColor:'blue'})}
};
/*
* stateBeaver
* Invoked whenever an event occur at the DOM
* Changes the state according to the event
*/
var stateBeaver = SB.createStateBeaver({
'to-green': function (state) { return {color: 'green', buttonText:'to-red'}; }
, 'to-blue' : function (state) { return {color: 'blue' , buttonText:'to-green' }; }
, 'to-red' : function (state) { return {color: 'red' , buttonText:'to-blue' }; }
});
/*
* Main
* Construct the initial DOM and the initial state.
* Give them to the DOMCONSTRUCTOR
*/
(function () {
var initialDOM = {
win: Ti.UI.createWindow()
};
var initialState = {
color:'green'
, buttonText:'to-red'
};
initialDOM.win.open();
SB.createDomManager(domPatcher, stateBeaver)(initialState, initialDOM);
}());
/*
* State Beaver -- Functional GUI programming pattern
* 2012 Minori Yamashita <ympbyc@gmail.com>
*/
/* ------------------------------ StateBeaver ------------------------------ */
exports.style = style;
exports.ui = ui;
exports.signal = signal;
exports.createStateBeaver = createStateBeaver;
exports.createDomManager = createDomManager;
/* -- Data Types -- */
function style (s) {
return {
isStyle: true
, style :s
};
}
function ui (TiUIConstructor, style, parentKey, sig) {
return {
isUi: true
, TiUIConstructor: TiUIConstructor
, style: style
, parentKey: parentKey
, sig : sig
};
}
function signal (raw, sig) {
return {
raw: raw
, signal: sig
};
}
/* -- Constructors -- */
function createStateBeaver (config) {
return function (signal, state, DOM, DOMConstructor) {
return DOMConstructor(merge(state, config[signal](state)), DOM);
};
}
function createDomManager (fn, mainloop) {
var dc = function (state, dom) {
dom.emitSignal = function (sig) {return mainloop(sig, state, dom, dc);};
var patch = fn(state, dom);
_each(patch, function (p, key) {
if (p.isStyle) return _merge(dom[key], p.style);
if (!!dom[key]) dom[p.parentKey].remove(dom[key]);
var ui = p.TiUIConstructor(p.style);
dom[p.parentKey].add(ui);
dom[key] = ui;
//Ultimately we want to watch for events from a higher place.
//This is an emulation of the disired behaviour
ui.addEventListener(p.sig.raw, function () {
return dom.emitSignal(p.sig.signal);
});
});
}
return dc;
}
/* -- Helpers -- */
//pure merge
function merge (dst, src) {
var o = {}, key;
_merge(o, dst);
_merge(o, src);
return o;
}
//impure merge
function _merge (dst, src) {
_each(src, function (s, k) {
dst[k] = s
});
return null;
}
function _each (obj, fn) {
var key;
for (key in obj)
if (obj.hasOwnProperty(key))
fn(obj[key], key);
return null;
}
@ympbyc
Copy link
Author

ympbyc commented Dec 20, 2012

The idea is to have the DOM that updates automatically whenever the state changes.

Events occur at the DOM are forwarded to the stateBeaver. The stateBeaver creates a new state from the previous state and the event occurred. The new state causes the DOM to update. and the cycle idles, until the next event.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment