-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ran unifiedDataLoader files through Prettier
- Loading branch information
1 parent
aa34ac3
commit 2e5a61e
Showing
2 changed files
with
115 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; |