Skip to content

Commit

Permalink
Create restricted-concurrent-actions
Browse files Browse the repository at this point in the history
  • Loading branch information
KSXGitHub committed May 4, 2018
1 parent 715476d commit b0e210d
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 0 deletions.
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 packages/typescript/restricted-concurrent-actions/index.test.ts
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 packages/typescript/restricted-concurrent-actions/index.ts
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 packages/typescript/restricted-concurrent-actions/package.json
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 packages/typescript/restricted-concurrent-actions/shrinkwrap.yaml
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

0 comments on commit b0e210d

Please sign in to comment.