{"size": 1861, "ext": "js", "max_stars_repo_path": "packages/utils/src/edge/setTimeout/sessionStorage.js", "max_stars_repo_name": "jzendo/favorite_box", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "packages/utils/src/edge/setTimeout/sessionStorage.js", "max_issues_repo_name": "jzendo/favorite_box", "max_issues_repo_issues_event_min_datetime": "2020-03-17T12:24:36.000Z", "max_issues_repo_issues_event_max_datetime": "2020-05-10T15:40:08.000Z", "max_forks_repo_path": "packages/utils/src/edge/setTimeout/sessionStorage.js", "max_forks_repo_name": "jzendo/favorite_box", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": 2, "max_forks_count": null, "avg_line_length": 20.6777777778, "max_line_length": 63, "alphanum_fraction": 0.6270822139}
import { storeKey } from './contant'

const nativeSetTimeout = window.setTimeout
const nativeClearTimeout = window.clearTimeout

const sessionStorage = {
  set (fn, { scheduledTimeouts, args }) {
    const instance = fn[storeKey]

    if (!instance) {
      fn[storeKey] = {
        current: 0,
        timer: null,
        count: scheduledTimeouts.length,
        timeouts: scheduledTimeouts,
        args
      }
    }

    return sessionStorage
  },

  get (fn) {
    const instance = fn[storeKey]
    return instance || null
  },

  args (fn) {
    const instance = this.get(fn)
    if (!instance) return null

    return instance.args
  },

  step (fn, callback) {
    /* eslint-disable standard/no-callback-literal */
    const instance = sessionStorage.get(fn)

    if (!instance) {
      return callback(true)
    }

    let current

    const { count, timeouts, args } = instance

    current = instance.current

    // Next timer
    if (current <= count) {
      current = ++instance.current
      const timeout = timeouts[current - 1]
      const timer = nativeSetTimeout(fn, timeout, ...args)
      // Set timer
      sessionStorage.updateTimer(fn, timer)
      callback(false, timeout, args)
    } else {
      // End
      sessionStorage.clear(fn)
      callback(true)
    }
    /* eslint-enable */
  },

  updateTimer (fn, targetTimer) {
    const instance = this.get(fn)
    if (!instance) return sessionStorage

    if (instance.timer) {
      nativeClearTimeout(instance.timer)
    }

    if (targetTimer !== undefined) instance.timer = targetTimer

    return sessionStorage
  },

  clear (fn) {
    const instance = this.get(fn)
    if (!instance) return sessionStorage

    // Clear timer
    sessionStorage.updateTimer(fn)
    if (sessionStorage.get(fn)) delete fn[storeKey]
    return sessionStorage
  }
}

export default sessionStorage
