Skip to content

Commit

Permalink
Merge pull request #1 from redabacha/initial-implementation
Browse files Browse the repository at this point in the history
feat: initial implementation
  • Loading branch information
redabacha authored Mar 31, 2024
2 parents 1281b3b + 10649f8 commit 042b472
Show file tree
Hide file tree
Showing 13 changed files with 659 additions and 13 deletions.
10 changes: 10 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
root = true

[*]
end_of_line = lf
insert_final_newline = true

[*.{ts,json,yml}]
charset = utf-8
indent_style = space
indent_size = 2
57 changes: 57 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
name: ci
on:
pull_request:

push:
branches:
- main
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: denoland/setup-deno@v1
with:
deno-version: v1.x
- run: deno fmt --check
- run: deno lint
- run: deno doc --lint ./mod.ts
- run: deno test --coverage
- run: deno coverage --lcov > coverage.lcov
- uses: codecov/codecov-action@v4
with:
file: ./coverage.lcov
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
test-build:
runs-on: ubuntu-latest
if: github.event_name == 'pull_request'
steps:
- uses: actions/checkout@v4
- uses: denoland/setup-deno@v1
with:
deno-version: v1.x
- run: deno run -A ./scripts/prepare_release.ts
release:
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
needs: [test]
runs-on: ubuntu-latest
permissions:
contents: write
id-token: write
steps:
- uses: actions/checkout@v4
- uses: denoland/setup-deno@v1
with:
deno-version: v1.x
- uses: actions/setup-node@v4
with:
node-version: 20
# this is a hack for @semantic-release/npm verifyConditions as it expects a package.json to exist already but it's only generated during the prepare step
- run: |
mkdir npm && echo '{ "name": "parallelize-generator-promises" }' > npm/package.json
- run: npx -p @semantic-release/changelog -p @semantic-release/exec -p @semantic-release/git -p semantic-release semantic-release
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
- run: deno publish
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/npm
25 changes: 25 additions & 0 deletions .releaserc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"branches": ["main"],
"plugins": [
[
"@semantic-release/commit-analyzer",
{
"releaseRules": [
{ "type": "chore", "release": "patch" },
{ "type": "docs", "release": "patch" }
]
}
],
"@semantic-release/release-notes-generator",
"@semantic-release/changelog",
[
"@semantic-release/exec",
{
"prepareCmd": "deno run -A ./scripts/prepare_release.ts ${nextRelease.version}"
}
],
["@semantic-release/npm", { "pkgRoot": "npm" }],
["@semantic-release/git", { "assets": ["CHANGELOG.md", "deno.jsonc"] }],
"@semantic-release/github"
]
}
5 changes: 5 additions & 0 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"recommendations": [
"denoland.vscode-deno"
]
}
4 changes: 4 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"deno.enable": true,
"editor.defaultFormatter": "denoland.vscode-deno"
}
25 changes: 12 additions & 13 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
MIT License

Copyright (c) 2024 Reda
Copyright (c) 2024 Reda Bacha

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
68 changes: 68 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# parallelize-generator-promises

[![bundle size](https://pkg-size.dev/badge/bundle/0)](https://pkg-size.dev/parallelize-generator-promises)
[![codecov](https://codecov.io/gh/redabacha/parallelize-generator-promises/graph/badge.svg?token=KMRTWA2DHQ)](https://codecov.io/gh/redabacha/parallelize-generator-promises)
[![JSR score](https://jsr.io/badges/@reda/parallelize-generator-promises/score)](https://jsr.io/@reda/parallelize-generator-promises/score)
[![JSR](https://jsr.io/badges/@reda/parallelize-generator-promises)](https://jsr.io/@reda/parallelize-generator-promises)
[![npm](https://shields.io/npm/v/parallelize-generator-promises)](https://www.npmjs.com/package/parallelize-generator-promises)
[![license](https://shields.io/github/license/redabacha/parallelize-generator-promises)](https://github.com/redabacha/parallelize-generator-promises/blob/main/LICENSE)

A simple utility to run promises yielded by a generator in parallel. This is
incredibly useful when you need to run a bunch of tasks as fast as possible
without waiting for one another.

## Installation

parallelize-generator-promises is available on both
[npm](https://www.npmjs.com/package/parallelize-generator-promises) and
[JSR](https://jsr.io/@reda/parallelize-generator-promises).

To use from npm, install the
[parallelize-generator-promises](https://www.npmjs.com/package/parallelize-generator-promises)
package and then import into a module:

```js
import { parallelizeGeneratorPromises } from "parallelize-generator-promises";
```

To use from JSR, install the
[@reda/parallelize-generator-promises](https://jsr.io/@reda/parallelize-generator-promises)
package and then import into a module:

```js
import { parallelizeGeneratorPromises } from "parallelize-generator-promises";
```

## Usage

```js
import { parallelizeGeneratorPromises } from "parallelize-generator-promises";

async function* getAllProductDetails() {
for (let page = 1; page <= 100; page++) {
const productsPromise = fetch(`/products?page=${page}`).then((res) =>
res.json()
);
yield productsPromise.then((products) =>
products.map(async (product) =>
fetch(`/product/${product.id}/details`).then((res) => res.json())
)
);
}
}

for await (
const productDetails of parallelizeGeneratorPromises(
getAllProductDetails(),
)
) {
console.log(productDetails);
}
```

The above example will fetch all product details for one page of products at a
time as fast as possible, yielding the product details in the fastest order
possible to keep the iterator fed.

If a concurrency limit is required, this utility pairs very well with a
semaphore library such as [async-sema](https://github.com/vercel/async-sema).
12 changes: 12 additions & 0 deletions deno.jsonc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "@reda/parallelize-generator-promises",
"version": "1.0.0",
"exports": "./mod.ts",
"exclude": ["CHANGELOG.md", "npm"],
"imports": {
"@deno/dnt": "jsr:@deno/dnt@^0.41.0",
"@std/assert": "jsr:@std/assert@^0.220.1",
"@std/async": "jsr:@std/async@^0.221.0",
"@std/testing": "jsr:@std/testing@^0.220.1"
}
}
Loading

0 comments on commit 042b472

Please sign in to comment.