UNPKG

@atlaskit/editor-plugin-engagement-platform

Version:

Engagement platform plugin for @atlaskit/editor-core“

60 lines (57 loc) 3.06 kB
import { ACTION, ACTION_SUBJECT, EVENT_TYPE } from '@atlaskit/editor-common/analytics'; import { engagementPlatformPmPluginCommand } from '../pm-plugins/engagementPlatformPmPlugin/engagementPlatformPmPluginCommand'; export function startMessage(api, coordinationClient) { return function (messageId, variationId) { var _api$engagementPlatfo, _api$engagementPlatfo2, _api$engagementPlatfo3, _api$engagementPlatfo4; if (!api) { return Promise.resolve(false); } // If the message is already started (even with `false` state), return the state 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 messageState = messageStates[messageId]; if (messageState !== undefined) { return Promise.resolve(messageState); } // If there is already a start message promise, return it to prevent multiple start requests var startMessagePromises = (_api$engagementPlatfo3 = (_api$engagementPlatfo4 = api.engagementPlatform.sharedState.currentState()) === null || _api$engagementPlatfo4 === void 0 ? void 0 : _api$engagementPlatfo4.startMessagePromises) !== null && _api$engagementPlatfo3 !== void 0 ? _api$engagementPlatfo3 : {}; var startMessagePromise = startMessagePromises[messageId]; if (startMessagePromise) { return startMessagePromise; } var newStartedMessagePromise = coordinationClient.start(messageId, variationId).then(function (isStarted) { // Update the message state in the shared state api.core.actions.execute(engagementPlatformPmPluginCommand({ type: 'setMessageState', messageId: messageId, state: isStarted })); return isStarted; }).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: 'setStartMessagePromise', messageId: messageId, promise: undefined })); }); // Store the promise in the shared state to prevent multiple start requests for the same message api.core.actions.execute(engagementPlatformPmPluginCommand({ type: 'setStartMessagePromise', messageId: messageId, promise: newStartedMessagePromise })); return newStartedMessagePromise; }; }