UNPKG

@ua/react-native-airship

Version:
103 lines (96 loc) 3.06 kB
"use strict"; import { Subscription } from "./UAEventEmitter.js"; /** * 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(); } /** * 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