@ua/react-native-airship
Version:
Airship plugin for React Native apps.
136 lines (125 loc) • 4.26 kB
JavaScript
"use strict";
import { Subscription } from "./UAEventEmitter.js";
/**
* Info for a pending embedded content instance.
*/
/**
* Airship InApp Experiences.
*/
export class AirshipInApp {
pendingEmbedded = new Map();
pendingEmbeddedListeners = new Map();
constructor(module, eventEmitter) {
this.module = module;
this.eventEmitter = eventEmitter;
this.eventEmitter.addListener("com.airship.pending_embedded_updated", event => {
let pending = event["pending"];
this.pendingEmbedded = pending.reduce((map, entry) => {
var embeddedId = entry.embeddedId;
if (!map.has(embeddedId)) {
map.set(embeddedId, [entry]);
} else {
map.get(embeddedId)?.push(entry);
}
return map;
}, new Map());
this.pendingEmbeddedListeners.forEach((listeners, embeddedId) => {
let pending = this.pendingEmbedded.get(embeddedId);
listeners.forEach(listener => {
listener(pending ?? []);
});
});
});
module.inAppResendPendingEmbeddedEvent();
}
/**
* Gets the pending embedded content info for the given embedded ID.
* @param embeddedId The embedded ID to check.
* @returns The pending embedded content info, including instance ID and extras.
*/
getPendingEmbedded(embeddedId) {
return this.pendingEmbedded.get(embeddedId) ?? [];
}
/**
* Adds a listener that is called whenever the list of pending embedded
* content changes for the given embedded ID.
* @param embeddedId The embedded ID to watch.
* @param listener The listener, called with the current pending list.
* @returns A subscription that can be used to cancel the listener.
*/
addPendingEmbeddedListener(embeddedId, listener) {
listener(this.getPendingEmbedded(embeddedId));
if (!this.pendingEmbeddedListeners.has(embeddedId)) {
this.pendingEmbeddedListeners.set(embeddedId, [listener]);
} else {
this.pendingEmbeddedListeners.get(embeddedId)?.push(listener);
}
return new Subscription(() => {
this.pendingEmbeddedListeners.set(embeddedId, this.pendingEmbeddedListeners.get(embeddedId)?.filter(obj => obj !== listener) ?? []);
});
}
/**
* Adds a listener to listen for if an embedded ID is ready to display or not.
* @param embeddedId The embedded ID to check.
* @param listener The listener.
* @returns A subscription that can be used to cancel the listener.
*/
addEmbeddedReadyListener(embeddedId, listener) {
var currentValue = this.isEmbeddedReady(embeddedId);
listener(currentValue);
let wrappedListener = pending => {
var nextValue = pending.length > 0;
if (currentValue != nextValue) {
listener(nextValue);
}
currentValue = nextValue;
};
if (!this.pendingEmbeddedListeners.has(embeddedId)) {
this.pendingEmbeddedListeners.set(embeddedId, [wrappedListener]);
} else {
this.pendingEmbeddedListeners.get(embeddedId)?.push(wrappedListener);
}
return new Subscription(() => {
this.pendingEmbeddedListeners.set(embeddedId, this.pendingEmbeddedListeners.get(embeddedId)?.filter(obj => obj !== wrappedListener) ?? []);
});
}
/**
* Checks if embedded message is ready for the given ID.
* @param embeddedId The embedded ID to check.
* @returns `true` if one is ready, otherwise `false`.
*/
isEmbeddedReady(embeddedId) {
return (this.pendingEmbedded.get(embeddedId)?.length ?? 0) > 0;
}
/**
* Pauses messages.
* @param paused `true` to pause, `false` to resume.
* @returns A promise.
*/
setPaused(paused) {
return this.module.inAppSetPaused(paused);
}
/**
* Checks if messages are paused.
* @returns A promise with the result.
*/
isPaused() {
return this.module.inAppIsPaused();
}
/**
* Sets the display interval for messages.
* @param milliseconds Display interval
* @returns A promise.
*/
setDisplayInterval(milliseconds) {
return this.module.inAppSetDisplayInterval(milliseconds);
}
/**
* Gets the display interval.
* @returns A promise with the result.
*/
getDisplayInterval() {
return this.module.inAppGetDisplayInterval();
}
}
//# sourceMappingURL=AirshipInApp.js.map