Merge pull request #29169 from Zlatkovsky/master

CustomFunctions: Allow Error object and clean up tests
This commit is contained in:
Andrew Casey
2018-09-26 16:33:41 -07:00
committed by GitHub
3 changed files with 42 additions and 13 deletions
@@ -7,27 +7,55 @@ 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;
function stockPriceStream(
ticker: string,
handler: CustomFunctions.StreamingHandler<number>
) {
const updateFrequency = 10 /* milliseconds*/;
let isPending = false;
const timer = setInterval(async () => {
// If there is already a pending request, skip this iteration:
if (isPending) {
return;
}
const url = `myService.com/prices/${ticker}`;
isPending = true;
try {
const response = await fetch(url);
const data = await response.json();
handler.setResult(data.price);
} catch (error) {
handler.setResult(error);
}
isPending = false;
}, updateFrequency);
handler.onCanceled = () => {
clearInterval(timer);
};
}
declare function pause(ms: number): Promise<void>;
declare function pause(ms: number): Promise<undefined>;
+1 -1
View File
@@ -24,7 +24,7 @@ declare namespace CustomFunctions {
* Sets the returned result for a streaming custom function.
* @beta
*/
setResult: (value: T) => void;
setResult: (value: T | Error) => void;
}
interface CancelableHandler {
+2 -1
View File
@@ -1,6 +1,7 @@
{
"extends": "dtslint/dt.json",
"rules": {
"ban-types": false
"ban-types": false,
"file-name-casing": false
}
}