@atlaskit/editor-core
Version:
A package contains Atlassian editor core functionality
45 lines • 1.52 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var EventDispatcher = (function () {
function EventDispatcher() {
this.listeners = {};
}
EventDispatcher.prototype.on = function (event, cb) {
if (!this.listeners[event]) {
this.listeners[event] = [];
}
this.listeners[event].push(cb);
};
EventDispatcher.prototype.off = function (event, cb) {
if (!this.listeners[event]) {
return;
}
this.listeners[event] = this.listeners[event].filter(function (callback) { return callback !== cb; });
};
EventDispatcher.prototype.emit = function (event, data) {
if (!this.listeners[event]) {
return;
}
this.listeners[event].forEach(function (cb) { return cb(data); });
};
EventDispatcher.prototype.destroy = function () {
this.listeners = {};
};
return EventDispatcher;
}());
exports.EventDispatcher = EventDispatcher;
/**
* Creates a dispatch function that can be called inside ProseMirror Plugin
* to notify listeners about that plugin's state change.
*/
function createDispatch(eventDispatcher) {
return function (eventName, data) {
if (!eventName) {
throw new Error('event name is required!');
}
var event = typeof eventName === 'string' ? eventName : eventName.key;
eventDispatcher.emit(event, data);
};
}
exports.createDispatch = createDispatch;
//# sourceMappingURL=index.js.map