1.1.0 • Published 4 years ago

tu-utils v1.1.0

Weekly downloads
1
License
ISC
Repository
-
Last release
4 years ago

工具类

目录

安装

$ npm i tu-utils

介绍

tu-utils是对常用工具的typescript封装

使用

双向链表

现在指向让他能够头尾添加删除,因为这样效率最高,如果有需要再加(继承)

import {LinkedList} from "tu-utils";

let list:LinkedList<String> = new LinkedList<String>();
list.addLast('tujiawei');
list.addLast('zhangjiayu');
list.addLast('chenwan');
list.addLast('ABC');
try {
    console.log(list.getFirst());
    console.log(list.getFirst());
    console.log(list.getFirst());
    console.log(list.getFirst());
    console.log(list.getFirst());
} catch (e) {
    console.log(e);
}
list.addLast('tujiawei');
list.addLast('zhangjiayu');
list.addLast('chenwan');
list.addLast('ABC');

console.log(list.getFirst());

事件监听器

只触发一次的事件监听器,监听完毕自动删除

import {EventEmitter} from "tu-utils";

class Pool extends EventEmitter {
    private restItem = 5;

    async getItem() {
        if (this.restItem > 0) {
            this.restItem--;
            return true;
        }
        return new Promise(resolve => {
            this.once('add', () => {
                this.restItem --;
                resolve(true);
            });
        }).catch(e => {
            console.log(e);
        });
    }
    addItem() {
        this.restItem ++;
        this.emit('add');
    }
}

let pool = new Pool();
let res;
(async function () {
    pool.addItem();
    setTimeout(() => {
       pool.addItem();
    }, 1000);
    setTimeout(() => {
       pool.addItem();
    }, 2000);
    setTimeout(() => {
       pool.addItem();
    }, 3000);
    for(let i = 0; i < 100; i++) {
        res = await pool.getItem();
        console.log(res, i);
    }
}());
1.1.0

4 years ago

1.0.9

4 years ago

1.0.8

4 years ago

1.0.7

4 years ago

1.0.6

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.2

4 years ago

1.0.3

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago