+
Skip to content

InkSha/time-loop

Repository files navigation

Timeloop

A lightweight and simple time loop library.

Install

pnpm add @inksha/timeloop

Usage

import Timeloop, { Timeout } from '@inksha/timeloop'

const timeloop = new Timeloop(new Timeout())

let count = 5

timeloop.registerTasks({
  name: 'example',
  interval: 1 /** second */,
  fn(timestamp) {
    console.log(timestamp)
    if (count-- <= 0) {
      timeloop.unregisterTask('example')
    }
  }
})

timeloop.run()

Custom Time Engine

import type { TimeEngine } from '@inksha/timeloop'

export class RAF implements TimeEngine {
  private running = false
  private timer: number | null = null

  constructor() {}

  execute(callback: () => void): void {
    this.close()
    this.running = true
    const executor = () => {
      this.timer = requestAnimationFrame(() => {
        callback()
        if (this.running) executor()
      })
    }
    executor()
  }

  close(): void {
    if (this.timer) {
      cancelAnimationFrame(this.timer)
      this.timer = null
    }
    this.running = false
  }
}

const frameLoop = new Timeloop(new RAF())

API

method comment
changePathname(pathname: string) on change route call, instance match old and new pathname, if not match, clean no set keeplive task
hasTask(name: string) query task name exist
registerTasks(...tasks: TimerTask[]) register task
unregisterTask(taskName: string): void unregister task
run() run loop
close() close loop

Types

export interface TimeEngine {
  execute(callback: () => void): void
  close(): void
}

export type TimerTask = {
  name: string
  once?: boolean
  /**
   * unit second
   */
  interval?: number
  delay?: number
  count?: number
  keepAlive?: boolean
  lastTimestamp?: number
  replace?: boolean
  fn: (timestamp: number) => void
}

About

A lightweight and simple time loop library.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载