Skip to content

Commit

Permalink
Add in timer validation.
Browse files Browse the repository at this point in the history
  • Loading branch information
ealmansi committed Apr 24, 2021
1 parent 1acf053 commit cb74cff
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
3 changes: 3 additions & 0 deletions src/clear.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
* For a copy, see the file LICENSE in the root directory.
*/

import { validateTimer } from './validation'

/**
* Stops an execution cycle started by setIntervalAsync.<br>
* Any ongoing function executions will run until completion,
Expand All @@ -14,6 +16,7 @@
* A promise which resolves when all pending executions have finished.
*/
export async function clearIntervalAsync (timer) {
validateTimer(timer)
timer.stopped = true
for (const iterationId in timer.timeouts) {
clearTimeout(timer.timeouts[iterationId])
Expand Down
8 changes: 5 additions & 3 deletions src/validation.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
/**
* Copyright (c) 2019 Emilio Almansi. All rights reserved.
* Copyright (c) 2019-2021 Emilio Almansi. All rights reserved.
* This work is licensed under the terms of the MIT license.
* For a copy, see the file LICENSE in the root directory.
*/

import SetIntervalAsyncError from './error'
import SetIntervalAsyncTimer from './timer'

const MIN_INTERVAL_MS = 10

Expand Down Expand Up @@ -47,7 +46,10 @@ export function validateInterval (interval) {
*/
export function validateTimer (timer) {
if (!(
timer instanceof SetIntervalAsyncTimer
timer &&
'stopped' in timer &&
'timeouts' in timer &&
'promises' in timer
)) {
throw new SetIntervalAsyncError(
'Invalid argument: "timer". Expected an intsance of SetIntervalAsyncTimer.'
Expand Down

0 comments on commit cb74cff

Please sign in to comment.