Skip to content

Commit

Permalink
feat: add method doWhileParallel
Browse files Browse the repository at this point in the history
  • Loading branch information
yisraelx committed Mar 4, 2018
1 parent dbb2519 commit 1cb37ec
Show file tree
Hide file tree
Showing 10 changed files with 302 additions and 0 deletions.
1 change: 1 addition & 0 deletions modules/-all/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export { default as compose } from '@promises/compose/add';
export { default as Promises } from '@promises/core';
export { default as create } from '@promises/create/add';
export { default as delay } from '@promises/delay/add';
export { default as doWhileParallel } from '@promises/do-while-parallel/add';
export { default as error } from '@promises/error/add';
export { default as everyParallel } from '@promises/every-parallel/add';
export { default as everySeries } from '@promises/every-series/add';
Expand Down
1 change: 1 addition & 0 deletions modules/-all/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"@promises/core": "^0.2.0",
"@promises/create": "^0.2.0",
"@promises/delay": "^0.2.0",
"@promises/do-while-parallel": "NEXT-PLACEHOLDER",
"@promises/error": "^0.2.0",
"@promises/every-parallel": "^0.2.0",
"@promises/every-series": "^0.2.0",
Expand Down
1 change: 1 addition & 0 deletions modules/-constructor/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export {default as compose} from '@promises/compose/add';
export {default as Promises} from '@promises/core';
export {default as create} from '@promises/create/add';
export {default as delay} from '@promises/delay/add';
export {default as doWhileParallel} from '@promises/do-while-parallel/add';
export {default as exec} from '@promises/exec/add';
export {default as forever} from '@promises/forever/add';
export {default as fromCallback} from '@promises/from-callback/add';
Expand Down
1 change: 1 addition & 0 deletions modules/-constructor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"@promises/core": "^0.2.0",
"@promises/create": "^0.2.0",
"@promises/delay": "^0.2.0",
"@promises/do-while-parallel": "NEXT-PLACEHOLDER",
"@promises/exec": "^0.2.0",
"@promises/forever": "NEXT-PLACEHOLDER",
"@promises/from-callback": "NEXT-PLACEHOLDER",
Expand Down
1 change: 1 addition & 0 deletions modules/-parallel/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/

export { default as Promises } from '@promises/core';
export { default as doWhileParallel } from '@promises/do-while-parallel/add';
export { default as everyParallel } from '@promises/every-parallel/add';
export { default as filterParallel } from '@promises/filter-parallel/add';
export { default as forEachParallel } from '@promises/for-each-parallel/add';
Expand Down
1 change: 1 addition & 0 deletions modules/-parallel/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
},
"dependencies": {
"@promises/core": "^0.2.0",
"@promises/do-while-parallel": "NEXT-PLACEHOLDER",
"@promises/every-parallel": "^0.2.0",
"@promises/filter-parallel": "^0.2.0",
"@promises/for-each-parallel": "^0.2.0",
Expand Down
41 changes: 41 additions & 0 deletions modules/do-while-parallel/add.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import Promises from '@promises/core';
import { IOptionalPromise } from '@promises/interfaces';
import doWhileParallel from './';

Promises._setOnConstructor('doWhileParallel', doWhileParallel);

export default doWhileParallel;

declare module '@promises/core' {
namespace Promises {
/**
* @example
*
* ```typescript
* let index: number = 0;
* Promises.whileParallel(() => {
* console.log(`test ${index}`);
* return index++ < 3;
* }, () => {
* let thisIndex = index;
* return Promises.timeout((resolve) => {
* console.log(`iteratee ${thisIndex}`);
* resolve();
* }, 4 - index);
* }).then(() => {
* console.log('completed');
* });
* // => 'test 0'
* // => 'test 1'
* // => 'test 2'
* // => 'test 3'
* // => 'iteratee 3'
* // => 'iteratee 2'
* // => 'iteratee 1'
* // => 'iteratee 0'
* // => 'completed'
* ```
*/
export function doWhileParallel(test: () => IOptionalPromise<boolean>, iteratee?: () => IOptionalPromise<any>, limit?: number): Promises<void>;
}
}
53 changes: 53 additions & 0 deletions modules/do-while-parallel/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/**
* @module @promises/do-while-parallel
* @copyright © 2017 Yisrael Eliav <yisraelx@gmail.com> (https://github.com/yisraelx)
* @license MIT
*/

import { IOptionalPromise } from '@promises/interfaces';
import exec from '@promises/exec';
import whileParallel from '@promises/while-parallel';

/**
* @example
*
* ```typescript
* let index: number = 0;
* whileParallel(() => {
* console.log(`test ${index}`);
* return index++ < 3;
* }, () => {
* let thisIndex = index;
* return timeout((resolve) => {
* console.log(`iteratee ${thisIndex}`);
* resolve();
* }, 4 - index);
* }).then(() => {
* console.log('completed');
* });
* // => 'test 0'
* // => 'test 1'
* // => 'test 2'
* // => 'test 3'
* // => 'iteratee 3'
* // => 'iteratee 2'
* // => 'iteratee 1'
* // => 'iteratee 0'
* // => 'completed'
* ```
*/
function doWhileParallel(test: () => IOptionalPromise<boolean>, iteratee: () => IOptionalPromise<any> = () => { }, limit: number = Infinity): Promise<void> {
limit--;
if (limit <= 0) {
return exec(iteratee).then(() => {
return whileParallel(test, iteratee, limit);
});
} else {
return Promise.all([
exec(iteratee),
whileParallel(test, iteratee, limit)
]).then(() => { });
}
}

export default doWhileParallel;
42 changes: 42 additions & 0 deletions modules/do-while-parallel/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"name": "@promises/do-while-parallel",
"version": "NEXT-PLACEHOLDER",
"description": "Do While Parallel is package from Promises library",
"main": "es5.js",
"browser": "umd.min.js",
"module": "index.js",
"es2015": "index.js",
"typings": "index.d.ts",
"bundle": "bundle.min.js",
"author": {
"name": "Yisrael Eliev",
"url": "https://github.com/yisraelx",
"email": "yisraelx@gmail.com"
},
"license": "MIT",
"keywords": [
"promise",
"promises",
"utility",
"modules",
"async",
"await",
"deferred"
],
"homepage": "https://github.com/yisraelx/promises#readme",
"repository": {
"type": "git",
"url": "https://github.com/yisraelx/promises.git"
},
"bugs": {
"url": "https://github.com/yisraelx/promises/issues"
},
"optionalDependencies": {
"@promises/core": "^0.2.0"
},
"dependencies": {
"@promises/exec": "^0.2.0",
"@promises/interfaces": "^0.2.0",
"@promises/while-parallel": "NEXT-PLACEHOLDER"
}
}
160 changes: 160 additions & 0 deletions test/do-while-parallel.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
import doWhileParallel from '@promises/do-while-parallel';
import timeout from '@promises/timeout';

