diff --git a/types/custom-functions-runtime/custom-functions-runtime-tests.ts b/types/custom-functions-runtime/custom-functions-runtime-tests.ts index 335e5a1f95..c35c172d7c 100644 --- a/types/custom-functions-runtime/custom-functions-runtime-tests.ts +++ b/types/custom-functions-runtime/custom-functions-runtime-tests.ts @@ -7,27 +7,55 @@ CustomFunctionMappings = { addTen: ADD10 }; -async function getStockValues(ticker: string, handler: CustomFunctions.StreamingHandler) { - const dollars = await (await fetch(`myService.com/prices/${ticker}`)).json(); - handler.setResult(dollars); +async function getStockValues(ticker: string): Promise { + 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 { 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 +) { + 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; +declare function pause(ms: number): Promise; diff --git a/types/custom-functions-runtime/index.d.ts b/types/custom-functions-runtime/index.d.ts index 8771ab8755..58acfaa6c0 100644 --- a/types/custom-functions-runtime/index.d.ts +++ b/types/custom-functions-runtime/index.d.ts @@ -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 { diff --git a/types/custom-functions-runtime/tslint.json b/types/custom-functions-runtime/tslint.json index a62d0d4e68..ad17c76132 100644 --- a/types/custom-functions-runtime/tslint.json +++ b/types/custom-functions-runtime/tslint.json @@ -1,6 +1,7 @@ { "extends": "dtslint/dt.json", "rules": { - "ban-types": false + "ban-types": false, + "file-name-casing": false } }