From 11e87ff24178f133c4d7fb5eba4567d8c845eacc Mon Sep 17 00:00:00 2001 From: 4eb0da <4eb0da@yandex-team.com> Date: Tue, 26 Nov 2024 18:24:46 +0300 Subject: [PATCH] Fix functions merge commit_hash:56540ef5107f4b7e403d64c8e70ff2c1fdde8ed2 --- client/web/divkit/src/components/Root.svelte | 7 +--- .../src/expressions/funcs/customFuncs.ts | 37 ++++++++++++++++++- .../web/divkit/src/expressions/funcs/funcs.ts | 4 ++ 3 files changed, 42 insertions(+), 6 deletions(-) diff --git a/client/web/divkit/src/components/Root.svelte b/client/web/divkit/src/components/Root.svelte index 7d4eb54bb..c31ea6ade 100644 --- a/client/web/divkit/src/components/Root.svelte +++ b/client/web/divkit/src/components/Root.svelte @@ -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>>( diff --git a/client/web/divkit/src/expressions/funcs/customFuncs.ts b/client/web/divkit/src/expressions/funcs/customFuncs.ts index 8d9afb90e..ba2ff24ee 100644 --- a/client/web/divkit/src/expressions/funcs/customFuncs.ts +++ b/client/web/divkit/src/expressions/funcs/customFuncs.ts @@ -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; @@ -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(); + 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; +} diff --git a/client/web/divkit/src/expressions/funcs/funcs.ts b/client/web/divkit/src/expressions/funcs/funcs.ts index 75e15575d..4985f4594 100644 --- a/client/web/divkit/src/expressions/funcs/funcs.ts +++ b/client/web/divkit/src/expressions/funcs/funcs.ts @@ -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('#'); +}