@aws-amplify/notifications 0.0.2-unstable.7462

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (110) hide show
  1. package/CHANGELOG.md +0 -0
  2. package/LICENSE +201 -0
  3. package/lib/.tsbuildinfo +3 -0
  4. package/lib/InAppMessaging/InAppMessaging.d.ts +55 -0
  5. package/lib/InAppMessaging/InAppMessaging.js +342 -0
  6. package/lib/InAppMessaging/InAppMessaging.js.map +1 -0
  7. package/lib/InAppMessaging/Providers/AWSPinpointProvider/index.d.ts +50 -0
  8. package/lib/InAppMessaging/Providers/AWSPinpointProvider/index.js +482 -0
  9. package/lib/InAppMessaging/Providers/AWSPinpointProvider/index.js.map +1 -0
  10. package/lib/InAppMessaging/Providers/AWSPinpointProvider/types.d.ts +25 -0
  11. package/lib/InAppMessaging/Providers/AWSPinpointProvider/types.js +11 -0
  12. package/lib/InAppMessaging/Providers/AWSPinpointProvider/types.js.map +1 -0
  13. package/lib/InAppMessaging/Providers/AWSPinpointProvider/utils.d.ts +18 -0
  14. package/lib/InAppMessaging/Providers/AWSPinpointProvider/utils.js +254 -0
  15. package/lib/InAppMessaging/Providers/AWSPinpointProvider/utils.js.map +1 -0
  16. package/lib/InAppMessaging/Providers/index.d.ts +1 -0
  17. package/lib/InAppMessaging/Providers/index.js +7 -0
  18. package/lib/InAppMessaging/Providers/index.js.map +1 -0
  19. package/lib/InAppMessaging/SessionTracker/SessionTracker.d.ts +9 -0
  20. package/lib/InAppMessaging/SessionTracker/SessionTracker.js +68 -0
  21. package/lib/InAppMessaging/SessionTracker/SessionTracker.js.map +1 -0
  22. package/lib/InAppMessaging/SessionTracker/SessionTracker.native.d.ts +10 -0
  23. package/lib/InAppMessaging/SessionTracker/SessionTracker.native.js +53 -0
  24. package/lib/InAppMessaging/SessionTracker/SessionTracker.native.js.map +1 -0
  25. package/lib/InAppMessaging/SessionTracker/index.d.ts +3 -0
  26. package/lib/InAppMessaging/SessionTracker/index.js +8 -0
  27. package/lib/InAppMessaging/SessionTracker/index.js.map +1 -0
  28. package/lib/InAppMessaging/SessionTracker/types.d.ts +6 -0
  29. package/lib/InAppMessaging/SessionTracker/types.js +5 -0
  30. package/lib/InAppMessaging/SessionTracker/types.js.map +1 -0
  31. package/lib/InAppMessaging/eventListeners.d.ts +3 -0
  32. package/lib/InAppMessaging/eventListeners.js +28 -0
  33. package/lib/InAppMessaging/eventListeners.js.map +1 -0
  34. package/lib/InAppMessaging/index.d.ts +4 -0
  35. package/lib/InAppMessaging/index.js +11 -0
  36. package/lib/InAppMessaging/index.js.map +1 -0
  37. package/lib/InAppMessaging/types.d.ts +99 -0
  38. package/lib/InAppMessaging/types.js +10 -0
  39. package/lib/InAppMessaging/types.js.map +1 -0
  40. package/lib/Notifications.d.ts +20 -0
  41. package/lib/Notifications.js +46 -0
  42. package/lib/Notifications.js.map +1 -0
  43. package/lib/index.d.ts +3 -0
  44. package/lib/index.js +9 -0
  45. package/lib/index.js.map +1 -0
  46. package/lib/types.d.ts +7 -0
  47. package/lib/types.js +3 -0
  48. package/lib/types.js.map +1 -0
  49. package/lib-esm/.tsbuildinfo +3 -0
  50. package/lib-esm/InAppMessaging/InAppMessaging.d.ts +55 -0
  51. package/lib-esm/InAppMessaging/InAppMessaging.js +340 -0
  52. package/lib-esm/InAppMessaging/InAppMessaging.js.map +1 -0
  53. package/lib-esm/InAppMessaging/Providers/AWSPinpointProvider/index.d.ts +50 -0
  54. package/lib-esm/InAppMessaging/Providers/AWSPinpointProvider/index.js +480 -0
  55. package/lib-esm/InAppMessaging/Providers/AWSPinpointProvider/index.js.map +1 -0
  56. package/lib-esm/InAppMessaging/Providers/AWSPinpointProvider/types.d.ts +25 -0
  57. package/lib-esm/InAppMessaging/Providers/AWSPinpointProvider/types.js +9 -0
  58. package/lib-esm/InAppMessaging/Providers/AWSPinpointProvider/types.js.map +1 -0
  59. package/lib-esm/InAppMessaging/Providers/AWSPinpointProvider/utils.d.ts +18 -0
  60. package/lib-esm/InAppMessaging/Providers/AWSPinpointProvider/utils.js +252 -0
  61. package/lib-esm/InAppMessaging/Providers/AWSPinpointProvider/utils.js.map +1 -0
  62. package/lib-esm/InAppMessaging/Providers/index.d.ts +1 -0
  63. package/lib-esm/InAppMessaging/Providers/index.js +4 -0
  64. package/lib-esm/InAppMessaging/Providers/index.js.map +1 -0
  65. package/lib-esm/InAppMessaging/SessionTracker/SessionTracker.d.ts +9 -0
  66. package/lib-esm/InAppMessaging/SessionTracker/SessionTracker.js +65 -0
  67. package/lib-esm/InAppMessaging/SessionTracker/SessionTracker.js.map +1 -0
  68. package/lib-esm/InAppMessaging/SessionTracker/SessionTracker.native.d.ts +10 -0
  69. package/lib-esm/InAppMessaging/SessionTracker/SessionTracker.native.js +50 -0
  70. package/lib-esm/InAppMessaging/SessionTracker/SessionTracker.native.js.map +1 -0
  71. package/lib-esm/InAppMessaging/SessionTracker/index.d.ts +3 -0
  72. package/lib-esm/InAppMessaging/SessionTracker/index.js +5 -0
  73. package/lib-esm/InAppMessaging/SessionTracker/index.js.map +1 -0
  74. package/lib-esm/InAppMessaging/SessionTracker/types.d.ts +6 -0
  75. package/lib-esm/InAppMessaging/SessionTracker/types.js +3 -0
  76. package/lib-esm/InAppMessaging/SessionTracker/types.js.map +1 -0
  77. package/lib-esm/InAppMessaging/eventListeners.d.ts +3 -0
  78. package/lib-esm/InAppMessaging/eventListeners.js +26 -0
  79. package/lib-esm/InAppMessaging/eventListeners.js.map +1 -0
  80. package/lib-esm/InAppMessaging/index.d.ts +4 -0
  81. package/lib-esm/InAppMessaging/index.js +6 -0
  82. package/lib-esm/InAppMessaging/index.js.map +1 -0
  83. package/lib-esm/InAppMessaging/types.d.ts +99 -0
  84. package/lib-esm/InAppMessaging/types.js +8 -0
  85. package/lib-esm/InAppMessaging/types.js.map +1 -0
  86. package/lib-esm/Notifications.d.ts +20 -0
  87. package/lib-esm/Notifications.js +44 -0
  88. package/lib-esm/Notifications.js.map +1 -0
  89. package/lib-esm/index.d.ts +3 -0
  90. package/lib-esm/index.js +5 -0
  91. package/lib-esm/index.js.map +1 -0
  92. package/lib-esm/types.d.ts +7 -0
  93. package/lib-esm/types.js +1 -0
  94. package/lib-esm/types.js.map +1 -0
  95. package/package.json +99 -0
  96. package/src/InAppMessaging/InAppMessaging.ts +330 -0
  97. package/src/InAppMessaging/Providers/AWSPinpointProvider/index.ts +522 -0
  98. package/src/InAppMessaging/Providers/AWSPinpointProvider/types.ts +38 -0
  99. package/src/InAppMessaging/Providers/AWSPinpointProvider/utils.ts +329 -0
  100. package/src/InAppMessaging/Providers/index.ts +4 -0
  101. package/src/InAppMessaging/SessionTracker/SessionTracker.native.ts +60 -0
  102. package/src/InAppMessaging/SessionTracker/SessionTracker.ts +78 -0
  103. package/src/InAppMessaging/SessionTracker/index.ts +6 -0
  104. package/src/InAppMessaging/SessionTracker/types.ts +11 -0
  105. package/src/InAppMessaging/eventListeners.ts +42 -0
  106. package/src/InAppMessaging/index.ts +20 -0
  107. package/src/InAppMessaging/types.ts +146 -0
  108. package/src/Notifications.ts +49 -0
  109. package/src/index.ts +20 -0
  110. package/src/types.ts +11 -0
