lite-ts-thread
TypeScript icon, indicating that this package has built-in type declarations

11.4.0 • Public • Published

Version

代码

MutexBase - 互斥锁

const mutex: MutexBase;
// 获取 key 等待锁, (最多尝试获取30次,每次间隔在10~20毫秒),获取不到则抛出异常
const unlock = await mutex.lock({
    key: 'key',
    tryCount: 30,
    sleepRange: [10, 20]
});
await unlock(); // 释放锁

// 获取 key 锁,并且设置锁时间为3秒,获取不到则返回 null
const unlock = await mutex.lock({
    key: 'key',
    timeoutSeconds: 3
});
if (!unlock) await unlock();

ThreadBase - 线程

const thread: ThreadBase;

await thread.sleep(1000); // 休眠1000毫秒

await thread.sleepRange(1000, 2000); // 随机休眠1000~2000毫秒

await thread.stopWatch('统计该函数的耗时', async () => {
    let total = 0;
    for (let i = 0; i < 1000; i++)
        total += i;
    return total;
});

// 每间隔 1000 毫秒尝试执行 1 次函数,最多尝试 5 次,如果期间有一次执行正常则返回,否则抛出异常
// 首次执行也需要等待 1000 毫秒
await thread.try(
    async () => {
        console.log('执行函数');
        return '';
    },
    5,
    1000
);
  • SetTimeoutThread - SetTimeout线程
const thread = new SetTimeoutThread();

Readme

Keywords

Package Sidebar

Install

npm i lite-ts-thread

Weekly Downloads

2

Version

11.4.0

License

GPL-3.0

Unpacked Size

42.8 kB

Total Files

13

Last publish

Collaborators

  • ahl5esoft
OSZAR »