Skip to content

Commit

Permalink
rename lease.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
jokester committed Apr 6, 2024
1 parent 6b90fd6 commit b8dacf1
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 21 deletions.
8 changes: 6 additions & 2 deletions src/concurrency/lazy-thenable.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
export function lazyThenable<T>(action: () => PromiseLike<T>): PromiseLike<T> {
/**
* create a lazy, at-most-once PromiseLike from async function
* @param io
*/
export function lazyThenable<T>(io: () => PromiseLike<T>): PromiseLike<T> {
let r: null | Promise<T> = null;
return {
then<TResult1, TResult2 = never>(
onfulfilled?: ((value: T) => PromiseLike<TResult1> | TResult1) | undefined | null,
onrejected?: ((reason: any) => PromiseLike<TResult2> | TResult2) | undefined | null,
): PromiseLike<TResult1 | TResult2> {
return (r ??= Promise.resolve(action())).then(onfulfilled, onrejected);
return (r ??= Promise.resolve(io())).then(onfulfilled, onrejected);
},
};
}
8 changes: 8 additions & 0 deletions src/concurrency/lease.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* An acquired lock or resource
*/
export interface Lease<T> {
value: T;
dispose(): PromiseLike<void>;
[Symbol.asyncDispose](): PromiseLike<void>;
}
Empty file added src/concurrency/lockable.ts
Empty file.
3 changes: 0 additions & 3 deletions src/concurrency/locks/lockable.spec.ts

This file was deleted.

11 changes: 0 additions & 11 deletions src/concurrency/locks/lockable.ts

This file was deleted.

14 changes: 9 additions & 5 deletions src/concurrency/resource-pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> {
Expand Down Expand Up @@ -41,7 +41,7 @@ export class ResourcePool<T> {

async use<R>(task: (res: T) => R): Promise<Awaited<R>> {
await using lease = await this.borrow();
return await task(lease.value);
return /* must not omit 'await' here */ await task(lease.value);
}

tryUse<R>(task: (res: T | null) => R): R | Promise<Awaited<R>> {
Expand Down Expand Up @@ -85,7 +85,8 @@ export class ResourcePool<T> {
}
}

async borrow(timeout?: number): Promise<Lease<T>> {
async borrow(): Promise<Lease<T>> {
// TODO: implement timeout
const v = await this._borrow();

// console.log('borrowed', v);
Expand All @@ -97,8 +98,11 @@ export class ResourcePool<T> {

return {
value: v,
[Symbol.asyncDispose]: async () => {
await _return;
dispose(): PromiseLike<void> {
return _return;

Check warning on line 102 in src/concurrency/resource-pool.ts

View check run for this annotation

Codecov / codecov/patch

src/concurrency/resource-pool.ts#L101-L102

Added lines #L101 - L102 were not covered by tests
},
[Symbol.asyncDispose]() {
return _return;
},
};
}
Expand Down

0 comments on commit b8dacf1

Please sign in to comment.