Fix functions merge

commit_hash:56540ef5107f4b7e403d64c8e70ff2c1fdde8ed2
This commit is contained in:
4eb0da
2024-11-26 18:27:55 +03:00
parent 8577211a5e
commit 11e87ff241
3 changed files with 42 additions and 6 deletions
+2 -5
View File
@@ -58,7 +58,7 @@
import { ROOT_CTX, type FocusableMethods, type NodeGetter, type ParentMethods, type RootCtxValue, type Running } from '../context/root';
import { applyTemplate } from '../utils/applyTemplate';
import { type LogError, wrapError, type WrappedError } from '../utils/wrapError';
import { checkCustomFunction, customFunctionWrap, type CustomFunctions } from '../expressions/funcs/customFuncs';
import { checkCustomFunction, customFunctionWrap, mergeCustomFunctions, type CustomFunctions } from '../expressions/funcs/customFuncs';
import { simpleCheckInput } from '../utils/simpleCheckInput';
import { ACTION_CTX, type ActionCtxValue } from '../context/action';
import { STATE_CTX, type StateCtxValue, type StateInterface } from '../context/state';
@@ -1607,10 +1607,7 @@
}
});
}
componentContext.customFunctions = mergeMaps(
res.customFunctions,
localCustomFunctions
);
componentContext.customFunctions = mergeCustomFunctions(res.customFunctions, localCustomFunctions);
if (Array.isArray(childProcessedJson.animators)) {
res.animators = childProcessedJson.animators.reduce<Record<string, MaybeMissing<Animator>>>(
@@ -5,7 +5,7 @@ import { parse } from '../expressions';
import type { MaybeMissing } from '../json';
import { cacheGet, cacheSet } from '../parserCache';
import { createConstVariable, Variable } from '../variable';
import type { Func } from './funcs';
import { funcToKey, type Func } from './funcs';
export type CustomFunctions = Map<string, Func[]>;
@@ -99,3 +99,38 @@ export function customFunctionWrap(fn: DivFunction): Func {
},
};
}
export function mergeCustomFunctions(
map0: CustomFunctions | undefined,
map1: CustomFunctions | undefined
): CustomFunctions | undefined {
if (!map0) {
return map1 || undefined;
}
if (!map1) {
return;
}
const merged = new Map();
const defined = new Set<string>();
for (const [name, funcs] of map1) {
for (const func of funcs) {
const key = funcToKey(name, func);
defined.add(key);
}
merged.set(name, funcs);
}
for (const [name, funcs] of map0) {
for (const func of funcs) {
const key = funcToKey(name, func);
if (!defined.has(key)) {
defined.add(key);
const list = merged.get(name) || [];
list.push(func);
merged.set(name, list);
}
}
}
return merged;
}
@@ -301,3 +301,7 @@ export function convertArgs(func: Func, args: EvalValue[]): EvalValue[] {
return arg;
});
}
export function funcToKey(funcName: string, func: Func): string {
return funcName + ':' + func.args.map(arg => typeof arg === 'string' ? arg : arg.type).join('#');
}