brix-runtime-sdk-api-web 1.0.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 (135) hide show
  1. package/dist/index.js +190 -0
  2. package/dist/types/api-exports.test.d.ts +33 -0
  3. package/dist/types/api-exports.test.d.ts.map +1 -0
  4. package/dist/types/context/RuntimeContext.d.ts +69 -0
  5. package/dist/types/context/RuntimeContext.d.ts.map +1 -0
  6. package/dist/types/context/index.d.ts +23 -0
  7. package/dist/types/context/index.d.ts.map +1 -0
  8. package/dist/types/index.d.ts +52 -0
  9. package/dist/types/index.d.ts.map +1 -0
  10. package/dist/types/types/auth.d.ts +405 -0
  11. package/dist/types/types/auth.d.ts.map +1 -0
  12. package/dist/types/types/capability.d.ts +218 -0
  13. package/dist/types/types/capability.d.ts.map +1 -0
  14. package/dist/types/types/common.d.ts +99 -0
  15. package/dist/types/types/common.d.ts.map +1 -0
  16. package/dist/types/types/config.d.ts +85 -0
  17. package/dist/types/types/config.d.ts.map +1 -0
  18. package/dist/types/types/event.d.ts +658 -0
  19. package/dist/types/types/event.d.ts.map +1 -0
  20. package/dist/types/types/http.d.ts +132 -0
  21. package/dist/types/types/http.d.ts.map +1 -0
  22. package/dist/types/types/i18n.d.ts +327 -0
  23. package/dist/types/types/i18n.d.ts.map +1 -0
  24. package/dist/types/types/index.d.ts +61 -0
  25. package/dist/types/types/index.d.ts.map +1 -0
  26. package/dist/types/types/layout.d.ts +301 -0
  27. package/dist/types/types/layout.d.ts.map +1 -0
  28. package/dist/types/types/module.d.ts +78 -0
  29. package/dist/types/types/module.d.ts.map +1 -0
  30. package/dist/types/types/navigation.d.ts +295 -0
  31. package/dist/types/types/navigation.d.ts.map +1 -0
  32. package/dist/types/types/plugin-loader-capability.d.ts +142 -0
  33. package/dist/types/types/plugin-loader-capability.d.ts.map +1 -0
  34. package/dist/types/types/plugin.d.ts +250 -0
  35. package/dist/types/types/plugin.d.ts.map +1 -0
  36. package/dist/types/types/state.d.ts +306 -0
  37. package/dist/types/types/state.d.ts.map +1 -0
  38. package/dist/types/types/theme.d.ts +279 -0
  39. package/dist/types/types/theme.d.ts.map +1 -0
  40. package/dist/types/types/ui/adapter.d.ts +181 -0
  41. package/dist/types/types/ui/adapter.d.ts.map +1 -0
  42. package/dist/types/types/ui/avatar.d.ts +102 -0
  43. package/dist/types/types/ui/avatar.d.ts.map +1 -0
  44. package/dist/types/types/ui/badge.d.ts +101 -0
  45. package/dist/types/types/ui/badge.d.ts.map +1 -0
  46. package/dist/types/types/ui/button.d.ts +135 -0
  47. package/dist/types/types/ui/button.d.ts.map +1 -0
  48. package/dist/types/types/ui/card.d.ts +105 -0
  49. package/dist/types/types/ui/card.d.ts.map +1 -0
  50. package/dist/types/types/ui/common.d.ts +29 -0
  51. package/dist/types/types/ui/common.d.ts.map +1 -0
  52. package/dist/types/types/ui/icon.d.ts +78 -0
  53. package/dist/types/types/ui/icon.d.ts.map +1 -0
  54. package/dist/types/types/ui/index.d.ts +59 -0
  55. package/dist/types/types/ui/index.d.ts.map +1 -0
  56. package/dist/types/types/ui/input.d.ts +200 -0
  57. package/dist/types/types/ui/input.d.ts.map +1 -0
  58. package/dist/types/types/ui/menu.d.ts +211 -0
  59. package/dist/types/types/ui/menu.d.ts.map +1 -0
  60. package/dist/types/types/ui/message.d.ts +121 -0
  61. package/dist/types/types/ui/message.d.ts.map +1 -0
  62. package/dist/types/types/ui/modal.d.ts +167 -0
  63. package/dist/types/types/ui/modal.d.ts.map +1 -0
  64. package/dist/types/types/ui/select.d.ts +213 -0
  65. package/dist/types/types/ui/select.d.ts.map +1 -0
  66. package/dist/types/types/ui/theme-tokens.d.ts +226 -0
  67. package/dist/types/types/ui/theme-tokens.d.ts.map +1 -0
  68. package/dist/types/types/ui/tooltip.d.ts +98 -0
  69. package/dist/types/types/ui/tooltip.d.ts.map +1 -0
  70. package/dist/types/types/ui.d.ts +49 -0
  71. package/dist/types/types/ui.d.ts.map +1 -0
  72. package/package.json +34 -0
  73. package/src/api-exports.test.ts +199 -0
  74. package/src/context/RuntimeContext.d.ts +69 -0
  75. package/src/context/RuntimeContext.d.ts.map +1 -0
  76. package/src/context/RuntimeContext.ts +75 -0
  77. package/src/context/index.d.ts +23 -0
  78. package/src/context/index.d.ts.map +1 -0
  79. package/src/context/index.ts +23 -0
  80. package/src/index.d.ts +52 -0
  81. package/src/index.d.ts.map +1 -0
  82. package/src/index.ts +59 -0
  83. package/src/types/auth.d.ts +146 -0
  84. package/src/types/auth.d.ts.map +1 -0
  85. package/src/types/auth.ts +479 -0
  86. package/src/types/capability.d.ts +218 -0
  87. package/src/types/capability.d.ts.map +1 -0
  88. package/src/types/capability.ts +302 -0
  89. package/src/types/common.d.ts +99 -0
  90. package/src/types/common.d.ts.map +1 -0
  91. package/src/types/common.ts +115 -0
  92. package/src/types/config.d.ts +64 -0
  93. package/src/types/config.d.ts.map +1 -0
  94. package/src/types/config.ts +96 -0
  95. package/src/types/event.d.ts +206 -0
  96. package/src/types/event.d.ts.map +1 -0
  97. package/src/types/event.ts +776 -0
  98. package/src/types/http.d.ts +132 -0
  99. package/src/types/http.d.ts.map +1 -0
  100. package/src/types/http.ts +156 -0
  101. package/src/types/i18n.ts +420 -0
  102. package/src/types/index.d.ts +50 -0
  103. package/src/types/index.d.ts.map +1 -0
  104. package/src/types/index.ts +120 -0
  105. package/src/types/layout.ts +394 -0
  106. package/src/types/module.d.ts +78 -0
  107. package/src/types/module.d.ts.map +1 -0
  108. package/src/types/module.ts +92 -0
  109. package/src/types/navigation.d.ts +101 -0
  110. package/src/types/navigation.d.ts.map +1 -0
  111. package/src/types/navigation.ts +361 -0
  112. package/src/types/plugin-loader-capability.ts +159 -0
  113. package/src/types/plugin.d.ts +250 -0
  114. package/src/types/plugin.d.ts.map +1 -0
  115. package/src/types/plugin.ts +344 -0
  116. package/src/types/state.d.ts +119 -0
  117. package/src/types/state.d.ts.map +1 -0
  118. package/src/types/state.ts +366 -0
  119. package/src/types/theme.ts +378 -0
  120. package/src/types/ui/adapter.ts +222 -0
  121. package/src/types/ui/avatar.ts +113 -0
  122. package/src/types/ui/badge.ts +112 -0
  123. package/src/types/ui/button.ts +148 -0
  124. package/src/types/ui/card.ts +116 -0
  125. package/src/types/ui/common.ts +29 -0
  126. package/src/types/ui/icon.ts +85 -0
  127. package/src/types/ui/index.ts +78 -0
  128. package/src/types/ui/input.ts +225 -0
  129. package/src/types/ui/menu.ts +237 -0
  130. package/src/types/ui/message.ts +135 -0
  131. package/src/types/ui/modal.ts +188 -0
  132. package/src/types/ui/select.ts +239 -0
  133. package/src/types/ui/theme-tokens.ts +357 -0
  134. package/src/types/ui/tooltip.ts +120 -0
  135. package/src/types/ui.ts +49 -0
