-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
330 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import Promises from '@promises/core'; | ||
import { IOptionalPromise } from '@promises/interfaces'; | ||
import whileParallel from './'; | ||
|
||
Promises._setOnConstructor('whileParallel', whileParallel); | ||
|
||
export default whileParallel; | ||
|
||
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' | ||
* // => 'completed' | ||
* ``` | ||
*/ | ||
export function whileParallel(test: () => IOptionalPromise<boolean>, iteratee?: () => IOptionalPromise<any>, limit?: number): Promises<void>; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/** | ||
* @module @promises/while-parallel | ||
* @copyright © 2017 Yisrael Eliav <[email protected]> (https://github.com/yisraelx) | ||
* @license MIT | ||
*/ | ||
|
||
import { IOptionalPromise } from '@promises/interfaces'; | ||
import exec from '@promises/exec'; | ||
|
||
/** | ||
* @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' | ||
* // => 'completed' | ||
* ``` | ||
*/ | ||
function whileParallel(test: () => IOptionalPromise<boolean>, iteratee: () => IOptionalPromise<any> = () => { }, limit: number = Infinity): Promise<void> { | ||
limit--; | ||
return new Promise((resolve, reject) => { | ||
let count = 0; | ||
let completed = 0; | ||
let isStop = false; | ||
let onReject = (error: any) => { | ||
isStop = true; | ||
reject(error); | ||
}; | ||
let each = () => { | ||
if (isStop) { | ||
if (count === completed) { | ||
resolve(); | ||
} | ||
} else { | ||
exec(test).then((isPass: boolean) => { | ||
if (isPass && !isStop) { | ||
count++; | ||
exec(iteratee).then(() => { | ||
completed++; | ||
if (limit <= 0 || isStop) { | ||
each(); | ||
} | ||
}).catch(onReject); | ||
if (limit > 0) { | ||
limit--; | ||
each(); | ||
} | ||
} else { | ||
isStop = true; | ||
each(); | ||
} | ||
}).catch(onReject); | ||
} | ||
}; | ||
|
||
each(); | ||
}); | ||
} | ||
|
||
export default whileParallel; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
{ | ||
"name": "@promises/while-parallel", | ||
"version": "NEXT-PLACEHOLDER", | ||
"description": "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": "[email protected]" | ||
}, | ||
"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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
import whileParallel from '@promises/while-parallel'; | ||
import timeout from '@promises/timeout'; | ||
|
||
describe('whileParallel', () => { | ||
|
||
it('should be not iteratee if test not pass', () => { | ||
let test = 0; | ||
let iteratee = 0; | ||
return whileParallel(() => { | ||
test++; | ||
return false; | ||
}, () => { | ||
iteratee++; | ||
}).then(() => { | ||
expect(test).toBe(1); | ||
expect(iteratee).toBe(0); | ||
}); | ||
}); | ||
|
||
it('should be iteratee without iteratee function', () => { | ||
let index = 0; | ||
return whileParallel(() => { | ||
return index++ < 5; | ||
}).then(() => { | ||
expect(index).toBe(6); | ||
}); | ||
}); | ||
|
||
it('should be reject on test error', () => { | ||
return whileParallel(() => { | ||
throw 'test'; | ||
}, () => { | ||
throw 'iteratee'; | ||
}).then(() => { | ||
throw 'resolve'; | ||
}).catch((error) => { | ||
expect(error).toBe('test'); | ||
}); | ||
}); | ||
|
||
it('should be reject on iteratee error', () => { | ||
let stop = false; | ||
return whileParallel(() => { | ||
return stop = !stop; | ||
}, () => { | ||
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 whileParallel(() => { | ||
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([1, 2, 3, 4, 5]); | ||
}); | ||
}); | ||
|
||
it('should be iteratee parallel 5 times with promise iteratee', () => { | ||
let index = 0; | ||
let test = []; | ||
let iteratee = []; | ||
return whileParallel(() => { | ||
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]); | ||
}); | ||
}); | ||
|
||
it('should be iteratee parallel limit 5 times with promise test', () => { | ||
let index = 0; | ||
let test = []; | ||
let iteratee = []; | ||
return whileParallel(() => { | ||
return timeout((resolve) => { | ||
test.push(index); | ||
resolve(index++ < 5); | ||
}); | ||
}, () => { | ||
iteratee.push(index); | ||
}, 5).then(() => { | ||
expect(index).toBe(6); | ||
expect(test).toEqual([0, 1, 2, 3, 4, 5]); | ||
expect(iteratee).toEqual([1, 2, 3, 4, 5]); | ||
}); | ||
}); | ||
|
||
it('should be iteratee series 5 times', () => { | ||
let count = 0; | ||
let length = 5; | ||
let test = []; | ||
let iteratee = []; | ||
return whileParallel(() => { | ||
test.push(count); | ||
return count++ < length; | ||
}, () => { | ||
iteratee.push(count); | ||
}, 1).then(() => { | ||
expect(count).toBe(6); | ||
expect(test).toEqual([0, 1, 2, 3, 4, 5]); | ||
expect(iteratee).toEqual([1, 2, 3, 4, 5]); | ||
}); | ||
}); | ||
|
||
it('should be iteratee series 5 times with promise iteratee', () => { | ||
let count = 0; | ||
let length = 5; | ||
let test = []; | ||
let iteratee = []; | ||
return whileParallel(() => { | ||
test.push(count); | ||
return count++ < length; | ||
}, () => { | ||
return timeout((resolve) => { | ||
iteratee.push(count); | ||
resolve(); | ||
}, length - count); | ||
}, 1).then(() => { | ||
expect(count).toBe(6); | ||
expect(test).toEqual([0, 1, 2, 3, 4, 5]); | ||
expect(iteratee).toEqual([1, 2, 3, 4, 5]); | ||
}); | ||
}); | ||
|
||
it('should be iteratee series 5 times with promise test', () => { | ||
let count = 0; | ||
let length = 5; | ||
let test = []; | ||
let iteratee = []; | ||
return whileParallel(() => { | ||
return timeout((resolve) => { | ||
test.push(count); | ||
resolve(count++ < length); | ||
}); | ||
}, () => { | ||
iteratee.push(count); | ||
}, 1).then(() => { | ||
expect(count).toBe(6); | ||
expect(test).toEqual([0, 1, 2, 3, 4, 5]); | ||
expect(iteratee).toEqual([1, 2, 3, 4, 5]); | ||
}); | ||
}); | ||
}); |