Maintain types for memoized functions
This commit is contained in:
1 parent
faa12537e4
commit
63876025d2
6 files changed
+10
-9
No files matched your search
@@ -285,7 +285,7 @@ const lang = parsimmon.createLanguage({
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
export function parse(str): Expression {
|
export function parse(str: string): Expression {
|
||||||
if (!str) return null;
|
if (!str) return null;
|
||||||
return lang.Expression.tryParse(str);
|
return lang.Expression.tryParse(str);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,9 +20,10 @@ function getKey(obj): string {
|
|||||||
return k;
|
return k;
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function memoize(func): Function {
|
export default function memoize<T extends Function>(func: T): T {
|
||||||
return (...args) => {
|
const funcKey = getKey(func);
|
||||||
const key = JSON.stringify(args.map(getKey)) + getKey(func);
|
return ((...args) => {
|
||||||
|
const key = JSON.stringify(args.map(getKey)) + funcKey;
|
||||||
|
|
||||||
if (cache1.has(key)) return cache1.get(key);
|
if (cache1.has(key)) return cache1.get(key);
|
||||||
|
|
||||||
@@ -31,7 +32,7 @@ export default function memoize(func): Function {
|
|||||||
else r = func(...args);
|
else r = func(...args);
|
||||||
cache1.set(key, r);
|
cache1.set(key, r);
|
||||||
return r;
|
return r;
|
||||||
};
|
}) as any;
|
||||||
}
|
}
|
||||||
|
|
||||||
const interval = setInterval(() => {
|
const interval = setInterval(() => {
|
||||||
|
|||||||
+1
-1
@@ -127,7 +127,7 @@ function parameterInfoList(xml: Element): [string, boolean][] {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
const getValueType = memoize(str =>
|
const getValueType = memoize((str: string) =>
|
||||||
parseAttrs(str)
|
parseAttrs(str)
|
||||||
.find(s => s.localName === "type")
|
.find(s => s.localName === "type")
|
||||||
.value.trim()
|
.value.trim()
|
||||||
|
|||||||
+1
-1
@@ -206,7 +206,7 @@ for (const [resource, flags] of Object.entries(resources)) {
|
|||||||
const now = Date.now();
|
const now = Date.now();
|
||||||
|
|
||||||
for (const [k, v] of Object.entries(columns)) {
|
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;
|
columns[k] = e;
|
||||||
for (const p of extractParams(e)) options.projection[p] = 1;
|
for (const p of extractParams(e)) options.projection[p] = 1;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ import * as expression from "../../lib/common/expression";
|
|||||||
import memoize from "../../lib/common/memoize";
|
import memoize from "../../lib/common/memoize";
|
||||||
import timeAgo from "../timeago";
|
import timeAgo from "../timeago";
|
||||||
|
|
||||||
const evaluateParam = memoize((exp, obj, now) => {
|
const evaluateParam = memoize((exp, obj, now: number) => {
|
||||||
let timestamp = now;
|
let timestamp = now;
|
||||||
const params = new Set();
|
const params = new Set();
|
||||||
const value = expression.evaluate(exp, obj, now, e => {
|
const value = expression.evaluate(exp, obj, now, e => {
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ function putActionHandler(prefix: string[], dataYaml: string): Promise<any> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Try parse to ensure valid expressions
|
// 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
|
store
|
||||||
.queryConfig(`${prefix.join(".")}.%`)
|
.queryConfig(`${prefix.join(".")}.%`)
|
||||||
|
|||||||
Reference in new issue
Block a user