项目作者: yusangeng

项目描述 :
Toolset about time & timing.
高级语言: TypeScript
项目地址: git://github.com/yusangeng/polygala.git
创建时间: 2018-01-04T16:52:25Z
项目社区:https://github.com/yusangeng/polygala

开源协议:MIT License

下载


polygala

TypeScript Build Status Coverage Status Npm Package Info Downloads

Abstract

TS library about timing.

Install

  1. npm install polygala --save

Usage

sleep

Asynchronous sleep.

  1. import { sleep } from 'polygala';
  2. async function main(): Promise<void> {
  3. await sleep(1000);
  4. }

task

Simulative micro/macro task in browser.

  1. import { micro, macro } from 'polygala';
  2. const task1 = macro(() => console.log('task1'));
  3. const task2 = micro(() => console.log('task2'));
  4. task1();
  5. task2();
  6. //=> Prints 'task2', 'task1'

fifo

FIFO promise queue.

  1. import { fifo, sleep } from 'polygala';
  2. async function fa(): Promise<void> {
  3. // 2s delay
  4. await sleep(2000);
  5. }
  6. async function fb(): Promise<void> {
  7. // 1s delay
  8. await sleep(1000);
  9. }
  10. const globalFIFOName = Symbol('foobar');
  11. const a = fifo(fa, globalFIFOName);
  12. const b = fifo(fb, globalFIFOName);
  13. let str = '';
  14. a().then(() => {
  15. str += 'Hello';
  16. });
  17. b().then(() => {
  18. str += 'World';
  19. });
  20. //=> str === 'Hello World'

poll

An easy-to-use polling implemention.

  1. import { poll } from 'polygala';
  2. const stop = poll(
  3. async (polling) => {
  4. const { url } = polling.context;
  5. await fetch(url);
  6. },
  7. {
  8. delay: 3000,
  9. limit: 1000, // Repeats at most 1000 times, 0 means NO limit.
  10. context: {
  11. url: '//foobar.com/heartbeat',
  12. },
  13. onError: (err) => {
  14. console.error(err);
  15. return false; // False means "Don't stop polling", if you want to stop, return true.
  16. },
  17. }
  18. );
  19. // ...
  20. stop(); // stop polling.

poll/until

Poll until compare function returns true.

  1. import { poll } from 'polygala';
  2. let i = 0;
  3. try {
  4. const data = await poll(async () => i++).until((curr: any) => curr > 100, 1000);
  5. } catch (err) {
  6. console.log(err.message);
  7. }

quittable

Quittable asynchronous task.

  1. import { quittable, sleep } from 'polygala';
  2. import ajax from './ajax';
  3. import store from './store';
  4. const task = quittable(
  5. async (task) => {
  6. await sleep(1000);
  7. if (task.quitted) {
  8. // Task has been quitted.
  9. return;
  10. }
  11. const { url } = task.context;
  12. const data = await ajax.get(url);
  13. if (task.quitted) {
  14. return;
  15. }
  16. store.data = data;
  17. },
  18. // Name of quittable task, null means on name.
  19. // A named task would be quitted if a new task with the same name was run.
  20. 'foobar',
  21. // context
  22. {
  23. url: '//foobar.com/heartbeat',
  24. }
  25. );
  26. task.run();
  27. setTimeout((_) => {
  28. task.quit();
  29. }, 1050);

API

sleep

Sleep Asynchronously.

  1. function sleep(milliseconds: number): Promise<void>;

micro & macro

Invoke simulative micro/macro tasks in browser.

  1. type FProcedure = (...args: any[]) => void;
  2. function micro<Fn extends FProcedure>(fn: Fn): Fn;
  3. function macro<Fn extends FProcedure>(fn: Fn): Fn;

fifo

Push an async function and its return value into a FIFO promise queue.

  1. type AsyncFunc<RetType> = (...args: any[]) => Promise<RetType>;
  2. export function fifo<Fn extends AsyncFunc<void>>(fn: Fn, queueName?: symbol): Fn;
  3. export function fifo<RetType, Fn extends AsyncFunc<RetType>>(fn: Fn, queueName?: symbol): Fn;

poll

Start polling.

  1. // Polling function type.
  2. type PollingFunc<ContextType> = (p: Polling<ContextType>) => void;
  3. // Error callbacl type.
  4. type ErrorCallback = (error: Error) => boolean;
  5. // Options type.
  6. type PollingOptions<ContextType> = {
  7. context?: ContextType;
  8. delay?: number;
  9. limit?: number;
  10. onError?: ErrorCallback;
  11. };
  12. // Function to stop polling.
  13. type StopFunc = () => void;
  14. function poll<ContextType>(fn: PollingFunc<ContextType>, options?: PollingOptions<ContextType>): StopFunc;

quittable

Create a quittable task.

  1. interface IQuittable<ContextType> {
  2. quitted: boolean;
  3. readonly context: ContextType;
  4. quit(): void;
  5. }
  6. type FQUITTED = () => void;
  7. class Quittable<ContextType, RetType> implements IQuittable<ContextType> {
  8. // ...
  9. run(): Promise<FQUITTED | RetType>;
  10. quit(): void;
  11. }
  12. type QuittableCallable<ContextType, RetType> = (q: IQuittable<ContextType>) => RetType;
  13. function quittable<ContextType, RetType = void>(
  14. context: ContextType,
  15. fn: QuittableCallable<ContextType, RetType>
  16. ): Quittable<ContextType, RetType>;

namedQuittable

Create a named quittable task.

If you’ve created a named quittable task, the last task with the same name was quitted automatically.

  1. function namedQuittable<ContextType, RetType = void>(
  2. name: symbol,
  3. context: ContextType,
  4. fn: QuittableCallable<ContextType, RetType>
  5. ): Quittable<ContextType, RetType>;

getRunningNamedQuittable

Find the running named quittable task.

  1. function getRunningNamedQuittable(name: symbol);

countdown

  1. export type CountDownOptions = {
  2. // Countdown interval, the default value is 1000ms.
  3. interval?: number;
  4. total: number;
  5. onTimeout?: (total: number, countdown: CountDown) => void;
  6. onTick?: (residue: number, total: number, countdown: CountDown) => void;
  7. };
  8. export function countdown(options: CountDownOptions);