userland implementation of proposed finally() API for web streams
import { ReadableStreamWithFinally } from '@jcoreio/web-streams-finally'
const stream = new ReadableStreamWithFinally({
async pull(controller) {
...
},
async finally(why: 'close' | 'cancel' | 'error', reason?: any) {
// this will be called when the stream is closed, canceled, or errored
}
})
import { ReadableStreamWithSignal } from '@jcoreio/web-streams-finally'
const stream = new ReadableStreamWithSignal({
async pull(controller) {
await doSomething({
signal: controller.signal, // will be aborted when stream errors is closed, canceled, or errored
})
...
},
async finally(why: 'close' | 'cancel' | 'error', reason?: any) {
// this will be called when the stream is closed, canceled, or errored
}
})
import { WritableStreamWithFinally } from '@jcoreio/web-streams-finally'
const stream = new WritableStreamWithFinally({
async write(controller) {
...
},
async finally(why: 'close' | 'abort' | 'error', reason?: any) {
// this will be called when the stream is closed, aborted, or errored
}
})