update non-blocking foreach/map
This commit is contained in:
parent
7138a8eaa0
commit
c3c79e5f0a
1 changed files with 42 additions and 21 deletions
|
@ -1,3 +1,5 @@
|
||||||
|
'use strict';
|
||||||
|
|
||||||
(function() {
|
(function() {
|
||||||
|
|
||||||
function asyncMap(forEach, col, iterator, that, callback) {
|
function asyncMap(forEach, col, iterator, that, callback) {
|
||||||
|
@ -32,7 +34,7 @@
|
||||||
(col, iterator[, that], callback[, ms]) -> <u> undefined
|
(col, iterator[, that], callback[, ms]) -> <u> undefined
|
||||||
@*/
|
@*/
|
||||||
Ox.nonblockingForEach = function(col, iterator, that, callback, ms) {
|
Ox.nonblockingForEach = function(col, iterator, that, callback, ms) {
|
||||||
var i, keys, last = Ox.last(arguments), n, time, type = Ox.typeOf(col);
|
var i = 0, keys, last = Ox.last(arguments), n, time, type = Ox.typeOf(col);
|
||||||
callback = Ox.isFunction(last) ? last : arguments[arguments.length - 2];
|
callback = Ox.isFunction(last) ? last : arguments[arguments.length - 2];
|
||||||
col = type == 'array' || type == 'object' ? col : Ox.toArray(col);
|
col = type == 'array' || type == 'object' ? col : Ox.toArray(col);
|
||||||
keys = type == 'object' ? Object.keys(col) : Ox.range(col.length);
|
keys = type == 'object' ? Object.keys(col) : Ox.range(col.length);
|
||||||
|
@ -44,16 +46,25 @@
|
||||||
time = +new Date();
|
time = +new Date();
|
||||||
iterate();
|
iterate();
|
||||||
function iterate() {
|
function iterate() {
|
||||||
keys[i] in col && iterator.call(that, col[keys[i]], keys[i], col);
|
Ox.forEach(keys.slice(i), function(key) {
|
||||||
if (++i < n) {
|
if (key in col) {
|
||||||
if (+new Date() < time + ms) {
|
try {
|
||||||
iterate();
|
iterator.call(that, col[key], key, col);
|
||||||
} else {
|
} catch(e) {
|
||||||
|
if (e === Ox.BreakError) {
|
||||||
|
i = n;
|
||||||
|
}
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
i++;
|
||||||
|
+new Date() >= time + ms && Ox.break();
|
||||||
|
});
|
||||||
|
if (i < n) {
|
||||||
setTimeout(function() {
|
setTimeout(function() {
|
||||||
time = +new Date();
|
time = +new Date();
|
||||||
iterate();
|
iterate();
|
||||||
});
|
}, 1);
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
callback();
|
callback();
|
||||||
}
|
}
|
||||||
|
@ -62,28 +73,38 @@
|
||||||
|
|
||||||
/*@
|
/*@
|
||||||
Ox.nonblockingMap <f> Non-blocking map with synchronous iterator
|
Ox.nonblockingMap <f> Non-blocking map with synchronous iterator
|
||||||
(col, iterator[, that], callback) -> <u> undefined
|
(col, iterator[, that]) -> <u> undefined
|
||||||
col <a|o|s> Collection
|
col <a|o|s> Collection
|
||||||
iterator <f> Iterator function
|
iterator <f> Iterator function
|
||||||
that <o> The iterator's this binding
|
that <o> The iterator's this binding
|
||||||
callback <f> Callback function
|
callback <f> Callback function
|
||||||
<script>
|
<script>
|
||||||
var time = +new Date();
|
var time = +new Date();
|
||||||
Ox.serialMap(
|
Ox.nonblockingMap(
|
||||||
Ox.range(10),
|
Ox.range(1000000),
|
||||||
function (value, index, array, callback) {
|
function (value, index, array) {
|
||||||
setTimeout(function() {
|
return +new Date() - time;
|
||||||
callback(+new Date() - time);
|
|
||||||
}, Ox.random(1000));
|
|
||||||
},
|
},
|
||||||
function(results) {
|
function(results) {
|
||||||
Ox.print(results);
|
Ox.print(results.length);
|
||||||
}
|
},
|
||||||
|
1000
|
||||||
);
|
);
|
||||||
</script>
|
</script>
|
||||||
@*/
|
@*/
|
||||||
Ox.nonblockingMap = function() {
|
Ox.nonblockingMap = function(col, iterator, that, callback, ms) {
|
||||||
asyncMap.apply(null, [Ox.nonblockingForEach].concat(Ox.toArray(arguments)));
|
var last = Ox.last(arguments),
|
||||||
|
type = Ox.typeOf(col),
|
||||||
|
results = type == 'object' ? {} : [];
|
||||||
|
callback = Ox.isFunction(last) ? last : arguments[arguments.length - 2];
|
||||||
|
that = arguments.length == 5 || (
|
||||||
|
arguments.length == 4 && Ox.isFunction(last)
|
||||||
|
) ? that : null;
|
||||||
|
Ox.nonblockingForEach(col, function(val, key, obj) {
|
||||||
|
results[key] = iterator.call(that, val, key, obj);
|
||||||
|
}, function() {
|
||||||
|
callback(type == 'string' ? results.join('') : results);
|
||||||
|
}, ms);
|
||||||
};
|
};
|
||||||
|
|
||||||
/*@
|
/*@
|
||||||
|
|
Loading…
Reference in a new issue