Skip to content

Commit

Permalink
Allow thaw to be used both as a Constructor and not. Diverge how each…
Browse files Browse the repository at this point in the history
… is used, but connect their same thawing private variable.

Also, detect window so can be reused in Node.js and other platforms.
  • Loading branch information
robertleeplummerjr committed Nov 19, 2014
1 parent a5d3a4d commit 1612ddf
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 51 deletions.
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "thaw.js",
"main": "thaw.js",
"version": "1.1.1",
"version": "1.1.2",
"homepage": "https://github.com/robertleeplummerjr/thaw.js",
"authors": [
"Robert Plummer <[email protected]> (http://visop-dev.com)"
Expand Down
120 changes: 70 additions & 50 deletions thaw.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,21 @@
/**
* thaw an array of items
* @param {Array} items
* @param {Object} [options]
* @constructor
*/
var Thaw = (function() {
var Thaw = (function(window) {

//private variables
var thawing = false,
thaws = [];

function setDefaults(options) {
var defaultSettings = Constructor.defaultSettings;

if (options !== undefined) {
for(var key in defaultSettings) {
if (defaultSettings.hasOwnProperty(key)) {
if (!options.hasOwnProperty(key)) {
options[key] = defaultSettings[key];
}
}
}
} else {
options = defaultSettings;
}
return options;
}

//Constructor
function Constructor(items, options) {
options = setDefaults(options);
/**
* thaw an array of items
* @param {Array} items
* @param {Object} [options]
* @constructor
*/
function Thaw(items, options) {
options = options || {};

var timeout,
each = options.each,
done = options.done,
each = options.each || null,
done = options.done || null,
self = this,
tick = this.tick = function () {
var items = self.items,
Expand Down Expand Up @@ -80,12 +62,12 @@ var Thaw = (function() {
*
* @type {{each: null, done: null}}
*/
Constructor.defaultSettings = {
Thaw.defaultSettings = {
each: null,
done: null
};

Constructor.prototype = {
Thaw.prototype = {
/**
*
* @param item
Expand Down Expand Up @@ -137,22 +119,22 @@ var Thaw = (function() {
* @param {Object} item
* @param {Object} [options]
*/
Constructor.it = function(item, options) {
Thaw.it = function(item, options) {
return new Constructor([item], options)
};

/**
* returns if Thaw.js is thawing
* @returns {boolean}
*/
Constructor.isThawing = function() {
Thaw.isThawing = function() {
return thawing;
};

/**
* Stops all Thaw.js instances
*/
Constructor.stopAll = function() {
Thaw.stopAll = function() {
var i = 0,
max = thaws.length;

Expand All @@ -161,22 +143,60 @@ var Thaw = (function() {
}
};

return Constructor;
})(),

/**
* wraps the constructor Thaw
* @param {Array} items
* @param {Object} [options]
* @returns Thaw
*/
thaw = (function(Thaw) {
function fn(items, options) {
return new Thaw(items, options);

/**
* simple thaw
* @param {Array} items
* @param {Object} [options]
* @returns Thaw
*/
function thaw(items, options) {
options = options || {};

var timeout,
i = 0,
done = options.done || null,
each = options.each || null;

function tick() {
if (i < 0) return;

timeout = setTimeout(tick, 0);

if (!thawing) {
if (i >= items.length) {

if (done !== null) {
thawing = true;
done.call(items[i]);
thawing = false;
i = -1;
}

clearTimeout(timeout);
return;
}

if (each !== null) {
thawing = true;
each.call(items[i], i);
thawing = false;
} else {
items[i]();
}
i++;
}
}

tick();
}

fn.stopAll = Thaw.stopAll;
fn.isThawing = Thaw.isThawing;
thaw.stopAll = Thaw.stopAll;
thaw.isThawing = Thaw.isThawing;

if (window !== null) {
window.thaw = thaw;
}

return fn;
})(Thaw);
return Thaw;
})(typeof window !== 'undefined' ? window : null);

0 comments on commit 1612ddf

Please sign in to comment.