Skip to content

Commit 39f7581

Browse files
Creation of new Thaw.Block concept
1 parent 337d910 commit 39f7581

File tree

4 files changed

+142
-1
lines changed

4 files changed

+142
-1
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.8",
4+
"version": "1.2.0",
55
"homepage": "https://github.com/robertleeplummerjr/thaw.js",
66
"authors": [
77
"Robert Plummer <robertleeplummerjr@gmail.com> (http://visop-dev.com)"

index.html

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,17 @@ <h2>Usage</h2>
9898
//this = item of array
9999
}
100100
});
101+
</code></pre>
102+
103+
<p><b>As a block or Thaws (can be even faster in some situations):</b></p>
104+
<pre><code>new Thaw.Block(200, {
105+
each:function(i) {
106+
//this = item of array
107+
},
108+
done: function() {
109+
//this = item of array
110+
}
111+
});
101112
</code></pre>
102113

103114
<h2>Methods</h2>

test/test.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,37 @@ tf.test('stopping, adding, going', function() {
7575
t.add(4);
7676

7777
tf.assertEquals(t.i > -1, true, 'is thawing');
78+
});
79+
80+
tf.test('use thaw block', function() {
81+
var t = new Thaw.Block({
82+
each: function() {
83+
84+
}
85+
});
86+
87+
t.addArray([0,1,2,3]);
88+
t.addArray([0,1,2,3]);
89+
t.addArray([0,1,2,3]);
90+
91+
tf.assertEquals(t.index === 3, true, 'is thawing');
92+
});
93+
94+
95+
tf.test('stop thaw block', function() {
96+
var t = new Thaw.Block({
97+
each: function() {
98+
99+
}
100+
});
101+
102+
t.addArray([0,1,2,3]);
103+
t.addArray([0,1,2,3]);
104+
t.addArray([0,1,2,3]);
105+
106+
t.stop();
107+
108+
tf.assertEquals(t.thaws[0].i === -1, true, 'is thawing');
109+
tf.assertEquals(t.thaws[1].i === -1, true, 'is thawing');
110+
tf.assertEquals(t.thaws[2].i === -1, true, 'is thawing');
78111
});

thaw.js

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,103 @@ var Thaw = (function(window) {
186186
}
187187
};
188188

189+
/**
190+
*
191+
* @param {Object} [options]
192+
* @param {Number} [count]
193+
* @constructor
194+
*/
195+
Thaw.Block = function(options, count) {
196+
this.index = 0;
197+
this.thaws = [];
198+
this.count = 200 || count;
199+
this.options = options;
200+
};
201+
202+
Thaw.Block.prototype = {
203+
204+
/**
205+
* add an item to the end of items
206+
* @param item
207+
* @returns {Thaw.Block}
208+
*/
209+
add: function(item) {
210+
this._next().add(item);
211+
212+
return this;
213+
},
214+
215+
/**
216+
* add an Array to the end of items
217+
* @param items
218+
* @returns {Thaw.Block}
219+
*/
220+
addArray: function(items) {
221+
this._next().addArray(items);
222+
return this;
223+
},
224+
225+
/**
226+
* insert an item into items @ current position
227+
* @param item
228+
* @returns {Thaw.Block}
229+
*/
230+
insert: function(item) {
231+
this._next().insert(item);
232+
return this;
233+
},
234+
235+
/**
236+
* insert and array into items @ current position
237+
* @param items
238+
* @returns {Thaw.Block}
239+
*/
240+
insertArray: function(items) {
241+
this._next().insertArray(items);
242+
return this;
243+
},
244+
245+
/**
246+
* Stops all thaws in this block
247+
* @returns {Thaw.Block}
248+
*/
249+
stop: function() {
250+
var i = 0,
251+
thaws = this.thaws,
252+
max = thaws.length;
253+
254+
for (;i < max;i++) {
255+
thaws[i].stop();
256+
}
257+
258+
return this;
259+
},
260+
261+
/**
262+
* Get next available in block
263+
* @returns {*}
264+
* @private
265+
*/
266+
_next: function() {
267+
var _thaw,
268+
i = this.index,
269+
thaws = this.thaws;
270+
271+
if (this.thaws.length < this.count) {
272+
this.thaws.push(_thaw = new Thaw([], this.options));
273+
} else {
274+
_thaw = thaws[i];
275+
}
276+
277+
this.index++;
278+
if (this.index > this.count) {
279+
this.index = 0;
280+
}
281+
282+
return _thaw;
283+
}
284+
};
285+
189286

190287
/**
191288
* simple thaw

0 commit comments

Comments
 (0)