UNPKG

@atlaskit/editor-plugin-engagement-platform

Version:

Engagement platform plugin for @atlaskit/editor-core“

62 lines (59 loc) 2.96 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 messageId => { var _api$engagementPlatfo, _api$engagementPlatfo2, _api$engagementPlatfo3, _api$engagementPlatfo4; if (!api) { return Promise.resolve(false); } // If the message is already stopped, return `true` const 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 : {}; const 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 const 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 : {}; const stopMessagePromise = stopMessagePromises[messageId]; if (stopMessagePromise) { return stopMessagePromise; } const newStopMessagePromise = coordinationClient.stop(messageId).then(isStopped => { // Update the message state in the shared state if (isStopped) { api.core.actions.execute(engagementPlatformPmPluginCommand({ type: 'setMessageState', messageId, state: false })); } return isStopped; }).catch(error => { var _api$analytics; api === null || api === void 0 ? void 0 : (_api$analytics = api.analytics) === null || _api$analytics === void 0 ? 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 : `${error}`, errorStack: error instanceof Error ? error.stack : undefined } }); return false; }).finally(() => { // Remove the promise from the state after it has been resolved api.core.actions.execute(engagementPlatformPmPluginCommand({ type: 'setStopMessagePromise', 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, promise: newStopMessagePromise })); return newStopMessagePromise; }; }