Skip to content

Commit

Permalink
Refactored augmentation mechanism to reduce memory footprint
Browse files Browse the repository at this point in the history
  • Loading branch information
ecogodi committed Jan 4, 2015
1 parent 72df8e5 commit 1390fe8
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 46 deletions.
90 changes: 45 additions & 45 deletions lib/f.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,14 @@
var step_revealed = {exit:boundExit, rewind:thisRun._rewind};

/**
* Methods and properties available to augmentations.
* Methods and properties available to augmentations of type 'stateF'.
* Bound as 'this' context.
*
* @type {Object}
*/
var augment_context = {exit:thisRun._exit, rewind:thisRun._rewind, setNextStepId: thisRun._setNextStepId, runOneStep: runOneStep };
for(var a in augmentations){
if(augmentations[a].type == 'stateF')
step_revealed[augmentations[a].name] = augmentations[a].f.bind(augment_context);
for(var a in augmentations['stateF']){
step_revealed[augmentations['stateF'][a].name] = augmentations['stateF'][a].f.bind(augment_context);
}

/**
Expand Down Expand Up @@ -84,7 +83,7 @@

// augmentations are allowed to enter our flow here, by registering as step filters
if(typeof step !== 'function')
step = doFilter('stepFilter', step);
step = doFilter('stepFilter', step, state);

if(typeof step == 'function'){
var numargs = (step._numArgs!==undefined) ? step._numArgs : step.length;
Expand Down Expand Up @@ -115,7 +114,7 @@
thisRun._parallel.finishing = (thisRun._parallel.c>0 && thisRun._parallel.p == 0);

//Hook for augments manipulating the end of parallelization
//thisRun._parallel = doFilter('endParallelFilter', thisRun._parallel);
//thisRun._parallel = doFilter('endParallelFilter', thisRun._parallel, state);

if(thisRun._parallel.finishing)
finishParallel(next);
Expand All @@ -124,34 +123,6 @@
throw('Unknown sequence step type for: '+step)
}

var doFilter = function(type, ob){
var filters;
if(!(augmentations[type])){
filters = [];
for(var a in augmentations){
if(augmentations[a].type == type)
filters.push(augmentations[a]);
}
if(filters.length){
filters.sort(function(a1,a2){return a1.options.order - a2.options.order});
augmentations[type] = filters;
}
}
else {
filters = augmentations[type];
}

if(filters.length){
var oldOb;
for(var fi in filters){
oldOb = ob;
ob = (filters[fi].f.bind(state))(oldOb);
}
}

return ob;
}

var finishParallel = function(endNext){
var next = seqNext(),
collectorArgsArray = [];
Expand Down Expand Up @@ -214,7 +185,6 @@
seqNext().apply(this, execArgsArray);
}

//thisRun.apply(this,Array.prototype.slice.call(arguments));
thisRun.exec.apply(thisRun, runArgsArray);
}

Expand All @@ -225,20 +195,50 @@
return _define(Array.prototype.slice.call(arguments));
}

var augmentations = [];
var augmentations = {};
F.augment = function(augs, force){
var ob, name;
var ob, name, type, tosort =[];
if(!(augs instanceof Array))
augs = [augs];
for(i in augs){
ob = augs[i];
name = ob.name;
if(F[name] && !force)
throw name+' was ignored: conflicting with existing augmentation.';
augs = [augs];
for(var ai in augs){
ob = augs[ai];
if(typeof ob.f !== 'function')
throw name+' was ignored: .f not a function.';
augmentations.push(ob);
throw 'Augmentation error: .f not a function.';
if(typeof ob.type !== 'string')
throw 'Augmentation error: .type not a string.';

name = ob.name || f.name;
type = ob.type;

if(!augmentations[type])
augmentations[type] = [];

augmentations[type].push(ob);

if(ob.options && ob.options.order)
tosort.push(type);
}

for(var ti in tosort){
augmentations[tosort[ti]].sort(function(a1,a2){
return (parseInt(a1.options.order,10) || Infinity) - (parseInt(a2.options.order,10) || Infinity);
});
}
}

var doFilter = function(type, ob, context){
context = context || F;
var filters = augmentations[type];
if(!filters || filters.length === 0)
return ob;

var oldOb;
for(var fi in filters){
oldOb = ob;
ob = (filters[fi].f.bind(context))(oldOb);
}

return ob;
}

if (typeof module.exports !== "undefined") {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fprime",
"version": "1.0.6",
"version": "1.0.7",
"description": "An async sequences execution library. It has a very simple core, but is instrumented to add more complex flow control features.",
"keywords": [
"async",
Expand Down

0 comments on commit 1390fe8

Please sign in to comment.