Update tests to allow a streaming error, and general test cleanup

This commit is contained in:
Zlatkovsky
2018-09-24 17:56:19 -07:00
parent dd5b155ae2
commit 12ac8b7383

View File

@@ -7,27 +7,61 @@ CustomFunctionMappings = {
addTen: ADD10
};
async function getStockValues(ticker: string, handler: CustomFunctions.StreamingHandler<number>) {
const dollars = await (await fetch(`myService.com/prices/${ticker}`)).json();
handler.setResult(dollars);
async function getStockValues(ticker: string): Promise<number> {
const response = await fetch(`myService.com/prices/${ticker}`);
return (await response.json())['price'];
}
async function getStockValuesOneTime(ticker: string, handler: CustomFunctions.CancelableHandler) {
async function getStockValuesCancellable(
ticker: string,
handler: CustomFunctions.CancelableHandler
): Promise<number> {
let shouldStop = false;
handler.onCanceled = () => shouldStop = true;
handler.onCanceled = () => (shouldStop = true);
await pause(1000);
if (shouldStop) {
return null;
}
const dollars = await (await fetch(`myService.com/prices/${ticker}`)).json();
return dollars;
const response = await fetch(`myService.com/prices/${ticker}`);
return (await response.json())['price'];
}
async function getStockValuesNowWithNoCancelling(ticker: string) {
const dollars = await (await fetch(`myService.com/prices/${ticker}`)).json();
return dollars;
async function stockPriceStream(
ticker: string,
handler: CustomFunctions.StreamingHandler<number>
) {
var updateFrequency = 10 /* milliseconds*/;
var isPending = false;
var timer = setInterval(function() {
// If there is already a pending request, skip this iteration:
if (isPending) {
return;
}
var url = `myService.com/prices/${ticker}`;
isPending = true;
fetch(url)
.then(function(response) {
return response.json();
})
.then(function(data) {
handler.setResult(data.price);
})
.catch(function(error) {
handler.setResult(new Error(error));
})
.then(function() {
isPending = false;
});
}, updateFrequency);
handler.onCanceled = () => {
clearInterval(timer);
};
}
declare function pause(ms: number): Promise<void>;