Skip to content

Commit 6325186

Browse files
authored
Merge pull request #699 from Iterable/MOB-12039-rn-add-in-app-manager-back-onto-iterable-object
[MOB-12039] rn-add-in-app-manager-back-onto-iterable-object
2 parents 1873f0f + 6deeae3 commit 6325186

File tree

3 files changed

+40
-14
lines changed

3 files changed

+40
-14
lines changed

src/__tests__/IterableInApp.test.ts

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { NativeEventEmitter } from 'react-native';
22

33
import { IterableLogger } from '../core';
4-
import { IterableInAppManager } from '../inApp';
54

65
import { MockRNIterableAPI } from '../__mocks__/MockRNIterableAPI';
76

@@ -202,10 +201,8 @@ describe('Iterable In App', () => {
202201

203202
// WHEN the simulated local queue is set to the in-app messages
204203
MockRNIterableAPI.setMessages(messages);
205-
const inAppManager = new IterableInAppManager();
206-
207-
// THEN Iterable,inAppManager.getMessages returns the list of in-app messages
208-
return await inAppManager.getMessages().then((messagesObtained) => {
204+
// THEN Iterable.inAppManager.getMessages returns the list of in-app messages
205+
return await Iterable.inAppManager?.getMessages().then((messagesObtained) => {
209206
expect(messagesObtained).toEqual(messages);
210207
});
211208
});
@@ -224,9 +221,8 @@ describe('Iterable In App', () => {
224221

225222
// WHEN the simulated clicked url is set to the clicked url
226223
MockRNIterableAPI.setClickedUrl(clickedUrl);
227-
const inAppManager = new IterableInAppManager();
228224
// THEN Iterable,inAppManager.showMessage returns the simulated clicked url
229-
return await inAppManager.showMessage(message, consume).then((url) => {
225+
return await Iterable.inAppManager?.showMessage(message, consume).then((url) => {
230226
expect(url).toEqual(clickedUrl);
231227
});
232228
});
@@ -242,10 +238,9 @@ describe('Iterable In App', () => {
242238
const location: IterableInAppLocation = IterableInAppLocation.inApp;
243239
const source: IterableInAppDeleteSource =
244240
IterableInAppDeleteSource.deleteButton;
245-
const inAppManager = new IterableInAppManager();
246241

247242
// WHEN Iterable.inAppManager.removeMessage is called
248-
inAppManager.removeMessage(message, location, source);
243+
Iterable.inAppManager?.removeMessage(message, location, source);
249244

250245
// THEN corresponding method is called on MockIterableAPI with appropriate parameters
251246
expect(MockRNIterableAPI.removeMessage).toBeCalledWith(
@@ -266,8 +261,7 @@ describe('Iterable In App', () => {
266261
const read: boolean = true;
267262

268263
// WHEN Iterable.inAppManager.setReadForMessage is called
269-
const inAppManager = new IterableInAppManager();
270-
inAppManager.setReadForMessage(message, read);
264+
Iterable.inAppManager?.setReadForMessage(message, read);
271265

272266
// THEN corresponding method is called on MockRNIterableAPI with appropriate parameters
273267
expect(MockRNIterableAPI.setReadForMessage).toBeCalledWith(
@@ -281,8 +275,7 @@ describe('Iterable In App', () => {
281275
const paused: boolean = true;
282276

283277
// WHEN Iterable.inAppManager.setAutoDisplayPaused is called
284-
const inAppManager = new IterableInAppManager();
285-
inAppManager.setAutoDisplayPaused(paused);
278+
Iterable.inAppManager?.setAutoDisplayPaused(paused);
286279

287280
// THEN corresponding method is called on MockRNIterableAPI with appropriate parameters
288281
expect(MockRNIterableAPI.setAutoDisplayPaused).toBeCalledWith(paused);

src/core/classes/Iterable.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ import { IterableInAppDeleteSource } from '../../inApp/enums/IterableInAppDelete
1515
import { IterableInAppLocation } from '../../inApp/enums/IterableInAppLocation';
1616
import { IterableAuthResponseResult, IterableEventName } from '../enums';
1717

18+
// Add this type-only import to avoid circular dependency
19+
import type { IterableInAppManager } from '../../inApp/classes/IterableInAppManager';
20+
1821
import { IterableAction } from './IterableAction';
1922
import { IterableActionContext } from './IterableActionContext';
2023
import { IterableAttributionInfo } from './IterableAttributionInfo';
@@ -56,6 +59,36 @@ export class Iterable {
5659
*/
5760
static savedConfig: IterableConfig = new IterableConfig();
5861

62+
/**
63+
* In-app message manager for the current user.
64+
*
65+
* This property provides access to in-app message functionality including
66+
* retrieving messages, displaying messages, removing messages, and more.
67+
*
68+
* @example
69+
* ```typescript
70+
* // Get all in-app messages
71+
* Iterable.inAppManager.getMessages().then(messages => {
72+
* console.log('Messages:', messages);
73+
* });
74+
*
75+
* // Show a specific message
76+
* Iterable.inAppManager.showMessage(message, true);
77+
* ```
78+
*/
79+
static get inAppManager() {
80+
// Lazy initialization to avoid circular dependency
81+
if (!this._inAppManager) {
82+
// Import here to avoid circular dependency at module level
83+
// eslint-disable-next-line @typescript-eslint/no-var-requires,@typescript-eslint/no-require-imports
84+
const { IterableInAppManager } = require('../../inApp/classes/IterableInAppManager');
85+
this._inAppManager = new IterableInAppManager();
86+
}
87+
return this._inAppManager;
88+
}
89+
90+
private static _inAppManager: IterableInAppManager | undefined;
91+
5992
/**
6093
* Initializes the Iterable React Native SDK in your app's Javascript or Typescript code.
6194
*

src/inApp/classes/IterableInAppManager.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ export class IterableInAppManager {
7777
* });
7878
* ```
7979
*
80-
* @param message - The message to show (an {@link_IterableInAppMessage} object)
80+
* @param message - The message to show (an {@link IterableInAppMessage} object)
8181
* @param consume - Whether or not the message should be consumed from the user's message queue after being shown. This should be defaulted to true.
8282
*
8383
* @returns A Promise that resolves to the URL of the button or link the user tapped to close the in-app message.

0 commit comments

Comments
 (0)