@matreshka/bff 0.38.0 → 0.39.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/components/inputs/input-component.d.ts +1 -1
- package/dist/core/actions/context-local-action-config.d.ts +13 -0
- package/dist/core/actions/context-local-action-config.js +7 -0
- package/dist/core/actions/dec-context-value.d.ts +7 -5
- package/dist/core/actions/dec-context-value.js +6 -2
- package/dist/core/actions/inc-context-value.d.ts +7 -5
- package/dist/core/actions/inc-context-value.js +6 -2
- package/dist/core/actions/set-context.d.ts +12 -4
- package/dist/core/actions/set-context.js +6 -1
- package/dist/core/actions/toggle-context-value.d.ts +7 -4
- package/dist/core/actions/toggle-context-value.js +6 -1
- package/dist/core/context/context-ref.d.ts +45 -11
- package/dist/core/context/context.d.ts +46 -10
- package/dist/core/types/context-value-ref.d.ts +23 -24
- package/package.json +1 -1
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { AnySchemaContextRef, Componentable, StandaloneComponent, WritableContextRefOfValue } from "../../core";
|
|
2
2
|
import { Component, ComponentInitConfig, ComponentProperties } from "../component";
|
|
3
|
-
/** Ref на поле ввода: значение `ValueType | undefined
|
|
3
|
+
/** Ref на поле ввода: значение `ValueType | undefined`, путь должен быть writable. */
|
|
4
4
|
export type InputContextRef<ValueType> = WritableContextRefOfValue<ValueType | undefined>;
|
|
5
5
|
/** Ref на строковое поле ввода. */
|
|
6
6
|
export type StringInputContextRef = InputContextRef<string>;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { ActionConfig } from "./action";
|
|
2
|
+
/**
|
|
3
|
+
* Config локальной записи Context.
|
|
4
|
+
* `sync: false` пишет только клиентское зеркало и не шлёт `context-values`.
|
|
5
|
+
* Поле не входит в общий {@link ActionConfig}, чтобы не появиться у прочих действий.
|
|
6
|
+
*/
|
|
7
|
+
export type ContextLocalActionConfig = ActionConfig & {
|
|
8
|
+
sync?: boolean;
|
|
9
|
+
};
|
|
10
|
+
/** В payload попадает только явное отключение синхронизации. */
|
|
11
|
+
export declare function contextLocalSyncField(sync: boolean | undefined): {
|
|
12
|
+
sync: false;
|
|
13
|
+
} | Record<string, never>;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.contextLocalSyncField = contextLocalSyncField;
|
|
4
|
+
/** В payload попадает только явное отключение синхронизации. */
|
|
5
|
+
function contextLocalSyncField(sync) {
|
|
6
|
+
return sync === false ? { sync: false } : {};
|
|
7
|
+
}
|
|
@@ -1,15 +1,17 @@
|
|
|
1
1
|
import { ValueOrContextRef, WritableContextRefOfValue } from "../types/context-value-ref";
|
|
2
|
-
import {
|
|
2
|
+
import { ContextLocalActionConfig } from "./context-local-action-config";
|
|
3
3
|
import { NumberValueRef } from "./inc-context-value";
|
|
4
4
|
import { LocalAction } from "./local-action";
|
|
5
5
|
/**
|
|
6
|
-
* Уменьшение числового значения в Context на клиенте на `step
|
|
7
|
-
*
|
|
6
|
+
* Уменьшение числового значения в Context на клиенте на `step`.
|
|
7
|
+
* По умолчанию синхронизируется на BFF через `context-values`.
|
|
8
|
+
* `config.sync: false` оставляет значение только в клиентском зеркале.
|
|
8
9
|
*/
|
|
9
10
|
export declare class DecContextValue extends LocalAction {
|
|
10
11
|
private readonly ref;
|
|
11
12
|
private readonly step;
|
|
12
|
-
|
|
13
|
+
private readonly sync;
|
|
14
|
+
constructor(ref: NumberValueRef, step: ValueOrContextRef<number>, config?: ContextLocalActionConfig);
|
|
13
15
|
class(): string;
|
|
14
16
|
payload(): object;
|
|
15
17
|
}
|
|
@@ -18,4 +20,4 @@ export declare class DecContextValue extends LocalAction {
|
|
|
18
20
|
* Принимает только `ContextRef` с числовым значением.
|
|
19
21
|
* Без `step` клиент вычитает 1.
|
|
20
22
|
*/
|
|
21
|
-
export declare function decContextValue(ref: WritableContextRefOfValue<number | undefined>, step?: ValueOrContextRef<number>, config?:
|
|
23
|
+
export declare function decContextValue(ref: WritableContextRefOfValue<number | undefined>, step?: ValueOrContextRef<number>, config?: ContextLocalActionConfig): DecContextValue;
|
|
@@ -3,16 +3,19 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.DecContextValue = void 0;
|
|
4
4
|
exports.decContextValue = decContextValue;
|
|
5
5
|
const condition_operand_1 = require("../conditions/condition-operand");
|
|
6
|
+
const context_local_action_config_1 = require("./context-local-action-config");
|
|
6
7
|
const local_action_1 = require("./local-action");
|
|
7
8
|
/**
|
|
8
|
-
* Уменьшение числового значения в Context на клиенте на `step
|
|
9
|
-
*
|
|
9
|
+
* Уменьшение числового значения в Context на клиенте на `step`.
|
|
10
|
+
* По умолчанию синхронизируется на BFF через `context-values`.
|
|
11
|
+
* `config.sync: false` оставляет значение только в клиентском зеркале.
|
|
10
12
|
*/
|
|
11
13
|
class DecContextValue extends local_action_1.LocalAction {
|
|
12
14
|
constructor(ref, step, config = {}) {
|
|
13
15
|
super(config.conditions);
|
|
14
16
|
this.ref = ref;
|
|
15
17
|
this.step = step;
|
|
18
|
+
this.sync = config.sync;
|
|
16
19
|
}
|
|
17
20
|
class() {
|
|
18
21
|
return "dec-context-value";
|
|
@@ -21,6 +24,7 @@ class DecContextValue extends local_action_1.LocalAction {
|
|
|
21
24
|
return {
|
|
22
25
|
ref: this.ref,
|
|
23
26
|
step: (0, condition_operand_1.toSerializedOperand)(this.step),
|
|
27
|
+
...(0, context_local_action_config_1.contextLocalSyncField)(this.sync),
|
|
24
28
|
};
|
|
25
29
|
}
|
|
26
30
|
}
|
|
@@ -1,16 +1,18 @@
|
|
|
1
1
|
import { ContextRefOfValue, ValueOrContextRef, WritableContextRefOfValue } from "../types/context-value-ref";
|
|
2
|
-
import {
|
|
2
|
+
import { ContextLocalActionConfig } from "./context-local-action-config";
|
|
3
3
|
import { LocalAction } from "./local-action";
|
|
4
4
|
/** Ref на числовое поле контекста. */
|
|
5
5
|
export type NumberValueRef = ContextRefOfValue<number | undefined>;
|
|
6
6
|
/**
|
|
7
|
-
* Изменение числового значения в Context на клиенте на `step
|
|
8
|
-
*
|
|
7
|
+
* Изменение числового значения в Context на клиенте на `step`.
|
|
8
|
+
* По умолчанию синхронизируется на BFF через `context-values`.
|
|
9
|
+
* `config.sync: false` оставляет значение только в клиентском зеркале.
|
|
9
10
|
*/
|
|
10
11
|
export declare class IncContextValue extends LocalAction {
|
|
11
12
|
private readonly ref;
|
|
12
13
|
private readonly step;
|
|
13
|
-
|
|
14
|
+
private readonly sync;
|
|
15
|
+
constructor(ref: NumberValueRef, step: ValueOrContextRef<number>, config?: ContextLocalActionConfig);
|
|
14
16
|
class(): string;
|
|
15
17
|
payload(): object;
|
|
16
18
|
}
|
|
@@ -19,4 +21,4 @@ export declare class IncContextValue extends LocalAction {
|
|
|
19
21
|
* Принимает только `ContextRef` с числовым значением.
|
|
20
22
|
* Без `step` клиент прибавляет 1.
|
|
21
23
|
*/
|
|
22
|
-
export declare function incContextValue(ref: WritableContextRefOfValue<number | undefined>, step?: ValueOrContextRef<number>, config?:
|
|
24
|
+
export declare function incContextValue(ref: WritableContextRefOfValue<number | undefined>, step?: ValueOrContextRef<number>, config?: ContextLocalActionConfig): IncContextValue;
|
|
@@ -3,16 +3,19 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.IncContextValue = void 0;
|
|
4
4
|
exports.incContextValue = incContextValue;
|
|
5
5
|
const condition_operand_1 = require("../conditions/condition-operand");
|
|
6
|
+
const context_local_action_config_1 = require("./context-local-action-config");
|
|
6
7
|
const local_action_1 = require("./local-action");
|
|
7
8
|
/**
|
|
8
|
-
* Изменение числового значения в Context на клиенте на `step
|
|
9
|
-
*
|
|
9
|
+
* Изменение числового значения в Context на клиенте на `step`.
|
|
10
|
+
* По умолчанию синхронизируется на BFF через `context-values`.
|
|
11
|
+
* `config.sync: false` оставляет значение только в клиентском зеркале.
|
|
10
12
|
*/
|
|
11
13
|
class IncContextValue extends local_action_1.LocalAction {
|
|
12
14
|
constructor(ref, step, config = {}) {
|
|
13
15
|
super(config.conditions);
|
|
14
16
|
this.ref = ref;
|
|
15
17
|
this.step = step;
|
|
18
|
+
this.sync = config.sync;
|
|
16
19
|
}
|
|
17
20
|
class() {
|
|
18
21
|
return "inc-context-value";
|
|
@@ -21,6 +24,7 @@ class IncContextValue extends local_action_1.LocalAction {
|
|
|
21
24
|
return {
|
|
22
25
|
ref: this.ref,
|
|
23
26
|
step: (0, condition_operand_1.toSerializedOperand)(this.step),
|
|
27
|
+
...(0, context_local_action_config_1.contextLocalSyncField)(this.sync),
|
|
24
28
|
};
|
|
25
29
|
}
|
|
26
30
|
}
|
|
@@ -1,21 +1,29 @@
|
|
|
1
1
|
import { JsonObject } from "@matreshka/client-bff-protocol/types/json";
|
|
2
|
+
import { Paths, PathValue } from "ts-essentials";
|
|
2
3
|
import { z } from "zod";
|
|
3
4
|
import { Context, WritableContextPath, WritablePathValue } from "../context/context";
|
|
4
5
|
import { ValueOrContextRef } from "../types/context-value-ref";
|
|
5
|
-
import {
|
|
6
|
+
import { ContextLocalActionConfig } from "./context-local-action-config";
|
|
6
7
|
import { LocalAction } from "./local-action";
|
|
7
8
|
export type LocalContextPathPatch<T extends JsonObject, S extends z.ZodObject | undefined> = Partial<{
|
|
8
9
|
[K in WritableContextPath<T, S>]: ValueOrContextRef<WritablePathValue<S, K>>;
|
|
9
10
|
}>;
|
|
11
|
+
/** Patch по любому пути данных. Для `sync: false`, без проверки схемы. */
|
|
12
|
+
export type LocalDataPathPatch<T extends JsonObject> = Partial<{
|
|
13
|
+
[K in Paths<T>]: ValueOrContextRef<PathValue<T, K>>;
|
|
14
|
+
}>;
|
|
10
15
|
/**
|
|
11
|
-
* Локальная запись patch в Context на
|
|
16
|
+
* Локальная запись patch в Context на клиенте.
|
|
17
|
+
* По умолчанию синхронизируется на BFF через `context-values`.
|
|
18
|
+
* `config.sync: false` оставляет значение только в клиентском зеркале.
|
|
12
19
|
*/
|
|
13
20
|
export declare class SetContext<T extends JsonObject, S extends z.ZodObject | undefined> extends LocalAction {
|
|
14
21
|
private readonly context;
|
|
15
22
|
private readonly patch;
|
|
16
|
-
|
|
23
|
+
private readonly sync;
|
|
24
|
+
constructor(context: Context<T, S>, patch: LocalDataPathPatch<T>, config?: ContextLocalActionConfig);
|
|
17
25
|
class(): string;
|
|
18
26
|
payload(): object;
|
|
19
27
|
}
|
|
20
28
|
/** @internal */
|
|
21
|
-
export declare function createSetContext<T extends JsonObject, S extends z.ZodObject | undefined>(context: Context<T, S>, patch:
|
|
29
|
+
export declare function createSetContext<T extends JsonObject, S extends z.ZodObject | undefined>(context: Context<T, S>, patch: LocalDataPathPatch<T>, config?: ContextLocalActionConfig): SetContext<T, S>;
|
|
@@ -3,15 +3,19 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.SetContext = void 0;
|
|
4
4
|
exports.createSetContext = createSetContext;
|
|
5
5
|
const condition_operand_1 = require("../conditions/condition-operand");
|
|
6
|
+
const context_local_action_config_1 = require("./context-local-action-config");
|
|
6
7
|
const local_action_1 = require("./local-action");
|
|
7
8
|
/**
|
|
8
|
-
* Локальная запись patch в Context на
|
|
9
|
+
* Локальная запись patch в Context на клиенте.
|
|
10
|
+
* По умолчанию синхронизируется на BFF через `context-values`.
|
|
11
|
+
* `config.sync: false` оставляет значение только в клиентском зеркале.
|
|
9
12
|
*/
|
|
10
13
|
class SetContext extends local_action_1.LocalAction {
|
|
11
14
|
constructor(context, patch, config = {}) {
|
|
12
15
|
super(config.conditions);
|
|
13
16
|
this.context = context;
|
|
14
17
|
this.patch = patch;
|
|
18
|
+
this.sync = config.sync;
|
|
15
19
|
}
|
|
16
20
|
class() {
|
|
17
21
|
return "set-context";
|
|
@@ -23,6 +27,7 @@ class SetContext extends local_action_1.LocalAction {
|
|
|
23
27
|
key,
|
|
24
28
|
(0, condition_operand_1.toSerializedOperand)(value),
|
|
25
29
|
])),
|
|
30
|
+
...(0, context_local_action_config_1.contextLocalSyncField)(this.sync),
|
|
26
31
|
};
|
|
27
32
|
}
|
|
28
33
|
}
|
|
@@ -1,14 +1,17 @@
|
|
|
1
1
|
import { ContextRefOfValue, WritableContextRefOfValue } from "../types/context-value-ref";
|
|
2
|
-
import {
|
|
2
|
+
import { ContextLocalActionConfig } from "./context-local-action-config";
|
|
3
3
|
import { LocalAction } from "./local-action";
|
|
4
4
|
/** Ref на boolean-поле контекста. */
|
|
5
5
|
export type BooleanValueRef = ContextRefOfValue<boolean | undefined>;
|
|
6
6
|
/**
|
|
7
|
-
* Инверсия boolean-значения в Context на
|
|
7
|
+
* Инверсия boolean-значения в Context на клиенте.
|
|
8
|
+
* По умолчанию синхронизируется на BFF через `context-values`.
|
|
9
|
+
* `config.sync: false` оставляет значение только в клиентском зеркале.
|
|
8
10
|
*/
|
|
9
11
|
export declare class ToggleContextValue extends LocalAction {
|
|
10
12
|
private readonly ref;
|
|
11
|
-
|
|
13
|
+
private readonly sync;
|
|
14
|
+
constructor(ref: BooleanValueRef, config?: ContextLocalActionConfig);
|
|
12
15
|
class(): string;
|
|
13
16
|
payload(): object;
|
|
14
17
|
}
|
|
@@ -16,4 +19,4 @@ export declare class ToggleContextValue extends LocalAction {
|
|
|
16
19
|
* Функциональная форма {@link ToggleContextValue}.
|
|
17
20
|
* Принимает только `ContextRef` с boolean-значением.
|
|
18
21
|
*/
|
|
19
|
-
export declare function toggleContextValue(ref: WritableContextRefOfValue<boolean | undefined>, config?:
|
|
22
|
+
export declare function toggleContextValue(ref: WritableContextRefOfValue<boolean | undefined>, config?: ContextLocalActionConfig): ToggleContextValue;
|
|
@@ -2,14 +2,18 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.ToggleContextValue = void 0;
|
|
4
4
|
exports.toggleContextValue = toggleContextValue;
|
|
5
|
+
const context_local_action_config_1 = require("./context-local-action-config");
|
|
5
6
|
const local_action_1 = require("./local-action");
|
|
6
7
|
/**
|
|
7
|
-
* Инверсия boolean-значения в Context на
|
|
8
|
+
* Инверсия boolean-значения в Context на клиенте.
|
|
9
|
+
* По умолчанию синхронизируется на BFF через `context-values`.
|
|
10
|
+
* `config.sync: false` оставляет значение только в клиентском зеркале.
|
|
8
11
|
*/
|
|
9
12
|
class ToggleContextValue extends local_action_1.LocalAction {
|
|
10
13
|
constructor(ref, config = {}) {
|
|
11
14
|
super(config.conditions);
|
|
12
15
|
this.ref = ref;
|
|
16
|
+
this.sync = config.sync;
|
|
13
17
|
}
|
|
14
18
|
class() {
|
|
15
19
|
return "toggle-context-value";
|
|
@@ -17,6 +21,7 @@ class ToggleContextValue extends local_action_1.LocalAction {
|
|
|
17
21
|
payload() {
|
|
18
22
|
return {
|
|
19
23
|
ref: this.ref,
|
|
24
|
+
...(0, context_local_action_config_1.contextLocalSyncField)(this.sync),
|
|
20
25
|
};
|
|
21
26
|
}
|
|
22
27
|
}
|
|
@@ -2,7 +2,7 @@ import { JsonObject } from "@matreshka/client-bff-protocol/types/json";
|
|
|
2
2
|
import { Observable } from "rxjs";
|
|
3
3
|
import { Paths, PathValue } from "ts-essentials";
|
|
4
4
|
import { z } from "zod";
|
|
5
|
-
import {
|
|
5
|
+
import { ContextLocalActionConfig } from "../actions/context-local-action-config";
|
|
6
6
|
import { DecContextValue } from "../actions/dec-context-value";
|
|
7
7
|
import { IncContextValue } from "../actions/inc-context-value";
|
|
8
8
|
import { SetContext } from "../actions/set-context";
|
|
@@ -10,19 +10,53 @@ import { ToggleContextValue } from "../actions/toggle-context-value";
|
|
|
10
10
|
import type { ContextUpdateDelivery } from "../types/context-update-delivery";
|
|
11
11
|
import { ValueOrContextRef } from "../types/context-value-ref";
|
|
12
12
|
import { Context, WritableContextPath, WritablePathValue } from "./context";
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
13
|
+
/** `0 extends 1 & any` даёт false; этот вариант корректно ловит `any`. */
|
|
14
|
+
type IsAny<T> = (<G>() => G extends T ? 1 : 2) extends <G>() => G extends any ? 1 : 2 ? true : false;
|
|
15
|
+
type SyncOnConfig = ContextLocalActionConfig & {
|
|
16
|
+
sync?: true;
|
|
17
|
+
};
|
|
18
|
+
type SyncOffConfig = ContextLocalActionConfig & {
|
|
19
|
+
sync: false;
|
|
18
20
|
};
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
21
|
+
/**
|
|
22
|
+
* Writable для local.set без `sync: false`.
|
|
23
|
+
* `SchemaType = any` (дефолт Context) — считаем writable: иначе ломаются inputs.
|
|
24
|
+
* `PathType = any` — не writable, чтобы не открывать обе ветки conditional.
|
|
25
|
+
*/
|
|
26
|
+
type IsWritableLocalPath<ContextType extends JsonObject, PathType extends string, SchemaType extends z.ZodObject | undefined> = IsAny<PathType> extends true ? false : IsAny<SchemaType> extends true ? true : PathType extends WritableContextPath<ContextType, SchemaType> ? true : false;
|
|
27
|
+
type ValueMatches<Value, Expected> = IsAny<Value> extends true ? false : [Value] extends [never] ? false : [Value] extends [Expected] ? true : false;
|
|
28
|
+
type ContextRefLocalSet<ContextType extends JsonObject, PathType extends string, SchemaType extends z.ZodObject | undefined> = IsWritableLocalPath<ContextType, PathType, SchemaType> extends true ? {
|
|
29
|
+
set: (value: ValueOrContextRef<PathValue<ContextType, PathType>>, config?: ContextLocalActionConfig) => SetContext<any, any>;
|
|
30
|
+
} : {
|
|
31
|
+
set: (value: ValueOrContextRef<PathValue<ContextType, PathType>>, config: SyncOffConfig) => SetContext<any, any>;
|
|
32
|
+
};
|
|
33
|
+
type ContextRefLocalToggle<ContextType extends JsonObject, PathType extends string, SchemaType extends z.ZodObject | undefined> = ValueMatches<PathValue<ContextType, PathType>, boolean | undefined> extends true ? IsWritableLocalPath<ContextType, PathType, SchemaType> extends true ? ValueMatches<WritablePathValue<SchemaType, PathType>, boolean | undefined> extends true ? {
|
|
34
|
+
toggle: {
|
|
35
|
+
(config?: SyncOnConfig): ToggleContextValue;
|
|
36
|
+
(config: SyncOffConfig): ToggleContextValue;
|
|
37
|
+
};
|
|
38
|
+
} : {
|
|
39
|
+
toggle: (config: SyncOffConfig) => ToggleContextValue;
|
|
40
|
+
} : {
|
|
41
|
+
toggle: (config: SyncOffConfig) => ToggleContextValue;
|
|
42
|
+
} : {};
|
|
43
|
+
type ContextRefLocalNumeric<ContextType extends JsonObject, PathType extends string, SchemaType extends z.ZodObject | undefined> = ValueMatches<PathValue<ContextType, PathType>, number | undefined> extends true ? IsWritableLocalPath<ContextType, PathType, SchemaType> extends true ? ValueMatches<WritablePathValue<SchemaType, PathType>, number | undefined> extends true ? {
|
|
44
|
+
inc: {
|
|
45
|
+
(step?: ValueOrContextRef<number>, config?: SyncOnConfig): IncContextValue;
|
|
46
|
+
(step: ValueOrContextRef<number> | undefined, config: SyncOffConfig): IncContextValue;
|
|
47
|
+
};
|
|
48
|
+
dec: {
|
|
49
|
+
(step?: ValueOrContextRef<number>, config?: SyncOnConfig): DecContextValue;
|
|
50
|
+
(step: ValueOrContextRef<number> | undefined, config: SyncOffConfig): DecContextValue;
|
|
51
|
+
};
|
|
52
|
+
} : ContextRefLocalNumericSyncOff : ContextRefLocalNumericSyncOff : {};
|
|
53
|
+
type ContextRefLocalNumericSyncOff = {
|
|
54
|
+
inc: (step: ValueOrContextRef<number> | undefined, config: SyncOffConfig) => IncContextValue;
|
|
55
|
+
dec: (step: ValueOrContextRef<number> | undefined, config: SyncOffConfig) => DecContextValue;
|
|
22
56
|
};
|
|
23
57
|
/** Локальные действия по ref; `toggle` / `inc` / `dec` — по типу значения по пути. */
|
|
24
|
-
export type ContextRefLocalActions<ContextType extends JsonObject, PathType extends string, SchemaType extends z.ZodObject | undefined> = ContextRefLocalSet<ContextType, PathType, SchemaType> &
|
|
25
|
-
export type AnySchemaContextRef<ContextType extends JsonObject = any, PathType extends string = any> = ContextRef<ContextType, PathType, undefined> | ContextRef<ContextType, PathType, z.ZodObject>;
|
|
58
|
+
export type ContextRefLocalActions<ContextType extends JsonObject, PathType extends string, SchemaType extends z.ZodObject | undefined> = ContextRefLocalSet<ContextType, PathType, SchemaType> & ContextRefLocalToggle<ContextType, PathType, SchemaType> & ContextRefLocalNumeric<ContextType, PathType, SchemaType>;
|
|
59
|
+
export type AnySchemaContextRef<ContextType extends JsonObject = any, PathType extends string = any> = ContextRef<ContextType, PathType, undefined> | ContextRef<ContextType, PathType, z.ZodObject> | ContextRef<ContextType, PathType, any>;
|
|
26
60
|
export type ContextRefValueType<R extends AnySchemaContextRef> = R extends {
|
|
27
61
|
readonly __valueType?: infer ValueType;
|
|
28
62
|
} ? Exclude<ValueType, undefined> : R extends ContextRef<infer ContextType, infer PathType, any> ? PathValue<ContextType, PathType> : never;
|
|
@@ -2,10 +2,10 @@ import { JsonObject } from "@matreshka/client-bff-protocol/types/json";
|
|
|
2
2
|
import { BehaviorSubject, Observable } from "rxjs";
|
|
3
3
|
import { Paths, PathValue } from "ts-essentials";
|
|
4
4
|
import { z } from "zod";
|
|
5
|
-
import {
|
|
5
|
+
import { ContextLocalActionConfig } from "../actions/context-local-action-config";
|
|
6
6
|
import { DecContextValue } from "../actions/dec-context-value";
|
|
7
7
|
import { IncContextValue } from "../actions/inc-context-value";
|
|
8
|
-
import { LocalContextPathPatch } from "../actions/set-context";
|
|
8
|
+
import { LocalContextPathPatch, LocalDataPathPatch, SetContext } from "../actions/set-context";
|
|
9
9
|
import { ToggleContextValue } from "../actions/toggle-context-value";
|
|
10
10
|
import type { Client } from "../client";
|
|
11
11
|
import { ContextUpdateDelivery } from "../types/context-update-delivery";
|
|
@@ -20,12 +20,53 @@ export type ContextPathPatch<T extends JsonObject> = Partial<{
|
|
|
20
20
|
/** Входная форма данных, которые клиент может отправить в `context-values`. */
|
|
21
21
|
export type WritableSchemaInput<S extends z.ZodObject | undefined> = S extends z.ZodObject ? z.input<S> extends JsonObject ? z.input<S> : never : never;
|
|
22
22
|
/** Dotted-пути, разрешённые для клиентской записи (по `schema`). */
|
|
23
|
-
|
|
23
|
+
/** `0 extends 1 & any` даёт false; этот вариант корректно ловит `any`. */
|
|
24
|
+
type IsAny<T> = (<G>() => G extends T ? 1 : 2) extends <G>() => G extends any ? 1 : 2 ? true : false;
|
|
24
25
|
export type WritablePath<S extends z.ZodObject | undefined> = IsAny<S> extends true ? string : S extends z.ZodObject ? Paths<WritableSchemaInput<S>> : never;
|
|
25
26
|
/** Тип значения по writable-пути `P` (из `schema` input). */
|
|
26
|
-
export type WritablePathValue<S extends z.ZodObject | undefined, P extends string> = IsAny<S> extends true ?
|
|
27
|
+
export type WritablePathValue<S extends z.ZodObject | undefined, P extends string> = IsAny<S> extends true ? any : S extends z.ZodObject ? P extends Paths<WritableSchemaInput<S>> ? PathValue<WritableSchemaInput<S>, P> : never : never;
|
|
27
28
|
/** Пересечение путей контекста и путей `schema`, доступных для client write. */
|
|
28
29
|
export type WritableContextPath<T extends JsonObject, S extends z.ZodObject | undefined> = Extract<Paths<T>, WritablePath<S>>;
|
|
30
|
+
type BooleanDataPath<T extends JsonObject> = {
|
|
31
|
+
[K in Paths<T>]: PathValue<T, K> extends boolean | undefined ? K : never;
|
|
32
|
+
}[Paths<T>];
|
|
33
|
+
type NumberDataPath<T extends JsonObject> = {
|
|
34
|
+
[K in Paths<T>]: PathValue<T, K> extends number | undefined ? K : never;
|
|
35
|
+
}[Paths<T>];
|
|
36
|
+
type SyncOnConfig = ContextLocalActionConfig & {
|
|
37
|
+
sync?: true;
|
|
38
|
+
};
|
|
39
|
+
type SyncOffConfig = ContextLocalActionConfig & {
|
|
40
|
+
sync: false;
|
|
41
|
+
};
|
|
42
|
+
type WritableBooleanPath<T extends JsonObject, S extends z.ZodObject | undefined> = {
|
|
43
|
+
[K in WritableContextPath<T, S>]: PathValue<T, K> extends boolean | undefined ? WritablePathValue<S, K> extends boolean | undefined ? K : never : never;
|
|
44
|
+
}[WritableContextPath<T, S>];
|
|
45
|
+
type WritableNumberPath<T extends JsonObject, S extends z.ZodObject | undefined> = {
|
|
46
|
+
[K in WritableContextPath<T, S>]: PathValue<T, K> extends number | undefined ? WritablePathValue<S, K> extends number | undefined ? K : never : never;
|
|
47
|
+
}[WritableContextPath<T, S>];
|
|
48
|
+
type ContextLocalSet<T extends JsonObject, S extends z.ZodObject | undefined> = [WritableContextPath<T, S>] extends [never] ? {
|
|
49
|
+
set: (patch: LocalDataPathPatch<T>, config: SyncOffConfig) => SetContext<T, S>;
|
|
50
|
+
} : {
|
|
51
|
+
set: {
|
|
52
|
+
(patch: LocalContextPathPatch<T, S>, config?: SyncOnConfig): SetContext<T, S>;
|
|
53
|
+
(patch: LocalDataPathPatch<T>, config: SyncOffConfig): SetContext<T, S>;
|
|
54
|
+
};
|
|
55
|
+
};
|
|
56
|
+
type ContextLocalApi<T extends JsonObject, S extends z.ZodObject | undefined> = ContextLocalSet<T, S> & {
|
|
57
|
+
toggle: {
|
|
58
|
+
(path: WritableBooleanPath<T, S>, config?: SyncOnConfig): ToggleContextValue;
|
|
59
|
+
(path: BooleanDataPath<T>, config: SyncOffConfig): ToggleContextValue;
|
|
60
|
+
};
|
|
61
|
+
inc: {
|
|
62
|
+
(path: WritableNumberPath<T, S>, step?: ValueOrContextRef<number>, config?: SyncOnConfig): IncContextValue;
|
|
63
|
+
(path: NumberDataPath<T>, step: ValueOrContextRef<number> | undefined, config: SyncOffConfig): IncContextValue;
|
|
64
|
+
};
|
|
65
|
+
dec: {
|
|
66
|
+
(path: WritableNumberPath<T, S>, step?: ValueOrContextRef<number>, config?: SyncOnConfig): DecContextValue;
|
|
67
|
+
(path: NumberDataPath<T>, step: ValueOrContextRef<number> | undefined, config: SyncOffConfig): DecContextValue;
|
|
68
|
+
};
|
|
69
|
+
};
|
|
29
70
|
/**
|
|
30
71
|
* Пустая strict-схема: клиент не может менять данные контекста через `context-values`.
|
|
31
72
|
* Используется во внутренних контекстах (ForEach, ItemList).
|
|
@@ -66,12 +107,7 @@ export declare class Context<T extends JsonObject, S extends z.ZodObject | undef
|
|
|
66
107
|
*/
|
|
67
108
|
readonly persistent: boolean;
|
|
68
109
|
/** Локальные (client-only) действия над данными этого контекста. */
|
|
69
|
-
readonly local:
|
|
70
|
-
set: (patch: LocalContextPathPatch<T, S>, config?: ActionConfig) => import("../actions/set-context").SetContext<T, S>;
|
|
71
|
-
toggle: <K extends WritableContextPath<T, S>>(path: PathValue<T, K> extends boolean | undefined ? WritablePathValue<S, K> extends boolean | undefined ? K : never : never, config?: ActionConfig) => ToggleContextValue;
|
|
72
|
-
inc: <K extends WritableContextPath<T, S>>(path: PathValue<T, K> extends number | undefined ? WritablePathValue<S, K> extends number | undefined ? K : never : never, step?: ValueOrContextRef<number>, config?: ActionConfig) => IncContextValue;
|
|
73
|
-
dec: <K extends WritableContextPath<T, S>>(path: PathValue<T, K> extends number | undefined ? WritablePathValue<S, K> extends number | undefined ? K : never : never, step?: ValueOrContextRef<number>, config?: ActionConfig) => DecContextValue;
|
|
74
|
-
};
|
|
110
|
+
readonly local: ContextLocalApi<T, S>;
|
|
75
111
|
/** Реактивное хранилище инициализации контекста */
|
|
76
112
|
private _init$;
|
|
77
113
|
readonly init$: Observable<void>;
|
|
@@ -1,7 +1,9 @@
|
|
|
1
|
-
import type { JsonObject } from "@matreshka/client-bff-protocol/types/json";
|
|
2
1
|
import type { Observable } from "rxjs";
|
|
3
|
-
import {
|
|
2
|
+
import type { ContextLocalActionConfig } from "../actions/context-local-action-config";
|
|
3
|
+
import type { DecContextValue } from "../actions/dec-context-value";
|
|
4
|
+
import type { IncContextValue } from "../actions/inc-context-value";
|
|
4
5
|
import type { SetContext } from "../actions/set-context";
|
|
6
|
+
import type { ToggleContextValue } from "../actions/toggle-context-value";
|
|
5
7
|
import { AnySchemaContextRef, ContextRef, ContextRefValueType } from "../context/context-ref";
|
|
6
8
|
import type { ContextUpdateDelivery } from "./context-update-delivery";
|
|
7
9
|
/** Общая часть ref-типов по значению: маркер типа, чтение и запись. */
|
|
@@ -32,30 +34,11 @@ export type ContextRefOfValue<ValueType> = ValueRefBase<ValueType> & ContextRef<
|
|
|
32
34
|
* Ref `R` совместим с ожидаемым типом значения `ValueType` (проверка на call site).
|
|
33
35
|
*/
|
|
34
36
|
export type CompatibleContextRefOfValue<ValueType, R extends AnySchemaContextRef> = ValueType extends ContextRefValueType<R> ? R : never;
|
|
35
|
-
/**
|
|
36
|
-
* Эталонный writable ref для значения `ValueType`: путь `k` гарантированно есть
|
|
37
|
-
* в schema. Из него выводится `local`, так что при добавлении
|
|
38
|
-
* методов в `ContextRef.local` типы вручную переписывать не нужно.
|
|
39
|
-
*/
|
|
40
|
-
type WritableReferenceRef<ValueType> = ContextRef<{
|
|
41
|
-
k: Exclude<ValueType, undefined>;
|
|
42
|
-
} extends infer C extends JsonObject ? C : never, "k", z.ZodObject<{
|
|
43
|
-
k: z.ZodType<Exclude<ValueType, undefined>, Exclude<ValueType, undefined>>;
|
|
44
|
-
}>>;
|
|
45
|
-
/**
|
|
46
|
-
* Стирает привязку `SetContext` к конкретному контексту/schema, остальные
|
|
47
|
-
* сигнатуры `local` остаются как у `ContextRef.local`.
|
|
48
|
-
*/
|
|
49
|
-
type EraseLocalContext<Local> = {
|
|
50
|
-
[K in keyof Local]: Local[K] extends (...args: infer Args) => infer Result ? Result extends SetContext<any, any> ? (...args: Args) => SetContext<any, any> : Local[K] : Local[K];
|
|
51
|
-
};
|
|
52
37
|
/**
|
|
53
38
|
* Writable-ref с известным типом значения без generic по ref.
|
|
54
39
|
*
|
|
55
|
-
*
|
|
56
|
-
* `
|
|
57
|
-
* для эталонного writable ref, поэтому ref с путём вне schema не пройдёт
|
|
58
|
-
* проверку на call site.
|
|
40
|
+
* `local.set` с optional config — sync-on ветка; ref с путём вне schema
|
|
41
|
+
* (только `sync: false`) на call site не пройдёт.
|
|
59
42
|
*
|
|
60
43
|
* @example
|
|
61
44
|
* ```ts
|
|
@@ -65,8 +48,24 @@ type EraseLocalContext<Local> = {
|
|
|
65
48
|
* }
|
|
66
49
|
* ```
|
|
67
50
|
*/
|
|
51
|
+
type WritableLocalByValue<ValueType> = {
|
|
52
|
+
set(value: ValueOrContextRef<Exclude<ValueType, undefined>>, config?: ContextLocalActionConfig & {
|
|
53
|
+
sync?: true;
|
|
54
|
+
}): SetContext<any, any>;
|
|
55
|
+
} & ([Exclude<ValueType, undefined>] extends [boolean] ? {
|
|
56
|
+
toggle(config?: ContextLocalActionConfig & {
|
|
57
|
+
sync?: true;
|
|
58
|
+
}): ToggleContextValue;
|
|
59
|
+
} : {}) & ([Exclude<ValueType, undefined>] extends [number] ? {
|
|
60
|
+
inc(step?: ValueOrContextRef<number>, config?: ContextLocalActionConfig & {
|
|
61
|
+
sync?: true;
|
|
62
|
+
}): IncContextValue;
|
|
63
|
+
dec(step?: ValueOrContextRef<number>, config?: ContextLocalActionConfig & {
|
|
64
|
+
sync?: true;
|
|
65
|
+
}): DecContextValue;
|
|
66
|
+
} : {});
|
|
68
67
|
export type WritableContextRefOfValue<ValueType> = ContextRefOfValue<ValueType> & {
|
|
69
|
-
readonly local:
|
|
68
|
+
readonly local: WritableLocalByValue<ValueType>;
|
|
70
69
|
};
|
|
71
70
|
/** Статическое значение или ссылка на совместимое значение в контексте. */
|
|
72
71
|
export type ValueOrContextRef<ValueType> = ValueType | ContextRefOfValue<Exclude<ValueType, undefined>>;
|
package/package.json
CHANGED