@signiant/media-shuttle-sdk-base
Version:
The base parent sdk behind other media shuttle sdks (e.g. media-shuttle-sdk)
197 lines (196 loc) • 9.39 kB
JavaScript
/* eslint-disable import/first */
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
/**
* An aggregate channel that serves as a container for sub-channels and collapses to the first subchannel that becomes active
* At which point this aggregate acts as a simple pass-through decorator to the sub-channel
*/
import MessageChannelProvider from './MessageChannelProvider';
import LogManager from '../../system/LogManager';
import * as constants from '../constants';
import { cloneDeep } from 'lodash';
import ErrorMessages from '../../../external/common/ErrorMessages';
import { CodedError, ErrorCode } from '../../../external';
var AggregateChannelProvider = /** @class */ (function (_super) {
__extends(AggregateChannelProvider, _super);
function AggregateChannelProvider(params, encryptionInfo) {
var _this = _super.call(this, params) || this;
_this._providers = [];
_this._foundActiveProvider = false;
_this._encryptionInfo = encryptionInfo;
_this._providersParams = [];
return _this;
}
AggregateChannelProvider.prototype.addProvider = function (provider) {
this._providers.push(provider);
this._providersParams.push(provider.getChannelParams());
};
AggregateChannelProvider.prototype.initialize = function () {
this._providers.forEach(function (provider) {
provider.initialize(provider.notifyMessage.bind(provider));
});
};
AggregateChannelProvider.prototype.setSessionActiveEventHandler = function (handler) {
var _this = this;
var webSocketProvider = this.getWebSocketProvider(this._providers);
this._providers.forEach(function (provider) {
provider.setSessionActiveEventHandler(function (activeProvider, data) {
if (!this._foundActiveProvider) {
if (activeProvider.getProviderInfo() === 'pubnub') {
// Signiant.Mst.googleAnalytics.sendEvent(
// Signiant.Mst.GoogleAnalytics.Constants.Category.transferApi,
// Signiant.Mst.GoogleAnalytics.Constants.Action.pubnubConnectionApp
// );
if (webSocketProvider) {
this.setSessionWebSocketHandler(webSocketProvider)
.then(function (webSocketActiveProvider) {
this.postSetSessionActiveHandler(webSocketActiveProvider.activeProvider, webSocketActiveProvider.data, handler);
// Signiant.Mst.googleAnalytics.sendEvent(
// Signiant.Mst.GoogleAnalytics.Constants.Category.transferApi,
// Signiant.Mst.GoogleAnalytics.Constants.Action.pubnubToWebsocketSwitchApp
// );
}.bind(this))
.catch(function () {
this.postSetSessionActiveHandler(activeProvider, data, handler);
}.bind(this));
}
else {
this.postSetSessionActiveHandler(activeProvider, data, handler);
}
}
else {
this.postSetSessionActiveHandler(activeProvider, data, handler);
}
}
}.bind(_this, provider));
});
};
AggregateChannelProvider.prototype.getWebSocketProvider = function (providers) {
var webSocketProvider = providers.filter(function (provider) { return provider.getChannelParams().provider === 'websocket'; });
return webSocketProvider[0];
};
AggregateChannelProvider.prototype.setSessionWebSocketHandler = function (webSocketProvider) {
webSocketProvider.initialize(webSocketProvider.notifyMessage);
return new Promise(function (resolve, reject) {
var connectionTimeoutMillis = 1000;
var timeout = setTimeout(function () {
var message = ErrorMessages.generatedErrorMessages[ErrorCode.WEBSOCKET_CONNECTION_TIMEOUT](connectionTimeoutMillis);
LogManager.warn(message);
reject(new CodedError({ code: ErrorCode.WEBSOCKET_CONNECTION_TIMEOUT, message: message }));
}, connectionTimeoutMillis);
webSocketProvider.setSessionActiveEventHandler(function (activeProvider, data) {
LogManager.info('WebSocket is active provider.');
clearTimeout(timeout);
resolve({
activeProvider: activeProvider,
data: data,
});
}.bind(this, webSocketProvider));
});
};
AggregateChannelProvider.prototype.postSetSessionActiveHandler = function (activeProvider, data, handler) {
LogManager.info('Active communication method: ', activeProvider.getProviderInfo());
this._foundActiveProvider = true;
this.removeOtherProviders(activeProvider);
this.upgradeToEncryptedChannel(data);
handler(data);
};
AggregateChannelProvider.prototype.setEventHandler = function (originatorId, event, handler) {
this._providers.forEach(function (provider) {
provider.setEventHandler(originatorId, event, handler);
});
};
AggregateChannelProvider.prototype.removeOtherProviders = function (activeProvider) {
this._providers.forEach(function (provider) {
if (provider !== activeProvider) {
provider.reset();
}
});
this._providers = [activeProvider];
};
AggregateChannelProvider.prototype.removeEventHandlers = function (originatorId) {
var returnValue = false;
this._providers.forEach(function (provider) {
returnValue = returnValue || provider.removeEventHandlers(originatorId);
});
return returnValue;
};
AggregateChannelProvider.prototype.onMessage = function (message) {
LogManager.error('Error onMessage: ' + message);
};
AggregateChannelProvider.prototype.notifyMessage = function (message) {
LogManager.error('Error notifyMessage:', message);
};
AggregateChannelProvider.prototype.publish = function (message) {
this._providers.forEach(function (provider) {
provider.publish(message);
});
};
AggregateChannelProvider.prototype.reset = function () {
this._providers.forEach(function (provider) {
provider.reset();
});
};
AggregateChannelProvider.prototype.getProviderInfo = function () {
if (this._foundActiveProvider) {
return this._providers[0].getProviderInfo();
}
};
Object.defineProperty(AggregateChannelProvider.prototype, "clientInfo", {
get: function () {
if (this._foundActiveProvider) {
return this._providers[0].clientInfo;
}
},
enumerable: false,
configurable: true
});
AggregateChannelProvider.prototype.unsubscribeFromChannels = function () {
this._providers.forEach(function (provider) {
provider.unsubscribeFromChannels();
});
};
AggregateChannelProvider.prototype.getChannelParams = function () {
var param = this._channelParams;
if (this._providersParams.length > 0) {
param.providers = cloneDeep(this._providersParams);
}
param.encryptionInfo = this._encryptionInfo;
param.sessionid = this.sessionId;
return param;
};
AggregateChannelProvider.prototype.upgradeToEncryptedChannel = function (data) {
if (data.app_encryption === constants.preferredEncryptionMode) {
LogManager.info('Using encryption: ' + data.app_encryption + ' to send messages to Signiant App');
this.updateSettings({ encrypt: true });
}
else {
LogManager.warn('SDK and App do not support same encryption. Sending plain messages to Signiant App');
}
};
AggregateChannelProvider.prototype.updateSettings = function (settings) {
this._providers.forEach(function (provider) {
provider.updateSettings(settings);
});
};
AggregateChannelProvider.prototype.setErrorCallback = function (callback) {
this._providers.forEach(function (provider) {
provider.setErrorCallback(callback);
});
};
return AggregateChannelProvider;
}(MessageChannelProvider));
export default AggregateChannelProvider;