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

fix: disabled breaker emits fire events #895

Merged
merged 2 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions lib/circuit.js
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,13 @@ class CircuitBreaker extends EventEmitter {

const args = rest.slice();

/**
* Emitted when the circuit breaker action is executed
* @event CircuitBreaker#fire
* @type {any} the arguments passed to the fired function
*/
this.emit('fire', args);

// Protection, caches and coalesce disabled.
if (!this[ENABLED]) {
const result = this.action.apply(context, args);
Expand All @@ -630,13 +637,6 @@ class CircuitBreaker extends EventEmitter {
// Need to create variable here to prevent extra calls if cache is disabled
let cacheKey = '';

/**
* Emitted when the circuit breaker action is executed
* @event CircuitBreaker#fire
* @type {any} the arguments passed to the fired function
*/
this.emit('fire', args);

// If cache is enabled, check if we have a cached value
if (this.options.cache) {
cacheKey = this.options.cacheGetKey.apply(this, rest);
Expand Down
13 changes: 13 additions & 0 deletions test/enable-disable-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,16 @@ test('When disabled the circuit should always be closed', t => {
.then(t.end);
});
});

test('When disabled fire event is still emitted', t => {
t.plan(1);
const breaker = new CircuitBreaker(passFail);
breaker.on('fire', () => {
t.pass('fire event');
t.end();
});

breaker.disable();
breaker.fire(1)
.finally(_ => breaker.shutdown());
});
Loading