@matreshka/bff 0.14.2 → 0.15.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.
@@ -21,6 +21,11 @@ export declare class Context<T extends JsonObject> {
21
21
  readonly schema?: ZodObject;
22
22
  /** Флаг, указывающий, что контекст должен быть инициализирован при авторизации клиента */
23
23
  readonly preload: boolean;
24
+ /**
25
+ * Не вызывать {@link destroy} автоматически при отвязке последнего клиента.
26
+ * Явный {@link destroy} по-прежнему обязателен, если данные не нужны весь runtime.
27
+ */
28
+ readonly persistent: boolean;
24
29
  /** Реактивное хранилище инициализации контекста */
25
30
  private _init$;
26
31
  readonly init$: import("rxjs").Observable<void>;
@@ -35,6 +40,7 @@ export declare class Context<T extends JsonObject> {
35
40
  private destroyed;
36
41
  private subscriptions;
37
42
  private dataSubscription?;
43
+ private clientSubscriptions;
38
44
  private assertNotDestroyed;
39
45
  /** Ошибка загрузки initDataCallback — в error$ всех клиентов, привязанных к контексту. */
40
46
  private reportInitFailureToClients;
@@ -48,6 +54,7 @@ export declare class Context<T extends JsonObject> {
48
54
  data: () => Promise<T>;
49
55
  schema?: ZodObject;
50
56
  preload?: boolean;
57
+ persistent?: boolean;
51
58
  });
52
59
  /** `data$` создан (init мог ещё выполняться — см. {@link init}). */
53
60
  inited(): boolean;
@@ -57,6 +64,7 @@ export declare class Context<T extends JsonObject> {
57
64
  isDestroyed(): boolean;
58
65
  destroy(): void;
59
66
  private sendContextInit;
67
+ private releaseClientBinding;
60
68
  authorizeClient(client: Client): void;
61
69
  protected subscribeToDataChanges(): void;
62
70
  /**
@@ -91,10 +91,12 @@ class Context {
91
91
  this.initializing = false;
92
92
  this.destroyed = false;
93
93
  this.subscriptions = new rxjs_1.Subscription();
94
+ this.clientSubscriptions = new Map();
94
95
  this.schema = config.schema;
95
96
  registerContext(this);
96
97
  this.initDataCallback = config.data;
97
98
  this.preload = config.preload ?? false;
99
+ this.persistent = config.persistent ?? false;
98
100
  }
99
101
  /** `data$` создан (init мог ещё выполняться — см. {@link init}). */
100
102
  inited() {
@@ -116,6 +118,10 @@ class Context {
116
118
  getRegistryEntry(this.id)?.clients.forEach((_, client) => {
117
119
  client.outcomingMessage$.next(new index_1.ContextDestroyMessage(this.id));
118
120
  });
121
+ for (const subscription of this.clientSubscriptions.values()) {
122
+ subscription.unsubscribe();
123
+ }
124
+ this.clientSubscriptions.clear();
119
125
  this.subscriptions.unsubscribe();
120
126
  this.dataSubscription?.unsubscribe();
121
127
  this.dataSubscription = undefined;
@@ -134,6 +140,10 @@ class Context {
134
140
  }
135
141
  client.outcomingMessage$.next(new index_1.ContextInitMessage(this.id, structuredClone(this.data$.getValue())));
136
142
  }
143
+ releaseClientBinding(client) {
144
+ this.clientSubscriptions.get(client)?.unsubscribe();
145
+ this.clientSubscriptions.delete(client);
146
+ }
137
147
  authorizeClient(client) {
138
148
  this.assertNotDestroyed();
139
149
  if (!bindClient(this, client)) {
@@ -148,16 +158,19 @@ class Context {
148
158
  markInitSent(this.id, client);
149
159
  });
150
160
  }
151
- this.subscriptions.add(client.destroy$.subscribe(() => {
161
+ const clientSubscription = new rxjs_1.Subscription();
162
+ this.clientSubscriptions.set(client, clientSubscription);
163
+ clientSubscription.add(client.destroy$.subscribe(() => {
152
164
  if (this.destroyed) {
153
165
  return;
154
166
  }
155
- client.outcomingMessage$.next(new index_1.ContextDestroyMessage(this.id));
156
- if (unbindClient(this.id, client)) {
167
+ this.releaseClientBinding(client);
168
+ const lastClientUnbound = unbindClient(this.id, client);
169
+ if (lastClientUnbound && !this.persistent) {
157
170
  this.destroy();
158
171
  }
159
172
  }));
160
- this.subscriptions.add(client.incomingMessage$
173
+ clientSubscription.add(client.incomingMessage$
161
174
  .pipe((0, rxjs_1.filter)((unsafeMessage) => (0, targeted_message_1.isTargetedMessage)(unsafeMessage) &&
162
175
  unsafeMessage.target === this.id))
163
176
  .subscribe((0, incoming_message_observer_1.incomingMessageObserver)(client, (unsafeMessage) => {
@@ -5,6 +5,7 @@ export * from "./ios-platform";
5
5
  export * from "./max-mini-app-platform";
6
6
  export * from "./mobile-platform";
7
7
  export * from "./platform";
8
+ export * from "./popover-show";
8
9
  export * from "./pwa-platform";
9
10
  export * from "./server-platform";
10
11
  export * from "./telegram-mini-app-platform";
@@ -21,6 +21,7 @@ __exportStar(require("./ios-platform"), exports);
21
21
  __exportStar(require("./max-mini-app-platform"), exports);
22
22
  __exportStar(require("./mobile-platform"), exports);
23
23
  __exportStar(require("./platform"), exports);
24
+ __exportStar(require("./popover-show"), exports);
24
25
  __exportStar(require("./pwa-platform"), exports);
25
26
  __exportStar(require("./server-platform"), exports);
26
27
  __exportStar(require("./telegram-mini-app-platform"), exports);
@@ -2,6 +2,7 @@ import { PlatformEnumeratedDevice } from "@matreshka/shared/messages/client-to-b
2
2
  import { Dialog, Popover } from "../components";
3
3
  import { Overlay } from "../components/types/container-overlay";
4
4
  import { Client, Componentable, ComponentInstance, ContextValueRef } from "../core";
5
+ import { type PopoverShowComponent, type PopoverShowPlacement } from "./popover-show";
5
6
  /**
6
7
  * Абстрактный класс платформы, управляющий клиентом и его состоянием.
7
8
  */
@@ -53,12 +54,16 @@ export declare abstract class Platform {
53
54
  */
54
55
  showDialog(dialog: Dialog): void;
55
56
  /**
56
- * Отображает всплывающее окно (popover) относительно заданного якоря.
57
+ * Отображает popover.
57
58
  *
58
- * @param anchor Использование компонента-якоря на клиенте ({@link ComponentInstance.id}).
59
+ * **source** — элемент, инициировавший открытие (Tab-order, `showPopover({ source })` на клиенте).
60
+ * **anchor** — визуальный якорь (`anchor-name` / `position-anchor`), если отличается от source.
61
+ *
62
+ * @param sourceOrPlacement Один инстанс (source = anchor) или `{ anchor, source }`.
59
63
  * @param popover Компонент всплывающего окна.
60
64
  */
61
- showPopover(anchor: ComponentInstance | Componentable, popover: Popover): void;
65
+ showPopover(source: PopoverShowComponent, popover: Popover): void;
66
+ showPopover(placement: PopoverShowPlacement, popover: Popover): void;
62
67
  showDatePicker(anchor: ComponentInstance | Componentable, ref: ContextValueRef<string | undefined>): void;
63
68
  showColorPicker(anchor: ComponentInstance | Componentable, ref: ContextValueRef<string | undefined>): void;
64
69
  showTimePicker(anchor: ComponentInstance | Componentable, ref: ContextValueRef<string | undefined>): void;
@@ -17,6 +17,7 @@ const rxjs_1 = require("rxjs");
17
17
  const v4_1 = require("zod/v4");
18
18
  const core_1 = require("../core");
19
19
  const incoming_message_observer_1 = require("../core/utils/incoming-message-observer");
20
+ const popover_show_1 = require("./popover-show");
20
21
  /**
21
22
  * Абстрактный класс платформы, управляющий клиентом и его состоянием.
22
23
  */
@@ -87,18 +88,19 @@ class Platform {
87
88
  const serializedDialog = dialog.serialize();
88
89
  this.client.outcomingMessage$.next(new dialog_show_message_1.DialogShowMessage(serializedDialog));
89
90
  }
90
- /**
91
- * Отображает всплывающее окно (popover) относительно заданного якоря.
92
- *
93
- * @param anchor Использование компонента-якоря на клиенте ({@link ComponentInstance.id}).
94
- * @param popover Компонент всплывающего окна.
95
- */
96
- showPopover(anchor, popover) {
91
+ showPopover(sourceOrPlacement, popover) {
97
92
  const serializedPopover = popover.serialize();
98
- this.client.outcomingMessage$.next(new popover_show_message_1.PopoverShowMessage({
99
- component_id: anchor.id,
100
- popover: serializedPopover,
101
- }));
93
+ const payload = (0, popover_show_1.isPopoverShowPlacement)(sourceOrPlacement)
94
+ ? {
95
+ source: sourceOrPlacement.source.id,
96
+ anchor: sourceOrPlacement.anchor.id,
97
+ popover: serializedPopover,
98
+ }
99
+ : {
100
+ source: sourceOrPlacement.id,
101
+ popover: serializedPopover,
102
+ };
103
+ this.client.outcomingMessage$.next(new popover_show_message_1.PopoverShowMessage(payload));
102
104
  }
103
105
  showDatePicker(anchor, ref) {
104
106
  this.emitNativePickerCommand(anchor, "date", ref);
@@ -0,0 +1,10 @@
1
+ import type { ComponentInstance } from "../core/types/component-instance";
2
+ import type { Componentable } from "../core/types/componentable";
3
+ /** Server-компонент или его инстанс на клиенте — аргумент showPopover в роли source и/или anchor. */
4
+ export type PopoverShowComponent = ComponentInstance | Componentable;
5
+ /** Раздельные source и визуальный anchor. */
6
+ export type PopoverShowPlacement = {
7
+ anchor: PopoverShowComponent;
8
+ source: PopoverShowComponent;
9
+ };
10
+ export declare function isPopoverShowPlacement(value: PopoverShowComponent | PopoverShowPlacement): value is PopoverShowPlacement;
@@ -0,0 +1,10 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isPopoverShowPlacement = isPopoverShowPlacement;
4
+ function isPopoverShowPlacement(value) {
5
+ return (typeof value === "object" &&
6
+ value !== null &&
7
+ "anchor" in value &&
8
+ "source" in value &&
9
+ !("component" in value));
10
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@matreshka/bff",
3
- "version": "0.14.2",
3
+ "version": "0.15.0",
4
4
  "description": "Matreshka BFF: server-driven UI runtime — routing, pages, context, component DSL, and platform APIs",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",
@@ -62,7 +62,7 @@
62
62
  "vitest": "^4.1.0"
63
63
  },
64
64
  "dependencies": {
65
- "@matreshka/shared": "^0.13.0",
65
+ "@matreshka/shared": "^0.15.0",
66
66
  "@simplewebauthn/server": "^13.1.1",
67
67
  "@simplewebauthn/types": "^12.0.0",
68
68
  "fast-xml-parser": "^5.3.3",