-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
102 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import * as logger from 'src/services/logger'; | ||
|
||
export class Counter { | ||
_counter = 0; | ||
|
||
constructor({ onEnable, onDisable }) { | ||
assert(onEnable); | ||
this.onEnable = onEnable; | ||
this.onDisable = onDisable; | ||
} | ||
|
||
enable = () => { | ||
assert(counter >= 0); | ||
if (counter <= 0) { | ||
counter = 1; | ||
|
||
const res = this.onEnable(); | ||
if (res) { | ||
if (this.onDisable) { | ||
logger.warn(new Error('both `onEnable` return value and `onDisable` callback were provided, giving priority to latter')); | ||
} else { | ||
this.onDisable = res; | ||
} | ||
} else { | ||
if (this.onDisable) { | ||
// nothing to do | ||
} else { | ||
logger.warn(new Error('both `onEnable` return value and `onDisable` callback were NOT provided')); | ||
} | ||
} | ||
} else { | ||
counter += 1; | ||
} | ||
|
||
return this.disable; | ||
} | ||
|
||
disable = () => { | ||
assert(counter > 0); | ||
if (counter > 1) { | ||
counter -= 1; | ||
} else if (counter < 1) { | ||
logger.warn(new Error('trying to disable already disabled counter')); | ||
counter = 0; | ||
} else { | ||
counter = 0; | ||
|
||
if (this.onDisable) { | ||
this.onDisable(); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { Counter } from 'src/counter'; | ||
import _ from 'lodash'; | ||
|
||
describe("counter tests", function() { | ||
it("should enable and disable", function() { | ||
onEnable = jasmine.createSpy('onEnable'); | ||
onDisable = jasmine.createSpy('onDisable'); | ||
const counter = new Counter({ onEnable, onDisable }); | ||
|
||
counter.enable(); | ||
expect(onEnable).toHaveBeenCalledTimes(1); | ||
expect(onDisable).not.toHaveBeenCalled(); | ||
counter.disable(); | ||
expect(onDisable).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it("should enable several times", function() { | ||
onEnable = jasmine.createSpy('onEnable'); | ||
onDisable = jasmine.createSpy('onDisable'); | ||
const counter = new Counter({ onEnable, onDisable }); | ||
|
||
counter.enable(); | ||
counter.enable(); | ||
expect(onEnable).toHaveBeenCalledTimes(1); | ||
counter.disable(); | ||
expect(onDisable).not.toHaveBeenCalled(); | ||
counter.disable(); | ||
expect(onDisable).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it("should chain enable and disable", function() { | ||
onEnable = jasmine.createSpy('onEnable'); | ||
onDisable = jasmine.createSpy('onDisable'); | ||
const counter = new Counter({ onEnable, onDisable }); | ||
|
||
counter.enable(); | ||
counter.enable(); | ||
expect(onEnable).toHaveBeenCalledTimes(1); | ||
counter.disable(); | ||
expect(onDisable).not.toHaveBeenCalled(); | ||
counter.disable(); | ||
expect(onDisable).toHaveBeenCalledTimes(1); | ||
|
||
counter.enable(); | ||
expect(onEnable).toHaveBeenCalledTimes(2); | ||
counter.disable(); | ||
expect(onDisable).toHaveBeenCalledTimes(2); | ||
}); | ||
}); |