function E() { // Keep this empty so it's easier to inherit from // (via https://github.com/lipsmack from https://github.com/scottcorgan/tiny-emitter/issues/3) } E.prototype = { on (name, callback, ctx) { let e = this.e || (this.e = {}); (e[name] || (e[name] = [])).push({ fn: callback, ctx, }); return this; }, once (name, callback, ctx) { let self = this; function listener() { self.off(name, listener); callback.apply(ctx, arguments); } listener._ = callback; return this.on(name, listener, ctx); }, emit (name) { let data = [].slice.call(arguments, 1); let evtArr = ((this.e || (this.e = {}))[name] || []).slice(); let i = 0; let len = evtArr.length; for (i; i < len; i++) { evtArr[i].fn.apply(evtArr[i].ctx, data); } return this; }, off (name, callback) { let e = this.e || (this.e = {}); let evts = e[name]; let liveEvents = []; if (evts && callback) { for (let i = 0, len = evts.length; i < len; i++) { if (evts[i].fn !== callback && evts[i].fn._ !== callback) liveEvents.push(evts[i]); } } // Remove event from queue to prevent memory leak // Suggested by https://github.com/lazd // Ref: https://github.com/scottcorgan/tiny-emitter/commit/c6ebfaa9bc973b33d110a84a307742b7cf94c953#commitcomment-5024910 liveEvents.length ? (e[name] = liveEvents) : delete e[name]; return this; }, }; export default E;