@theoplayer/react-native-engage
Version:
Engage connector for @theoplayer/react-native
98 lines (96 loc) • 3.44 kB
JavaScript
;
import { ClusterType } from "@theoplayer/react-native-engage";
import { NativeEventEmitter, NativeModules } from "react-native";
import { DefaultEventDispatcher } from "./event/DefaultEventDispatcher";
import { EngageErrorEvent } from "../api/EngageEvent";
import { readCluster, storeCluster } from "./storage/Storage";
const TAG = "EngageClient";
export class DefaultEngageClient extends 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 NativeEventEmitter(NativeModules.EngageModule);
this._emitter.addListener('onEngageReady', this.onEngageReady);
this._emitter.addListener('onEngageError', this.onError);
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)}`);
}
NativeModules.EngageModule.publishSignInEntity(entity);
} else {
if (this._configuration.debug) {
console.debug(TAG, `Removing SignIn entity`);
}
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)}`);
}
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
NativeModules.EngageModule.publishCluster(Object.freeze({
...cluster,
// Don't pass the client
engageClient: undefined
}));
// Persistently store
void storeCluster(cluster);
}
onEngageReady = async event => {
const clusterPromises = Object.keys(ClusterType).map(async key => {
const type = ClusterType[key];
const cluster = await 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 EngageErrorEvent(event.message));
};
destroy() {
this._emitter.removeAllListeners('onError');
NativeModules.EngageModule.destroy();
}
}
//# sourceMappingURL=DefaultEngageClient.js.map