Skip to content

Commit

Permalink
fix: not using lease in testee code for now
Browse files Browse the repository at this point in the history
  • Loading branch information
jokester committed Apr 6, 2024
1 parent 40314d7 commit a2a0b85
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 11 deletions.
17 changes: 11 additions & 6 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
module.exports = {
preset: 'ts-jest',
preset: 'ts-jest/presets/js-with-ts',
roots: ['src'],
transformIgnorePatterns: ['<rootDir>/node_modules/.*\\.js', '<rootDir>/build/.*\\.js'],
transform: {
'^.+\\.tsx?$': [
'ts-jest',
{
tsconfig: {
target: 'es2022',
},
},
],
},
testMatch: ['**/__test__/*\\.(ts|js|tsx|jsx)', '**/*\\.(spec|test)\\.(ts|js|tsx|jsx)'],
collectCoverageFrom: ['src/**/*.(ts|tsx)', '!build/', '!**/node_modules', '!/coverage'],
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json'],
coverageReporters: ['json', 'lcov', 'text', 'html'],
globals: {
'ts-jest': {
isolatedModules: true,
},
},
};
2 changes: 1 addition & 1 deletion src/concurrency/locks/lockable.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
describe('ResourcePool lock', () => {
it.skip('');
it.todo('');
});
13 changes: 10 additions & 3 deletions src/concurrency/resource-pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,12 @@ 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);
const lease = await this.borrow();
try {
return await task(lease.value);
} finally {
await lease[Symbol.asyncDispose]();
}
}

tryUse<R>(task: (res: T | null) => R): R | Promise<Awaited<R>> {
Expand Down Expand Up @@ -88,13 +92,16 @@ export class ResourcePool<T> {
async borrow(timeout?: number): Promise<Lease<T>> {
const v = await this._borrow();

// console.log('borrowed', v);

const _return = lazyThenable(async () => {
this._return(v);
// console.log('returned', v);
});

return {
value: v,
async [Symbol.asyncDispose]() {
[Symbol.asyncDispose]: async () => {
await _return;
},
};
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"jsx": "preserve",
"module": "commonjs",
"moduleResolution": "node",
"target": "es2021",
"target": "es2022",
"skipLibCheck": true,
"rootDir": "src",
"outDir": "lib"
Expand Down

0 comments on commit a2a0b85

Please sign in to comment.