Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add AbortSignal options to retry and parallel #262

Open
wants to merge 18 commits into
base: main
Choose a base branch
from

Conversation

SaidbekAbdiganiev
Copy link

@SaidbekAbdiganiev SaidbekAbdiganiev commented Oct 7, 2024

Tip

The owner of this PR can publish a preview release by commenting /publish in this PR. Afterwards, anyone can try it out by running pnpm add radashi@pr<PR_NUMBER>.

Summary

This PR adds an option to pass AbortController.signal to retry and parallel to end their execution before looping over all of the array items, or going through all of the retry attempts. When aborted, the functions throw an Error with the message Operation aborted.

// _.retry
import * as _ from 'radashi'

const abortController = new AbortController()
const signal = abortController.signal

const promise = _.retry({ times: 3, delay: 1000, signal }, api.users.list)

// To abort the operation:
abortController.abort()

try {
  await promise
} catch (err) {
  if (err.message === 'Operation aborted') {
    console.log('Retry operation was aborted')
  }
}

//_.parallel
import * as _ from 'radashi'

const userIds = [1, 2, 3, 4, 5, 6, 7, 8, 9]
const abortController = new AbortController()
const signal = abortController.signal

const [err, users] = await _.tryit(_.parallel)(3, userIds, async userId => {
  return await api.users.find(userId)
}, signal)

// To abort the operation:
abortController.abort()

console.log(err) // => Error: Operation Aborted
console.log(err.message) // => "Operation Aborted"

Related issue, if any:

#255
https://github.com/orgs/radashi-org/discussions/256

For any code change,

  • Related documentation has been updated, if needed
  • Related tests have been added or updated, if needed
  • Related benchmarks have been added or updated, if needed

Does this PR introduce a breaking change?

Yes

I do not know entirely what are the implications of this change, but I added the DOM parameter to lib in tsconfig.json. According to this article, it adds the types for browser API's which in this case includes the AbortController. According to can i use, it is supported in all major browsers and has been implemented in node since v15, so it shouldn't break any existing applications using older versions of radashi.

Bundle impact

Status File Size 1 Difference
M src/async/parallel.ts 1522 +250 (+20%)
M src/async/retry.ts 535 +32 (+6%)

Footnotes

  1. Function size includes the import dependencies of the function.

src/async/retry.ts Outdated Show resolved Hide resolved
src/async/parallel.ts Show resolved Hide resolved
src/async/parallel.ts Outdated Show resolved Hide resolved
src/async/parallel.ts Outdated Show resolved Hide resolved
tsconfig.json Outdated Show resolved Hide resolved
@aleclarson aleclarson added this to the v12.3.0 milestone Oct 12, 2024
Copy link
Member

@aleclarson aleclarson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a few more changes and it should be good :)

src/async/parallel.ts Outdated Show resolved Hide resolved
src/async/parallel.ts Outdated Show resolved Hide resolved
src/async/parallel.ts Outdated Show resolved Hide resolved
src/async/parallel.ts Outdated Show resolved Hide resolved
src/async/parallel.ts Outdated Show resolved Hide resolved
src/async/parallel.ts Outdated Show resolved Hide resolved
@aleclarson
Copy link
Member

Hey! There's a new requirement for PRs that introduce new features. Without this requirement met, we won't be able to merge this. Note that this PR can still be included in a beta prerelease before this requirement is fulfilled.

⚠️ Note: You need to run git rebase main before this file will appear locally.

@SaidbekAbdiganiev
Copy link
Author

Hi @aleclarson! I’ve updated next-minor.md. When you have a moment, please check it out

@aleclarson aleclarson added the prerelease Publish to NPM under the "beta" or "next" tag label Nov 12, 2024
@aleclarson aleclarson added the new feature This PR adds a new function or extends an existing one label Nov 12, 2024
aleclarson pushed a commit that referenced this pull request Nov 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
new feature This PR adds a new function or extends an existing one prerelease Publish to NPM under the "beta" or "next" tag
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants