From b8dacf1df7f8c45f50c59044d135700bdb676f67 Mon Sep 17 00:00:00 2001 From: Wang Guan Date: Sat, 6 Apr 2024 23:03:46 +0900 Subject: [PATCH] rename lease.ts --- src/concurrency/lazy-thenable.ts | 8 ++++++-- src/concurrency/lease.ts | 8 ++++++++ src/concurrency/lockable.ts | 0 src/concurrency/locks/lockable.spec.ts | 3 --- src/concurrency/locks/lockable.ts | 11 ----------- src/concurrency/resource-pool.ts | 14 +++++++++----- 6 files changed, 23 insertions(+), 21 deletions(-) create mode 100644 src/concurrency/lease.ts create mode 100644 src/concurrency/lockable.ts delete mode 100644 src/concurrency/locks/lockable.spec.ts delete mode 100644 src/concurrency/locks/lockable.ts diff --git a/src/concurrency/lazy-thenable.ts b/src/concurrency/lazy-thenable.ts index 633c7e8..d12cdfa 100644 --- a/src/concurrency/lazy-thenable.ts +++ b/src/concurrency/lazy-thenable.ts @@ -1,11 +1,15 @@ -export function lazyThenable(action: () => PromiseLike): PromiseLike { +/** + * create a lazy, at-most-once PromiseLike from async function + * @param io + */ +export function lazyThenable(io: () => PromiseLike): PromiseLike { let r: null | Promise = null; return { then( onfulfilled?: ((value: T) => PromiseLike | TResult1) | undefined | null, onrejected?: ((reason: any) => PromiseLike | TResult2) | undefined | null, ): PromiseLike { - return (r ??= Promise.resolve(action())).then(onfulfilled, onrejected); + return (r ??= Promise.resolve(io())).then(onfulfilled, onrejected); }, }; } diff --git a/src/concurrency/lease.ts b/src/concurrency/lease.ts new file mode 100644 index 0000000..03e994a --- /dev/null +++ b/src/concurrency/lease.ts @@ -0,0 +1,8 @@ +/** + * An acquired lock or resource + */ +export interface Lease { + value: T; + dispose(): PromiseLike; + [Symbol.asyncDispose](): PromiseLike; +} diff --git a/src/concurrency/lockable.ts b/src/concurrency/lockable.ts new file mode 100644 index 0000000..e69de29 diff --git a/src/concurrency/locks/lockable.spec.ts b/src/concurrency/locks/lockable.spec.ts deleted file mode 100644 index 9739d84..0000000 --- a/src/concurrency/locks/lockable.spec.ts +++ /dev/null @@ -1,3 +0,0 @@ -describe('ResourcePool lock', () => { - it.todo(''); -}); diff --git a/src/concurrency/locks/lockable.ts b/src/concurrency/locks/lockable.ts deleted file mode 100644 index da8c236..0000000 --- a/src/concurrency/locks/lockable.ts +++ /dev/null @@ -1,11 +0,0 @@ -import { ResourcePool } from '../resource-pool'; - -export class LockableError extends Error { - static A = class L2 extends LockableError {}; -} - -export interface Lease { - value: T; - // close(): Promise; - [Symbol.asyncDispose](): Promise; -} diff --git a/src/concurrency/resource-pool.ts b/src/concurrency/resource-pool.ts index e0fc940..60a41e7 100644 --- a/src/concurrency/resource-pool.ts +++ b/src/concurrency/resource-pool.ts @@ -5,7 +5,7 @@ * - NOT supported: replace / refresh / timeout of tasks */ import { wait } from './timing'; -import { Lease } from './locks/lockable'; +import { Lease } from './lease'; import { lazyThenable } from './lazy-thenable'; export class ResourcePool { @@ -41,7 +41,7 @@ export class ResourcePool { async use(task: (res: T) => R): Promise> { await using lease = await this.borrow(); - return await task(lease.value); + return /* must not omit 'await' here */ await task(lease.value); } tryUse(task: (res: T | null) => R): R | Promise> { @@ -85,7 +85,8 @@ export class ResourcePool { } } - async borrow(timeout?: number): Promise> { + async borrow(): Promise> { + // TODO: implement timeout const v = await this._borrow(); // console.log('borrowed', v); @@ -97,8 +98,11 @@ export class ResourcePool { return { value: v, - [Symbol.asyncDispose]: async () => { - await _return; + dispose(): PromiseLike { + return _return; + }, + [Symbol.asyncDispose]() { + return _return; }, }; }