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

feat(api): add .resumeTimer(), .toggleTimer(), .increaseTimer(n) #1325

Merged
merged 1 commit into from
Dec 6, 2018
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
31 changes: 28 additions & 3 deletions src/staticMethods/timer.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,42 @@
import globalState from '../globalState'

/**
* If `timer` parameter is set, returns number os milliseconds of timer remained.
* If `timer` parameter is set, returns number of milliseconds of timer remained.
* Otherwise, returns undefined.
*/
export const getTimerLeft = () => {
return globalState.timeout && globalState.timeout.getTimerLeft()
}

/**
* Stop timer manually. Returns number os milliseconds of timer remained.
* Otherwise, returns undefined.
* Stop timer. Returns number of milliseconds of timer remained.
* If `timer` parameter isn't set, returns undefined.
*/
export const stopTimer = () => {
return globalState.timeout && globalState.timeout.stop()
}

/**
* Resume timer. Returns number of milliseconds of timer remained.
* If `timer` parameter isn't set, returns undefined.
*/
export const resumeTimer = () => {
return globalState.timeout && globalState.timeout.start()
}

/**
* Resume timer. Returns number of milliseconds of timer remained.
* If `timer` parameter isn't set, returns undefined.
*/
export const toggleTimer = () => {
const timer = globalState.timeout
return timer && (timer.running ? timer.stop() : timer.start())
}

/**
* Increase timer. Returns number of milliseconds of an updated timer.
* If `timer` parameter isn't set, returns undefined.
*/
export const increaseTimer = (n) => {
return globalState.timeout && globalState.timeout.increase(n)
}
21 changes: 16 additions & 5 deletions src/utils/Timer.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,36 @@
export default class Timer {
constructor (callback, delay) {
let id, started, running
let remaining = delay
let id, started, remaining = delay
this.running = false

this.start = function () {
running = true
this.running = true
started = new Date()
id = setTimeout(callback, remaining)
return remaining
}

this.stop = function () {
running = false
this.running = false
clearTimeout(id)
remaining -= new Date() - started
return remaining
}

this.increase = function (n) {
this.stop()
remaining += n
return this.start()
}

this.getTimerLeft = function () {
if (running) {
if (this.running) {
this.stop()
this.start()
}
return remaining
}

this.start()
}
}
26 changes: 23 additions & 3 deletions sweetalert2.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,17 +226,37 @@ declare module 'sweetalert2' {
function getValidationMessage(): HTMLElement;

/**
* If `timer` parameter is set, returns number os milliseconds of timer remained.
* If `timer` parameter is set, returns number of milliseconds of timer remained.
* Otherwise, returns undefined.
*/
function getTimerLeft(): number | undefined;

/**
* Stop timer manually. Returns number os milliseconds of timer remained.
* Otherwise, returns undefined.
* Stop timer. Returns number of milliseconds of timer remained.
* If `timer` parameter isn't set, returns undefined.
*/
function stopTimer(): number | undefined;

/**
* Resume timer. Returns number of milliseconds of timer remained.
* If `timer` parameter isn't set, returns undefined.
*/
function resumeTimer(): number | undefined;

/**
* Toggle timer. Returns number of milliseconds of timer remained.
* If `timer` parameter isn't set, returns undefined.
*/
function toggleTimer(): number | undefined;

/**
* Increase timer. Returns number of milliseconds of an updated timer.
* If `timer` parameter isn't set, returns undefined.
*
* @param n The number of milliseconds to add to the currect timer
*/
function increaseTimer(n: number): number | undefined;

/**
* Provide an array of SweetAlert2 parameters to show multiple modals, one modal after another.
*
Expand Down
2 changes: 1 addition & 1 deletion test/qunit/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export const $ = document.querySelector.bind(document)
export const isVisible = (elem) => elem && (elem.offsetWidth || elem.offsetHeight || elem.getClientRects().length)
export const isHidden = (elem) => !isVisible(elem)

export let TIMEOUT = 1
export let TIMEOUT = 50

if (browser.name === 'ie') {
TIMEOUT = 100
Expand Down
21 changes: 21 additions & 0 deletions test/qunit/methods/increaseTimer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Swal, SwalWithoutAnimation, TIMEOUT } from '../helpers.js'

QUnit.test('increaseTimer() method', (assert) => {
const done = assert.async()

SwalWithoutAnimation({
timer: 5 * TIMEOUT
})

assert.ok(Swal.increaseTimer(4 * TIMEOUT) > 0)

setTimeout(() => {
assert.ok(Swal.isVisible())
}, 7 * TIMEOUT)

setTimeout(() => {
assert.notOk(Swal.isVisible())
done()
}, 10 * TIMEOUT)
})

22 changes: 22 additions & 0 deletions test/qunit/methods/resumeTimer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Swal, SwalWithoutAnimation, TIMEOUT } from '../helpers.js'

QUnit.test('resumeTimer() method', (assert) => {
const done = assert.async()

SwalWithoutAnimation({
timer: 5 * TIMEOUT
})

Swal.stopTimer()

setTimeout(() => {
assert.ok(Swal.isVisible())
Swal.resumeTimer()
}, 7 * TIMEOUT)

setTimeout(() => {
assert.notOk(Swal.isVisible())
done()
}, 15 * TIMEOUT)
})

10 changes: 5 additions & 5 deletions test/qunit/methods/stopTimer.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import { Swal } from '../helpers.js'
import { Swal, SwalWithoutAnimation, TIMEOUT } from '../helpers.js'

QUnit.test('stopTimer() method', (assert) => {
const done = assert.async()

Swal({
timer: 500
SwalWithoutAnimation({
timer: 5 * TIMEOUT
})

setTimeout(() => {
assert.ok(Swal.stopTimer() > 0)
}, 250)
}, 3 * TIMEOUT)

setTimeout(() => {
assert.ok(Swal.isVisible())
done()
}, 750)
}, 7 * TIMEOUT)
})

22 changes: 22 additions & 0 deletions test/qunit/methods/toggleTimer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Swal, SwalWithoutAnimation, TIMEOUT } from '../helpers.js'

QUnit.test('toggleTimer() method', (assert) => {
const done = assert.async()

SwalWithoutAnimation({
timer: 5 * TIMEOUT
})

Swal.toggleTimer()

setTimeout(() => {
assert.ok(Swal.isVisible())
Swal.toggleTimer()
}, 7 * TIMEOUT)

setTimeout(() => {
assert.notOk(Swal.isVisible())
done()
}, 15 * TIMEOUT)
})