1 | 'use strict'
|
2 |
|
3 | const MinipassPipeline = require('minipass-pipeline')
|
4 |
|
5 | class CachingMinipassPipeline extends MinipassPipeline {
|
6 | #events = []
|
7 | #data = new Map()
|
8 |
|
9 | constructor (opts, ...streams) {
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 | super()
|
16 | this.#events = opts.events
|
17 |
|
18 |
|
19 | if (streams.length) {
|
20 | this.push(...streams)
|
21 | }
|
22 | }
|
23 |
|
24 | on (event, handler) {
|
25 | if (this.#events.includes(event) && this.#data.has(event)) {
|
26 | return handler(...this.#data.get(event))
|
27 | }
|
28 |
|
29 | return super.on(event, handler)
|
30 | }
|
31 |
|
32 | emit (event, ...data) {
|
33 | if (this.#events.includes(event)) {
|
34 | this.#data.set(event, data)
|
35 | }
|
36 |
|
37 | return super.emit(event, ...data)
|
38 | }
|
39 | }
|
40 |
|
41 | module.exports = CachingMinipassPipeline
|