From 63876025d2b0e5737fe5a64f8cd6a87d0bea7929 Mon Sep 17 00:00:00 2001 From: Zaid Abdulla Date: Mon, 20 May 2019 16:37:55 -0700 Subject: [PATCH] Maintain types for memoized functions --- lib/common/expression-parser.ts | 2 +- lib/common/memoize.ts | 9 +++++---- lib/soap.ts | 2 +- lib/ui/api.ts | 2 +- ui/components/parameter.ts | 2 +- ui/ui-config-component.ts | 2 +- 6 files changed, 10 insertions(+), 9 deletions(-) diff --git a/lib/common/expression-parser.ts b/lib/common/expression-parser.ts index 1ae017a..cb34db6 100644 --- a/lib/common/expression-parser.ts +++ b/lib/common/expression-parser.ts @@ -285,7 +285,7 @@ const lang = parsimmon.createLanguage({ } }); -export function parse(str): Expression { +export function parse(str: string): Expression { if (!str) return null; return lang.Expression.tryParse(str); } diff --git a/lib/common/memoize.ts b/lib/common/memoize.ts index f7d984e..1048a66 100644 --- a/lib/common/memoize.ts +++ b/lib/common/memoize.ts @@ -20,9 +20,10 @@ function getKey(obj): string { return k; } -export default function memoize(func): Function { - return (...args) => { - const key = JSON.stringify(args.map(getKey)) + getKey(func); +export default function memoize(func: T): T { + const funcKey = getKey(func); + return ((...args) => { + const key = JSON.stringify(args.map(getKey)) + funcKey; if (cache1.has(key)) return cache1.get(key); @@ -31,7 +32,7 @@ export default function memoize(func): Function { else r = func(...args); cache1.set(key, r); return r; - }; + }) as any; } const interval = setInterval(() => { diff --git a/lib/soap.ts b/lib/soap.ts index 282961f..87c783c 100644 --- a/lib/soap.ts +++ b/lib/soap.ts @@ -127,7 +127,7 @@ function parameterInfoList(xml: Element): [string, boolean][] { }); } -const getValueType = memoize(str => +const getValueType = memoize((str: string) => parseAttrs(str) .find(s => s.localName === "type") .value.trim() diff --git a/lib/ui/api.ts b/lib/ui/api.ts index fb00a88..f122401 100644 --- a/lib/ui/api.ts +++ b/lib/ui/api.ts @@ -206,7 +206,7 @@ for (const [resource, flags] of Object.entries(resources)) { const now = Date.now(); for (const [k, v] of Object.entries(columns)) { - const e = evaluate(parse(v), null, now); + const e = evaluate(parse(v as string), null, now); columns[k] = e; for (const p of extractParams(e)) options.projection[p] = 1; } diff --git a/ui/components/parameter.ts b/ui/components/parameter.ts index eb25121..c8da983 100644 --- a/ui/components/parameter.ts +++ b/ui/components/parameter.ts @@ -6,7 +6,7 @@ import * as expression from "../../lib/common/expression"; import memoize from "../../lib/common/memoize"; import timeAgo from "../timeago"; -const evaluateParam = memoize((exp, obj, now) => { +const evaluateParam = memoize((exp, obj, now: number) => { let timestamp = now; const params = new Set(); const value = expression.evaluate(exp, obj, now, e => { diff --git a/ui/ui-config-component.ts b/ui/ui-config-component.ts index 063af6d..19ad405 100644 --- a/ui/ui-config-component.ts +++ b/ui/ui-config-component.ts @@ -27,7 +27,7 @@ function putActionHandler(prefix: string[], dataYaml: string): Promise { } // Try parse to ensure valid expressions - for (const v of Object.values(updated)) parse(v); + for (const v of Object.values(updated)) parse(v as string); store .queryConfig(`${prefix.join(".")}.%`)