UNPKG

@phasor/array

Version:

Lightweight ordered function execution; phases

82 lines (76 loc) 2.18 kB
// Generated by CoffeeScript 1.10.0 var PhasorArray; exports["default"] = exports.PhasorArray = PhasorArray = (function() { function PhasorArray() { this._phaseActions = []; this._phaseCounts = {}; this._phases = []; } PhasorArray.prototype.add = function(arg0, arg1) { var actionIndex, after, fn, i, index, j, len, len1, otherId, otherIndex, phase, phaseId, phaseIndex, ref, ref1, ref2, ref3; phaseId = (ref = arg0.id) != null ? ref : arg0; fn = arg1 != null ? arg1 : arg0.fn; otherId = (ref1 = arg0.after) != null ? ref1 : arg0.before; after = arg0.after; phaseIndex = -1; otherIndex = -1; actionIndex = 0; ref2 = this._phases; for (index = i = 0, len = ref2.length; i < len; index = ++i) { phase = ref2[index]; if (!(phaseIndex === -1)) { continue; } if (phase === otherId) { otherIndex = index; } if (phase === phaseId) { phaseIndex = index; } else { actionIndex += this._phaseCounts[phase]; } } if (phaseIndex === -1) { phaseIndex = otherIndex; if (phaseIndex > -1) { if (after) { phaseIndex++; } if (fn != null) { actionIndex = 0; ref3 = this._phases; for (index = j = 0, len1 = ref3.length; j < len1; index = ++j) { phase = ref3[index]; if (index < phaseIndex) { actionIndex += this._phaseCounts[phase]; } } } } else { phaseIndex = this._phases.length; } this._phases.splice(phaseIndex, 0, phaseId); this._phaseCounts[phaseId] = 0; } if (fn != null) { this._phaseActions.splice(actionIndex, 0, fn); this._phaseCounts[phaseId]++; } }; PhasorArray.prototype.run = function(context) { var fn, i, len, ref, result; if (context == null) { context = {}; } ref = this._phaseActions; for (i = 0, len = ref.length; i < len; i++) { fn = ref[i]; result = fn.call(context, context); if (result === false) { break; } } return context; }; return PhasorArray; })();