UNPKG

@atlaskit/editor-common

Version:

A package that contains common classes and components for editor and renderer

75 lines (72 loc) 2.08 kB
import _classCallCheck from "@babel/runtime/helpers/classCallCheck"; import _createClass from "@babel/runtime/helpers/createClass"; import _defineProperty from "@babel/runtime/helpers/defineProperty"; // Ignored via go/ees005 // eslint-disable-next-line @typescript-eslint/no-explicit-any // Ignored via go/ees005 // eslint-disable-next-line @typescript-eslint/no-explicit-any // Ignored via go/ees005 // eslint-disable-next-line @typescript-eslint/no-explicit-any export var EventDispatcher = /*#__PURE__*/function () { function EventDispatcher() { _classCallCheck(this, EventDispatcher); _defineProperty(this, "listeners", {}); } return _createClass(EventDispatcher, [{ key: "on", value: function on(event, cb) { if (!this.listeners[event]) { this.listeners[event] = new Set(); } this.listeners[event].add(cb); } }, { key: "has", value: function has(event, cb) { if (!this.listeners[event]) { return false; } return this.listeners[event].has(cb); } }, { key: "off", value: function off(event, cb) { if (!this.listeners[event]) { return; } if (this.listeners[event].has(cb)) { this.listeners[event].delete(cb); } } }, { key: "emit", value: function emit(event, data) { if (!this.listeners[event]) { return; } this.listeners[event].forEach(function (cb) { return cb(data); }); } }, { key: "destroy", value: function destroy() { this.listeners = {}; } }]); }(); function getEventFromEventName(eventName) { return typeof eventName === 'string' ? eventName : eventName.key; } /** * Creates a dispatch function that can be called inside ProseMirror Plugin * to notify listeners about that plugin's state change. */ export function createDispatch(eventDispatcher) { return function (eventName, data) { if (!eventName) { throw new Error('event name is required!'); } eventDispatcher.emit(getEventFromEventName(eventName), data); }; }