How to use worker-timers - 6 common examples

To help you get started, we’ve selected a few worker-timers examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github Nike-Inc / cerberus / dashboard / app / actions / authenticationActions.js View on Github external
tokenExpiresDate.setTime(cerberusAuthTokenExpiresDateInMilliseconds)

    log.debug(`Setting session timeout to ${tokenExpiresDate}`)

    let timeToExpireTokenInMillis = tokenExpiresDate.getTime() - now.getTime()

    let sessionExpirationCheckIntervalInMillis = 2000
    let sessionExpirationCheckIntervalId = workerTimers.setInterval(() => {
        let currentTimeInMillis = new Date().getTime()
        let sessionExpirationTimeInMillis = tokenExpiresDate.getTime()
        if (currentTimeInMillis >= sessionExpirationTimeInMillis) {
            dispatch(handleSessionExpiration())
        }
    }, sessionExpirationCheckIntervalInMillis)

    let sessionWarningTimeoutId = workerTimers.setTimeout(() => {
        dispatch(warnSessionExpiresSoon(token))
    }, timeToExpireTokenInMillis - 120000)  // warn two minutes before expiration

    dispatch(setSessionWarningTimeoutId(sessionWarningTimeoutId))

    sessionStorage.setItem('token', JSON.stringify(response.data))
    sessionStorage.setItem('tokenExpiresDate', tokenExpiresDate)
    sessionStorage.setItem('userRespondedToSessionWarning', false)
    dispatch(messengerActions.clearAllMessages())
    dispatch(loginUserSuccess(response.data, sessionExpirationCheckIntervalId))
    dispatch(appActions.fetchSideBarData(token))
    if (redirectToWelcome) {
        hashHistory.push("/")
    }
}
github Nike-Inc / cerberus / dashboard / app / actions / authenticationActions.js View on Github external
const millisecondsPerSecond = 1000
    const bestGuessOfRequestLatencyInMilliseconds = 120 * millisecondsPerSecond // take 2 minutes off of duration to account for latency

    let now = new Date()
    let cerberusAuthTokenExpiresDateInMilliseconds = (now.getTime() + ((leaseDurationInSeconds * millisecondsPerSecond) - bestGuessOfRequestLatencyInMilliseconds))

    let tokenExpiresDate = new Date()
    let token = response.data.data.client_token.client_token
    tokenExpiresDate.setTime(cerberusAuthTokenExpiresDateInMilliseconds)

    log.debug(`Setting session timeout to ${tokenExpiresDate}`)

    let timeToExpireTokenInMillis = tokenExpiresDate.getTime() - now.getTime()

    let sessionExpirationCheckIntervalInMillis = 2000
    let sessionExpirationCheckIntervalId = workerTimers.setInterval(() => {
        let currentTimeInMillis = new Date().getTime()
        let sessionExpirationTimeInMillis = tokenExpiresDate.getTime()
        if (currentTimeInMillis >= sessionExpirationTimeInMillis) {
            dispatch(handleSessionExpiration())
        }
    }, sessionExpirationCheckIntervalInMillis)

    let sessionWarningTimeoutId = workerTimers.setTimeout(() => {
        dispatch(warnSessionExpiresSoon(token))
    }, timeToExpireTokenInMillis - 120000)  // warn two minutes before expiration

    dispatch(setSessionWarningTimeoutId(sessionWarningTimeoutId))

    sessionStorage.setItem('token', JSON.stringify(response.data))
    sessionStorage.setItem('tokenExpiresDate', tokenExpiresDate)
    sessionStorage.setItem('userRespondedToSessionWarning', false)
github Nike-Inc / cerberus / dashboard / app / actions / authenticationActions.js View on Github external
return function(dispatch, getState) {
        let sessionExpirationCheckIntervalId = getState().auth.sessionExpirationCheckIntervalId
        log.debug(`Removing session expiration check interval, id: ${sessionExpirationCheckIntervalId}`)
        try {
            workerTimers.clearInterval(sessionExpirationCheckIntervalId)
        } catch(err) {
            console.log(`Failed to clear auth token timeout, id=${sessionExpirationCheckIntervalId}`)
            console.log(err)
        }
        dispatch(removeSessionExpirationCheck())
    }
}
github Nike-Inc / cerberus / dashboard / app / actions / authenticationActions.js View on Github external
return function(dispatch, getState) {
        let sessionWarningTimeoutId = getState().auth.sessionWarningTimeoutId
        log.debug(`Removing warning timeout, id: ${sessionWarningTimeoutId}`)
        try {
            workerTimers.clearTimeout(getState().auth.sessionWarningTimeoutId)
        } catch(err) {
            console.log(`Failed to clear session warning timeout, id=${sessionWarningTimeoutId}`)
            console.log(err)
        }
        dispatch(removeSessionWarningTimeoutId())
    }
}
github Nike-Inc / cerberus / dashboard / app / actions / authenticationActions.js View on Github external
return function(dispatch) {
        let userHasRespondedToSessionWarning = sessionStorage.getItem('userRespondedToSessionWarning') === "true";

        if (! userHasRespondedToSessionWarning) {
            let sessionWarningTimeoutId = workerTimers.setTimeout(() => {
                dispatch(warnSessionExpiresSoon(tokenStr))
            }, timeToWarnInMillis)

            dispatch(setSessionWarningTimeoutId(sessionWarningTimeoutId))
        }
    }
}
github Nike-Inc / cerberus / dashboard / app / actions / authenticationActions.js View on Github external
.then(function (response) {
            dispatch(handleRemoveSessionExpirationCheck())
            dispatch(removeSessionWarningTimeout())
            workerTimers.setTimeout(function(){
                handleUserLogin(response, dispatch, false)
                if (redirect) {
                    hashHistory.push(redirectPath)
                }
            }, 2000);

        })
        .catch(function (response) {

worker-timers

A replacement for setInterval() and setTimeout() which works in unfocused windows.

MIT
Latest version published 21 days ago

Package Health Score

73 / 100
Full package analysis