68d823b42e357b0d49815feb8c109425bcbf7f1d0e713c39c4241a77359fee5ae03ad874e9e767deff8136f7d5d5dcaaba10c63d16a12a640fdd0c16dce69c 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. # minipass-flush
  2. A Minipass stream that calls a flush function before emitting 'end'
  3. ## USAGE
  4. ```js
  5. const Flush = require('minipass-flush')
  6. cons f = new Flush({
  7. flush (cb) {
  8. // call the cb when done, or return a promise
  9. // the 'end' event will wait for it, along with
  10. // close, finish, and prefinish.
  11. // call the cb with an error, or return a rejecting
  12. // promise to emit 'error' instead of doing the 'end'
  13. return rerouteAllEncryptions().then(() => clearAllChannels())
  14. },
  15. // all other minipass options accepted as well
  16. })
  17. someDataSource.pipe(f).on('end', () => {
  18. // proper flushing has been accomplished
  19. })
  20. // Or as a subclass implementing a 'flush' method:
  21. class MyFlush extends Flush {
  22. flush (cb) {
  23. // old fashioned callback style!
  24. rerouteAllEncryptions(er => {
  25. if (er)
  26. return cb(er)
  27. clearAllChannels(er => {
  28. if (er)
  29. cb(er)
  30. cb()
  31. })
  32. })
  33. }
  34. }
  35. ```
  36. That's about it.
  37. If your `flush` method doesn't have to do anything asynchronous, then it's
  38. better to call the callback right away in this tick, rather than returning
  39. `Promise.resolve()`, so that the `end` event can happen as soon as
  40. possible.