Skip to content

Commit

Permalink
createServicePolicy: Convert callbacks to methods (#5188)
Browse files Browse the repository at this point in the history
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(() => {
  // ...
});
```
  • Loading branch information
mcmire authored Jan 23, 2025
1 parent aec3860 commit dcc0df0
Show file tree
Hide file tree
Showing 4 changed files with 373 additions and 311 deletions.
6 changes: 4 additions & 2 deletions packages/controller-utils/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

- Add `createServicePolicy` function to assist with reducing boilerplate for service classes ([#5154](https://github.com/MetaMask/core/pull/5154), [#5143](https://github.com/MetaMask/core/pull/5143), [#5149](https://github.com/MetaMask/core/pull/5149))
- Also add `DEFAULT_CIRCUIT_BREAK_DURATION`, `DEFAULT_DEGRADED_THRESHOLD`, `DEFAULT_MAX_CONSECUTIVE_FAILURES`, and `DEFAULT_MAX_RETRIES` constants and `IServicePolicy` type
- Add utility function for reducing boilerplate for service classes ([#5154](https://github.com/MetaMask/core/pull/5154), [#5143](https://github.com/MetaMask/core/pull/5143), [#5149](https://github.com/MetaMask/core/pull/5149), [#5188](https://github.com/MetaMask/core/pull/5188))
- Add function `createServicePolicy`
- Add constants `DEFAULT_CIRCUIT_BREAK_DURATION`, `DEFAULT_DEGRADED_THRESHOLD`, `DEFAULT_MAX_CONSECUTIVE_FAILURES`, and `DEFAULT_MAX_RETRIES`
- Add types `ServicePolicy` and `CreateServicePolicyOptions`

## [11.4.5]

Expand Down
Loading

0 comments on commit dcc0df0

Please sign in to comment.