expensify-common 2.0.181 → 2.0.183

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.
@@ -1,154 +1,37 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.default = ReportHistoryStore;
3
4
  const simply_deferred_1 = require("simply-deferred");
4
- class ReportHistoryStore {
5
- // We need to instantiate the history cache with the platform specific implementations
6
- constructor(API, PubSub) {
7
- if (!API) {
8
- throw new Error('Cannot instantiate ReportHistoryStore without API');
9
- }
10
- this.API = API;
11
- /**
12
- * Main report history cache
13
- * Map of reportIDs with value of report history items array
14
- */
15
- this.cache = {};
16
- /**
17
- * PubSub instance used for the bindCacheClearingEvents method.
18
- *
19
- * Since Mobile and Web use different instances of PubSub, this is unfortunately necessary to subscribe to
20
- * events in both code bases.
21
- */
22
- this.PubSub = PubSub;
23
- /**
24
- * Filters out actions we never want to display on web or mobile.
25
- *
26
- * @param {Object[]} historyItems
27
- *
28
- * @returns {Object[]}
29
- */
30
- this.filterHiddenActions = (historyItems) => historyItems.filter((historyItem) => historyItem.shouldShow);
31
- /**
32
- * Public Methods
33
- */
34
- return {
35
- /**
36
- * Returns the history for a given report.
37
- * Note that we are unable to ask for the cached history.
38
- *
39
- * @param {Number} reportID
40
- * @param {Boolean} ignoreCache - useful if you need to force the report history to reload completely.
41
- *
42
- * @returns {Deferred}
43
- */
44
- get: (reportID, ignoreCache = false) => {
45
- const promise = new simply_deferred_1.Deferred();
46
- this.get(reportID, ignoreCache)
47
- .done((reportHistory) => {
48
- promise.resolve(this.filterHiddenActions(reportHistory));
49
- })
50
- .fail(promise.reject);
51
- return promise;
52
- },
53
- /**
54
- * Returns the history for a given report.
55
- * Note that we are unable to ask for the cached history.
56
- *
57
- * @param {Number} reportID
58
- * @param {Boolean} ignoreCache - useful if you need to force the report history to reload completely.
59
- *
60
- * @returns {Deferred}
61
- */
62
- getFlatHistory: (reportID, ignoreCache = false) => {
63
- const promise = new simply_deferred_1.Deferred();
64
- this.getFlatHistory(reportID, ignoreCache)
65
- .done((reportHistory) => {
66
- promise.resolve(this.filterHiddenActions(reportHistory));
67
- })
68
- .fail(promise.reject);
69
- return promise;
70
- },
71
- /**
72
- * Set a history item directly into the cache. Checks to see if we have the previous item first.
73
- *
74
- * @param {Number} reportID
75
- * @param {Object} reportAction
76
- *
77
- * @returns {Deferred}
78
- */
79
- insertIntoCache: (reportID, reportAction) => {
80
- const promise = new simply_deferred_1.Deferred();
81
- this.getFromCache(reportID)
82
- .done((cachedHistory) => {
83
- const sequenceNumber = reportAction.sequenceNumber;
84
- // Do we have the reportAction immediately before this one?
85
- // Note: History is zero indexed. So if we want to check if we have the previous message before an
86
- // incoming message with a sequenceNumber of 18 then we'd be looking for a cache length of 18
87
- // which would indicate that we have sequenceNumber 17)
88
- if (cachedHistory.length >= sequenceNumber) {
89
- // If we have the previous one then we can assume we have an up to date history minus the most recent
90
- // and must merge it into the cache
91
- this.mergeItems(reportID, [reportAction]);
92
- return promise.resolve(this.filterHiddenActions(this.cache[reportID]));
93
- }
94
- // If we get here we have an incomplete history and should get
95
- // the report history again, but this time do not check the cache first.
96
- this.get(reportID)
97
- .done((reportHistory) => promise.resolve(this.filterHiddenActions(reportHistory)))
98
- .fail(promise.reject);
99
- })
100
- .fail(promise.reject);
101
- return promise;
102
- },
103
- /**
104
- * Set a history item directly into the cache. Checks to see if we have the previous item first.
105
- *
106
- * @param {Number} reportID
107
- * @param {Object} reportAction
108
- *
109
- * @returns {Deferred}
110
- */
111
- insertIntoCacheByActionID: (reportID, reportAction) => {
112
- const promise = new simply_deferred_1.Deferred();
113
- this.getFromCache(reportID)
114
- .done((cachedHistory) => {
115
- // Do we have the reportAction immediately before this one?
116
- if (cachedHistory.some(({ reportActionID }) => reportActionID === reportAction.reportActionID)) {
117
- // If we have the previous one then we can assume we have an up to date history minus the most recent
118
- // and must merge it into the cache
119
- this.mergeHistoryByTimestamp(reportID, [reportAction]);
120
- return promise.resolve(this.filterHiddenActions(this.cache[reportID]));
121
- }
122
- // If we get here we have an incomplete history and should get
123
- // the report history again, but this time do not check the cache first.
124
- this.getFlatHistory(reportID)
125
- .done((reportHistory) => promise.resolve(this.filterHiddenActions(reportHistory)))
126
- .fail(promise.reject);
127
- })
128
- .fail(promise.reject);
129
- return promise;
130
- },
131
- /**
132
- * Certain events need to completely clear the cache. This method allows other code modules using this
133
- * (like Web, Mobile) to assign which events would do so.
134
- *
135
- * @param {String[]} events
136
- */
137
- bindCacheClearingEvents: (events) => {
138
- events.forEach((event) => this.PubSub.subscribe(event, () => (this.cache = {})));
139
- },
140
- // We need this to be publically available for cases where we get the report history
141
- // via PHP or html pages within the app e.g. printablereport.html
142
- filterHiddenActions: this.filterHiddenActions,
143
- };
5
+ /**
6
+ * Creates a report history store with platform-specific API and PubSub implementations.
7
+ *
8
+ * @param {Object} API
9
+ * @param {Object} PubSub
10
+ * @returns {Object}
11
+ */
12
+ function ReportHistoryStore(API, PubSub) {
13
+ if (!API) {
14
+ throw new Error('Cannot instantiate ReportHistoryStore without API');
144
15
  }
16
+ const store = {
17
+ API,
18
+ cache: {},
19
+ PubSub,
20
+ };
21
+ /**
22
+ * Filters out actions we never want to display on web or mobile.
23
+ *
24
+ * @param {Object[]} historyItems
25
+ * @returns {Object[]}
26
+ */
27
+ const filterHiddenActions = (historyItems) => historyItems.filter((historyItem) => historyItem.shouldShow);
145
28
  /**
146
29
  * Merges history items into the cache and creates it if it doesn't yet exist.
147
30
  *
148
31
  * @param {Number} reportID
149
32
  * @param {Object[]} newHistory
150
33
  */
151
- mergeItems(reportID, newHistory) {
34
+ function mergeItems(reportID, newHistory) {
152
35
  if (newHistory.length === 0) {
153
36
  return;
154
37
  }
@@ -157,9 +40,8 @@ class ReportHistoryStore {
157
40
  prev.unshift(curr);
158
41
  }
159
42
  return prev;
160
- }, this.cache[reportID] || []);
161
- // Sort items in case they have become out of sync
162
- this.cache[reportID] = newCache.sort((a, b) => b.sequenceNumber - a.sequenceNumber);
43
+ }, store.cache[reportID] || []);
44
+ store.cache[reportID] = newCache.sort((a, b) => b.sequenceNumber - a.sequenceNumber);
163
45
  }
