import { Pipe } from './index'; import Core from '../core'; // 节流,下一个pipe将接收到一个数组 export const createThrottlePipe = function (aegis: Core, maxLength?: number): Pipe { let timer: any; const msgs: any[] = []; const { config } = aegis; aegis.lifeCycle.on('destroy', () => { msgs.length = 0; }); return function (msg, resolve) { msgs.push(msg); if (maxLength && msgs.length >= maxLength) { resolve(msgs.splice(0, msgs.length)); timer && clearTimeout(timer); return; } if (timer) { clearTimeout(timer); } timer = setTimeout(() => { timer = null; resolve(msgs.splice(0, msgs.length)); }, config.delay); }; };