@matreshka/bff 0.38.0 → 0.40.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.
Files changed (31) hide show
  1. package/dist/components/file-picker.d.ts +37 -0
  2. package/dist/components/file-picker.js +78 -0
  3. package/dist/components/index.d.ts +1 -1
  4. package/dist/components/index.js +1 -1
  5. package/dist/components/inputs/input-component.d.ts +1 -1
  6. package/dist/core/actions/context-local-action-config.d.ts +13 -0
  7. package/dist/core/actions/context-local-action-config.js +7 -0
  8. package/dist/core/actions/dec-context-value.d.ts +7 -5
  9. package/dist/core/actions/dec-context-value.js +6 -2
  10. package/dist/core/actions/inc-context-value.d.ts +7 -5
  11. package/dist/core/actions/inc-context-value.js +6 -2
  12. package/dist/core/actions/set-context.d.ts +12 -4
  13. package/dist/core/actions/set-context.js +6 -1
  14. package/dist/core/actions/toggle-context-value.d.ts +7 -4
  15. package/dist/core/actions/toggle-context-value.js +6 -1
  16. package/dist/core/context/context-ref.d.ts +45 -11
  17. package/dist/core/context/context.d.ts +46 -10
  18. package/dist/core/types/context-value-ref.d.ts +23 -24
  19. package/dist/platforms/client-platform.d.ts +16 -0
  20. package/dist/platforms/client-platform.js +24 -0
  21. package/dist/platforms/index.d.ts +3 -0
  22. package/dist/platforms/index.js +3 -0
  23. package/dist/platforms/local-file.d.ts +54 -0
  24. package/dist/platforms/local-file.js +106 -0
  25. package/dist/platforms/media-stream-ref.d.ts +29 -0
  26. package/dist/platforms/media-stream-ref.js +78 -0
  27. package/dist/platforms/recording.d.ts +58 -0
  28. package/dist/platforms/recording.js +203 -0
  29. package/package.json +2 -2
  30. package/dist/components/file-upload-area.d.ts +0 -79
  31. package/dist/components/file-upload-area.js +0 -135
@@ -0,0 +1,37 @@
1
+ import { ServerComponentClass } from "@matreshka/client-bff-protocol/enums/server-component-class";
2
+ import type { FilePickerConfig } from "@matreshka/client-bff-protocol/types/file-picker-config";
3
+ import { Componentable, ComponentTreeNode, MixedWithCallbacksArray, StandaloneComponent } from "../core";
4
+ import { LocalFile } from "../platforms/local-file";
5
+ import { Component, ComponentInitConfig, ComponentProperties } from "./component";
6
+ /**
7
+ * Свойства компонента выбора файла.
8
+ */
9
+ export type FilePickerProperties = {
10
+ multiple: boolean;
11
+ content: ComponentTreeNode[];
12
+ accept?: string;
13
+ } & ComponentProperties;
14
+ /**
15
+ * Конфигурация инициализации компонента выбора файла.
16
+ */
17
+ export type FilePickerInitConfig<ComponentType extends Componentable = FilePicker, PropertiesType extends FilePickerProperties = FilePickerProperties> = {
18
+ multiple: boolean;
19
+ content: MixedWithCallbacksArray<ComponentTreeNode>;
20
+ accept?: string;
21
+ onSelect?: (files: LocalFile[]) => void;
22
+ } & ComponentInitConfig<ComponentType, PropertiesType>;
23
+ type DefaultInitConfigType = FilePickerInitConfig<FilePicker>;
24
+ export declare function filePicker(options: Omit<DefaultInitConfigType, "content">, content: DefaultInitConfigType["content"]): FilePicker;
25
+ export declare function filePicker(config: DefaultInitConfigType): FilePicker;
26
+ /**
27
+ * Компонент выбора файлов (без авто-upload).
28
+ */
29
+ export declare class FilePicker<InitConfigType extends FilePickerInitConfig<any> = DefaultInitConfigType, PropertiesType extends FilePickerProperties = FilePickerProperties> extends Component<InitConfigType, PropertiesType> implements StandaloneComponent {
30
+ constructor(config: InitConfigType);
31
+ standalone(): true;
32
+ protected class(): ServerComponentClass;
33
+ protected initProperties(initValues: InitConfigType): PropertiesType;
34
+ protected serializeRuleOverrides(overrides: Partial<PropertiesType>): Record<string, unknown>;
35
+ serialize(): FilePickerConfig;
36
+ }
37
+ export {};
@@ -0,0 +1,78 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.FilePicker = void 0;
4
+ exports.filePicker = filePicker;
5
+ const server_component_class_1 = require("@matreshka/client-bff-protocol/enums/server-component-class");
6
+ const zod_1 = require("zod");
7
+ const core_1 = require("../core");
8
+ const client_context_1 = require("../core/client-context");
9
+ const local_file_1 = require("../platforms/local-file");
10
+ const component_1 = require("./component");
11
+ function filePicker(arg0, arg1) {
12
+ if (arg1 !== undefined) {
13
+ return new FilePicker({
14
+ ...arg0,
15
+ content: arg1,
16
+ });
17
+ }
18
+ return new FilePicker(arg0);
19
+ }
20
+ const filePayloadSchema = zod_1.z.strictObject({
21
+ id: zod_1.z.string(),
22
+ type: zod_1.z.string(),
23
+ size: zod_1.z.number(),
24
+ name: zod_1.z.string(),
25
+ lastModified: zod_1.z.number(),
26
+ url: zod_1.z.string().refine((val) => val.startsWith("blob:"), {
27
+ message: "url must be a blob",
28
+ }),
29
+ });
30
+ /**
31
+ * Компонент выбора файлов (без авто-upload).
32
+ */
33
+ class FilePicker extends component_1.Component {
34
+ constructor(config) {
35
+ super(config);
36
+ this.onInteraction("select", ({ payload }) => {
37
+ const schema = zod_1.z.strictObject({
38
+ files: zod_1.z.array(filePayloadSchema).min(1),
39
+ });
40
+ const safePayload = schema.parse(payload);
41
+ const client = (0, client_context_1.currentClient)();
42
+ const files = safePayload.files.map((file) => new local_file_1.LocalFile(client, file));
43
+ this.config.onSelect?.(files);
44
+ });
45
+ }
46
+ standalone() {
47
+ return true;
48
+ }
49
+ class() {
50
+ return server_component_class_1.ServerComponentClass.FilePicker;
51
+ }
52
+ initProperties(initValues) {
53
+ return {
54
+ ...super.initProperties(initValues),
55
+ content: (0, core_1.calculateComponents)(initValues.content),
56
+ accept: initValues.accept,
57
+ multiple: initValues.multiple,
58
+ };
59
+ }
60
+ serializeRuleOverrides(overrides) {
61
+ const out = super.serializeRuleOverrides(overrides);
62
+ if (overrides.content !== undefined) {
63
+ out.content = (0, core_1.serializeComponentList)(overrides.content);
64
+ }
65
+ return out;
66
+ }
67
+ serialize() {
68
+ const result = super.serialize();
69
+ return {
70
+ ...result,
71
+ class: server_component_class_1.ServerComponentClass.FilePicker,
72
+ properties: this.buildPropertiesForClient({
73
+ content: (0, core_1.serializeComponentList)(this.properties.content),
74
+ }),
75
+ };
76
+ }
77
+ }
78
+ exports.FilePicker = FilePicker;
@@ -19,7 +19,7 @@ export * from "./charts/polar-area-chart";
19
19
  export * from "./charts/radar-chart";
