Skip to content

Commit 1612ddf

Browse files
Allow thaw to be used both as a Constructor and not. Diverge how each is used, but connect their same thawing private variable.
Also, detect window so can be reused in Node.js and other platforms.
1 parent a5d3a4d commit 1612ddf

File tree

2 files changed

+71
-51
lines changed

2 files changed

+71
-51
lines changed

bower.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "thaw.js",
33
"main": "thaw.js",
4-
"version": "1.1.1",
4+
"version": "1.1.2",
55
"homepage": "https://github.com/robertleeplummerjr/thaw.js",
66
"authors": [
77
"Robert Plummer <robertleeplummerjr@gmail.com> (http://visop-dev.com)"

thaw.js

Lines changed: 70 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,21 @@
1-
/**
2-
* thaw an array of items
3-
* @param {Array} items
4-
* @param {Object} [options]
5-
* @constructor
6-
*/
7-
var Thaw = (function() {
1+
var Thaw = (function(window) {
82

93
//private variables
104
var thawing = false,
115
thaws = [];
126

13-
function setDefaults(options) {
14-
var defaultSettings = Constructor.defaultSettings;
15-
16-
if (options !== undefined) {
17-
for(var key in defaultSettings) {
18-
if (defaultSettings.hasOwnProperty(key)) {
19-
if (!options.hasOwnProperty(key)) {
20-
options[key] = defaultSettings[key];
21-
}
22-
}
23-
}
24-
} else {
25-
options = defaultSettings;
26-
}
27-
return options;
28-
}
29-
30-
//Constructor
31-
function Constructor(items, options) {
32-
options = setDefaults(options);
7+
/**
8+
* thaw an array of items
9+
* @param {Array} items
10+
* @param {Object} [options]
11+
* @constructor
12+
*/
13+
function Thaw(items, options) {
14+
options = options || {};
3315

3416
var timeout,
35-
each = options.each,
36-
done = options.done,
17+
each = options.each || null,
18+
done = options.done || null,
3719
self = this,
3820
tick = this.tick = function () {
3921
var items = self.items,
@@ -80,12 +62,12 @@ var Thaw = (function() {
8062
*
8163
* @type {{each: null, done: null}}
8264
*/
83-
Constructor.defaultSettings = {
65+
Thaw.defaultSettings = {
8466
each: null,
8567
done: null
8668
};
8769

88-
Constructor.prototype = {
70+
Thaw.prototype = {
8971
/**
9072
*
9173
* @param item
@@ -137,22 +119,22 @@ var Thaw = (function() {
137119
* @param {Object} item
138120
* @param {Object} [options]
139121
*/
140-
Constructor.it = function(item, options) {
122+
Thaw.it = function(item, options) {
141123
return new Constructor([item], options)
142124
};
143125

144126
/**
145127
* returns if Thaw.js is thawing
146128
* @returns {boolean}
147129
*/
148-
Constructor.isThawing = function() {
130+
Thaw.isThawing = function() {
149131
return thawing;
150132
};
151133

152134
/**
153135
* Stops all Thaw.js instances
154136
*/
155-
Constructor.stopAll = function() {
137+
Thaw.stopAll = function() {
156138
var i = 0,
157139
max = thaws.length;
158140

@@ -161,22 +143,60 @@ var Thaw = (function() {
161143
}
162144
};
163145

164-
return Constructor;
165-
})(),
166-
167-
/**
168-
* wraps the constructor Thaw
169-
* @param {Array} items
170-
* @param {Object} [options]
171-
* @returns Thaw
172-
*/
173-
thaw = (function(Thaw) {
174-
function fn(items, options) {
175-
return new Thaw(items, options);
146+
147+
/**
148+
* simple thaw
149+
* @param {Array} items
150+
* @param {Object} [options]
151+
* @returns Thaw
152+
*/
153+
function thaw(items, options) {
154+
options = options || {};
155+
156+
var timeout,
157+
i = 0,
158+
done = options.done || null,
159+
each = options.each || null;
160+
161+
function tick() {
162+
if (i < 0) return;
163+
164+
timeout = setTimeout(tick, 0);
165+
166+
if (!thawing) {
167+
if (i >= items.length) {
168+
169+
if (done !== null) {
170+
thawing = true;
171+
done.call(items[i]);
172+
thawing = false;
173+
i = -1;
174+
}
175+
176+
clearTimeout(timeout);
177+
return;
178+
}
179+
180+
if (each !== null) {
181+
thawing = true;
182+
each.call(items[i], i);
183+
thawing = false;
184+
} else {
185+
items[i]();
186+
}
187+
i++;
188+
}
189+
}
190+
191+
tick();
176192
}
177193

178-
fn.stopAll = Thaw.stopAll;
179-
fn.isThawing = Thaw.isThawing;
194+
thaw.stopAll = Thaw.stopAll;
195+
thaw.isThawing = Thaw.isThawing;
196+
197+
if (window !== null) {
198+
window.thaw = thaw;
199+
}
180200

181-
return fn;
182-
})(Thaw);
201+
return Thaw;
202+
})(typeof window !== 'undefined' ? window : null);

0 commit comments

Comments
 (0)