Skip to content

Commit

Permalink
Add async dispose support
Browse files Browse the repository at this point in the history
  • Loading branch information
malthe committed Feb 28, 2024
1 parent 57cef65 commit 73f950d
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 9 deletions.
6 changes: 4 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ jobs:
strategy:
matrix:
version:
- 18.x
- 20.x
- 18
- 20
runs-on: ubuntu-latest
needs: [build]
timeout-minutes: 3
Expand Down Expand Up @@ -135,13 +135,15 @@ jobs:
pkg_path=`ls $PWD/pkg/ts-postgres-*.tgz`
cd examples/connect
npm i . "$pkg_path"
cd v$NODE_VERSION || true
set -o pipefail
PGSSLMODE=disable node main.cjs | tee /dev/stderr | grep -q false
PGSSLMODE=require node main.cjs | tee /dev/stderr | grep -q true
npx tsc
PGSSLMODE=disable node main.mjs | tee /dev/stderr | grep -q false
PGSSLMODE=require node main.mjs | tee /dev/stderr | grep -q true
env:
NODE_VERSION: ${{ matrix.version }}
NODE_EXTRA_CA_CERTS: ${{ github.workspace }}/ssl-cert-snakeoil.pem
PGPORT: ${{ job.services.postgres.ports[5432] }}
PGUSER: postgres
Expand Down
3 changes: 3 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ In next release ...
connected when the promise returns. The `Client` symbol is now a
type instead of a value.

- Add `[Symbol.asyncDispose]` method to support [Explicit Resource
Management](https://github.com/tc39/proposal-explicit-resource-management).

- Add `off` method to disable event listening.

- Remove dependency on
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ try {
}
```

With TypeScript 5.2+ there is also support for `await using`:
```typescript
await using client = await connect();
// Will be disposed of automatically at the end of the block.
```

Waiting on the result (i.e., result iterator) returns the complete query result.

```typescript
Expand Down
8 changes: 4 additions & 4 deletions examples/connect/main.cjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { connect } = require('ts-postgres');
module.exports = connect().then((client) => {
console.log('Encrypted: ' + client.encrypted);
return client.end();
});
module.exports = (async () => {
await using client = await connect();
console.log('Encrypted: ' + client.encrypted);
})();
3 changes: 1 addition & 2 deletions examples/connect/main.mts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { connect } from 'ts-postgres';
const client = await connect();
await using client = await connect();
console.log('Encrypted: ' + client.encrypted);
await client.end();
5 changes: 5 additions & 0 deletions examples/connect/v18/main.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const { connect } = require('ts-postgres');
module.exports = connect().then((client) => {
console.log('Encrypted: ' + client.encrypted);
return client.end();
});
4 changes: 4 additions & 0 deletions examples/connect/v18/main.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { connect } from 'ts-postgres';
const client = await connect();
console.log('Encrypted: ' + client.encrypted);
await client.end();
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
},
"license": "MIT",
"engines": {
"node": ">=18.0.0"
"node": ">=18.0"
},
"scripts": {
"lint": "eslint -c .eslintrc.json --ext .ts src test",
Expand Down
4 changes: 4 additions & 0 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -759,6 +759,10 @@ export class ClientImpl {
}
}

[Symbol.asyncDispose]() {
return this.end();
}

private send() {
if (this.mustDrain || !this.connected) return;
this.sendUsing(this.writer);
Expand Down

0 comments on commit 73f950d

Please sign in to comment.