From dd5b155ae286a546e880a4be9551f185f29f64a9 Mon Sep 17 00:00:00 2001 From: Zlatkovsky Date: Mon, 24 Sep 2018 16:07:38 -0700 Subject: [PATCH 1/3] Allow streaming Custom Functions to be set with an Error result --- types/custom-functions-runtime/index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 { From 12ac8b7383e607ae228ff838c73ebbdcd56d64a5 Mon Sep 17 00:00:00 2001 From: Zlatkovsky Date: Mon, 24 Sep 2018 17:56:19 -0700 Subject: [PATCH 2/3] Update tests to allow a streaming error, and general test cleanup --- .../custom-functions-runtime-tests.ts | 54 +++++++++++++++---- 1 file changed, 44 insertions(+), 10 deletions(-) diff --git a/types/custom-functions-runtime/custom-functions-runtime-tests.ts b/types/custom-functions-runtime/custom-functions-runtime-tests.ts index 335e5a1f95..d0733305ce 100644 --- a/types/custom-functions-runtime/custom-functions-runtime-tests.ts +++ b/types/custom-functions-runtime/custom-functions-runtime-tests.ts @@ -7,27 +7,61 @@ 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; +async function stockPriceStream( + ticker: string, + handler: CustomFunctions.StreamingHandler +) { + 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; From 5172e5c1057395cab5163bd034cf5bb01f308e21 Mon Sep 17 00:00:00 2001 From: Zlatkovsky Date: Mon, 24 Sep 2018 18:02:54 -0700 Subject: [PATCH 3/3] Fix tslint issues with test --- .../custom-functions-runtime-tests.ts | 38 ++++++++----------- types/custom-functions-runtime/tslint.json | 3 +- 2 files changed, 18 insertions(+), 23 deletions(-) diff --git a/types/custom-functions-runtime/custom-functions-runtime-tests.ts b/types/custom-functions-runtime/custom-functions-runtime-tests.ts index d0733305ce..c35c172d7c 100644 --- a/types/custom-functions-runtime/custom-functions-runtime-tests.ts +++ b/types/custom-functions-runtime/custom-functions-runtime-tests.ts @@ -9,7 +9,7 @@ CustomFunctionMappings = { async function getStockValues(ticker: string): Promise { const response = await fetch(`myService.com/prices/${ticker}`); - return (await response.json())['price']; + return (await response.json())["price"]; } async function getStockValuesCancellable( @@ -25,38 +25,32 @@ async function getStockValuesCancellable( } const response = await fetch(`myService.com/prices/${ticker}`); - return (await response.json())['price']; + return (await response.json())["price"]; } -async function stockPriceStream( +function stockPriceStream( ticker: string, handler: CustomFunctions.StreamingHandler ) { - var updateFrequency = 10 /* milliseconds*/; - var isPending = false; + const updateFrequency = 10 /* milliseconds*/; + let isPending = false; - var timer = setInterval(function() { + const timer = setInterval(async () => { // If there is already a pending request, skip this iteration: if (isPending) { return; } - var url = `myService.com/prices/${ticker}`; + const 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; - }); + 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 = () => { @@ -64,4 +58,4 @@ async function stockPriceStream( }; } -declare function pause(ms: number): Promise; +declare function pause(ms: number): Promise; 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 } }