UNPKG

@atlaskit/editor-plugin-engagement-platform

Version:

Engagement platform plugin for @atlaskit/editor-core“

62 lines (59 loc) 3 kB
import { ACTION, ACTION_SUBJECT, EVENT_TYPE } from '@atlaskit/editor-common/analytics'; import { engagementPlatformPmPluginCommand } from '../pm-plugins/engagementPlatformPmPlugin/engagementPlatformPmPluginCommand'; export function stopMessage(api, coordinationClient) { return function (messageId) { var _api$engagementPlatfo, _api$engagementPlatfo2, _api$engagementPlatfo3, _api$engagementPlatfo4; if (!api) { return Promise.resolve(false); } // If the message is already stopped, return `true` var messageStates = (_api$engagementPlatfo = (_api$engagementPlatfo2 = api.engagementPlatform.sharedState.currentState()) === null || _api$engagementPlatfo2 === void 0 ? void 0 : _api$engagementPlatfo2.messageStates) !== null && _api$engagementPlatfo !== void 0 ? _api$engagementPlatfo : {}; var isActive = messageStates[messageId]; if (isActive === false) { return Promise.resolve(true); } // If there is already a stop message promise, return it to prevent multiple stop requests var stopMessagePromises = (_api$engagementPlatfo3 = (_api$engagementPlatfo4 = api.engagementPlatform.sharedState.currentState()) === null || _api$engagementPlatfo4 === void 0 ? void 0 : _api$engagementPlatfo4.stopMessagePromises) !== null && _api$engagementPlatfo3 !== void 0 ? _api$engagementPlatfo3 : {}; var stopMessagePromise = stopMessagePromises[messageId]; if (stopMessagePromise) { return stopMessagePromise; } var newStopMessagePromise = coordinationClient.stop(messageId).then(function (isStopped) { // Update the message state in the shared state if (isStopped) { api.core.actions.execute(engagementPlatformPmPluginCommand({ type: 'setMessageState', messageId: messageId, state: false })); } return isStopped; }).catch(function (error) { var _api$analytics; api === null || api === void 0 || (_api$analytics = api.analytics) === null || _api$analytics === void 0 || _api$analytics.actions.fireAnalyticsEvent({ action: ACTION.ERRORED, actionSubject: ACTION_SUBJECT.ENGAGEMENT_PLATFORM, eventType: EVENT_TYPE.OPERATIONAL, attributes: { error: error instanceof Error ? error.message : "".concat(error), errorStack: error instanceof Error ? error.stack : undefined } }); return false; }).finally(function () { // Remove the promise from the state after it has been resolved api.core.actions.execute(engagementPlatformPmPluginCommand({ type: 'setStopMessagePromise', messageId: messageId, promise: undefined })); }); // Store the promise in the shared state to prevent multiple stop requests for the same message api.core.actions.execute(engagementPlatformPmPluginCommand({ type: 'setStopMessagePromise', messageId: messageId, promise: newStopMessagePromise })); return newStopMessagePromise; }; }