Skip to content

Commit

Permalink
Ran unifiedDataLoader files through Prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
the-bay-kay committed Oct 31, 2023
1 parent aa34ac3 commit 2e5a61e
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 80 deletions.
26 changes: 20 additions & 6 deletions www/__tests__/unifiedDataLoader.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ const promiseGenerator = (values: Array<ServerData<any>>) => {
};
const badPromiseGenerator = (input: string) => {
return Promise.reject(input);
}
};

it('throws an error on an empty input', async () => {
expect(() => {
Expand All @@ -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 () => {
Expand Down
169 changes: 95 additions & 74 deletions www/js/services/unifiedDataLoader.ts
Original file line number Diff line number Diff line change
@@ -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<ServerData<any>>, list2: Array<any>) {
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<ServerData<any>>, list2: Array<any>) {
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<Promise<any>>,
combiner: (list1: Array<any>, list2: Array<any>) => Array<any> ) {
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<Promise<any>>,
combiner: (list1: Array<any>, list2: Array<any>) => Array<any>,
) {
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<any>) {
}
};

if (promiseList.length === 1) {
return promiseList[0].then(
function (result: Array<any>) {
resolve(result);
}, function (err) {
},
function (err) {
reject([err]);
});
}
},
);
}

const firstPromise = promiseList[0];
const nextPromise = combinedPromises(promiseList.slice(1), combiner);

firstPromise.then(function(currentFirstResult: Array<any>) {
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<any>) {
nextResult = currentNextResult;
nextPromiseDone = true;
}, function(error) {
nextResult = [];
nextError = error;
nextPromiseDone = true;
}).then(checkAndResolve);
});
firstPromise
.then(
function (currentFirstResult: Array<any>) {
firstResult = currentFirstResult;
firstPromiseDone = true;
},
function (error) {
firstResult = [];
firstError = error;
firstPromiseDone = true;
},
)
.then(checkAndResolve);

nextPromise
.then(
function (currentNextResult: Array<any>) {
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<any>) {
const test = true;
const getPromise = localGetMethod(key, tq, test);
const remotePromise = getRawEntries([key], tq.startTs, tq.endTs)
.then(function(serverResponse: ServerResponse<any>) {
return serverResponse.phone_data;
});
var promiseList = [getPromise, remotePromise]
return combinedPromises(promiseList, combineWithDedup);
};
export const getUnifiedDataForInterval = function (
key: string,
tq: TimeQuery,
localGetMethod: (key: string, tq: TimeQuery, flag: boolean) => Promise<any>,
) {
const test = true;
const getPromise = localGetMethod(key, tq, test);
const remotePromise = getRawEntries([key], tq.startTs, tq.endTs).then(function (
serverResponse: ServerResponse<any>,
) {
return serverResponse.phone_data;
});
var promiseList = [getPromise, remotePromise];
return combinedPromises(promiseList, combineWithDedup);
};

0 comments on commit 2e5a61e

Please sign in to comment.