UNPKG

bubbly

Version:

Shorthand for event handling

52 lines (40 loc) 1.09 kB
/*global CustomEvent */ "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.dispatch = dispatch; exports.bubble = bubble; exports.on = on; exports.off = off; exports.once = once; function dispatch(type, detail) { var opts = arguments.length <= 2 || arguments[2] === undefined ? {} : arguments[2]; var init = Object.assign({ detail: detail }, opts); var event = new CustomEvent(type, init); this.dispatchEvent(event); } function bubble(type, detail) { return dispatch.call(this, type, detail, { bubbles: true }); } function on(type, cb) { this.addEventListener(type, cb); } function off(type, cb) { this.removeEventListener(type, cb); } function noop() {} function once(type) { var _this = this; var cb = arguments.length <= 1 || arguments[1] === undefined ? noop : arguments[1]; return new Promise(function (resolve) { var eventHandler = function eventHandler(event) { off.call(_this, type, eventHandler); resolve(event); return cb(event); }; on.call(_this, type, eventHandler); }); }