diff --git a/www/__tests__/unifiedDataLoader.test.ts b/www/__tests__/unifiedDataLoader.test.ts index cbbede1a6..285971f0a 100644 --- a/www/__tests__/unifiedDataLoader.test.ts +++ b/www/__tests__/unifiedDataLoader.test.ts @@ -51,7 +51,7 @@ const promiseGenerator = (values: Array>) => { }; const badPromiseGenerator = (input: string) => { return Promise.reject(input); -} +}; it('throws an error on an empty input', async () => { expect(() => { @@ -61,11 +61,25 @@ it('throws an error on an empty input', async () => { it('catches when all promises fails', async () => { expect(combinedPromises([badPromiseGenerator('')], combineWithDedup)).rejects.toEqual(['']); - expect(combinedPromises([badPromiseGenerator('bad'), badPromiseGenerator('promise')], combineWithDedup)).rejects.toEqual(['bad','promise']); - expect(combinedPromises([badPromiseGenerator('very'), badPromiseGenerator('bad'), badPromiseGenerator('promise')], combineWithDedup)).rejects.toEqual(['very','bad','promise']); - - expect(combinedPromises([badPromiseGenerator('bad'), promiseGenerator([testOne])], combineWithDedup)).resolves.toEqual([testOne]); - expect(combinedPromises([promiseGenerator([testOne]), badPromiseGenerator('bad')], combineWithDedup)).resolves.toEqual([testOne]); + expect( + combinedPromises( + [badPromiseGenerator('bad'), badPromiseGenerator('promise')], + combineWithDedup, + ), + ).rejects.toEqual(['bad', 'promise']); + expect( + combinedPromises( + [badPromiseGenerator('very'), badPromiseGenerator('bad'), badPromiseGenerator('promise')], + combineWithDedup, + ), + ).rejects.toEqual(['very', 'bad', 'promise']); + + expect( + combinedPromises([badPromiseGenerator('bad'), promiseGenerator([testOne])], combineWithDedup), + ).resolves.toEqual([testOne]); + expect( + combinedPromises([promiseGenerator([testOne]), badPromiseGenerator('bad')], combineWithDedup), + ).resolves.toEqual([testOne]); }); it('work with arrays of len 1', async () => { diff --git a/www/js/services/unifiedDataLoader.ts b/www/js/services/unifiedDataLoader.ts index 45144ff56..4d644b998 100644 --- a/www/js/services/unifiedDataLoader.ts +++ b/www/js/services/unifiedDataLoader.ts @@ -1,104 +1,125 @@ -import { logDebug } from '../plugin/logger' +import { logDebug } from '../plugin/logger'; import { getRawEntries } from './commHelper'; import { ServerResponse, ServerData, TimeQuery } from '../types/serverData'; /** - * combineWithDedup is a helper function for combinedPromises + * combineWithDedup is a helper function for combinedPromises * @param list1 values evaluated from a BEMUserCache promise - * @param list2 same as list1 + * @param list2 same as list1 * @returns a dedup array generated from the input lists */ -export const combineWithDedup = function(list1: Array>, list2: Array) { - const combinedList = list1.concat(list2); - return combinedList.filter(function(value, i, array) { - const firstIndexOfValue = array.findIndex(function(element) { - return element.metadata.write_ts == value.metadata.write_ts; - }); - return firstIndexOfValue == i; +export const combineWithDedup = function (list1: Array>, list2: Array) { + const combinedList = list1.concat(list2); + return combinedList.filter(function (value, i, array) { + const firstIndexOfValue = array.findIndex(function (element) { + return element.metadata.write_ts == value.metadata.write_ts; }); + return firstIndexOfValue == i; + }); }; /** - * combinedPromises is a recursive function that joins multiple promises - * @param promiseList 1 or more promises + * combinedPromises is a recursive function that joins multiple promises + * @param promiseList 1 or more promises * @param combiner a function that takes two arrays and joins them * @returns A promise which evaluates to a combined list of values or errors */ -export const combinedPromises = function(promiseList: Array>, - combiner: (list1: Array, list2: Array) => Array ) { - if (promiseList.length === 0) { - throw new RangeError('combinedPromises needs input array.length >= 1'); - } - return new Promise(function(resolve, reject) { - var firstResult = []; - var firstError = null; +export const combinedPromises = function ( + promiseList: Array>, + combiner: (list1: Array, list2: Array) => Array, +) { + if (promiseList.length === 0) { + throw new RangeError('combinedPromises needs input array.length >= 1'); + } + return new Promise(function (resolve, reject) { + var firstResult = []; + var firstError = null; - var nextResult = []; - var nextError = null; + var nextResult = []; + var nextError = null; - var firstPromiseDone = false; - var nextPromiseDone = false; + var firstPromiseDone = false; + var nextPromiseDone = false; - const checkAndResolve = function() { - if (firstPromiseDone && nextPromiseDone) { - if (firstError && nextError) { - reject([firstError].concat(nextError)); - } else { - logDebug(`About to dedup firstResult = ${firstResult.length}` + - ` nextResult = ${nextResult.length}`); - const dedupedList = combiner(firstResult, nextResult); - logDebug(`Deduped list = ${dedupedList.length}`); - resolve(dedupedList); - } + const checkAndResolve = function () { + if (firstPromiseDone && nextPromiseDone) { + if (firstError && nextError) { + reject([firstError].concat(nextError)); + } else { + logDebug( + `About to dedup firstResult = ${firstResult.length}` + + ` nextResult = ${nextResult.length}`, + ); + const dedupedList = combiner(firstResult, nextResult); + logDebug(`Deduped list = ${dedupedList.length}`); + resolve(dedupedList); } - }; - - if (promiseList.length === 1) { - return promiseList[0].then(function(result: Array) { + } + }; + + if (promiseList.length === 1) { + return promiseList[0].then( + function (result: Array) { resolve(result); - }, function (err) { + }, + function (err) { reject([err]); - }); - } + }, + ); + } - const firstPromise = promiseList[0]; - const nextPromise = combinedPromises(promiseList.slice(1), combiner); - - firstPromise.then(function(currentFirstResult: Array) { - firstResult = currentFirstResult; - firstPromiseDone = true; - }, function(error) { - firstResult = []; - firstError = error; - firstPromiseDone = true; - }).then(checkAndResolve); + const firstPromise = promiseList[0]; + const nextPromise = combinedPromises(promiseList.slice(1), combiner); - nextPromise.then(function(currentNextResult: Array) { - nextResult = currentNextResult; - nextPromiseDone = true; - }, function(error) { - nextResult = []; - nextError = error; - nextPromiseDone = true; - }).then(checkAndResolve); - }); + firstPromise + .then( + function (currentFirstResult: Array) { + firstResult = currentFirstResult; + firstPromiseDone = true; + }, + function (error) { + firstResult = []; + firstError = error; + firstPromiseDone = true; + }, + ) + .then(checkAndResolve); + + nextPromise + .then( + function (currentNextResult: Array) { + nextResult = currentNextResult; + nextPromiseDone = true; + }, + function (error) { + nextResult = []; + nextError = error; + nextPromiseDone = true; + }, + ) + .then(checkAndResolve); + }); }; /** - * getUnifiedDataForInterval is a generalized method to fetch data by its timestamps + * getUnifiedDataForInterval is a generalized method to fetch data by its timestamps * @param key string corresponding to a data entry * @param tq an object that contains interval start and end times * @param localGetMethod a BEMUserCache method that fetches certain data via a promise * @returns A promise that evaluates to the all values found within the queried data */ -export const getUnifiedDataForInterval = function(key: string, tq: TimeQuery, - localGetMethod: (key: string, tq: TimeQuery, flag: boolean) => Promise) { - const test = true; - const getPromise = localGetMethod(key, tq, test); - const remotePromise = getRawEntries([key], tq.startTs, tq.endTs) - .then(function(serverResponse: ServerResponse) { - return serverResponse.phone_data; - }); - var promiseList = [getPromise, remotePromise] - return combinedPromises(promiseList, combineWithDedup); -}; \ No newline at end of file +export const getUnifiedDataForInterval = function ( + key: string, + tq: TimeQuery, + localGetMethod: (key: string, tq: TimeQuery, flag: boolean) => Promise, +) { + const test = true; + const getPromise = localGetMethod(key, tq, test); + const remotePromise = getRawEntries([key], tq.startTs, tq.endTs).then(function ( + serverResponse: ServerResponse, + ) { + return serverResponse.phone_data; + }); + var promiseList = [getPromise, remotePromise]; + return combinedPromises(promiseList, combineWithDedup); +};