-
Notifications
You must be signed in to change notification settings - Fork 843
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Timer service. Don't dismiss toasts while the user is interacting…
… with them.
- Loading branch information
Showing
7 changed files
with
147 additions
and
25 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
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
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
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
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 @@ | ||
export { Timer } from './timer'; |
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,32 @@ | ||
export class Timer { | ||
constructor(callback, timeMs) { | ||
this.id = setTimeout(this.finish, timeMs); | ||
this.callback = callback; | ||
this.finishTime = Date.now() + timeMs; | ||
this.timeRemaining = undefined; | ||
} | ||
|
||
pause = () => { | ||
clearTimeout(this.id); | ||
this.id = undefined; | ||
this.timeRemaining = this.finishTime - Date.now(); | ||
}; | ||
|
||
resume = () => { | ||
this.id = setTimeout(this.finish, this.timeRemaining); | ||
this.timeRemaining = undefined; | ||
}; | ||
|
||
clear = () => { | ||
clearTimeout(this.id); | ||
this.id = undefined; | ||
this.callback = undefined; | ||
this.finishTime = undefined; | ||
this.timeRemaining = undefined; | ||
}; | ||
|
||
finish = () => { | ||
this.callback(); | ||
this.clear(); | ||
}; | ||
} |
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,68 @@ | ||
import sinon from 'sinon'; | ||
|
||
import { | ||
Timer, | ||
} from './timer'; | ||
|
||
describe('Timer', () => { | ||
describe('constructor', () => { | ||
test('counts down until time elapses and calls callback', done => { | ||
const callbackSpy = sinon.spy(); | ||
const timer = new Timer(callbackSpy, 5); // eslint-disable-line no-unused-vars | ||
|
||
setTimeout(() => { | ||
expect(callbackSpy.called).toBe(true); | ||
done(); | ||
}, 8); | ||
}); | ||
}); | ||
|
||
describe('pause', () => { | ||
test('stops timer', done => { | ||
const callbackSpy = sinon.spy(); | ||
const timer = new Timer(callbackSpy, 5); | ||
timer.pause(0); | ||
|
||
setTimeout(() => { | ||
expect(callbackSpy.called).toBe(false); | ||
done(); | ||
}, 8); | ||
}); | ||
}); | ||
|
||
describe('resume', () => { | ||
test('starts timer again', done => { | ||
const callbackSpy = sinon.spy(); | ||
const timer = new Timer(callbackSpy, 5); | ||
timer.pause(0); | ||
timer.resume(); | ||
|
||
setTimeout(() => { | ||
expect(callbackSpy.called).toBe(true); | ||
done(); | ||
}, 8); | ||
}); | ||
}); | ||
|
||
describe('clear', () => { | ||
test('prevents timer from being called', done => { | ||
const callbackSpy = sinon.spy(); | ||
const timer = new Timer(callbackSpy, 5); | ||
timer.clear(0); | ||
|
||
setTimeout(() => { | ||
expect(callbackSpy.called).toBe(false); | ||
done(); | ||
}, 8); | ||
}); | ||
}); | ||
|
||
describe('finish', () => { | ||
test('calls callback immediately', () => { | ||
const callbackSpy = sinon.spy(); | ||
const timer = new Timer(callbackSpy, 5); | ||
timer.finish(); | ||
expect(callbackSpy.called).toBe(true); | ||
}); | ||
}); | ||
}); |