UNPKG

@ff0000-ad-tech/ad-velvet

Version:

[![npm (tag)](https://img.shields.io/npm/v/@ff0000-ad-tech%2Fad-velvet.svg?style=flat-square)](https://www.npmjs.com/package/@ff0000-ad-tech%2Fad-velvet) [![GitHub issues](https://img.shields.io/github/issues/ff0000-ad-tech/ad-velvet.svg?style=flat-square

33 lines (29 loc) 568 B
let _eventPool = {} export function add(type, handler) { if (!_eventPool[type]) { _eventPool[type] = [] } const arr = _eventPool[type] for (let i = 0, k = arr.length; i < k; i++) { if (arr[i] === handler) return } arr.push(handler) } export function remove(type, handler) { const arr = _eventPool[type] if (arr) { for (let i = 0, k = arr.length; i < k; i++) { if (arr[i] === handler) { arr.splice(i, 1) break } } } } export function dispatch(type) { if (_eventPool[type]) { _eventPool[type].map(handler => { handler() }) } }