20
20
  export * from "./page";
21
21
  export * from "./dialog";
22
- export * from "./file-upload-area";
22
+ export * from "./file-picker";
23
23
  export * from "./for-each";
24
24
  export * from "./inputs/input-component";
25
25
  export * from "./inputs/number-input";
@@ -37,7 +37,7 @@ __exportStar(require("./charts/polar-area-chart"), exports);
37
37
  __exportStar(require("./charts/radar-chart"), exports);
38
38
  __exportStar(require("./page"), exports);
39
39
  __exportStar(require("./dialog"), exports);
40
- __exportStar(require("./file-upload-area"), exports);
40
+ __exportStar(require("./file-picker"), exports);
41
41
  __exportStar(require("./for-each"), exports);
42
42
  __exportStar(require("./inputs/input-component"), exports);
43
43
  __exportStar(require("./inputs/number-input"), exports);
@@ -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 { ActionConfig } from "./action";
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
- * (с sync на BFF через `context-values`).
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
- constructor(ref: NumberValueRef, step: ValueOrContextRef<number>, config?: ActionConfig);
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?: ActionConfig): DecContextValue;
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
- * (с sync на BFF через `context-values`).
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 { ActionConfig } from "./action";
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
- * (с sync на BFF через `context-values`).
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
- constructor(ref: NumberValueRef, step: ValueOrContextRef<number>, config?: ActionConfig);
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?: ActionConfig): IncContextValue;
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
- * (с sync на BFF через `context-values`).
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 { ActionConfig } from "./action";
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 на клиенте (с sync на BFF через `context-values`).
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
- constructor(context: Context<T, S>, patch: LocalContextPathPatch<T, S>, config?: ActionConfig);
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: LocalContextPathPatch<T, S>, config?: ActionConfig): SetContext<T, S>;
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 на клиенте (с sync на BFF через `context-values`).
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 { ActionConfig } from "./action";
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 на клиенте (с sync на BFF через `context-values`).
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
- constructor(ref: BooleanValueRef, config?: ActionConfig);
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?: ActionConfig): ToggleContextValue;
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 на клиенте (с sync на BFF через `context-values`).
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 { ActionConfig } from "../actions/action";
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
- type ContextRefLocalSet<ContextType extends JsonObject, PathType extends string, SchemaType extends z.ZodObject | undefined> = PathType extends WritableContextPath<ContextType, SchemaType> ? {
14
- set: (value: ValueOrContextRef<WritablePathValue<SchemaType, PathType>>, config?: ActionConfig) => SetContext<ContextType, SchemaType>;
15
- } : {};
16
- type ContextRefLocalToggle = {
17
- toggle: (config?: ActionConfig) => ToggleContextValue;
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
- type ContextRefLocalNumeric = {
20
- inc: (step?: ValueOrContextRef<number>, config?: ActionConfig) => IncContextValue;
21
- dec: (step?: ValueOrContextRef<number>, config?: ActionConfig) => DecContextValue;
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> & (PathType extends WritableContextPath<ContextType, SchemaType> ? WritablePathValue<SchemaType, PathType> extends boolean | undefined ? ContextRefLocalToggle : {} : {}) & (PathType extends WritableContextPath<ContextType, SchemaType> ? WritablePathValue<SchemaType, PathType> extends number | undefined ? ContextRefLocalNumeric : {} : {});
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 { ActionConfig } from "../actions/action";
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
- type IsAny<T> = 0 extends 1 & T ? true : false;
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 ? unknown : S extends z.ZodObject ? P extends Paths<WritableSchemaInput<S>> ? PathValue<WritableSchemaInput<S>, P> : never : never;
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>;