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

createServicePolicy: Convert callbacks to methods #5188

Merged
merged 1 commit into from
Jan 23, 2025

Conversation

mcmire
Copy link
Contributor

@mcmire mcmire commented Jan 23, 2025

Explanation

Both RetryPolicy and CircuitBreakerPolicy from the Cockatiel library allow for listening for events using on* methods. For instance, if you have a retry policy, you can listen for when the function is retried like so:

retryPolicy.onRetry(() => {
  // ...
})

Our "service policy" allows for listening to some of the same events as well as the "degraded" event. Instead of exposing on* methods, however, createServicePolicy takes callbacks in an options bag:

createServicePolicy({
  // ...
  onRetry: () => {
    // ...
  }
)

For parity with Cockatiel — and for easier use in general — this commit changes the API so that the object that createServicePolicy returns now includes similar on* methods. This way you can say:

const policy = createServicePolicy({ ... });
policy.onRetry(() => {
  // ...
});

References

Progresses #4994.
Pre-requisite to #4990.

Changelog

(Updated in PR.)

Checklist

  • I've updated the test suite for new or updated code as appropriate
  • I've updated documentation (JSDoc, Markdown, etc.) for new or updated code as appropriate
  • I've highlighted breaking changes using the "BREAKING" category above as appropriate
  • I've prepared draft pull requests for clients and consumer packages to resolve any breaking changes

@mcmire mcmire force-pushed the convert-callbacks-to-methods branch from f743806 to c576f76 Compare January 23, 2025 00:11
Both RetryPolicy and CircuitBreakerPolicy from the Cockatiel library
allow for listening for events using `on*` methods. Our "service policy"
allows for listening to some of the same events as well as the
"degraded" event. For parity with Cockatiel — and for easier use in
general — this commit removes the callbacks that `createServicePolicy`
takes and replaces them with `on*` methods.
@mcmire mcmire force-pushed the convert-callbacks-to-methods branch from c576f76 to a811bf6 Compare January 23, 2025 00:22
@mcmire mcmire marked this pull request as ready for review January 23, 2025 00:29
@mcmire mcmire requested a review from a team as a code owner January 23, 2025 00:29
Copy link
Contributor

@cryptodev-2s cryptodev-2s left a comment

Choose a reason for hiding this comment

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

Great improvements

@mcmire mcmire merged commit dcc0df0 into main Jan 23, 2025
126 checks passed
@mcmire mcmire deleted the convert-callbacks-to-methods branch January 23, 2025 23:00
zone-live pushed a commit that referenced this pull request Jan 27, 2025
Both RetryPolicy and CircuitBreakerPolicy from the Cockatiel library
allow for listening for events using `on*` methods. For instance, if you
have a retry policy, you can listen for when the function is retried
like so:

``` typescript
retryPolicy.onRetry(() => {
  // ...
})
```

Our "service policy" allows for listening to some of the same events as
well as the "degraded" event. Instead of exposing `on*` methods,
however, `createServicePolicy` takes callbacks in an options bag:

``` typescript
createServicePolicy({
  // ...
  onRetry: () => {
    // ...
  }
)
```

For parity with Cockatiel — and for easier use in general — this commit
changes the API so that the object that `createServicePolicy` returns
now includes similar `on*` methods. This way you can say:

``` typescript
const policy = createServicePolicy({ ... });
policy.onRetry(() => {
  // ...
});
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants