UNPKG

@atlaskit/editor-plugin-engagement-platform

Version:

Engagement platform plugin for @atlaskit/editor-core“

66 lines (62 loc) 3.3 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.startMessage = startMessage; var _analytics = require("@atlaskit/editor-common/analytics"); var _engagementPlatformPmPluginCommand = require("../pm-plugins/engagementPlatformPmPlugin/engagementPlatformPmPluginCommand"); 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((0, _engagementPlatformPmPluginCommand.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: _analytics.ACTION.ERRORED, actionSubject: _analytics.ACTION_SUBJECT.ENGAGEMENT_PLATFORM, eventType: _analytics.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((0, _engagementPlatformPmPluginCommand.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((0, _engagementPlatformPmPluginCommand.engagementPlatformPmPluginCommand)({ type: 'setStartMessagePromise', messageId: messageId, promise: newStartedMessagePromise })); return newStartedMessagePromise; }; }