@theoplayer/react-native-engage
Version:
Engage connector for @theoplayer/react-native
103 lines (101 loc) • 3.75 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.DefaultEngageClient = void 0;
var _reactNativeEngage = require("@theoplayer/react-native-engage");
var _reactNative = require("react-native");
var _DefaultEventDispatcher = require("./event/DefaultEventDispatcher");
var _EngageEvent = require("../api/EngageEvent");
var _Storage = require("./storage/Storage");
const TAG = "EngageClient";
class DefaultEngageClient extends _DefaultEventDispatcher.DefaultEventDispatcher {
_clusters = new Map();
constructor(configuration, onReady) {
super();
if (configuration.debug) {
console.debug(TAG, 'Creating Engage client');
}
this._onReady = onReady;
this._configuration = configuration;
this._emitter = new _reactNative.NativeEventEmitter(_reactNative.NativeModules.EngageModule);
this._emitter.addListener('onEngageReady', this.onEngageReady);
this._emitter.addListener('onEngageError', this.onError);
_reactNative.NativeModules.EngageModule.initialize(configuration);
}
getCluster(type, config) {
const cluster = this._clusters.get(type);
if (config && cluster) {
cluster.config = config;
}
return cluster;
}
clearCluster(type) {
this._clusters.get(type)?.removeAllEntities();
}
setSignInEntity(entity) {
if (entity) {
if (this._configuration.debug) {
console.debug(TAG, `Setting SignIn entity ${JSON.stringify(entity, null, 2)}`);
}
_reactNative.NativeModules.EngageModule.publishSignInEntity(entity);
} else {
if (this._configuration.debug) {
console.debug(TAG, `Removing SignIn entity`);
}
_reactNative.NativeModules.EngageModule.deleteSignInEntity();
}
}
setSubscription(accountProfile, subscription) {
if (this._configuration.debug) {
console.debug(TAG, `Setting Subscription ${JSON.stringify(accountProfile, null, 2)} - ${JSON.stringify(subscription, null, 2)}`);
}
_reactNative.NativeModules.EngageModule.publishSubscription(accountProfile, subscription);
}
/**
* Request to publish the given cluster.
*
* @param cluster either a "Continuation" (or "Continue Watching"), "Featured", or "Recommendation" cluster.
*
* @remarks
* <br/> - Any previously set list is replaced, there is no need to remove the old list first.
* <br/> - The content can be unpersonalized if 'guest' sessions are supported.
*/
publish(cluster) {
if (this._configuration.debug) {
console.debug(TAG, `Requesting to publish cluster "${cluster.type}}" with ${cluster.entities.length} entities`);
}
// Publish
_reactNative.NativeModules.EngageModule.publishCluster(Object.freeze({
...cluster,
// Don't pass the client
engageClient: undefined
}));
// Persistently store
void (0, _Storage.storeCluster)(cluster);
}
onEngageReady = async event => {
const clusterPromises = Object.keys(_reactNativeEngage.ClusterType).map(async key => {
const type = _reactNativeEngage.ClusterType[key];
const cluster = await (0, _Storage.readCluster)(this, type);
this._clusters.set(type, cluster);
});
await Promise.all(clusterPromises);
if (this._configuration.debug) {
console.debug(TAG, 'Engage ready');
}
this._onReady(this);
};
onError = event => {
if (this._configuration.debug) {
console.error(TAG, event.message);
}
this._eventDispatcher.dispatchEvent(new _EngageEvent.EngageErrorEvent(event.message));
};
destroy() {
this._emitter.removeAllListeners('onError');
_reactNative.NativeModules.EngageModule.destroy();
}
}
exports.DefaultEngageClient = DefaultEngageClient;
//# sourceMappingURL=DefaultEngageClient.js.map