Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(in-app-message): Normalize in-app message schema #1

Merged
merged 3 commits into from
Sep 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export default function InAppMessagingProvider<T = unknown>({
const [inAppMessages, setInAppMessages] = useState<T[]>([]);

useEffect(() => {
Notifications.setFilteredInAppMessagesHandler(setInAppMessages);
Notifications.setInAppMessagesHandler(setInAppMessages);
}, []);

const clearInAppMessages = useCallback(() => {
Expand Down
63 changes: 27 additions & 36 deletions packages/notifications/src/Notifications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import flatten from 'lodash/flatten';
import noop from 'lodash/noop';
import { AWSPinpointProvider } from './Providers';
import {
FilteredInAppMessagesHandler,
InAppMessagesHandler,
InAppMessage,
NotificationEvent,
NotificationsCategory,
Expand All @@ -38,7 +38,7 @@ const logger = new Logger('Notifications');

class NotificationsClass {
private config: Record<string, any> = {};
private filteredInAppMessagesHandler: FilteredInAppMessagesHandler = noop;
private inAppMessagesHandler: InAppMessagesHandler = noop;
private listeningForAnalyticEvents = false;
private pluggables: NotificationsProvider[] = [];
private storageSynced = false;
Expand All @@ -51,7 +51,7 @@ class NotificationsClass {

configure = ({
listenForAnalyticsEvents = true,
filteredInAppMessagesHandler,
inAppMessagesHandler,
...config
}: NotificationsConfig = {}) => {
// TODO: parseMobileHubConfig call needs to be updated with notifications config
Expand All @@ -64,8 +64,8 @@ class NotificationsClass {

logger.debug('configure Notifications', config);

this.filteredInAppMessagesHandler = this.setFilteredInAppMessagesHandler(
filteredInAppMessagesHandler
this.inAppMessagesHandler = this.setInAppMessagesHandler(
inAppMessagesHandler
);

this.pluggables.forEach(pluggable => {
Expand All @@ -83,7 +83,7 @@ class NotificationsClass {
Hub.listen('analytics', this.analyticsListener);
this.listeningForAnalyticEvents = true;
}
}
};

getModuleName(): NotificationsCategory {
return 'Notifications';
Expand All @@ -100,7 +100,7 @@ class NotificationsClass {
}

return pluggable;
}
};

/**
* add plugin into Analytics category
Expand All @@ -119,7 +119,7 @@ class NotificationsClass {
pluggable.configure(config);
return config;
}
}
};

removePluggable = (providerName: string): void => {
const index = this.pluggables.findIndex(
Expand All @@ -130,29 +130,28 @@ class NotificationsClass {
} else {
this.pluggables.splice(index, 1);
}
}
};

setFilteredInAppMessagesHandler = (
handler: FilteredInAppMessagesHandler
): FilteredInAppMessagesHandler => {
if (this.filteredInAppMessagesHandler === noop && handler) {
this.filteredInAppMessagesHandler = handler;
setInAppMessagesHandler = (
handler: InAppMessagesHandler
): InAppMessagesHandler => {
if (handler && this.inAppMessagesHandler !== handler) {
this.inAppMessagesHandler = handler;
}
return this.filteredInAppMessagesHandler;
}
return this.inAppMessagesHandler;
};

syncInAppMessages = async (providerName = 'AWSPinpoint'): Promise<any> => {
syncInAppMessages = async (providerName = 'AWSPinpoint'): Promise<void> => {
if (this.config.disabled) {
logger.debug('Notifications has been disabled');
return;
}

const pluggable = this.getPluggable(providerName);
const messages = await pluggable.syncInAppMessages();
const messages = await pluggable.getInAppMessages();
const key = `${pluggable.getProviderName()}${STORAGE_KEY_SUFFIX}`;
await this.storeMessages(key, messages);
return messages;
}
};

clearStoredInAppMessages = async (
providerName = 'AWSPinpoint'
Expand All @@ -171,26 +170,18 @@ class NotificationsClass {
} catch (err) {
logger.error('Failed to remove in-app messages from storage', err);
}
}
};

invokeInAppMessages = async (event: NotificationEvent): Promise<void> => {
const messages: any[] = await Promise.all<any[]>(
this.pluggables.map(async pluggable => {
const key = `${pluggable.getProviderName()}${STORAGE_KEY_SUFFIX}`;
const messages = await this.getStoredMessages(key);
return pluggable.filterMessages(messages, event);
return pluggable.processInAppMessages(messages, event);
})
);
this.filteredInAppMessagesHandler(flatten(messages));
}

recordInAppMessageDisplayed = async (messageId: string): Promise<void[]> => {
return Promise.all(
this.pluggables.map(pluggable =>
pluggable.recordInAppMessageDisplayed(messageId)
)
);
}
this.inAppMessagesHandler(flatten(messages));
};

private analyticsListener: HubCallback = ({ payload }: HubCapsule) => {
const { event, data } = payload;
Expand All @@ -202,7 +193,7 @@ class NotificationsClass {
default:
break;
}
}
};

private syncStorage = async (): Promise<void> => {
const { storage } = this.config;
Expand All @@ -215,7 +206,7 @@ class NotificationsClass {
} catch (err) {
logger.error('Failed to sync storage', err);
}
}
};

private getStoredMessages = async (key: string): Promise<any> => {
try {
Expand All @@ -228,7 +219,7 @@ class NotificationsClass {
} catch (err) {
logger.error('Failed to retrieve in-app messages from storage', err);
}
}
};

private storeMessages = async (
key: string,
Expand All @@ -247,7 +238,7 @@ class NotificationsClass {
} catch (err) {
logger.error('Failed to store in-app messages', err);
}
}
};
}

const Notifications = new NotificationsClass();
Expand Down
Loading