Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 30 additions & 29 deletions src/core/classes/Iterable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { IterableAuthResponseResult } from '../enums/IterableAuthResponseResult'
import { IterableEventName } from '../enums/IterableEventName';

// Add this type-only import to avoid circular dependency
import type { IterableInAppManager } from '../../inApp/classes/IterableInAppManager';
import { IterableInAppManager } from '../../inApp/classes/IterableInAppManager';

import { IterableAction } from './IterableAction';
import { IterableActionContext } from './IterableActionContext';
Expand All @@ -23,6 +23,7 @@ import type { IterableCommerceItem } from './IterableCommerceItem';
import { IterableConfig } from './IterableConfig';
import { IterableLogger } from './IterableLogger';
import type { IterableAuthFailure } from '../types/IterableAuthFailure';
import { IterableAuthManager } from './IterableAuthManager';

const RNEventEmitter = new NativeEventEmitter(RNIterableAPI);

Expand Down Expand Up @@ -56,6 +57,19 @@ export class Iterable {
*/
static savedConfig: IterableConfig = new IterableConfig();

/**
* Auth manager for the Iterable SDK
*
* This property is set to an instance of provides access to authentication functionality including
* pausing authentication retries.
*
* @example
* ```typescript
* Iterable.authManager.pauseAuthRetries(true);
* ```
*/
static authManager: IterableAuthManager | undefined;

/**
* In-app message manager for the current user.
*
Expand All @@ -73,21 +87,7 @@ export class Iterable {
* Iterable.inAppManager.showMessage(message, true);
* ```
*/
static get inAppManager() {
// Lazy initialization to avoid circular dependency
if (!this._inAppManager) {
// Import here to avoid circular dependency at module level

const {
IterableInAppManager,
// eslint-disable-next-line @typescript-eslint/no-var-requires,@typescript-eslint/no-require-imports
} = require('../../inApp/classes/IterableInAppManager');
this._inAppManager = new IterableInAppManager();
}
return this._inAppManager;
}

private static _inAppManager: IterableInAppManager | undefined;
static inAppManager: IterableInAppManager | undefined;

/**
* Initializes the Iterable React Native SDK in your app's Javascript or Typescript code.
Expand Down Expand Up @@ -124,13 +124,7 @@ export class Iterable {
apiKey: string,
config: IterableConfig = new IterableConfig()
): Promise<boolean> {
Iterable.savedConfig = config;

Iterable.logger = new IterableLogger(Iterable.savedConfig);

Iterable?.logger?.log('initialize: ' + apiKey);

this.setupEventHandlers();
this.setupIterable(config);

const version = this.getVersionFromPackageJson();

Expand All @@ -148,13 +142,8 @@ export class Iterable {
config: IterableConfig = new IterableConfig(),
apiEndPoint: string
): Promise<boolean> {
Iterable.savedConfig = config;

Iterable.logger = new IterableLogger(Iterable.savedConfig);
this.setupIterable(config);

Iterable?.logger?.log('initialize2: ' + apiKey);

this.setupEventHandlers();
const version = this.getVersionFromPackageJson();

return RNIterableAPI.initialize2WithApiKey(
Expand All @@ -165,6 +154,18 @@ export class Iterable {
);
}

private static setupIterable(config: IterableConfig = new IterableConfig()) {
Iterable.savedConfig = config;

const logger = new IterableLogger(Iterable.savedConfig);

Iterable.logger = logger;
Iterable.authManager = new IterableAuthManager(logger);
Iterable.inAppManager = new IterableInAppManager(logger);

this.setupEventHandlers();
}

/**
* Associate the current user with the passed in email parameter.
*
Expand Down
29 changes: 29 additions & 0 deletions src/core/classes/IterableAuthManager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import RNIterableAPI from '../../api';
import { IterableConfig } from './IterableConfig';
import { IterableLogger } from './IterableLogger';

export class IterableAuthManager {
/**
* The logger for the Iterable SDK.
*/
private _logger: IterableLogger = new IterableLogger(new IterableConfig());

constructor(logger: IterableLogger) {
this._logger = logger;
}

/**
* Pause the authentication retry mechanism.
*
* @param pauseRetry - Whether to pause the authentication retry mechanism
*
* @example
* ```typescript
* Iterable.pauseAuthRetries(true);
* ```
*/
pauseAuthRetries(pauseRetry: boolean) {
this._logger?.log('pauseAuthRetries');
RNIterableAPI.pauseAuthRetries(pauseRetry);
}
}
38 changes: 27 additions & 11 deletions src/inApp/classes/IterableInAppManager.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { RNIterableAPI } from '../../api';
import { Iterable } from '../../core/classes/Iterable';
import { IterableConfig } from '../../core/classes/IterableConfig';
import { IterableLogger } from '../../core/classes/IterableLogger';
import type {
IterableInAppDeleteSource,
IterableInAppLocation,
Expand All @@ -16,6 +17,15 @@ import { IterableInAppMessage } from './IterableInAppMessage';
* The `inAppManager` property of an `Iterable` instance is set to an instance of this class.
*/
export class IterableInAppManager {
/**
* The logger for the Iterable SDK.
*/
private _logger: IterableLogger = new IterableLogger(new IterableConfig());

constructor(logger: IterableLogger) {
this._logger = logger;
}

/**
* Retrieve the current user's list of in-app messages stored in the local queue.
*
Expand All @@ -33,9 +43,11 @@ export class IterableInAppManager {
* @returns A Promise that resolves to an array of in-app messages.
*/
getMessages(): Promise<IterableInAppMessage[]> {
Iterable?.logger?.log('InAppManager.getMessages');
this._logger?.log('InAppManager.getMessages');

return RNIterableAPI.getInAppMessages() as unknown as Promise<IterableInAppMessage[]>;
return RNIterableAPI.getInAppMessages() as unknown as Promise<
IterableInAppMessage[]
>;
}

/**
Expand All @@ -56,9 +68,11 @@ export class IterableInAppManager {
* @returns A Promise that resolves to an array of messages marked as `saveToInbox`.
*/
getInboxMessages(): Promise<IterableInAppMessage[]> {
Iterable?.logger?.log('InAppManager.getInboxMessages');
this?._logger?.log('InAppManager.getInboxMessages');

return RNIterableAPI.getInboxMessages() as unknown as Promise<IterableInAppMessage[]>;
return RNIterableAPI.getInboxMessages() as unknown as Promise<
IterableInAppMessage[]
>;
}

/**
Expand All @@ -83,7 +97,7 @@ export class IterableInAppManager {
message: IterableInAppMessage,
consume: boolean
): Promise<string | null> {
Iterable?.logger?.log('InAppManager.show');
this?._logger?.log('InAppManager.show');

return RNIterableAPI.showMessage(message.messageId, consume);
}
Expand Down Expand Up @@ -111,7 +125,7 @@ export class IterableInAppManager {
location: IterableInAppLocation,
source: IterableInAppDeleteSource
): void {
Iterable?.logger?.log('InAppManager.remove');
this?._logger?.log('InAppManager.remove');

return RNIterableAPI.removeMessage(message.messageId, location, source);
}
Expand All @@ -128,7 +142,7 @@ export class IterableInAppManager {
* ```
*/
setReadForMessage(message: IterableInAppMessage, read: boolean) {
Iterable?.logger?.log('InAppManager.setRead');
this?._logger?.log('InAppManager.setRead');

RNIterableAPI.setReadForMessage(message.messageId, read);
}
Expand All @@ -148,9 +162,11 @@ export class IterableInAppManager {
getHtmlContentForMessage(
message: IterableInAppMessage
): Promise<IterableHtmlInAppContent> {
Iterable?.logger?.log('InAppManager.getHtmlContentForMessage');
this?._logger?.log('InAppManager.getHtmlContentForMessage');

return RNIterableAPI.getHtmlInAppContentForMessage(message.messageId) as unknown as Promise<IterableHtmlInAppContent>;
return RNIterableAPI.getHtmlInAppContentForMessage(
message.messageId
) as unknown as Promise<IterableHtmlInAppContent>;
}

/**
Expand All @@ -168,7 +184,7 @@ export class IterableInAppManager {
* ```
*/
setAutoDisplayPaused(paused: boolean) {
Iterable?.logger?.log('InAppManager.setAutoDisplayPaused');
this?._logger?.log('InAppManager.setAutoDisplayPaused');

RNIterableAPI.setAutoDisplayPaused(paused);
}
Expand Down