164
46
  /**
165
47
  * Merges history items into the cache and creates it if it doesn't yet exist.
@@ -167,7 +49,7 @@ class ReportHistoryStore {
167
49
  * @param {Number} reportID
168
50
  * @param {Object[]} newHistory
169
51
  */
170
- mergeHistoryByTimestamp(reportID, newHistory) {
52
+ function mergeHistoryByTimestamp(reportID, newHistory) {
171
53
  if (newHistory.length === 0) {
172
54
  return;
173
55
  }
@@ -176,9 +58,8 @@ class ReportHistoryStore {
176
58
  prev.unshift(curr);
177
59
  }
178
60
  return prev;
179
- }, this.cache[reportID] || []);
180
- // Sort items in case they have become out of sync
181
- this.cache[reportID] = newCache.sort((a, b) => b.reportActionTimestamp - a.reportActionTimestamp);
61
+ }, store.cache[reportID] || []);
62
+ store.cache[reportID] = newCache.sort((a, b) => b.reportActionTimestamp - a.reportActionTimestamp);
182
63
  }
183
64
  /**
184
65
  * Merges history items by reportActionID into the cache and creates it if it doesn't yet exist.
@@ -186,7 +67,7 @@ class ReportHistoryStore {
186
67
  * @param {Number} reportID
187
68
  * @param {Object[]} newHistory
188
69
  */
189
- mergeHistoryByReportActionID(reportID, newHistory) {
70
+ function mergeHistoryByReportActionID(reportID, newHistory) {
190
71
  if (newHistory.length === 0) {
191
72
  return;
192
73
  }
@@ -195,37 +76,30 @@ class ReportHistoryStore {
195
76
  prev.unshift(curr);
196
77
  }
197
78
  return prev;
198
- }, this.cache[reportID] || []);
199
- // Sort items in case they have become out of sync
200
- this.cache[reportID] = newCache.sort((a, b) => b.reportActionTimestamp - a.reportActionTimestamp);
79
+ }, store.cache[reportID] || []);
80
+ store.cache[reportID] = newCache.sort((a, b) => b.reportActionTimestamp - a.reportActionTimestamp);
201
81
  }
