Comparing version 0.1.6 to 0.1.7
@@ -1,1 +0,2 @@ | ||
module.exports = require('./lib/flow'); | ||
var Flow = require('./lib/flow'); | ||
module.exports = Flow.Flow = Flow; |
@@ -45,10 +45,17 @@ 'use strict'; | ||
['ready', 'check'].forEach(function(methodName) { | ||
['ready', 'check', 'checkIn', 'checkOut'].forEach(function(methodName) { | ||
debug.enabled && debug('methodName: %s', methodName); | ||
self[methodName] = function(card, step) { | ||
self[methodName] = function() { | ||
if (debug.enabled && methodName === 'check') { | ||
debug(methodName + '() - card: %s / step: %s', card, step); | ||
debug(methodName + '() - arguments: %s', arguments); | ||
} | ||
if (arguments.length === 0) { | ||
throw new Error('method_arguments_must_not_be_empty'); | ||
} | ||
var step = arguments[arguments.length - 1]; | ||
var method = stepObjects[step] && stepObjects[step][methodName]; | ||
return (method) ? method.call(stepObjects[step], card) : undefined; | ||
if (typeof(method) !== 'function') { | ||
throw new Error('the_last_argument_should_be_correct_step'); | ||
} | ||
return method.apply(stepObjects[step], arguments); | ||
} | ||
@@ -64,6 +71,7 @@ }); | ||
Flow.instance = Flow.singleton = function() { | ||
instance = instance || new Flow(); | ||
return instance; | ||
return (instance = instance || new Flow()); | ||
} | ||
Flow.Step = Step; | ||
module.exports = Flow; |
131
lib/step.js
@@ -8,71 +8,90 @@ 'use strict'; | ||
var self = this; | ||
var state = { cards: [], timeout: 0, isTimeout: false }; | ||
this._ctx_ = { | ||
callbacks: [], | ||
cards: [], | ||
timeout: 0, | ||
isTimeout: false, | ||
timeoutHandler: null | ||
}; | ||
var timeoutHandler = null; | ||
var initTimeoutHandler = function(timeout) { | ||
if (typeof(timeout) === 'number') { | ||
if (timeoutHandler) { | ||
clearTimeout(timeoutHandler); | ||
timeoutHandler = null; | ||
} | ||
state.isTimeout = false; | ||
state.timeout = timeout; | ||
if (state.timeout > 0) { | ||
timeoutHandler = setTimeout(function() { | ||
state.isTimeout = true; | ||
self.check(); | ||
}, state.timeout); | ||
} | ||
} | ||
} | ||
this.reset(params); | ||
this.reset = function(params) { | ||
params = params || {}; | ||
debug.enabled && debug('reset() with: %s', JSON.stringify(params)); | ||
debug.enabled && debug(' - constructor end!'); | ||
}; | ||
state.cards = (params.cards instanceof Array) ? params.cards : []; | ||
initTimeoutHandler(params.timeout || 0); | ||
Step.prototype.reset = function(params) { | ||
params = params || {}; | ||
debug.enabled && debug('reset() with: %s', JSON.stringify(params)); | ||
return this; | ||
this._ctx_.callbacks = []; | ||
this._ctx_.cards = (params.cards instanceof Array) ? params.cards : []; | ||
initTimeoutHandler.call(this, params.timeout || 0); | ||
return this; | ||
} | ||
Step.prototype.ready = function(callback) { | ||
debug.enabled && debug('ready() - append a new callback'); | ||
if (typeof(callback) === 'function') { | ||
this._ctx_.callbacks.push(callback); | ||
} | ||
return check.call(this); | ||
} | ||
var callbacks = []; | ||
this.ready = function(callback) { | ||
debug.enabled && debug('ready() - append a new callback'); | ||
if (typeof(callback) === 'function') { | ||
callbacks.push(callback); | ||
Step.prototype.checkIn = function(card) { | ||
debug.enabled && debug('checkIn() - card: %s', card || 'N/A'); | ||
this._ctx_.cards.push(card); | ||
return this; | ||
} | ||
Step.prototype.checkOut = function(card) { | ||
debug.enabled && debug('checkOut() - for card: %s', card || 'N/A'); | ||
var cardInd = this._ctx_.cards.indexOf(card); | ||
if (cardInd >= 0 && !this._ctx_.isTimeout) { | ||
this._ctx_.cards.splice(cardInd, 1); | ||
} | ||
return check.call(this); | ||
} | ||
Step.prototype.check = Step.prototype.checkOut; | ||
var check = function(card) { | ||
var self = this; | ||
debug.enabled && debug('check() - cards: %s', JSON.stringify(self._ctx_.cards)); | ||
if (self._ctx_.isTimeout || self._ctx_.cards.length === 0) { | ||
var info = { isTimeout: self._ctx_.isTimeout, unchecked: self._ctx_.cards }; | ||
if (self._ctx_.isTimeout) { | ||
debug.enabled && debug('check() - step is expired, run callbacks'); | ||
} else { | ||
debug.enabled && debug('check() - all of cards has been checked'); | ||
} | ||
return this.check(); | ||
var cbs = self._ctx_.callbacks || []; | ||
self._ctx_.callbacks = []; | ||
if (cbs.length > 0) { | ||
cbs.forEach(function(callback) { | ||
callback(info); | ||
}); | ||
} | ||
} | ||
return self; | ||
} | ||
this.check = function(card) { | ||
debug.enabled && debug('check() - for card: %s', card || 'N/A'); | ||
var cardInd = state.cards.indexOf(card); | ||
if (cardInd >= 0 && !state.isTimeout) { | ||
state.cards.splice(cardInd, 1); | ||
var initTimeoutHandler = function(timeout) { | ||
var self = this; | ||
if (typeof(timeout) === 'number') { | ||
if (self._ctx_.timeoutHandler) { | ||
clearTimeout(self._ctx_.timeoutHandler); | ||
self._ctx_.timeoutHandler = null; | ||
} | ||
if (state.isTimeout || state.cards.length === 0) { | ||
var info = { isTimeout: state.isTimeout, unchecked: state.cards }; | ||
if (state.isTimeout) { | ||
debug.enabled && debug('check() - step is expired, run callbacks'); | ||
} else { | ||
debug.enabled && debug('check() - all of cards has been checked'); | ||
} | ||
var cbs = callbacks || []; | ||
callbacks = []; | ||
if (cbs.length > 0) { | ||
cbs.forEach(function(callback) { | ||
callback(info); | ||
}); | ||
} | ||
self._ctx_.isTimeout = false; | ||
self._ctx_.timeout = timeout; | ||
if (self._ctx_.timeout > 0) { | ||
self._ctx_.timeoutHandler = setTimeout(function() { | ||
self._ctx_.isTimeout = true; | ||
check.call(self); | ||
}, self._ctx_.timeout); | ||
} | ||
return this; | ||
} | ||
} | ||
this.reset(params); | ||
debug.enabled && debug(' - constructor end!'); | ||
}; | ||
module.exports = Step; |
{ | ||
"name": "loadsync", | ||
"version": "0.1.6", | ||
"version": "0.1.7", | ||
"description": "Load javascript libraries in browser synchronously", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -55,3 +55,3 @@ # loadsync | ||
loadsync.ready(function(info) { | ||
// code should be run after STATE1, STATE2, STATE3 have been checked | ||
// will be run after STATE1, STATE2, STATE3 have been checked | ||
// or there are some cards not be checked (timeout) | ||
@@ -58,0 +58,0 @@ // info.isTimeout: true/false |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
6506
147