UNPKG

@atlaskit/editor-plugin-card

Version:

Card plugin for @atlaskit/editor-core

48 lines 1.49 kB
/** * Simple mechanism to defer event related callbacks * * Probably think twice whether your event should use this instead of * editor's built-in `editor-plugin-analytics` * * Editor-Plugin-Analytics provides methods methods to dispatch events, and attach events into prosemiror transactions * However we do not have access to the smart card context in prosemirror * * We are using this queue to relay events occurring in prosemirror (which does not have access to the react context) * to be subscribed to elsewhere where the react context is available (contentComponent or otherwise) (smart card context) * in order to be able to annotate events with additional attributes to events */ export var createEventsQueue = function createEventsQueue() { var queue = []; var subscribers = new Set(); var subscribe = function subscribe(subscriber) { subscribers.add(subscriber); return function () { subscribers.delete(subscriber); }; }; var push = function push() { queue.push.apply(queue, arguments); }; var flush = function flush() { var _loop = function _loop() { var event = queue.shift(); if (event) { subscribers.forEach(function (subscriber) { subscriber(event); }); } }; while (queue.length) { _loop(); } }; var getSize = function getSize() { return queue.length; }; return { push: push, flush: flush, subscribe: subscribe, getSize: getSize }; };