Skip to content

Commit 1ff637b

Browse files
author
AJ ONeal
committed
switched to forEachAsync, fixed empty-array and sync bugs
1 parent 914d144 commit 1ff637b

File tree

6 files changed

+49
-13
lines changed

6 files changed

+49
-13
lines changed

README.md

+1-3
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,4 @@ API
8585
TODO
8686
---
8787

88-
Replace `forAllAsync` with `forEachAsync`
89-
90-
Make work with synchronous callbacks as well
88+
The code is a little hairy and could use some cleaning.

bower.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "lateral",
33
"main": "lateral.js",
4-
"version": "3.0.1",
4+
"version": "3.0.2",
55
"homepage": "https://github.com/FuturesJS/lateral",
66
"authors": [
77
"AJ ONeal <[email protected]>"
@@ -21,7 +21,7 @@
2121
"browser"
2222
],
2323
"dependencies": {
24-
"forallasync": "~ 1.0"
24+
"forEachAsync": "~ 3.0"
2525
},
2626
"license": "Apache-v2",
2727
"ignore": [

lateral.js

+20-6
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
, curThread = 0
88
, nThreads = _nThreads || 4
99
, running = 0
10-
//, forEachAsync = exports.forEachAsync || require('forEachAsync').forEachAsync
11-
, forAllAsync = exports.forAllAsync || require('forallasync').forAllAsync
10+
, forEachAsync = exports.forEachAsync || require('forEachAsync').forEachAsync
1211
, tasks = []
1312
, callbacks = []
13+
, completedAll = true
1414
;
1515

1616
function startOne() {
@@ -33,7 +33,8 @@
3333

3434
function onNext() {
3535
if (!threads.length) {
36-
if (0 === running) {
36+
if (0 === running && !completedAll) {
37+
completedAll = true;
3738
callbacks.forEach(function (cb) {
3839
cb();
3940
});
@@ -43,7 +44,13 @@
4344

4445
if (running < nThreads) {
4546
curThread = (curThread + 1) % threads.length;
46-
threads[curThread].next();
47+
if (threads[curThread].next) {
48+
threads[curThread].nowNext = threads[curThread].next;
49+
threads[curThread].next = null;
50+
threads[curThread].nowNext();
51+
} else {
52+
curThread = Math.max(0, (curThread + (threads.length - 1))) % threads.length;
53+
}
4754
}
4855
}
4956

@@ -98,11 +105,18 @@
98105

99106
return {
100107
add: function (arr) {
108+
if (0 === arr.length) {
109+
return {
110+
then: function (fn) {
111+
fn();
112+
}
113+
};
114+
}
115+
completedAll = false;
101116
var t = newThread(arr.length)
102117
;
103118

104-
//forEachAsync(arr, t.each);
105-
forAllAsync(arr, t.each, 1);
119+
forEachAsync(arr, t.each);
106120

107121
return {
108122
then: function (fn) {

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "lateral",
3-
"version": "3.0.1",
3+
"version": "3.0.2",
44
"description": "Like forEachAsync and Join had a baby: sequences out n batches of async functions rather than all at once. Part of FuturesJS (Browser, Node.js, Bower, Pakmanager)",
55
"homepage": "https://github.com/FuturesJS/lateral",
66
"keywords": [
@@ -30,7 +30,7 @@
3030
"lib": "."
3131
},
3232
"dependencies": {
33-
"forallasync": "~ 1.0"
33+
"forEachAsync": "~ 3.0"
3434
},
3535
"engines": {
3636
"node": "*",

test-sync.js

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
(function () {
2+
"use strict";
3+
4+
var Lateral = require('./lateral').Lateral
5+
, lateralSync
6+
;
7+
8+
lateralSync = Lateral.create(function (complete, item, i) {
9+
console.log(item, i);
10+
complete();
11+
}, 4);
12+
lateralSync.add([]).then(function () {
13+
console.log('empty sync batch complete');
14+
});
15+
lateralSync.add('abc'.split('')).then(function () {
16+
console.log('sync batch complete');
17+
});
18+
lateralSync.then(function () {
19+
console.log('all sync batches complete');
20+
});
21+
}());

test.js

+3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
}, timeout);
1414
}, 4);
1515

16+
lateral.add([]).then(function () {
17+
console.log('empty batch complete');
18+
});
1619
lateral.add('abcdefghijkl'.split('')).then(function () {
1720
console.log('batch 1 complete');
1821
});

0 commit comments

Comments
 (0)