-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create restricted-concurrent-actions
- Loading branch information
Showing
5 changed files
with
137 additions
and
0 deletions.
There are no files selected for viewing
52 changes: 52 additions & 0 deletions
52
packages/typescript/restricted-concurrent-actions/__snapshots__/index.test.ts.snap
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,52 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`matches snapshot 1`] = ` | ||
Array [ | ||
Array [ | ||
0, | ||
1, | ||
2, | ||
3, | ||
4, | ||
], | ||
Array [ | ||
5, | ||
6, | ||
7, | ||
8, | ||
9, | ||
], | ||
Array [ | ||
10, | ||
11, | ||
12, | ||
13, | ||
14, | ||
], | ||
Array [ | ||
15, | ||
16, | ||
17, | ||
18, | ||
19, | ||
], | ||
Array [ | ||
20, | ||
21, | ||
22, | ||
23, | ||
24, | ||
], | ||
Array [ | ||
25, | ||
26, | ||
27, | ||
28, | ||
29, | ||
], | ||
Array [ | ||
30, | ||
31, | ||
], | ||
] | ||
`; |
11 changes: 11 additions & 0 deletions
11
packages/typescript/restricted-concurrent-actions/index.test.ts
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,11 @@ | ||
import * as assets from 'monorepo-shared-assets' | ||
import subject from './index' | ||
|
||
it('matches snapshot', async () => { | ||
const actions = Array | ||
.from(assets.iter.fns.range(32)) | ||
.map(x => async () => x) | ||
|
||
const array = await subject.asArray(actions, 5) | ||
expect(array).toMatchSnapshot() | ||
}) |
42 changes: 42 additions & 0 deletions
42
packages/typescript/restricted-concurrent-actions/index.ts
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,42 @@ | ||
import * as assets from 'monorepo-shared-assets' | ||
import urge = assets.iter.fns.urge | ||
|
||
export async function * restrictedConcurrentActions<Y> ( | ||
actions: restrictedConcurrentActions.ActionList<Y>, | ||
partLength: restrictedConcurrentActions.PartLength, | ||
handleRemain?: restrictedConcurrentActions.RemainingHandler<Y> | ||
): restrictedConcurrentActions.ReturningPromise<Y> { | ||
for (const segment of urge(actions, partLength, handleRemain)) { | ||
yield await Promise.all(segment.map(fn => fn())) | ||
} | ||
} | ||
|
||
export namespace restrictedConcurrentActions { | ||
export type Action<Y> = () => Promise<Y> | ||
export type ActionList<Y> = Iterable<Action<Y>> | ||
export type PartLength = number | ||
export type RemainingHandler<Y> = urge.RemainingHandler<Action<Y>> | ||
export type ReturningValue<Y> = ReadonlyArray<Y> | ||
export type ReturningPromise<Y> = AsyncIterableIterator<ReturningValue<Y>> | ||
|
||
export async function asArray<Y> ( | ||
actions: ActionList<Y>, | ||
partLength: PartLength, | ||
handleRemain?: RemainingHandler<Y> | ||
): asArray.ReturningPromise<Y> { | ||
let array = Array<ReadonlyArray<Y>>() | ||
|
||
for await (const part of restrictedConcurrentActions(actions, partLength, handleRemain)) { | ||
array.push(part) | ||
} | ||
|
||
return array | ||
} | ||
|
||
export namespace asArray { | ||
export type ReturningValue<Y> = ReadonlyArray<restrictedConcurrentActions.ReturningValue<Y>> | ||
export type ReturningPromise<Y> = Promise<ReturningValue<Y>> | ||
} | ||
} | ||
|
||
export default restrictedConcurrentActions |
18 changes: 18 additions & 0 deletions
18
packages/typescript/restricted-concurrent-actions/package.json
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,18 @@ | ||
{ | ||
"name": "restricted-concurrent-actions", | ||
"version": "0.0.0", | ||
"author": "Hoàng Văn Khải <hvksmr1996@gmail.com>", | ||
"license": "MIT", | ||
"homepage": "https://github.com/ksxnodemodules/nodemonorepo#readme", | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/ksxnodemodules/nodemonorepo.git" | ||
}, | ||
"bugs": { | ||
"url": "https://github.com/ksxnodemodules/nodemonorepo/issues" | ||
}, | ||
"dependencies": { | ||
"monorepo-shared-assets": "^0.0.5", | ||
"@types/node": "^10.0.3" | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
packages/typescript/restricted-concurrent-actions/shrinkwrap.yaml
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,14 @@ | ||
dependencies: | ||
'@types/node': 10.0.3 | ||
monorepo-shared-assets: 'link:../monorepo-shared-assets' | ||
packages: | ||
/@types/node/10.0.3: | ||
dev: false | ||
resolution: | ||
integrity: sha512-J7nx6JzxmtT4zyvfLipYL7jNaxvlCWpyG7JhhCQ4fQyG+AGfovAHoYR55TFx+X8akfkUJYpt5JG6GPeFMjZaCQ== | ||
registry: 'https://registry.npmjs.org/' | ||
shrinkwrapMinorVersion: 5 | ||
shrinkwrapVersion: 3 | ||
specifiers: | ||
'@types/node': ^10.0.3 | ||
monorepo-shared-assets: ^0.0.5 |