0.1.1 • Published 5 years ago

@byzanteam/carbonium-timer v0.1.1

Weekly downloads
3
License
MIT
Repository
github
Last release
5 years ago

Carbonium-Timer

Timer support for Carbonium.

Installation

$ npm install @byzanteam/carbonium-timer

Usage

Create Timer

import {createTimer} from '@byzanteam/carbonium-timer'

const timer = createTimer()

// Create timer with given defaults.
const timer = createTimer({delay: 0, duration: 1e3, easingFunction: 'easeInOut'})

Subscribe

timer.subscribe() 向 timer 中添加任务,返回退订函数。调用退订函数会从任务队列中移除相关的所有任务。该函数一共有三种调用形式,如下所示。

interface StateType {
  width: number,
  height: number,
}

const handler = (progress: number, startState: StateType, endState: StateType) => {
  console.log(progress, startState, endState)
}

// 处理函数和配置分离的但任务。
timer.subscribe<StateType>(handler, {
  delay: 250,
  startState: {width: 100, height: 100},
  endState: {width: 200, height: 200},
})

// 单个任务配置对象。
timer.subscribe<StateType>({
  handler,
  duration: 2e3,
  startState: {width: 100, height: 100},
  endState: {width: 200, height: 200},
})

// 多个任务配置对象数组。
const unsubscribe = timer.subscribe<StateType>([
  {
    handler,
  },
  {
    handler,
  }
])

// 调用 unsubscribe 函数会移除第三次订阅的所有任务。
unsubscribe()