Skip to content

Commit

Permalink
deprecate: clulib.async.forEach and clulib.async.forEachRight in favo…
Browse files Browse the repository at this point in the history
…r of clulib.array.asyncForEach and clulib.array.asyncForEachRight
  • Loading branch information
b-strauss committed Jul 9, 2017
1 parent fc56665 commit 8fc8ecc
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 76 deletions.
24 changes: 24 additions & 0 deletions lib/array/array.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,28 @@ goog.provide('clulib.array');
*/
clulib.array.removeHoles = function (array) {
return array.filter(e => true);
};

/**
* @param {Array<T>} array
* @param {function(T, number, Array<T>): Promise} action
* @returns {Promise}
* @template T
*/
clulib.array.asyncForEach = function (array, action) {
return array.reduce((promise, element, index, arr) => {
return promise.then(() => action(element, index, arr));
}, Promise.resolve());
};

/**
* @param {Array<T>} array
* @param {function(T, number, Array<T>): Promise} action
* @returns {Promise}
* @template T
*/
clulib.array.asyncForEachRight = function (array, action) {
return array.reduceRight((promise, element, index, arr) => {
return promise.then(() => action(element, index, arr));
}, Promise.resolve());
};
16 changes: 6 additions & 10 deletions lib/async/async.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,21 @@
goog.provide('clulib.async');

goog.require('clulib.array');

/**
* @param {Array<T>} array
* @param {function(T, number, Array<T>): Promise} action
* @returns {Promise}
* @template T
* @deprecated Use [clulib.array.asyncForEach] instead
*/
clulib.async.forEach = function (array, action) {
return array.reduce((promise, element, index, arr) => {
return promise.then(() => action(element, index, arr));
}, Promise.resolve());
};
clulib.async.forEach = clulib.array.asyncForEach;

/**
* @param {Array<T>} array
* @param {function(T, number, Array<T>): Promise} action
* @returns {Promise}
* @template T
* @deprecated Use [clulib.array.asyncForEachRight] instead
*/
clulib.async.forEachRight = function (array, action) {
return array.reduceRight((promise, element, index, arr) => {
return promise.then(() => action(element, index, arr));
}, Promise.resolve());
};
clulib.async.forEachRight = clulib.array.asyncForEachRight;
2 changes: 1 addition & 1 deletion lib/cm/NodeTree.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ clulib.cm.NodeTree.prototype.createTree = function (rootElement) {
*/
const denseOrderedNodeGroups = clulib.array.removeHoles(sparseOrderedNodeGroups);

return clulib.async.forEachRight(denseOrderedNodeGroups, group => {
return clulib.array.asyncForEachRight(denseOrderedNodeGroups, group => {
return Promise.all(group.map(node => node.initialize()));
});
};
Expand Down
55 changes: 55 additions & 0 deletions test/array/array_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ goog.provide('clulib.array.test');

goog.require('clulib.array');

goog.require('goog.array');

clulib.array.test.main = () => {
describe('clulib.array', () => {
describe('removeHoles', () => {
Expand All @@ -12,5 +14,58 @@ clulib.array.test.main = () => {
expect(a).toEqual(b);
});
});

describe('asyncForEach', () => {
it('should execute async functions for every element in an array consecutively', done => {
const strings = ['first', 'second', 'third'];
const stringsCopy = goog.array.clone(strings);
const delays = [500, 200, 50];
let result = '';

clulib.array.asyncForEach(strings, (element, index, array) => {
expect(array).toEqual(strings);
expect(element).toEqual(array[index]);

return new Promise(resolve => {
setTimeout(() => {
result += element;
resolve();
}, delays[index]);
});
}).then(() => {
expect(result).toEqual(strings.join(''));
expect(strings).toEqual(stringsCopy);
done();
});
});
});

describe('asyncForEachRight', () => {
it('should execute async functions for every element in an array consecutively, in reverse order', done => {
const strings = ['first', 'second', 'third'];
const stringsCopy = goog.array.clone(strings);
const stringsCopyReversed = goog.array.clone(strings);
stringsCopyReversed.reverse();

const delays = [500, 200, 50];
let result = '';

clulib.array.asyncForEachRight(strings, (element, index, array) => {
expect(array).toEqual(strings);
expect(element).toEqual(array[index]);

return new Promise(resolve => {
setTimeout(() => {
result += element;
resolve();
}, delays[index]);
});
}).then(() => {
expect(result).toEqual(stringsCopyReversed.join(''));
expect(strings).toEqual(stringsCopy);
done();
});
});
});
});
};
62 changes: 0 additions & 62 deletions test/async/async_spec.js

This file was deleted.

4 changes: 1 addition & 3 deletions test_main.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
goog.provide('test_main');

goog.require('clulib.array.test');
goog.require('clulib.async.test');

clulib.array.test.main();
clulib.async.test.main();
clulib.array.test.main();

0 comments on commit 8fc8ecc

Please sign in to comment.