11cb0ef41Sopenharmony_ci'use strict' 21cb0ef41Sopenharmony_ci 31cb0ef41Sopenharmony_ciconst MinipassPipeline = require('minipass-pipeline') 41cb0ef41Sopenharmony_ci 51cb0ef41Sopenharmony_ciclass CachingMinipassPipeline extends MinipassPipeline { 61cb0ef41Sopenharmony_ci #events = [] 71cb0ef41Sopenharmony_ci #data = new Map() 81cb0ef41Sopenharmony_ci 91cb0ef41Sopenharmony_ci constructor (opts, ...streams) { 101cb0ef41Sopenharmony_ci // CRITICAL: do NOT pass the streams to the call to super(), this will start 111cb0ef41Sopenharmony_ci // the flow of data and potentially cause the events we need to catch to emit 121cb0ef41Sopenharmony_ci // before we've finished our own setup. instead we call super() with no args, 131cb0ef41Sopenharmony_ci // finish our setup, and then push the streams into ourselves to start the 141cb0ef41Sopenharmony_ci // data flow 151cb0ef41Sopenharmony_ci super() 161cb0ef41Sopenharmony_ci this.#events = opts.events 171cb0ef41Sopenharmony_ci 181cb0ef41Sopenharmony_ci /* istanbul ignore next - coverage disabled because this is pointless to test here */ 191cb0ef41Sopenharmony_ci if (streams.length) { 201cb0ef41Sopenharmony_ci this.push(...streams) 211cb0ef41Sopenharmony_ci } 221cb0ef41Sopenharmony_ci } 231cb0ef41Sopenharmony_ci 241cb0ef41Sopenharmony_ci on (event, handler) { 251cb0ef41Sopenharmony_ci if (this.#events.includes(event) && this.#data.has(event)) { 261cb0ef41Sopenharmony_ci return handler(...this.#data.get(event)) 271cb0ef41Sopenharmony_ci } 281cb0ef41Sopenharmony_ci 291cb0ef41Sopenharmony_ci return super.on(event, handler) 301cb0ef41Sopenharmony_ci } 311cb0ef41Sopenharmony_ci 321cb0ef41Sopenharmony_ci emit (event, ...data) { 331cb0ef41Sopenharmony_ci if (this.#events.includes(event)) { 341cb0ef41Sopenharmony_ci this.#data.set(event, data) 351cb0ef41Sopenharmony_ci } 361cb0ef41Sopenharmony_ci 371cb0ef41Sopenharmony_ci return super.emit(event, ...data) 381cb0ef41Sopenharmony_ci } 391cb0ef41Sopenharmony_ci} 401cb0ef41Sopenharmony_ci 411cb0ef41Sopenharmony_cimodule.exports = CachingMinipassPipeline 42