202
82
  /**
203
83
  * Gets the history.
204
84
  *
205
85
  * @param {Number} reportID
206
86
  * @param {Boolean} ignoreCache
207
- *
208
87
  * @returns {Deferred}
209
88
  */
210
- get(reportID, ignoreCache) {
89
+ function get(reportID, ignoreCache) {
211
90
  const promise = new simply_deferred_1.Deferred();
212
- // Remove the cache entry if we're ignoring the cache, since we'll be replacing it later.
213
91
  if (ignoreCache) {
214
- delete this.cache[reportID];
92
+ delete store.cache[reportID];
215
93
  }
216
- // We'll poll the API for the un-cached history
217
- const cachedHistory = this.cache[reportID] || [];
94
+ const cachedHistory = store.cache[reportID] || [];
218
95
  const firstHistoryItem = cachedHistory[0] || {};
219
- // Grab the most recent sequenceNumber we have and poll the API for fresh data
220
- this.API.Report_GetHistory({
96
+ store.API.Report_GetHistory({
221
97
  reportID,
222
98
  offset: firstHistoryItem.sequenceNumber || 0,
223
99
  })
224
100
  .done((recentHistory) => {
225
- // Update history with new items fetched
226
- this.mergeItems(reportID, recentHistory);
227
- // Return history for this report
228
- promise.resolve(this.cache[reportID]);
101
+ mergeItems(reportID, recentHistory);
102
+ promise.resolve(store.cache[reportID]);
229
103
  })
230
104
  .fail(promise.reject);
231
105
  return promise;
@@ -235,23 +109,19 @@ class ReportHistoryStore {
235
109
  *
236
110
  * @param {Number} reportID
237
111
  * @param {Boolean} ignoreCache
238
- *
239
112
  * @returns {Deferred}
240
113
  */
241
- getFlatHistory(reportID, ignoreCache) {
114
+ function getFlatHistory(reportID, ignoreCache) {
242
115
  const promise = new simply_deferred_1.Deferred();
243
- // Remove the cache entry if we're ignoring the cache, since we'll be replacing it later.
244
116
  if (ignoreCache) {
245
- delete this.cache[reportID];
117
+ delete store.cache[reportID];
246
118
  }
247
- this.API.Report_GetHistory({
119
+ store.API.Report_GetHistory({
248
120
  reportID,
249
121
  })
250
122
  .done((recentHistory) => {
251
- // Update history with new items fetched
252
- this.mergeHistoryByReportActionID(reportID, recentHistory);
253
- // Return history for this report
254
- promise.resolve(this.cache[reportID]);
123
+ mergeHistoryByReportActionID(reportID, recentHistory);
124
+ promise.resolve(store.cache[reportID]);
255
125
  })
256
126
  .fail(promise.reject);
257
127
  return promise;
@@ -260,17 +130,73 @@ class ReportHistoryStore {
260
130
  * Gets the history from the cache if it exists. Otherwise fully loads the history.
261
131
  *
262
132
  * @param {Number} reportID
263
- *
264
- * @return {Deferrred}
133
+ * @returns {Deferred}
265
134
  */
266
- getFromCache(reportID) {
135
+ function getFromCache(reportID) {
267
136
  const promise = new simply_deferred_1.Deferred();
268
- const cachedHistory = this.cache[reportID] || [];
269
- // If comment is not in cache then fetch it
137
+ const cachedHistory = store.cache[reportID] || [];
270
138
  if (cachedHistory.length === 0) {
271
- return this.getFlatHistory(reportID);
139
+ return getFlatHistory(reportID);
272
140
  }
273
141
  return promise.resolve(cachedHistory);
274
142
  }
143
+ return {
144
+ get: (reportID, ignoreCache = false) => {
145
+ const promise = new simply_deferred_1.Deferred();
146
+ get(reportID, ignoreCache)
147
+ .done((reportHistory) => {
148
+ promise.resolve(filterHiddenActions(reportHistory));
149
+ })
150
+ .fail(promise.reject);
151
+ return promise;
152
+ },
153
+ getFlatHistory: (reportID, ignoreCache = false) => {
154
+ const promise = new simply_deferred_1.Deferred();
155
+ getFlatHistory(reportID, ignoreCache)
156
+ .done((reportHistory) => {
157
+ promise.resolve(filterHiddenActions(reportHistory));
158
+ })
159
+ .fail(promise.reject);
160
+ return promise;
161
+ },
162
+ insertIntoCache: (reportID, reportAction) => {
163
+ const promise = new simply_deferred_1.Deferred();
164
+ getFromCache(reportID)
165
+ .done((cachedHistory) => {
166
+ const sequenceNumber = reportAction.sequenceNumber;
167
+ if (cachedHistory.length >= sequenceNumber) {
168
+ mergeItems(reportID, [reportAction]);
169
+ return promise.resolve(filterHiddenActions(store.cache[reportID]));
170
+ }
171
+ get(reportID)
172
+ .done((reportHistory) => promise.resolve(filterHiddenActions(reportHistory)))
173
+ .fail(promise.reject);
174
+ })
175
+ .fail(promise.reject);
176
+ return promise;
177
+ },
178
+ insertIntoCacheByActionID: (reportID, reportAction) => {
179
+ const promise = new simply_deferred_1.Deferred();
180
+ getFromCache(reportID)
181
+ .done((cachedHistory) => {
182
+ if (cachedHistory.some(({ reportActionID }) => reportActionID === reportAction.reportActionID)) {
183
+ mergeHistoryByTimestamp(reportID, [reportAction]);
184
+ return promise.resolve(filterHiddenActions(store.cache[reportID]));
185
+ }
186
+ getFlatHistory(reportID)
187
+ .done((reportHistory) => promise.resolve(filterHiddenActions(reportHistory)))
188
+ .fail(promise.reject);
189
+ })
190
+ .fail(promise.reject);
191
+ return promise;
192
+ },
193
+ bindCacheClearingEvents: (events) => {
194
+ for (const event of events) {
195
+ store.PubSub.subscribe(event, () => {
196
+ store.cache = {};
197
+ });
198
+ }
199
+ },
200
+ filterHiddenActions,
201
+ };
275
202
  }
276
- exports.default = ReportHistoryStore;
@@ -1,35 +1,36 @@
1
- declare const _default: {
1
+ export default Templates;
2
+ declare namespace Templates {
2
3
  /**
3
4
  * Given a templatePath, return the value
4
5
  *
5
6
  * @param {Array} templatePath
6
7
  * @param {Object} [data] Information that need to be injected into the template
7
- * @return {String}
8
+ * @returns {String}
8
9
  */
9
- get(templatePath: any[], data?: Object): string;
10
+ function get(templatePath: any[], data?: Object): string;
10
11
  /**
11
12
  * Given a templatePath, does it have a registered template?
12
13
  * @param {Array} templatePath
13
- * @return {Boolean}
14
+ * @returns {Boolean}
14
15
  */
15
- has(templatePath: any[]): boolean;
16
+ function has(templatePath: any[]): boolean;
16
17
  /**
17
18
  * Inits the templating engine, and slurps all DOM templates in an internal data structure
18
19
  */
19
- init(): void;
20
+ function init(): void;
20
21
  /**
21
22
  * Register a new json object in the template manager
22
23
  *
23
24
  * @param {Array} wantedNamespace Namespace where we want to store the templates
24
25
  * @param {object} templateData The literal object that contain the templates
25
26
  */
26
- register(wantedNamespace: any[], templateData: object): void;
27
+ function register(wantedNamespace: any[], templateData: object): void;
27
28
  /**
28
29
  * Removes a namespace from the templateStore (only used for testing purposes)
29
30
  *
30
31
  * @param {String} nameSpace
31
32
  */
32
- unregister(nameSpace: string): void;
33
+ function unregister(nameSpace: string): void;
33
34
  /**
34
35
  * Replace the DOM HTML with the template value
35
36
  *
@@ -37,7 +38,7 @@ declare const _default: {
37
38
  * @param {Array} templatePath
38
39
  * @param {Object} [data] Information that need to be injected into the template
39
40
  */
40
- insert($target: JQueryStatic, templatePath: any[], data?: Object): void;
41
+ function insert($target: JQueryStatic, templatePath: any[], data?: Object): void;
41
42
  /**
42
43
  * Append the template value into a DOM elements
43
44
  *
@@ -45,7 +46,7 @@ declare const _default: {
45
46
  * @param {Array} templatePath
46
47
  * @param {Object} data Information that need to be injected into the template
47
48
  */
48
- prepend($target: JQueryStatic, templatePath: any[], data: Object): void;
49
+ function prepend($target: JQueryStatic, templatePath: any[], data: Object): void;
49
50
  /**
50
51
  * Prepend the template value into a DOM elements
51
52
  *
@@ -53,6 +54,5 @@ declare const _default: {
53
54
  * @param {array} templatePath
54
55
  * @param {object} [data] Information that need to be injected into the template
55
56
  */
56
- append($target: JQueryStatic, templatePath: array, data?: object): void;
57
- };
58
- export default _default;
57
+ function append($target: JQueryStatic, templatePath: array, data?: object): void;
58
+ }