Skip to content

Commit ebadb9a

Browse files
authored
Add asyncDispose support to Readable and Writable (#19)
1 parent df784e9 commit ebadb9a

2 files changed

Lines changed: 27 additions & 0 deletions

File tree

index.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,12 @@ exports.Readable = class Readable extends stream.Readable {
6565

6666
super.unshift(chunk)
6767
}
68+
69+
async [Symbol.asyncDispose]() {
70+
if (!this.destroyed) this.destroy()
71+
72+
await new Promise((resolve) => exports.finished(this, resolve))
73+
}
6874
}
6975

7076
exports.Writable = class Writable extends stream.Writable {
@@ -131,6 +137,12 @@ exports.Writable = class Writable extends stream.Writable {
131137

132138
return result
133139
}
140+
141+
async [Symbol.asyncDispose]() {
142+
if (!this.destroyed) this.destroy()
143+
144+
await new Promise((resolve) => exports.finished(this, resolve))
145+
}
134146
}
135147

136148
exports.Duplex = class Duplex extends stream.Duplex {

test/basic.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -720,4 +720,19 @@ test('duplexPair', (t) => {
720720
pairA.end()
721721
})
722722

723+
test('async disposable', async (t) => {
724+
t.plan(2)
725+
726+
const readable = new Readable()
727+
const writable = new Writable()
728+
729+
{
730+
await using disposableReadable = readable
731+
await using disposableWritable = writable
732+
}
733+
734+
t.ok(readable.destroyed)
735+
t.ok(writable.destroyed)
736+
})
737+
723738
function noop() {}

0 commit comments

Comments
 (0)