mirror of
https://github.com/divkit/divkit.git
synced 2026-05-07 20:02:32 +00:00
scope in set_stored_value
commit_hash:06f30a8b92130d60fa7cdd70d32d067a409c70c3
This commit is contained in:
@@ -64,7 +64,7 @@
|
||||
import type { TintMode } from '../types/image';
|
||||
import type { VideoElements } from '../types/video';
|
||||
import type { ComponentContext, StateSetter } from '../types/componentContext';
|
||||
import type { Store, StoreAllTypes, StoreTypes } from '../../typings/store';
|
||||
import type { Store, StoreAllTypes, StoreScope, StoreTypes } from '../../typings/store';
|
||||
import Unknown from './utilities/Unknown.svelte';
|
||||
import RootSvgFilters from './utilities/RootSvgFilters.svelte';
|
||||
import { ROOT_CTX, type FocusableMethods, type NodeGetter, type ParentMethods, type RootCtxValue, type Running } from '../context/root';
|
||||
@@ -1173,12 +1173,14 @@
|
||||
});
|
||||
}
|
||||
|
||||
// eslint-disable-next-line max-params
|
||||
function callSetStoredValue(
|
||||
componentContext: ComponentContext | undefined,
|
||||
name: string | null | undefined,
|
||||
value: object | string | bigint | number | boolean | null | undefined,
|
||||
type: string | null | undefined,
|
||||
lifetime: string | number | null | undefined
|
||||
lifetime: string | number | null | undefined,
|
||||
scope?: string | null | undefined
|
||||
): void {
|
||||
const log = componentContext?.logError || logError;
|
||||
if (!store) {
|
||||
@@ -1201,8 +1203,12 @@
|
||||
val = val === 'true' || val === '1';
|
||||
}
|
||||
|
||||
const scopeValue: StoreScope | undefined = (scope === 'global' || scope === 'card') ?
|
||||
scope :
|
||||
undefined;
|
||||
|
||||
if (store.set) {
|
||||
store.set(name, type as StoreAllTypes, val, Number(lifetime));
|
||||
store.set(name, type as StoreAllTypes, val, Number(lifetime), scopeValue);
|
||||
} else if (store.setValue) {
|
||||
if (!AVAIL_SET_STORED_TYPES.has(type)) {
|
||||
log(wrapError(new Error('Incorrect stored type')));
|
||||
@@ -1447,7 +1453,8 @@
|
||||
actionTyped.name,
|
||||
actionTyped.value?.value,
|
||||
actionTyped.value?.type,
|
||||
actionTyped.lifetime
|
||||
actionTyped.lifetime,
|
||||
actionTyped.scope
|
||||
);
|
||||
break;
|
||||
}
|
||||
@@ -1584,7 +1591,14 @@
|
||||
callHideTooltip(params.get('id'), componentContext);
|
||||
break;
|
||||
case 'set_stored_value': {
|
||||
callSetStoredValue(componentContext, params.get('name'), params.get('value'), params.get('type'), params.get('lifetime'));
|
||||
callSetStoredValue(
|
||||
componentContext,
|
||||
params.get('name'),
|
||||
params.get('value'),
|
||||
params.get('type'),
|
||||
params.get('lifetime'),
|
||||
params.get('scope')
|
||||
);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
|
||||
@@ -2,18 +2,23 @@ import type { EvalContext, EvalValue, StringValue } from '../eval';
|
||||
import { registerFunc } from './funcs';
|
||||
import { ARRAY, BOOLEAN, COLOR, DICT, INTEGER, NUMBER, STRING, URL } from '../const';
|
||||
import { checkUrl, convertJsValueToDivKit } from '../utils';
|
||||
import type { StoreScope } from '../../../typings/store';
|
||||
|
||||
export function getStored(evalType: 'string' | 'number' | 'integer' | 'boolean' | 'color' | 'url' | 'array' | 'dict') {
|
||||
return (ctx: EvalContext, name: StringValue, fallback?: EvalValue): EvalValue => {
|
||||
return (ctx: EvalContext, name: StringValue, scope?: EvalValue, fallback?: EvalValue): EvalValue => {
|
||||
const fallbackValue = evalType === DICT || evalType === ARRAY ? undefined : (fallback || scope);
|
||||
const scopeValue = (evalType === DICT || evalType === ARRAY || fallback !== undefined) ? scope : undefined;
|
||||
|
||||
if (!ctx.store) {
|
||||
if (!fallback) {
|
||||
if (!fallbackValue) {
|
||||
throw new Error('Missing value.');
|
||||
}
|
||||
return {
|
||||
type: evalType,
|
||||
value: fallback.value
|
||||
value: fallbackValue.value
|
||||
} as EvalValue;
|
||||
}
|
||||
|
||||
let expectedType: 'boolean' | 'number' | 'string';
|
||||
if (evalType === 'boolean') {
|
||||
expectedType = 'boolean';
|
||||
@@ -22,23 +27,33 @@ export function getStored(evalType: 'string' | 'number' | 'integer' | 'boolean'
|
||||
} else {
|
||||
expectedType = 'string';
|
||||
}
|
||||
|
||||
let scopeStr: StoreScope | undefined;
|
||||
if (scopeValue) {
|
||||
if (scopeValue.value === 'global' || scopeValue.value === 'card') {
|
||||
scopeStr = scopeValue.value;
|
||||
} else {
|
||||
throw new Error('Incorrect scope value');
|
||||
}
|
||||
}
|
||||
|
||||
let val;
|
||||
if (ctx.store.get) {
|
||||
val = ctx.store.get(name.value, evalType);
|
||||
val = ctx.store.get(name.value, evalType, scopeStr);
|
||||
} else if (ctx.store.getValue) {
|
||||
val = ctx.store.getValue(name.value, expectedType);
|
||||
}
|
||||
|
||||
if (val === undefined) {
|
||||
if (!fallback) {
|
||||
if (!fallbackValue) {
|
||||
throw new Error('Missing value.');
|
||||
}
|
||||
if (evalType === 'url') {
|
||||
checkUrl(fallback.value);
|
||||
checkUrl(fallbackValue.value);
|
||||
}
|
||||
return {
|
||||
type: evalType,
|
||||
value: fallback.value
|
||||
value: fallbackValue.value
|
||||
} as EvalValue;
|
||||
} else if (evalType === 'url') {
|
||||
checkUrl(val);
|
||||
@@ -50,13 +65,23 @@ export function getStored(evalType: 'string' | 'number' | 'integer' | 'boolean'
|
||||
|
||||
export function registerStored(): void {
|
||||
registerFunc('getStoredIntegerValue', [STRING, INTEGER], getStored(INTEGER));
|
||||
registerFunc('getStoredIntegerValue', [STRING, STRING, INTEGER], getStored(INTEGER));
|
||||
registerFunc('getStoredNumberValue', [STRING, NUMBER], getStored(NUMBER));
|
||||
registerFunc('getStoredNumberValue', [STRING, STRING, NUMBER], getStored(NUMBER));
|
||||
registerFunc('getStoredStringValue', [STRING, STRING], getStored(STRING));
|
||||
registerFunc('getStoredStringValue', [STRING, STRING, STRING], getStored(STRING));
|
||||
registerFunc('getStoredUrlValue', [STRING, URL], getStored(URL));
|
||||
registerFunc('getStoredUrlValue', [STRING, STRING, URL], getStored(URL));
|
||||
registerFunc('getStoredUrlValue', [STRING, STRING], getStored(URL));
|
||||
registerFunc('getStoredUrlValue', [STRING, STRING, STRING], getStored(URL));
|
||||
registerFunc('getStoredColorValue', [STRING, COLOR], getStored(COLOR));
|
||||
registerFunc('getStoredColorValue', [STRING, STRING, COLOR], getStored(COLOR));
|
||||
registerFunc('getStoredColorValue', [STRING, STRING], getStored(COLOR));
|
||||
registerFunc('getStoredColorValue', [STRING, STRING, STRING], getStored(COLOR));
|
||||
registerFunc('getStoredBooleanValue', [STRING, BOOLEAN], getStored(BOOLEAN));
|
||||
registerFunc('getStoredBooleanValue', [STRING, STRING, BOOLEAN], getStored(BOOLEAN));
|
||||
registerFunc('getStoredArrayValue', [STRING], getStored(ARRAY));
|
||||
registerFunc('getStoredArrayValue', [STRING, STRING], getStored(ARRAY));
|
||||
registerFunc('getStoredDictValue', [STRING], getStored(DICT));
|
||||
registerFunc('getStoredDictValue', [STRING, STRING], getStored(DICT));
|
||||
}
|
||||
|
||||
+2
@@ -1,3 +1,4 @@
|
||||
import type { StoreScope } from './store';
|
||||
import type { Variable } from './variables';
|
||||
|
||||
export type Subscriber<T> = (value: T) => void;
|
||||
@@ -303,6 +304,7 @@ export interface ActionStore {
|
||||
name: string;
|
||||
value: TypedValue;
|
||||
lifetime: number;
|
||||
scope?: StoreScope;
|
||||
}
|
||||
|
||||
export interface ActionSetState {
|
||||
|
||||
Vendored
+7
-2
@@ -1,5 +1,6 @@
|
||||
export type StoreTypes = 'string' | 'number' | 'boolean';
|
||||
export type StoreAllTypes = 'string' | 'integer' | 'number' | 'boolean' | 'color' | 'url' | 'dict' | 'array';
|
||||
export type StoreScope = 'global' | 'card';
|
||||
|
||||
export interface Store {
|
||||
/**
|
||||
@@ -32,10 +33,12 @@ export interface Store {
|
||||
* Fetches primitive from the store
|
||||
* @param name
|
||||
* @param type Expected value tpye
|
||||
* @param scope Value scope
|
||||
*/
|
||||
get?(
|
||||
name: string,
|
||||
type: StoreAllTypes
|
||||
type: StoreAllTypes,
|
||||
scope?: StoreScope
|
||||
): object | string | bigint | number | boolean | undefined;
|
||||
|
||||
/**
|
||||
@@ -44,11 +47,13 @@ export interface Store {
|
||||
* @param type Value type (for example, can be url)
|
||||
* @param value
|
||||
* @param lifetime Value lifetime in seconds
|
||||
* @param scope Value scope
|
||||
*/
|
||||
set?(
|
||||
name: string,
|
||||
type: StoreAllTypes,
|
||||
value: object | string | bigint | number | boolean,
|
||||
lifetime: number
|
||||
lifetime: number,
|
||||
scope?: StoreScope
|
||||
): void;
|
||||
}
|
||||
|
||||
@@ -461,7 +461,8 @@
|
||||
"return_type": "integer",
|
||||
"platforms": [
|
||||
"android",
|
||||
"ios"
|
||||
"ios",
|
||||
"web"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -519,7 +520,8 @@
|
||||
"return_type": "number",
|
||||
"platforms": [
|
||||
"android",
|
||||
"ios"
|
||||
"ios",
|
||||
"web"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -577,7 +579,8 @@
|
||||
"return_type": "string",
|
||||
"platforms": [
|
||||
"android",
|
||||
"ios"
|
||||
"ios",
|
||||
"web"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -635,7 +638,8 @@
|
||||
"return_type": "url",
|
||||
"platforms": [
|
||||
"android",
|
||||
"ios"
|
||||
"ios",
|
||||
"web"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -693,7 +697,8 @@
|
||||
"return_type": "url",
|
||||
"platforms": [
|
||||
"android",
|
||||
"ios"
|
||||
"ios",
|
||||
"web"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -751,7 +756,8 @@
|
||||
"return_type": "color",
|
||||
"platforms": [
|
||||
"android",
|
||||
"ios"
|
||||
"ios",
|
||||
"web"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -809,7 +815,8 @@
|
||||
"return_type": "color",
|
||||
"platforms": [
|
||||
"android",
|
||||
"ios"
|
||||
"ios",
|
||||
"web"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -867,7 +874,8 @@
|
||||
"return_type": "boolean",
|
||||
"platforms": [
|
||||
"android",
|
||||
"ios"
|
||||
"ios",
|
||||
"web"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -912,7 +920,8 @@
|
||||
"return_type": "array",
|
||||
"platforms": [
|
||||
"android",
|
||||
"ios"
|
||||
"ios",
|
||||
"web"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -957,7 +966,8 @@
|
||||
"return_type": "dict",
|
||||
"platforms": [
|
||||
"android",
|
||||
"ios"
|
||||
"ios",
|
||||
"web"
|
||||
]
|
||||
}
|
||||
]
|
||||
|
||||
@@ -28,7 +28,8 @@
|
||||
"card"
|
||||
],
|
||||
"platforms": [
|
||||
"android"
|
||||
"android",
|
||||
"web"
|
||||
]
|
||||
}
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user