describe('doWhileParallel', () => {

it('should be iteratee once if test not pass', () => {
let test = 0;
let iteratee = 0;
return doWhileParallel(() => {
test++;
return false;
}, () => {
iteratee++;
}).then(() => {
expect(test).toBe(1);
expect(iteratee).toBe(1);
});
});

it('should be iteratee without iteratee function', () => {
let index = 0;
return doWhileParallel(() => {
return index++ < 5;
}).then(() => {
expect(index).toBe(6);
});
});

it('should be reject on test error', () => {
return doWhileParallel(() => {
throw 'test';
}).then(() => {
throw 'resolve';
}).catch((error) => {
expect(error).toBe('test');
});
});

it('should be reject on iteratee error', () => {
return doWhileParallel(() => {
throw 'test';
}, () => {
throw 'iteratee';
}).then(() => {
throw 'resolve';
}).catch((error) => {
expect(error).toBe('iteratee');
});
});

it('should be iteratee parallel 5 times', () => {
let index = 0;
let length = 5;
let test = [];
let iteratee = [];
return doWhileParallel(() => {
test.push(index);
return index++ < length;
}, () => {
iteratee.push(index);
}).then(() => {
expect(index).toBe(6);
expect(test).toEqual([0, 1, 2, 3, 4, 5]);
expect(iteratee).toEqual([0, 1, 2, 3, 4, 5]);
});
});

it('should be iteratee parallel 6 times with promise iteratee', () => {
let index = 0;
let test = [];
let iteratee = [];
return doWhileParallel(() => {
test.push(index);
return index++ < 5;
}, () => {
let thisIndex = index;
return timeout((resolve) => {
iteratee.push(thisIndex);
resolve();
}, 6 - index);
}).then(() => {
expect(index).toBe(6);
expect(test).toEqual([0, 1, 2, 3, 4, 5]);
expect(iteratee).toEqual([5, 4, 3, 2, 1, 0]);
});
});

it('should be iteratee parallel limit 6 times with promise test', () => {
let index = 0;
let test = [];
let iteratee = [];
return doWhileParallel(() => {
return timeout((resolve) => {
test.push(index);
resolve(index++ < 5);
});
}, () => {
iteratee.push(index);
}, 6).then(() => {
expect(index).toBe(6);
expect(test).toEqual([0, 1, 2, 3, 4, 5]);
expect(iteratee).toEqual([0, 1, 2, 3, 4, 5]);
});
});

it('should be iteratee series 6 times', () => {
let index = 0;
let length = 5;
let test = [];
let iteratee = [];
return doWhileParallel(() => {
test.push(index);
return index++ < length;
}, () => {
iteratee.push(index);
}, 1).then(() => {
expect(index).toBe(6);
expect(test).toEqual([0, 1, 2, 3, 4, 5]);
expect(iteratee).toEqual([0, 1, 2, 3, 4, 5]);
});
});

it('should be iteratee series 6 times with promise iteratee', () => {
let index = 0;
let test = [];
let iteratee = [];
return doWhileParallel(() => {
test.push(index);
return index++ < 5;
}, () => {
let thisIndex = index;
return timeout((resolve) => {
iteratee.push(thisIndex);
resolve();
}, 6 - index);
}, 1).then(() => {
expect(index).toBe(6);
expect(test).toEqual([0, 1, 2, 3, 4, 5]);
expect(iteratee).toEqual([0, 1, 2, 3, 4, 5]);
});
});

it('should be iteratee series 6 times with promise test', () => {
let index = 0;
let test = [];
let iteratee = [];
return doWhileParallel(() => {
return timeout((resolve) => {
test.push(index);
resolve(index++ < 5);
});
}, () => {
iteratee.push(index);
}, 1).then(() => {
expect(index).toBe(6);
expect(test).toEqual([0, 1, 2, 3, 4, 5]);
expect(iteratee).toEqual([0, 1, 2, 3, 4, 5]);
});
});
});

0 comments on commit 1cb37ec

Please sign in to comment.