Last change
on this file since 76712b2 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago |
initial commit
|
-
Property mode
set to
100644
|
File size:
1.2 KB
|
Rev | Line | |
---|
[6a3a178] | 1 | # minipass-flush
|
---|
| 2 |
|
---|
| 3 | A Minipass stream that calls a flush function before emitting 'end'
|
---|
| 4 |
|
---|
| 5 | ## USAGE
|
---|
| 6 |
|
---|
| 7 | ```js
|
---|
| 8 | const Flush = require('minipass-flush')
|
---|
| 9 | cons f = new Flush({
|
---|
| 10 | flush (cb) {
|
---|
| 11 | // call the cb when done, or return a promise
|
---|
| 12 | // the 'end' event will wait for it, along with
|
---|
| 13 | // close, finish, and prefinish.
|
---|
| 14 | // call the cb with an error, or return a rejecting
|
---|
| 15 | // promise to emit 'error' instead of doing the 'end'
|
---|
| 16 | return rerouteAllEncryptions().then(() => clearAllChannels())
|
---|
| 17 | },
|
---|
| 18 | // all other minipass options accepted as well
|
---|
| 19 | })
|
---|
| 20 |
|
---|
| 21 | someDataSource.pipe(f).on('end', () => {
|
---|
| 22 | // proper flushing has been accomplished
|
---|
| 23 | })
|
---|
| 24 |
|
---|
| 25 | // Or as a subclass implementing a 'flush' method:
|
---|
| 26 | class MyFlush extends Flush {
|
---|
| 27 | flush (cb) {
|
---|
| 28 | // old fashioned callback style!
|
---|
| 29 | rerouteAllEncryptions(er => {
|
---|
| 30 | if (er)
|
---|
| 31 | return cb(er)
|
---|
| 32 | clearAllChannels(er => {
|
---|
| 33 | if (er)
|
---|
| 34 | cb(er)
|
---|
| 35 | cb()
|
---|
| 36 | })
|
---|
| 37 | })
|
---|
| 38 | }
|
---|
| 39 | }
|
---|
| 40 | ```
|
---|
| 41 |
|
---|
| 42 | That's about it.
|
---|
| 43 |
|
---|
| 44 | If your `flush` method doesn't have to do anything asynchronous, then it's
|
---|
| 45 | better to call the callback right away in this tick, rather than returning
|
---|
| 46 | `Promise.resolve()`, so that the `end` event can happen as soon as
|
---|
| 47 | possible.
|
---|
Note:
See
TracBrowser
for help on using the repository browser.