@@ -0,0 +1,776 @@
1
+ /**
2
+ * Copyright 2026 Brix Platform Authors
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+ /**
17
+ * @file Event-Related Type Definitions
18
+ * @description Defines core types for the event system, including event messages, handlers, subscription options, etc.
19
+ * @module @brix/runtime-sdk-api-web/types/event
20
+ * @version 3.3.0
21
+ *
22
+ * [v3.3 Changes - Phase 3 Contract Layer Cleanup]
23
+ * - Migrated BaseEvent, MetadataEvent from platform-shared to runtime-sdk-api-web (LL3 compliance)
24
+ * - Added EventPriority, EventSubscriptionOptions, EventPublishOptions contract types
25
+ * - runtime-sdk-api-web is now the authoritative source for all event contract types
26
+ *
27
+ * [v3.2 Changes]
28
+ * Extracted from index.ts into a standalone type file.
29
+ *
30
+ * [Design Principles]
31
+ * - Supports both simple Event Bus and Governed Event Bus modes
32
+ * - Governed Event Bus provides complete event metadata and audit information
33
+ * - All event-related contract types are defined here (single source of truth)
34
+ */
35
+
36
+ // =========================================
37
+ // Base Event Types (Contract Layer - Authoritative Source)
38
+ // =========================================
39
+
40
+ /**
41
+ * Base Event Interface
42
+ *
43
+ * <p>Defines the common fields for all events in the Brix Platform.
44
+ * This is the foundational contract type for the event system.</p>
45
+ *
46
+ * <h3>Design Rationale</h3>
47
+ * <ul>
48
+ * <li>Provides minimal required fields for event identification</li>
49
+ * <li>Extensible through interface inheritance</li>
50
+ * <li>Platform-agnostic (works on Web/Mobile/Server)</li>
51
+ * </ul>
52
+ *
53
+ * <h3>Usage Example</h3>
54
+ * ```typescript
55
+ * interface UserCreatedEvent extends BaseEvent {
56
+ * type: 'user:created';
57
+ * userId: string;
58
+ * email: string;
59
+ * }
60
+ * ```
61
+ *
62
+ * @since 3.3.0 Migrated from platform-shared (LL3 compliance)
63
+ */
64
+ export interface BaseEvent {
65
+ /**
66
+ * Event type identifier
67
+ *
68
+ * <p>Follows the convention: `{domain}:{action}` (e.g., 'user:created', 'booking:confirmed').</p>
69
+ */
70
+ readonly type: string;
71
+
72
+ /**
73
+ * Event timestamp in milliseconds (Unix epoch)
74
+ *
75
+ * <p>Represents when the event was created/emitted.</p>
76
+ */
77
+ readonly timestamp: number;
78
+
79
+ /**
80
+ * Event source identifier (typically plugin ID)
81
+ *
82
+ * <p>Identifies which plugin or system component emitted the event.</p>
83
+ */
84
+ readonly source?: string;
85
+ }
86
+
87
+ /**
88
+ * Event with Metadata
89
+ *
90
+ * <p>Extends BaseEvent with rich metadata for observability, auditing, and governance.
91
+ * Used in scenarios requiring event tracing, multi-tenancy support, or detailed logging.</p>
92
+ *
93
+ * <h3>Relationship with Other Event Types</h3>
94
+ * <ul>
95
+ * <li>{@link BaseEvent}: Minimal event contract</li>
96
+ * <li>{@link MetadataEvent}: Extended with observability metadata (this interface)</li>
97
+ * <li>{@link GovernedEvent}: Full governance support with strict typing</li>
98
+ * </ul>
99
+ *
100
+ * <h3>Usage Example</h3>
101
+ * ```typescript
102
+ * const event: MetadataEvent = {
103
+ * type: 'order:placed',
104
+ * timestamp: Date.now(),
105
+ * source: 'plugin-orders',
106
+ * metadata: {
107
+ * sourcePlugin: 'plugin-orders',
108
+ * scope: 'host',
109
+ * traceId: 'trace-123',
110
+ * timestamp: Date.now(),
111
+ * tenantId: 'tenant-abc'
112
+ * }
113
+ * };
114
+ * ```
115
+ *
116
+ * @since 3.3.0 Migrated from platform-shared (LL3 compliance)
117
+ */
118
+ export interface MetadataEvent extends BaseEvent {
119
+ /**
120
+ * Event metadata for observability and governance
121
+ *
122
+ * <p>Contains tracing information, tenant context, and scope designation.</p>
123
+ */
124
+ readonly metadata: EventMetadata;
125
+ }
126
+
127
+ /**
128
+ * Event Priority Levels
129
+ *
130
+ * <p>Defines the priority levels for event processing order.
131
+ * Higher priority events are processed before lower priority ones.</p>
132
+ *
133
+ * <h3>Priority Semantics</h3>
134
+ * <ul>
135
+ * <li>`low`: Background tasks, analytics, non-critical updates</li>
136
+ * <li>`normal`: Standard business events (default)</li>
137
+ * <li>`high`: Important business events requiring prompt processing</li>
138
+ * <li>`critical`: System-critical events, error handling, security alerts</li>
139
+ * </ul>
140
+ *
141
+ * @since 3.3.0 Migrated from platform-shared (LL3 compliance)
142
+ */
143
+ export type EventPriority = 'low' | 'normal' | 'high' | 'critical';
144
+
145
+ /**
146
+ * Event Subscription Options (Extended)
147
+ *
148
+ * <p>Configuration options for event subscription with priority and filtering support.
149
+ * Extends the basic {@link SubscriptionOptions} with additional capabilities.</p>
150
+ *
151
+ * <h3>Relationship with SubscriptionOptions</h3>
152
+ * <ul>
153
+ * <li>{@link SubscriptionOptions}: Basic subscription options (once, filter)</li>
154
+ * <li>{@link EventSubscriptionOptions}: Extended options with priority support</li>
155
+ * </ul>
156
+ *
157
+ * <h3>Usage Example</h3>
158
+ * ```typescript
159
+ * eventBus.on('user:action', handler, {
160
+ * priority: 'high',
161
+ * once: false,
162
+ * filter: (event) => event.userId === currentUserId
163
+ * });
164
+ * ```
165
+ *
166
+ * @since 3.3.0 Migrated from platform-shared (LL3 compliance)
167
+ */
168
+ export interface EventSubscriptionOptions {
169
+ /**
170
+ * Whether to trigger only once
171
+ *
172
+ * <p>If true, the handler is automatically unsubscribed after first invocation.</p>
173
+ */
174
+ readonly once?: boolean;
175
+
176
+ /**
177
+ * Subscription priority
178
+ *
179
+ * <p>Determines the order in which handlers are invoked.
180
+ * Higher priority handlers are called before lower priority ones.</p>
181
+ *
182
+ * @default 'normal'
183
+ */
184
+ readonly priority?: EventPriority;
185
+
186
+ /**
187
+ * Filter function for selective event handling
188
+ *
189
+ * <p>If provided, the handler is only invoked when the filter returns true.</p>
190
+ *
191
+ * @param event The event payload
192
+ * @returns Whether to invoke the handler
193
+ */
194
+ readonly filter?: (event: unknown) => boolean;
195
+ }
196
+
197
+ /**
198
+ * Event Publish Options
199
+ *
200
+ * <p>Configuration options for event publishing behavior.
201
+ * Supports async/sync modes, delayed emission, and rate limiting.</p>
202
+ *
203
+ * <h3>Usage Example</h3>
204
+ * ```typescript
205
+ * eventBus.emit('analytics:track', payload, {
206
+ * async: true,
207
+ * debounce: 300,
208
+ * tags: ['analytics', 'user-action']
209
+ * });
210
+ * ```
211
+ *
212
+ * @since 3.3.0 Migrated from platform-shared (LL3 compliance)
213
+ */
214
+ export interface EventPublishOptions {
215
+ /**
216
+ * Whether to emit asynchronously
217
+ *
218
+ * <p>If true, the event is queued and emitted in the next microtask.
219
+ * If false, handlers are invoked synchronously.</p>
220
+ *
221
+ * @default true
222
+ */
223
+ readonly async?: boolean;
224
+
225
+ /**
226
+ * Delay before emission in milliseconds
227
+ *
228
+ * <p>If specified, the event emission is delayed by this amount.</p>
229
+ */
230
+ readonly delay?: number;
231
+
232
+ /**
233
+ * Debounce interval in milliseconds
234
+ *
235
+ * <p>If specified, rapid emissions of the same event type are debounced.
236
+ * Only the last emission within the interval is actually sent.</p>
237
+ */
238
+ readonly debounce?: number;
239
+
240
+ /**
241
+ * Throttle interval in milliseconds
242
+ *
243
+ * <p>If specified, emissions are throttled to at most one per interval.</p>
244
+ */
245
+ readonly throttle?: number;
246
+ }
247
+
248
+ // =========================================
249
+ // Event Message
250
+ // =========================================
251
+
252
+ /**
253
+ * Event Message
254
+ *
255
+ * <p>Encapsulates complete event information including type, payload, timestamp, and source.</p>
256
+ */
257
+ export interface EventMessage<T = unknown> {
258
+ /** Event Type */
259
+ readonly eventType: string;
260
+ /** Event Payload */
261
+ readonly payload: T;
262
+ /** Timestamp */
263
+ readonly timestamp: number;
264
+ /** Event Source (Plugin ID) */
265
+ readonly source?: string;
266
+ }
267
+
268
+ // =========================================
269
+ // Subscription Options
270
+ // =========================================
271
+
272
+ /**
273
+ * Event Subscription Options
274
+ */
275
+ export interface SubscriptionOptions {
276
+ /** Whether to trigger only once */
277
+ readonly once?: boolean;
278
+ /** Filter function */
279
+ readonly filter?: (payload: unknown) => boolean;
280
+ }
281
+
282
+ // =========================================
283
+ // Event Handler
284
+ // =========================================
285
+
286
+ /**
287
+ * Event Handler
288
+ *
289
+ * @typeParam T - Event payload type
290
+ */
291
+ export type EventHandler<T = unknown> = (payload: T) => void;
292
+
293
+ /**
294
+ * Unsubscribe Function
295
+ */
296
+ export type Unsubscribe = () => void;
297
+
298
+ // =========================================
299
+ // Event Bus Capability
300
+ // =========================================
301
+
302
+ /**
303
+ * Event Bus Capability Type Identifier
304
+ */
305
+ export const EventBusCapabilityType = Symbol.for('EventBusCapability');
306
+
307
+ /**
308
+ * Event Bus Capability Contract
309
+ *
310
+ * <p>Provides cross-plugin communication capability for plugins.</p>
311
+ *
312
+ * <h3>Usage Example</h3>
313
+ * ```typescript
314
+ * const eventBus = context.getCapability<EventBusCapability>(EventBusCapabilityType);
315
+ *
316
+ * // Emit event
317
+ * eventBus.emit('booking:selected', { bookingId: '123' });
318
+ *
319
+ * // Subscribe to event
320
+ * const unsubscribe = eventBus.on('booking:selected', (payload) => {
321
+ * console.log('Booking selected:', payload);
322
+ * });
323
+ * ```
324
+ */
325
+ export interface EventBusCapability {
326
+ /**
327
+ * Emit event
328
+ *
329
+ * @param eventType Event type
330
+ * @param payload Event payload
331
+ */
332
+ emit(eventType: string, payload: unknown): void;
333
+
334
+ /**
335
+ * Subscribe to event
336
+ *
337
+ * @param eventType Event type
338
+ * @param handler Event handler
339
+ * @returns Unsubscribe function
340
+ */
341
+ on(eventType: string, handler: EventHandler): () => void;
342
+
343
+ /**
344
+ * Unsubscribe from event
345
+ *
346
+ * @param eventType Event type
347
+ * @param handler Event handler
348
+ */
349
+ off(eventType: string, handler: EventHandler): void;
350
+ }
351
+
352
+ // =========================================
353
+ // Governed Event Bus Capability
354
+ // =========================================
355
+
356
+ /**
357
+ * Governed Event Bus Capability Type Identifier
358
+ */
359
+ export const GovernedEventBusCapabilityType = Symbol.for('GovernedEventBusCapability');
360
+
361
+ /**
362
+ * Governed Event
363
+ *
364
+ * <p>Event containing complete metadata for observability and auditing.</p>
365
+ */
366
+ export interface GovernedEvent<T = unknown> {
367
+ /** Event Type */
368
+ readonly type: string;
369
+
370
+ /** Event Payload */
371
+ readonly payload: T;
372
+
373
+ /** Event Metadata */
374
+ readonly metadata: GovernedEventMetadata;
375
+ }
376
+
377
+ /**
378
+ * Governed Event Metadata
379
+ */
380
+ export interface GovernedEventMetadata {
381
+ /** Event ID (Unique Identifier) */
382
+ readonly eventId: string;
383
+
384
+ /** Emit Timestamp */
385
+ readonly timestamp: number;
386
+
387
+ /** Sender (Plugin ID) */
388
+ readonly source: string;
389
+
390
+ /** Tenant ID */
391
+ readonly tenantId?: string;
392
+
393
+ /**
394
+ * Event Scope
395
+ * - 'plugin': Visible only within the plugin
396
+ * - 'host': Globally visible
397
+ */
398
+ readonly scope: 'plugin' | 'host';
399
+ }
400
+
401
+ // =========================================
402
+ // Event Metadata (Extended Version for Observability)
403
+ // =========================================
404
+
405
+ /**
406
+ * Event Metadata
407
+ *
408
+ * <p>Extended metadata for Web-side event tracking and observability.
409
+ * Compared to GovernedEventMetadata, provides more flexible field naming for Web scenarios.</p>
410
+ *
411
+ * <h3>Relationship with GovernedEventMetadata</h3>
412
+ * <ul>
413
+ * <li>GovernedEventMetadata: Used for Governed Event Bus</li>
414
+ * <li>EventMetadata: Used for general event emission scenarios</li>
415
+ * </ul>
416
+ *
417
+ * @since 3.2.0
418
+ */
419
+ export interface EventMetadata {
420
+ /** Sender Plugin ID */
421
+ readonly sourcePlugin: string;
422
+
423
+ /**
424
+ * Event Scope
425
+ * - 'plugin': Visible only within the plugin
426
+ * - 'host': Globally visible (cross-plugin)
427
+ */
428
+ readonly scope: 'plugin' | 'host';
429
+
430
+ /** Trace ID (for distributed tracing) */
431
+ readonly traceId: string;
432
+
433
+ /** Emit Timestamp (milliseconds) */
434
+ readonly timestamp: number;
435
+
436
+ /** Tenant ID (multi-tenant scenario) */
437
+ readonly tenantId?: string;
438
+
439
+ /** Custom Tags (for filtering and classification) */
440
+ readonly tags?: ReadonlyArray<string>;
441
+ }
442
+
443
+ // =========================================
444
+ // Web Event Emit Options
445
+ // =========================================
446
+
447
+ /**
448
+ * Web Event Emit Options
449
+ *
450
+ * <p>Configuration parameters controlling event emission behavior.</p>
451
+ *
452
+ * @since 3.2.0
453
+ */
454
+ export interface WebEventEmitOptions {
455
+ /**
456
+ * Event Scope
457
+ * - 'plugin': Visible only within the current plugin
458
+ * - 'host': Globally visible, can be subscribed across plugins
459
+ * @default 'host'
460
+ */
461
+ readonly scope?: 'plugin' | 'host';
462
+
463
+ /**
464
+ * Whether to emit synchronously
465
+ *
466
+ * <p>Default is asynchronous (via queueMicrotask),
467
+ * set to true to immediately invoke all subscribers synchronously.</p>
468
+ *
469
+ * @default false
470
+ */
471
+ readonly sync?: boolean;
472
+
473
+ /**
474
+ * Custom Tags
475
+ *
476
+ * <p>Used for event classification and filtering, visible in logs and monitoring.</p>
477
+ */
478
+ readonly tags?: ReadonlyArray<string>;
479
+ }
480
+
481
+ // =========================================
482
+ // Backpressure Configuration
483
+ // =========================================
484
+
485
+ /**
486
+ * Backpressure Overflow Strategy
487
+ *
488
+ * <p>Defines behavior when event queue reaches maxQueueDepth.</p>
489
+ *
490
+ * @since 3.3.0
491
+ */
492
+ export type BackpressureOverflowStrategy =
493
+ /**
494
+ * Drop oldest events to make room for new ones
495
+ * 丢弃最旧的事件以为新事件腾出空间
496
+ */
497
+ | 'drop-oldest'
498
+ /**
499
+ * Reject new events when queue is full (throws BackpressureError)
500
+ * 队列满时拒绝新事件(抛出 BackpressureError)
501
+ */
502
+ | 'reject'
503
+ /**
504
+ * Block the emit call until space is available (async only)
505
+ * 阻塞 emit 调用直到有空间可用(仅异步)
506
+ */
507
+ | 'block';
508
+
509
+ /**
510
+ * Backpressure Configuration
511
+ *
512
+ * <p>Configuration for event bus backpressure management.
513
+ * Prevents memory exhaustion and ensures system stability under high load.</p>
514
+ *
515
+ * 事件总线背压管理配置。
516
+ * 防止内存耗尽,确保高负载下的系统稳定性。
517
+ *
518
+ * @since 3.3.0
519
+ */
520
+ export interface BackpressureConfig {
521
+ /**
522
+ * Maximum queue depth per event type
523
+ *
524
+ * <p>When the number of pending events exceeds this limit,
525
+ * the overflow strategy is applied.</p>
526
+ *
527
+ * 每个事件类型的最大队列深度。
528
+ * 当待处理事件数量超过此限制时,将应用溢出策略。
529
+ *
530
+ * @default 1000
531
+ */
532
+ readonly maxQueueDepth?: number;
533
+
534
+ /**
535
+ * Global maximum queue depth (across all event types)
536
+ *
537
+ * 全局最大队列深度(跨所有事件类型)
538
+ *
539
+ * @default 10000
540
+ */
541
+ readonly globalMaxQueueDepth?: number;
542
+
543
+ /**
544
+ * Overflow strategy when queue is full
545
+ *
546
+ * 队列满时的溢出策略
547
+ *
548
+ * @default 'drop-oldest'
549
+ */
550
+ readonly overflowStrategy?: BackpressureOverflowStrategy;
551
+
552
+ /**
553
+ * Warning threshold percentage (0-100)
554
+ *
555
+ * <p>Emits a warning event when queue reaches this percentage of maxQueueDepth.</p>
556
+ *
557
+ * 警告阈值百分比。当队列达到 maxQueueDepth 的此百分比时发出警告事件。
558
+ *
559
+ * @default 80
560
+ */
561
+ readonly warningThreshold?: number;
562
+
563
+ /**
564
+ * Enable backpressure metrics collection
565
+ *
566
+ * 启用背压指标收集
567
+ *
568
+ * @default true
569
+ */
570
+ readonly enableMetrics?: boolean;
571
+ }
572
+
573
+ /**
574
+ * Backpressure Metrics
575
+ *
576
+ * <p>Runtime metrics for monitoring backpressure state.</p>
577
+ *
578
+ * @since 3.3.0
579
+ */
580
+ export interface BackpressureMetrics {
581
+ /** Current queue depth per event type */
582
+ readonly queueDepthByType: ReadonlyMap<string, number>;
583
+
584
+ /** Total events in queue */
585
+ readonly totalQueueDepth: number;
586
+
587
+ /** Number of events dropped due to backpressure */
588
+ readonly droppedCount: number;
589
+
590
+ /** Number of events rejected due to backpressure */
591
+ readonly rejectedCount: number;
592
+
593
+ /** Number of times warning threshold was reached */
594
+ readonly warningCount: number;
595
+
596
+ /** Last warning timestamp */
597
+ readonly lastWarningTimestamp?: number;
598
+
599
+ /** Queue utilization percentage (0-100) */
600
+ readonly utilizationPercent: number;
601
+ }
602
+
603
+ /**
604
+ * Backpressure Error
605
+ *
606
+ * <p>Thrown when event is rejected due to backpressure.</p>
607
+ *
608
+ * @since 3.3.0
609
+ */
610
+ export class BackpressureError extends Error {
611
+ readonly eventType: string;
612
+ readonly queueDepth: number;
613
+ readonly maxQueueDepth: number;
614
+
615
+ constructor(eventType: string, queueDepth: number, maxQueueDepth: number) {
616
+ super(
617
+ `Backpressure limit reached for event type '${eventType}': ` +
618
+ `queue depth ${queueDepth} >= max ${maxQueueDepth}`
619
+ );
620
+ this.name = 'BackpressureError';
621
+ this.eventType = eventType;
622
+ this.queueDepth = queueDepth;
623
+ this.maxQueueDepth = maxQueueDepth;
624
+ }
625
+ }
626
+
627
+ // =========================================
628
+ // Web Event Subscribe Options
629
+ // =========================================
630
+
631
+ /**
632
+ * Web Event Subscribe Options
633
+ *
634
+ * <p>Configuration parameters controlling event subscription behavior.</p>
635
+ *
636
+ * @since 3.2.0
637
+ */
638
+ export interface WebEventSubscribeOptions {
639
+ /**
640
+ * Debounce Time (milliseconds)
641
+ *
642
+ * <p>For consecutive events triggered in a short time, only the last one is processed.</p>
643
+ */
644
+ readonly debounce?: number;
645
+
646
+ /**
647
+ * Throttle Time (milliseconds)
648
+ *
649
+ * <p>Limits the minimum time interval between event processing.</p>
650
+ */
651
+ readonly throttle?: number;
652
+
653
+ /**
654
+ * Event Filter
655
+ *
656
+ * <p>Handler is called only when this returns true.</p>
657
+ */
658
+ readonly filter?: (payload: unknown) => boolean;
659
+
660
+ /**
661
+ * Whether to trigger only once
662
+ *
663
+ * @default false
664
+ */
665
+ readonly once?: boolean;
666
+ }
667
+
668
+ /**
669
+ * Governed Event Handler Function
670
+ */
671
+ export type GovernedEventHandler<T = unknown> = (event: GovernedEvent<T>) => void;
672
+
673
+ /**
674
+ * Governed Event Bus Capability Contract
675
+ *
676
+ * <p>Unlike the simple EventBusCapability, GovernedEventBusCapability provides:</p>
677
+ * <ul>
678
+ * <li>Automatic injection of event metadata (eventId, timestamp, source)</li>
679
+ * <li>Event scope control (plugin/host)</li>
680
+ * <li>Complete event audit information</li>
681
+ * </ul>
682
+ *
683
+ * <h3>Usage Example</h3>
684
+ * ```typescript
685
+ * const eventBus = context.getCapability<GovernedEventBusCapability>(GovernedEventBusCapabilityType);
686
+ *
687
+ * // Emit global event
688
+ * eventBus.emit('booking:created', { bookingId: '123' }, 'host');
689
+ *
690
+ * // Subscribe to event (receive complete metadata)
691
+ * eventBus.on('booking:created', (event) => {
692
+ * console.log(`Event ${event.metadata.eventId} from ${event.metadata.source}`);
693
+ * console.log('Payload:', event.payload);
694
+ * });
695
+ * ```
696
+ */
697
+ export interface GovernedEventBusCapability {
698
+ /**
699
+ * Emit Event
700
+ *
701
+ * @param eventType Event type
702
+ * @param payload Event payload
703
+ * @param scope Event scope (default 'host')
704
+ */
705
+ emit<T = unknown>(eventType: string, payload: T, scope?: 'plugin' | 'host'): void;
706
+
707
+ /**
708
+ * Subscribe to Event
709
+ *
710
+ * @param eventType Event type
711
+ * @param handler Event handler function, receives complete GovernedEvent
712
+ * @returns Unsubscribe function
713
+ */
714
+ on<T = unknown>(eventType: string, handler: GovernedEventHandler<T>): Unsubscribe;
715
+
716
+ /**
717
+ * Subscribe to Event Once
718
+ *
719
+ * @param eventType Event type
720
+ * @param handler Event handler function
721
+ * @returns Unsubscribe function
722
+ */
723
+ once<T = unknown>(eventType: string, handler: GovernedEventHandler<T>): Unsubscribe;
724
+
725
+ /**
726
+ * Unsubscribe from Event
727
+ *
728
+ * @param eventType Event type
729
+ * @param handler Event handler function
730
+ */
731
+ off<T = unknown>(eventType: string, handler: GovernedEventHandler<T>): void;
732
+
733
+ // =========================================
734
+ // Backpressure Management (v3.3.0)
735
+ // =========================================
736
+
737
+ /**
738
+ * Configure backpressure settings
739
+ *
740
+ * 配置背压设置
741
+ *
742
+ * @param config Backpressure configuration
743
+ * @since 3.3.0
744
+ */
745
+ configureBackpressure?(config: BackpressureConfig): void;
746
+
747
+ /**
748
+ * Get current backpressure metrics
749
+ *
750
+ * 获取当前背压指标
751
+ *
752
+ * @returns Current backpressure metrics
753
+ * @since 3.3.0
754
+ */
755
+ getBackpressureMetrics?(): BackpressureMetrics;
756
+
757
+ /**
758
+ * Check if backpressure is active for an event type
759
+ *
760
+ * 检查某事件类型是否处于背压状态
761
+ *
762
+ * @param eventType Event type to check
763
+ * @returns True if backpressure is active
764
+ * @since 3.3.0
765
+ */
766
+ isBackpressureActive?(eventType?: string): boolean;
767
+
768
+ /**
769
+ * Reset backpressure metrics
770
+ *
771
+ * 重置背压指标
772
+ *
773
+ * @since 3.3.0
774
+ */
775
+ resetBackpressureMetrics?(): void;
776
+ }