@@ -0,0 +1,20 @@
1
+ import InAppMessaging from './InAppMessaging';
2
+ import { NotificationsCategory, NotificationsConfig } from './types';
3
+ declare class NotificationsClass {
4
+ private config;
5
+ private inAppMessaging;
6
+ constructor();
7
+ /**
8
+ * Get the name of the module category
9
+ * @returns {string} name of the module category
10
+ */
11
+ getModuleName(): NotificationsCategory;
12
+ /**
13
+ * Configure Notifications
14
+ * @param {Object} config - Notifications configuration object
15
+ */
16
+ configure: ({ Notifications: config }?: NotificationsConfig) => Record<string, any>;
17
+ get InAppMessaging(): InAppMessaging;
18
+ }
19
+ declare const Notifications: NotificationsClass;
20
+ export default Notifications;
@@ -0,0 +1,44 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+ import { __assign } from "tslib";
4
+ import { Amplify, ConsoleLogger as Logger } from '@aws-amplify/core';
5
+ import InAppMessaging from './InAppMessaging';
6
+ var logger = new Logger('Notifications');
7
+ var NotificationsClass = /** @class */ (function () {
8
+ function NotificationsClass() {
9
+ var _this = this;
10
+ this.config = {};
11
+ /**
12
+ * Configure Notifications
13
+ * @param {Object} config - Notifications configuration object
14
+ */
15
+ this.configure = function (_a) {
16
+ var config = (_a === void 0 ? {} : _a).Notifications;
17
+ _this.config = __assign(__assign({}, _this.config), config);
18
+ logger.debug('configure Notifications', config);
19
+ // Configure sub-categories
20
+ _this.inAppMessaging.configure(_this.config.InAppMessaging);
21
+ return _this.config;
22
+ };
23
+ this.inAppMessaging = new InAppMessaging();
24
+ }
25
+ /**
26
+ * Get the name of the module category
27
+ * @returns {string} name of the module category
28
+ */
29
+ NotificationsClass.prototype.getModuleName = function () {
30
+ return 'Notifications';
31
+ };
32
+ Object.defineProperty(NotificationsClass.prototype, "InAppMessaging", {
33
+ get: function () {
34
+ return this.inAppMessaging;
35
+ },
36
+ enumerable: true,
37
+ configurable: true
38
+ });
39
+ return NotificationsClass;
40
+ }());
41
+ var Notifications = new NotificationsClass();
42
+ export default Notifications;
43
+ Amplify.register(Notifications);
44
+ //# sourceMappingURL=Notifications.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Notifications.js","sourceRoot":"","sources":["../src/Notifications.ts"],"names":[],"mappings":"AAAA,qEAAqE;AACrE,sCAAsC;;AAEtC,OAAO,EAAE,OAAO,EAAE,aAAa,IAAI,MAAM,EAAE,MAAM,mBAAmB,CAAC;AACrE,OAAO,cAAc,MAAM,kBAAkB,CAAC;AAG9C,IAAM,MAAM,GAAG,IAAI,MAAM,CAAC,eAAe,CAAC,CAAC;AAE3C;IAIC;QAAA,iBAEC;QALO,WAAM,GAAwB,EAAE,CAAC;QAezC;;;WAGG;QACH,cAAS,GAAG,UAAC,EAAmD;gBAAjD,gDAAqB;YACnC,KAAI,CAAC,MAAM,yBAAQ,KAAI,CAAC,MAAM,GAAK,MAAM,CAAE,CAAC;YAE5C,MAAM,CAAC,KAAK,CAAC,yBAAyB,EAAE,MAAM,CAAC,CAAC;YAEhD,2BAA2B;YAC3B,KAAI,CAAC,cAAc,CAAC,SAAS,CAAC,KAAI,CAAC,MAAM,CAAC,cAAc,CAAC,CAAC;YAE1D,OAAO,KAAI,CAAC,MAAM,CAAC;QACpB,CAAC,CAAC;QAxBD,IAAI,CAAC,cAAc,GAAG,IAAI,cAAc,EAAE,CAAC;IAC5C,CAAC;IAED;;;OAGG;IACH,0CAAa,GAAb;QACC,OAAO,eAAe,CAAC;IACxB,CAAC;IAiBD,sBAAI,8CAAc;aAAlB;YACC,OAAO,IAAI,CAAC,cAAc,CAAC;QAC5B,CAAC;;;OAAA;IACF,yBAAC;AAAD,CAAC,AAlCD,IAkCC;AAED,IAAM,aAAa,GAAG,IAAI,kBAAkB,EAAE,CAAC;AAE/C,eAAe,aAAa,CAAC;AAC7B,OAAO,CAAC,QAAQ,CAAC,aAAa,CAAC,CAAC"}
@@ -0,0 +1,3 @@
1
+ export { default as Notifications } from './Notifications';
2
+ export { AWSPinpointUserInfo, InAppMessage, InAppMessageAction, InAppMessageButton, InAppMessageContent, InAppMessageImage, InAppMessageInteractionEvent, InAppMessageLayout, InAppMessageStyle, InAppMessageTextAlign, InAppMessagingConfig, InAppMessagingEvent, UserInfo, } from './InAppMessaging';
3
+ export { NotificationsConfig } from './types';
@@ -0,0 +1,5 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+ export { default as Notifications } from './Notifications';
4
+ export { InAppMessageInteractionEvent, } from './InAppMessaging';
5
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,qEAAqE;AACrE,sCAAsC;AAEtC,OAAO,EAAE,OAAO,IAAI,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAC3D,OAAO,EAON,4BAA4B,GAO5B,MAAM,kBAAkB,CAAC"}
@@ -0,0 +1,7 @@
1
+ import { InAppMessagingConfig } from './InAppMessaging';
2
+ export declare type NotificationsCategory = 'Notifications';
3
+ export interface NotificationsConfig extends Record<any, any> {
4
+ Notifications?: {
5
+ InAppMessaging?: InAppMessagingConfig;
6
+ };
7
+ }
@@ -0,0 +1 @@
1
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":""}
package/package.json ADDED
@@ -0,0 +1,99 @@
1
+ {
2
+ "name": "@aws-amplify/notifications",
3
+ "version": "0.0.2-unstable.7462+751349a34",
4
+ "description": "Notifications category of aws-amplify",
5
+ "main": "./lib/index.js",
6
+ "module": "./lib-esm/index.js",
7
+ "typings": "./lib-esm/index.d.ts",
8
+ "sideEffects": [
9
+ "./lib/Notifications.js",
10
+ "./lib-esm/Notifications.js"
11
+ ],
12
+ "publishConfig": {
13
+ "access": "public"
14
+ },
15
+ "scripts": {
16
+ "test": "tslint 'src/**/*.ts' && jest -w 1 --coverage",
17
+ "test:watch": "tslint 'src/**/*.ts' && jest -w 1 --watch",
18
+ "build-with-test": "npm run clean && npm test && tsc && webpack",
19
+ "build:cjs": "node ./build es5 && webpack && webpack --config ./webpack.config.dev.js",
20
+ "build:esm": "node ./build es6",
21
+ "build:cjs:watch": "node ./build es5 --watch",
22
+ "build:esm:watch": "node ./build es6 --watch",
23
+ "build": "npm run clean && npm run build:esm && npm run build:cjs",
24
+ "clean": "rimraf lib-esm lib dist",
25
+ "format": "echo \"Not implemented\"",
26
+ "lint": "tslint 'src/**/*.ts' && npm run ts-coverage",
27
+ "ts-coverage": "typescript-coverage-report -p ./tsconfig.build.json -t 88.21"
28
+ },
29
+ "react-native": {
30
+ "./lib/index": "./lib-esm/index.js"
31
+ },
32
+ "repository": {
33
+ "type": "git",
34
+ "url": "https://github.com/aws-amplify/amplify-js.git"
35
+ },
36
+ "author": "Amazon Web Services",
37
+ "license": "Apache-2.0",
38
+ "bugs": {
39
+ "url": "https://github.com/aws/aws-amplify/issues"
40
+ },
41
+ "homepage": "https://docs.amplify.aws/",
42
+ "files": [
43
+ "lib",
44
+ "lib-esm",
45
+ "src"
46
+ ],
47
+ "dependencies": {
48
+ "@aws-amplify/cache": "4.0.64-unstable.4+751349a34",
49
+ "@aws-amplify/core": "4.7.13-unstable.4+751349a34",
50
+ "@aws-sdk/client-pinpoint": "3.186.0",
51
+ "lodash": "^4.17.21",
52
+ "uuid": "^3.2.1"
53
+ },
54
+ "jest": {
55
+ "globals": {
56
+ "ts-jest": {
57
+ "diagnostics": false,
58
+ "tsConfig": {
59
+ "lib": [
60
+ "es5",
61
+ "es2015",
62
+ "dom",
63
+ "esnext.asynciterable",
64
+ "es2017.object"
65
+ ],
66
+ "allowJs": true
67
+ }
68
+ }
69
+ },
70
+ "transform": {
71
+ "^.+\\.(js|jsx|ts|tsx)$": "ts-jest"
72
+ },
73
+ "testRegex": "(/__tests__/.*|\\.(test|spec))\\.(tsx?|jsx?)$",
74
+ "moduleFileExtensions": [
75
+ "ts",
76
+ "tsx",
77
+ "js",
78
+ "json",
79
+ "jsx"
80
+ ],
81
+ "testEnvironment": "jsdom",
82
+ "testURL": "http://localhost/",
83
+ "coverageThreshold": {
84
+ "global": {
85
+ "branches": 0,
86
+ "functions": 0,
87
+ "lines": 0,
88
+ "statements": 0
89
+ }
90
+ },
91
+ "coveragePathIgnorePatterns": [
92
+ "/node_modules/",
93
+ "dist",
94
+ "lib",
95
+ "lib-esm"
96
+ ]
97
+ },
98
+ "gitHead": "751349a340e10c0dded302db94bc5c2095f5651a"
99
+ }
@@ -0,0 +1,330 @@
1
+ // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import {
5
+ ConsoleLogger as Logger,
6
+ HubCallback,
7
+ HubCapsule,
8
+ Hub,
9
+ StorageHelper,
10
+ } from '@aws-amplify/core';
11
+ import flatten from 'lodash/flatten';
12
+ import { AWSPinpointProvider } from './Providers';
13
+ import {
14
+ addMessageInteractionEventListener,
15
+ notifyMessageInteractionEventListeners,
16
+ } from './eventListeners';
17
+ import {
18
+ InAppMessage,
19
+ InAppMessageInteractionEvent,
20
+ InAppMessagingConfig,
21
+ InAppMessageConflictHandler,
22
+ InAppMessagingEvent,
23
+ InAppMessagingProvider,
24
+ NotificationsSubcategory,
25
+ OnMessageInteractionEventHandler,
26
+ OnMessageInteractionEventListener,
27
+ UserInfo,
28
+ } from './types';
29
+
30
+ const STORAGE_KEY_SUFFIX = '_inAppMessages';
31
+
32
+ const logger = new Logger('Notifications.InAppMessaging');
33
+
34
+ export default class InAppMessaging {
35
+ private config: Record<string, any> = {};
36
+ private conflictHandler: InAppMessageConflictHandler;
37
+ private listeningForAnalyticEvents = false;
38
+ private pluggables: InAppMessagingProvider[] = [];
39
+ private storageSynced = false;
40
+
41
+ constructor() {
42
+ this.config = {
43
+ storage: new StorageHelper().getStorage(),
44
+ };
45
+ this.setConflictHandler(this.defaultConflictHandler);
46
+ }
47
+
48
+ /**
49
+ * Configure InAppMessaging
50
+ * @param {Object} config - InAppMessaging configuration object
51
+ */
52
+ configure = ({
53
+ listenForAnalyticsEvents = true,
54
+ ...config
55
+ }: InAppMessagingConfig = {}): InAppMessagingConfig => {
56
+ this.config = { ...this.config, ...config };
57
+
58
+ logger.debug('configure InAppMessaging', this.config);
59
+
60
+ this.pluggables.forEach(pluggable => {
61
+ pluggable.configure(this.config[pluggable.getProviderName()]);
62
+ });
63
+
64
+ if (this.pluggables.length === 0) {
65
+ this.addPluggable(new AWSPinpointProvider());
66
+ }
67
+
68
+ if (listenForAnalyticsEvents && !this.listeningForAnalyticEvents) {
69
+ Hub.listen('analytics', this.analyticsListener);
70
+ this.listeningForAnalyticEvents = true;
71
+ }
72
+
73
+ return this.config;
74
+ };
75
+
76
+ /**
77
+ * Get the name of this module
78
+ * @returns {string} name of this module
79
+ */
80
+ getModuleName(): NotificationsSubcategory {
81
+ return 'InAppMessaging';
82
+ }
83
+
84
+ /**
85
+ * Get a plugin from added plugins
86
+ * @param {string} providerName - the name of the plugin to get
87
+ */
88
+ getPluggable = (providerName: string): InAppMessagingProvider => {
89
+ const pluggable =
90
+ this.pluggables.find(
91
+ pluggable => pluggable.getProviderName() === providerName
92
+ ) ?? null;
93
+
94
+ if (!pluggable) {
95
+ logger.debug(`No plugin found with name ${providerName}`);
96
+ }
97
+
98
+ return pluggable;
99
+ };
100
+
101
+ /**
102
+ * Add plugin into InAppMessaging
103
+ * @param {InAppMessagingProvider} pluggable - an instance of the plugin
104
+ */
105
+ addPluggable = (pluggable: InAppMessagingProvider): void => {
106
+ if (
107
+ pluggable &&
108
+ pluggable.getCategory() === 'Notifications' &&
109
+ pluggable.getSubCategory() === 'InAppMessaging'
110
+ ) {
111
+ if (this.getPluggable(pluggable.getProviderName())) {
112
+ throw new Error(
113
+ `Pluggable ${pluggable.getProviderName()} has already been added.`
114
+ );
115
+ }
116
+ this.pluggables.push(pluggable);
117
+ pluggable.configure(this.config[pluggable.getProviderName()]);
118
+ }
119
+ };
120
+
121
+ /**
122
+ * Remove a plugin from added plugins
123
+ * @param {string} providerName - the name of the plugin to remove
124
+ */
125
+ removePluggable = (providerName: string): void => {
126
+ const index = this.pluggables.findIndex(
127
+ pluggable => pluggable.getProviderName() === providerName
128
+ );
129
+ if (index === -1) {
130
+ logger.debug(`No plugin found with name ${providerName}`);
131
+ } else {
132
+ this.pluggables.splice(index, 1);
133
+ }
134
+ };
135
+
136
+ /**
137
+ * Get the map resources that are currently available through the provider
138
+ * @param {string} provider
139
+ * @returns - Array of available map resources
140
+ */
141
+ syncMessages = (): Promise<void[]> =>
142
+ Promise.all<void>(
143
+ this.pluggables.map(async pluggable => {
144
+ try {
145
+ const messages = await pluggable.getInAppMessages();
146
+ const key = `${pluggable.getProviderName()}${STORAGE_KEY_SUFFIX}`;
147
+ await this.setMessages(key, messages);
148
+ } catch (err) {
149
+ logger.error('Failed to sync messages', err);
150
+ throw err;
151
+ }
152
+ })
153
+ );
154
+
155
+ clearMessages = (): Promise<void[]> =>
156
+ Promise.all<void>(
157
+ this.pluggables.map(async pluggable => {
158
+ const key = `${pluggable.getProviderName()}${STORAGE_KEY_SUFFIX}`;
159
+ await this.removeMessages(key);
160
+ })
161
+ );
162
+
163
+ dispatchEvent = async (event: InAppMessagingEvent): Promise<void> => {
164
+ const messages: InAppMessage[][] = await Promise.all<InAppMessage[]>(
165
+ this.pluggables.map(async pluggable => {
166
+ const key = `${pluggable.getProviderName()}${STORAGE_KEY_SUFFIX}`;
167
+ const messages = await this.getMessages(key);
168
+ return pluggable.processInAppMessages(messages, event);
169
+ })
170
+ );
171
+
172
+ const flattenedMessages = flatten(messages);
173
+
174
+ if (flattenedMessages.length) {
175
+ notifyMessageInteractionEventListeners(
176
+ this.conflictHandler(flattenedMessages),
177
+ InAppMessageInteractionEvent.MESSAGE_RECEIVED
178
+ );
179
+ }
180
+ };
181
+
182
+ identifyUser = (userId: string, userInfo: UserInfo): Promise<void[]> =>
183
+ Promise.all<void>(
184
+ this.pluggables.map(async pluggable => {
185
+ try {
186
+ await pluggable.identifyUser(userId, userInfo);
187
+ } catch (err) {
188
+ logger.error('Failed to identify user', err);
189
+ throw err;
190
+ }
191
+ })
192
+ );
193
+
194
+ onMessageReceived = (
195
+ handler: OnMessageInteractionEventHandler
196
+ ): OnMessageInteractionEventListener =>
197
+ addMessageInteractionEventListener(
198
+ handler,
199
+ InAppMessageInteractionEvent.MESSAGE_RECEIVED
200
+ );
201
+
202
+ onMessageDisplayed = (
203
+ handler: OnMessageInteractionEventHandler
204
+ ): OnMessageInteractionEventListener =>
205
+ addMessageInteractionEventListener(
206
+ handler,
207
+ InAppMessageInteractionEvent.MESSAGE_DISPLAYED
208
+ );
209
+
210
+ onMessageDismissed = (
211
+ handler: OnMessageInteractionEventHandler
212
+ ): OnMessageInteractionEventListener =>
213
+ addMessageInteractionEventListener(
214
+ handler,
215
+ InAppMessageInteractionEvent.MESSAGE_DISMISSED
216
+ );
217
+
218
+ onMessageActionTaken = (
219
+ handler: OnMessageInteractionEventHandler
220
+ ): OnMessageInteractionEventListener =>
221
+ addMessageInteractionEventListener(
222
+ handler,
223
+ InAppMessageInteractionEvent.MESSAGE_ACTION_TAKEN
224
+ );
225
+
226
+ notifyMessageInteraction = (
227
+ message: InAppMessage,
228
+ event: InAppMessageInteractionEvent
229
+ ): void => {
230
+ notifyMessageInteractionEventListeners(message, event);
231
+ };
232
+
233
+ setConflictHandler = (handler: InAppMessageConflictHandler): void => {
234
+ this.conflictHandler = handler;
235
+ };
236
+
237
+ private analyticsListener: HubCallback = ({ payload }: HubCapsule) => {
238
+ const { event, data } = payload;
239
+ switch (event) {
240
+ case 'record': {
241
+ this.dispatchEvent(data);
242
+ break;
243
+ }
244
+ default:
245
+ break;
246
+ }
247
+ };
248
+
249
+ private syncStorage = async (): Promise<void> => {
250
+ const { storage } = this.config;
251
+ try {
252
+ // Only run sync() if it's available (i.e. React Native)
253
+ if (typeof storage.sync === 'function') {
254
+ await storage.sync();
255
+ }
256
+ this.storageSynced = true;
257
+ } catch (err) {
258
+ logger.error('Failed to sync storage', err);
259
+ }
260
+ };
261
+
262
+ private getMessages = async (key: string): Promise<any> => {
263
+ try {
264
+ if (!this.storageSynced) {
265
+ await this.syncStorage();
266
+ }
267
+ const { storage } = this.config;
268
+ const storedMessages = storage.getItem(key);
269
+ return storedMessages ? JSON.parse(storedMessages) : [];
270
+ } catch (err) {
271
+ logger.error('Failed to retrieve in-app messages from storage', err);
272
+ }
273
+ };
274
+
275
+ private setMessages = async (
276
+ key: string,
277
+ messages: InAppMessage[]
278
+ ): Promise<void> => {
279
+ if (!messages) {
280
+ return;
281
+ }
282
+
283
+ try {
284
+ if (!this.storageSynced) {
285
+ await this.syncStorage();
286
+ }
287
+ const { storage } = this.config;
288
+ storage.setItem(key, JSON.stringify(messages));
289
+ } catch (err) {
290
+ logger.error('Failed to store in-app messages', err);
291
+ }
292
+ };
293
+
294
+ private removeMessages = async (key: string): Promise<void> => {
295
+ try {
296
+ if (!this.storageSynced) {
297
+ await this.syncStorage();
298
+ }
299
+ const { storage } = this.config;
300
+ storage.removeItem(key);
301
+ } catch (err) {
302
+ logger.error('Failed to remove in-app messages from storage', err);
303
+ }
304
+ };
305
+
306
+ private defaultConflictHandler = (messages: InAppMessage[]): InAppMessage => {
307
+ // default behavior is to return the message closest to expiry
308
+ // this function assumes that messages processed by providers already filters out expired messages
309
+ const sorted = messages.sort((a, b) => {
310
+ const endDateA = a.metadata?.endDate;
311
+ const endDateB = b.metadata?.endDate;
312
+ // if both message end dates are falsy or have the same date string, treat them as equal
313
+ if (endDateA === endDateB) {
314
+ return 0;
315
+ }
316
+ // if only message A has an end date, treat it as closer to expiry
317
+ if (endDateA && !endDateB) {
318
+ return -1;
319
+ }
320
+ // if only message B has an end date, treat it as closer to expiry
321
+ if (!endDateA && endDateB) {
322
+ return 1;
323
+ }
324
+ // otherwise, compare them
325
+ return new Date(endDateA) < new Date(endDateB) ? -1 : 1;
326
+ });
327
+ // always return the top sorted
328
+ return sorted[0];
329
+ };
330
+ }