Ok so if you don't know any haskell you have absolutely no idea what I'm talking about, but bear with me. I wrote up a javascript library for fun and then a week later a colleague showed me machina.js which infuriated me due to the approach it suggests, but maybe it's just me. Maybe I'm nuts.
So looking for opinions from other experienced javascript folks if my approach is truly bonkers.
My library provides "then", "or", and "until" functions which chain as well as "success" and "failure" functions which are to be used for the return value in the functions you chain.
Go take a look at machina.js's first example over here (scroll down a touch you'll see it):
https://github.com/i...else/machina.js
Now look at how you would implement the identical behaviour using my library's functions which I mention above, and tell me which is clearer and if I'm nuts in my approach. Is my library actually useful considering machina.js is apparently seen as such (I did mine for fun presuming it useless to the community).
var isOnline = function(a) {
var online = true;
// checks window.navigator.online and more, sets the online value
return online ? success(a) : failure(a);
};
var setOfflineTime = function(a) {
a.offlineMarkerTime = new Date();
return success(a);
};
var saveBatchToRemote = function(a) {
return storage.saveBatchToRemote(a) : success(a) ? failure(a);
};
var saveToRemote = function(a) {
return storage.saveToRemote(a) : success(a) ? failure(a);
};
var saveToLocal = function(a) {
return storage.saveToLocal(a) : success(a) : failure(a);
};
var getFromLocal = function(a) {
return success(storage.getFromLocal(a.offlineMarkerTime));
// should have validation and failure but example didn't
};
var goOnline = getFromLocal.then(isOnline).then(saveBatchToRemote);
var goOffline = setOfflineTime.then(goOnline.or(saveToLocal));
var machine = goOnline.or(goOffline);
var currentCustomer = machine(someCustomer).a; // how it might be run
Note, the last 3/4 lines are the key part where I'm actually composing the machine itself from it's component parts which is what I believe is preferable to the approach machina.js gives which isn't compositional.

New Topic/Question
Reply


MultiQuote




|