@asaidimu/react-store 1.5.0 → 2.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.
- package/index.cjs +1 -1
- package/index.d.cts +5 -714
- package/index.d.ts +5 -714
- package/index.js +1 -1
- package/package.json +3 -1
package/index.d.cts
CHANGED
|
@@ -1,418 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
*/
|
|
4
|
-
interface DataStorePersistence<T> {
|
|
5
|
-
/**
|
|
6
|
-
* Persists data to storage
|
|
7
|
-
* @param id The id of the consumer instance, not of the data/state being
|
|
8
|
-
* persisted. The consumer instance should implement some mechanism to
|
|
9
|
-
* identify itself. Best done with a uuid created at instantiation.
|
|
10
|
-
* @param state The state to persist
|
|
11
|
-
* @returns boolean indicating success or Promise resolving to success status
|
|
12
|
-
*/
|
|
13
|
-
set(id: string, state: T): boolean | Promise<boolean>;
|
|
14
|
-
/**
|
|
15
|
-
* Retrieves persisted data from storage
|
|
16
|
-
* @returns The retrieved state or null if not found
|
|
17
|
-
*/
|
|
18
|
-
get(): T | null | Promise<T | null>;
|
|
19
|
-
/**
|
|
20
|
-
* Subscribes to changes in persisted data (e.g., from other tabs/instances)
|
|
21
|
-
* if persistence is shared
|
|
22
|
-
* @param id The id of the consumer instance
|
|
23
|
-
* @param callback Function to call when persisted data changes
|
|
24
|
-
* @returns Function to unsubscribe
|
|
25
|
-
*/
|
|
26
|
-
subscribe(id: string, callback: (state: T) => void): () => void;
|
|
27
|
-
/**
|
|
28
|
-
* Clears the persisted data
|
|
29
|
-
* @returns true if successful
|
|
30
|
-
*/
|
|
31
|
-
clear(): boolean | Promise<boolean>;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* Reactive Data Store Implementation
|
|
36
|
-
* A type-safe reactive data state for managing complex application state.
|
|
37
|
-
* Arrays are treated as primitive values (stored and updated as whole units).
|
|
38
|
-
*/
|
|
39
|
-
|
|
40
|
-
/**
|
|
41
|
-
* Utility type for representing partial updates to the state.
|
|
42
|
-
*/
|
|
43
|
-
type DeepPartial<T> = {
|
|
44
|
-
[P in keyof T]?: T[P] extends object ? DeepPartial<T[P]> : T[P] | symbol;
|
|
45
|
-
};
|
|
46
|
-
/**
|
|
47
|
-
* Interface for performance metrics of the state.
|
|
48
|
-
*/
|
|
49
|
-
interface StoreMetrics {
|
|
50
|
-
updateCount: number;
|
|
51
|
-
listenerExecutions: number;
|
|
52
|
-
averageUpdateTime: number;
|
|
53
|
-
largestUpdateSize: number;
|
|
54
|
-
mostActiveListenerPaths: string[];
|
|
55
|
-
}
|
|
56
|
-
/**
|
|
57
|
-
* Extended store state for monitoring execution status
|
|
58
|
-
*/
|
|
59
|
-
interface StoreExecutionState<T> {
|
|
60
|
-
executing: boolean;
|
|
61
|
-
changes: DeepPartial<T> | null;
|
|
62
|
-
pendingChanges: Array<StateUpdater<T>>;
|
|
63
|
-
middlewares: string[];
|
|
64
|
-
runningMiddleware: {
|
|
65
|
-
id: string;
|
|
66
|
-
name: string;
|
|
67
|
-
startTime: number;
|
|
68
|
-
} | null;
|
|
69
|
-
transactionActive: boolean;
|
|
70
|
-
}
|
|
71
|
-
/**
|
|
72
|
-
* Event types emitted by the state for observability
|
|
73
|
-
*/
|
|
74
|
-
type StoreEvent = "update:start" | "update:complete" | "middleware:start" | "middleware:complete" | "middleware:error" | "middleware:blocked" | "transaction:start" | "transaction:complete" | "transaction:error" | "persistence:ready";
|
|
75
|
-
/**
|
|
76
|
-
* Type for middleware functions. Return false to block the update.
|
|
77
|
-
*/
|
|
78
|
-
type Middleware<T> = (state: T, update: DeepPartial<T>) => Promise<DeepPartial<T>> | Promise<void> | DeepPartial<T> | void;
|
|
79
|
-
/**
|
|
80
|
-
* Type for blocking middleware functions that can prevent updates.
|
|
81
|
-
*/
|
|
82
|
-
type BlockingMiddleware<T> = (state: T, update: DeepPartial<T>) => boolean | Promise<boolean>;
|
|
83
|
-
/**
|
|
84
|
-
* Middleware execution information for tracking
|
|
85
|
-
*/
|
|
86
|
-
interface MiddlewareExecution {
|
|
87
|
-
id: string;
|
|
88
|
-
name: string;
|
|
89
|
-
startTime: number;
|
|
90
|
-
endTime: number;
|
|
91
|
-
duration: number;
|
|
92
|
-
blocked: boolean;
|
|
93
|
-
error?: Error;
|
|
94
|
-
}
|
|
95
|
-
/**
|
|
96
|
-
* Represents a state update, which can be either a partial state object or a function.
|
|
97
|
-
*
|
|
98
|
-
* The function can be synchronous or asynchronous, and it receives the current state
|
|
99
|
-
* as an argument and returns a partial state object.
|
|
100
|
-
*
|
|
101
|
-
* @template T The type of the state object.
|
|
102
|
-
*/
|
|
103
|
-
type StateUpdater<T> = DeepPartial<T> | ((state: T) => DeepPartial<T> | Promise<DeepPartial<T>>);
|
|
104
|
-
/**
|
|
105
|
-
* Interface defining the contract for the data state.
|
|
106
|
-
*/
|
|
107
|
-
interface DataStore<T extends object> {
|
|
108
|
-
get(clone?: boolean): T;
|
|
109
|
-
set(update: StateUpdater<T>): Promise<void>;
|
|
110
|
-
subscribe(path: string, listener: (state: T) => void): () => void;
|
|
111
|
-
transaction<R>(operation: () => R | Promise<R>): Promise<R>;
|
|
112
|
-
getPerformanceMetrics(): StoreMetrics;
|
|
113
|
-
onStoreEvent(event: StoreEvent, listener: (data: any) => void): () => void;
|
|
114
|
-
getMiddlewareExecutions(): MiddlewareExecution[];
|
|
115
|
-
}
|
|
116
|
-
/**
|
|
117
|
-
* Implementation of the reactive data state.
|
|
118
|
-
*/
|
|
119
|
-
declare class ReactiveDataStore<T extends object> implements DataStore<T> {
|
|
120
|
-
private readonly metrics;
|
|
121
|
-
private middleware;
|
|
122
|
-
private blockingMiddleware;
|
|
123
|
-
private middlewareExecutions;
|
|
124
|
-
private readonly maxExecutionHistory;
|
|
125
|
-
private pendingUpdates;
|
|
126
|
-
private isUpdating;
|
|
127
|
-
private updateTimes;
|
|
128
|
-
private cache;
|
|
129
|
-
private updateBus;
|
|
130
|
-
private eventBus;
|
|
131
|
-
private executionState;
|
|
132
|
-
private persistence?;
|
|
133
|
-
private instanceID;
|
|
134
|
-
private persistenceReady;
|
|
135
|
-
/**
|
|
136
|
-
* Creates a new instance of ReactiveDataStore.
|
|
137
|
-
* @param initialData The initial state data.
|
|
138
|
-
*/
|
|
139
|
-
constructor(initialData: T, persistence?: DataStorePersistence<T>);
|
|
140
|
-
private setPersistenceReady;
|
|
141
|
-
private setPersistence;
|
|
142
|
-
isReady(): boolean;
|
|
143
|
-
/**
|
|
144
|
-
* Gets current execution state for monitoring
|
|
145
|
-
* @returns The current execution state of the store
|
|
146
|
-
*/
|
|
147
|
-
getExecutionState(): Readonly<StoreExecutionState<T>>;
|
|
148
|
-
/**
|
|
149
|
-
* Gets a specific part of the state at the given path.
|
|
150
|
-
* @returns The current state
|
|
151
|
-
*/
|
|
152
|
-
get(clone?: boolean): T;
|
|
153
|
-
/**
|
|
154
|
-
* Updates the state with the provided partial state.
|
|
155
|
-
* @param update The partial state to update with.
|
|
156
|
-
*/
|
|
157
|
-
set(update: StateUpdater<T>): Promise<void>;
|
|
158
|
-
/**
|
|
159
|
-
* Applies blocking middleware to the update.
|
|
160
|
-
* If any middleware returns false or throws, the update is blocked.
|
|
161
|
-
*
|
|
162
|
-
* @param partialUpdate The partial update to apply middlewares to
|
|
163
|
-
* @returns Result indicating if update was blocked and any error
|
|
164
|
-
*/
|
|
165
|
-
private applyBlockingMiddleware;
|
|
166
|
-
/**
|
|
167
|
-
* Applies a chain of middleware functions to an initial state.
|
|
168
|
-
*
|
|
169
|
-
* @template T The type of the state object.
|
|
170
|
-
* @param initialState The initial state to be transformed.
|
|
171
|
-
* @param partialUpdate The original partial update (for context)
|
|
172
|
-
* @returns The final transformed state.
|
|
173
|
-
*/
|
|
174
|
-
private applyMiddleware;
|
|
175
|
-
/**
|
|
176
|
-
* Subscribes a callback function to changes in the data at the specified path.
|
|
177
|
-
* @param path The path to subscribe to.
|
|
178
|
-
* @param callback The callback function to call when the path changes.
|
|
179
|
-
* @returns Function to unsubscribe the listener.
|
|
180
|
-
*/
|
|
181
|
-
subscribe(path: string | Array<string>, callback: (state: T) => void): () => void;
|
|
182
|
-
/**
|
|
183
|
-
* returns instance id
|
|
184
|
-
* @param path The path to subscribe to.
|
|
185
|
-
* @param callback The callback function to call when the path changes.
|
|
186
|
-
* @returns Function to unsubscribe the listener.
|
|
187
|
-
*/
|
|
188
|
-
id(): string;
|
|
189
|
-
/**
|
|
190
|
-
* Executes an operation within a transaction that can be rolled back if an error occurs.
|
|
191
|
-
* @param operation The operation to execute.
|
|
192
|
-
* @returns The result of the operation.
|
|
193
|
-
*/
|
|
194
|
-
/**
|
|
195
|
-
* Executes an operation within a transaction that can be rolled back if an error occurs.
|
|
196
|
-
* @param operation The operation to execute.
|
|
197
|
-
* @returns The result of the operation.
|
|
198
|
-
*/
|
|
199
|
-
transaction<R>(operation: () => R | Promise<R>): Promise<R>;
|
|
200
|
-
/**
|
|
201
|
-
* Adds a transformation middleware to the state.
|
|
202
|
-
* These middleware functions can modify updates but cannot block them.
|
|
203
|
-
*
|
|
204
|
-
* @param middleware The middleware function
|
|
205
|
-
* @param name Optional name for the middleware (for debugging)
|
|
206
|
-
* @returns ID of the registered middleware for removal
|
|
207
|
-
*/
|
|
208
|
-
use(middleware: Middleware<T>, name?: string): string;
|
|
209
|
-
/**
|
|
210
|
-
* Adds a blocking middleware to the state.
|
|
211
|
-
* These middleware functions can block updates by returning false or throwing.
|
|
212
|
-
*
|
|
213
|
-
* @param middleware The blocking middleware function
|
|
214
|
-
* @param name Optional name for the middleware (for debugging)
|
|
215
|
-
* @returns ID of the registered middleware for removal
|
|
216
|
-
*/
|
|
217
|
-
useBlockingMiddleware(middleware: BlockingMiddleware<T>, name?: string): string;
|
|
218
|
-
/**
|
|
219
|
-
* Removes a middleware by ID
|
|
220
|
-
* @param id The middleware ID to remove
|
|
221
|
-
* @returns true if middleware was found and removed
|
|
222
|
-
*/
|
|
223
|
-
removeMiddleware(id: string): boolean;
|
|
224
|
-
/**
|
|
225
|
-
* Returns current performance metrics of the state.
|
|
226
|
-
* @returns The state metrics.
|
|
227
|
-
*/
|
|
228
|
-
getPerformanceMetrics(): StoreMetrics;
|
|
229
|
-
/**
|
|
230
|
-
* Returns the history of middleware executions.
|
|
231
|
-
* @returns Array of middleware execution information
|
|
232
|
-
*/
|
|
233
|
-
getMiddlewareExecutions(): MiddlewareExecution[];
|
|
234
|
-
/**
|
|
235
|
-
* Subscribe to state events for observability
|
|
236
|
-
* @param event Event type to listen for
|
|
237
|
-
* @param listener Callback function when event occurs
|
|
238
|
-
* @returns Function to unsubscribe
|
|
239
|
-
*/
|
|
240
|
-
onStoreEvent(event: StoreEvent, listener: (data: any) => void): () => void;
|
|
241
|
-
/**
|
|
242
|
-
* Notifies listeners whose subscribed paths are affected by the changes.
|
|
243
|
-
* @param changedPaths Array of paths that have changed.
|
|
244
|
-
*/
|
|
245
|
-
private notifyListeners;
|
|
246
|
-
/**
|
|
247
|
-
* Tracks middleware execution for debugging purposes
|
|
248
|
-
* @param execution The middleware execution information
|
|
249
|
-
*/
|
|
250
|
-
private trackMiddlewareExecution;
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
/**
|
|
254
|
-
* Store Observability Module
|
|
255
|
-
* A separate module to add debugging and observability capabilities to the ReactiveDataStore.
|
|
256
|
-
*/
|
|
257
|
-
|
|
258
|
-
/**
|
|
259
|
-
* Interface for debug event structure
|
|
260
|
-
*/
|
|
261
|
-
interface DebugEvent {
|
|
262
|
-
type: string;
|
|
263
|
-
timestamp: number;
|
|
264
|
-
data: any;
|
|
265
|
-
}
|
|
266
|
-
/**
|
|
267
|
-
* Configuration options for the observability module
|
|
268
|
-
*/
|
|
269
|
-
interface ObservabilityOptions {
|
|
270
|
-
/** Maximum number of events to keep in history */
|
|
271
|
-
maxEvents?: number;
|
|
272
|
-
/** Whether to enable console logging */
|
|
273
|
-
enableConsoleLogging?: boolean;
|
|
274
|
-
maxStateHistory?: number;
|
|
275
|
-
/** Options for specific event types to log */
|
|
276
|
-
logEvents?: {
|
|
277
|
-
updates?: boolean;
|
|
278
|
-
middleware?: boolean;
|
|
279
|
-
transactions?: boolean;
|
|
280
|
-
};
|
|
281
|
-
/** Time threshold in ms to highlight slow operations */
|
|
282
|
-
performanceThresholds?: {
|
|
283
|
-
updateTime?: number;
|
|
284
|
-
middlewareTime?: number;
|
|
285
|
-
};
|
|
286
|
-
}
|
|
287
|
-
/**
|
|
288
|
-
* Class for observing and debugging a ReactiveDataStore instance
|
|
289
|
-
*/
|
|
290
|
-
declare class StoreObservability<T extends object> {
|
|
291
|
-
protected store: DataStore<T>;
|
|
292
|
-
private eventHistory;
|
|
293
|
-
private maxEvents;
|
|
294
|
-
private enableConsoleLogging;
|
|
295
|
-
private logEvents;
|
|
296
|
-
private performanceThresholds;
|
|
297
|
-
private unsubscribers;
|
|
298
|
-
private stateHistory;
|
|
299
|
-
private maxStateHistory;
|
|
300
|
-
private activeTransactionCount;
|
|
301
|
-
private activeBatches;
|
|
302
|
-
/**
|
|
303
|
-
* Creates a new StoreObservability instance
|
|
304
|
-
* @param store The ReactiveDataStore to observe
|
|
305
|
-
* @param options Configuration options
|
|
306
|
-
*/
|
|
307
|
-
constructor(store: DataStore<T>, options?: ObservabilityOptions);
|
|
308
|
-
/**
|
|
309
|
-
* Sets up all event listeners
|
|
310
|
-
*/
|
|
311
|
-
private setupEventListeners;
|
|
312
|
-
/**
|
|
313
|
-
* Records a state snapshot
|
|
314
|
-
*/
|
|
315
|
-
private recordStateSnapshot;
|
|
316
|
-
/**
|
|
317
|
-
* Records an event to the history
|
|
318
|
-
* @param type Event type
|
|
319
|
-
* @param data Event data
|
|
320
|
-
*/
|
|
321
|
-
private recordEvent;
|
|
322
|
-
/**
|
|
323
|
-
* Logs an event to the console with appropriate formatting
|
|
324
|
-
* @param type Event type
|
|
325
|
-
* @param data Event data
|
|
326
|
-
*/
|
|
327
|
-
private logEventToConsole;
|
|
328
|
-
/**
|
|
329
|
-
* Checks for performance issues in the events
|
|
330
|
-
* @param type Event type
|
|
331
|
-
* @param data Event data
|
|
332
|
-
*/
|
|
333
|
-
private checkPerformance;
|
|
334
|
-
/**
|
|
335
|
-
* Returns the event history
|
|
336
|
-
* @returns Array of debug events
|
|
337
|
-
*/
|
|
338
|
-
getEventHistory(): DebugEvent[];
|
|
339
|
-
/**
|
|
340
|
-
* Returns the state history
|
|
341
|
-
* @returns Array of state snapshots
|
|
342
|
-
*/
|
|
343
|
-
getStateHistory(): T[];
|
|
344
|
-
/**
|
|
345
|
-
* Returns middleware execution history from the state
|
|
346
|
-
* @returns Array of middleware executions
|
|
347
|
-
*/
|
|
348
|
-
getMiddlewareExecutions(): MiddlewareExecution[];
|
|
349
|
-
/**
|
|
350
|
-
* Returns state performance metrics
|
|
351
|
-
* @returns Store metrics object
|
|
352
|
-
*/
|
|
353
|
-
getPerformanceMetrics(): StoreMetrics;
|
|
354
|
-
/**
|
|
355
|
-
* Returns current transaction status
|
|
356
|
-
* @returns Object with transaction status information
|
|
357
|
-
*/
|
|
358
|
-
getTransactionStatus(): {
|
|
359
|
-
activeTransactions: number;
|
|
360
|
-
activeBatches: string[];
|
|
361
|
-
};
|
|
362
|
-
/**
|
|
363
|
-
* Creates a middleware that logs all updates
|
|
364
|
-
* @param options Options for the logging middleware
|
|
365
|
-
* @returns A middleware function
|
|
366
|
-
*/
|
|
367
|
-
createLoggingMiddleware(options?: {
|
|
368
|
-
logLevel?: "debug" | "info" | "warn";
|
|
369
|
-
logUpdates?: boolean;
|
|
370
|
-
}): (state: T, update: DeepPartial<T>) => DeepPartial<T>;
|
|
371
|
-
/**
|
|
372
|
-
* Creates a middleware that validates updates against a schema
|
|
373
|
-
* @param validator Function that validates updates
|
|
374
|
-
* @returns A blocking middleware function
|
|
375
|
-
*/
|
|
376
|
-
createValidationMiddleware(validator: (state: T, update: DeepPartial<T>) => boolean | {
|
|
377
|
-
valid: boolean;
|
|
378
|
-
reason?: string;
|
|
379
|
-
}): (state: T, update: DeepPartial<T>) => boolean;
|
|
380
|
-
/**
|
|
381
|
-
* Clears all event and state history
|
|
382
|
-
*/
|
|
383
|
-
clearHistory(): void;
|
|
384
|
-
/**
|
|
385
|
-
* Returns a simplified view of recent state changes
|
|
386
|
-
* @param limit Maximum number of state changes to compare
|
|
387
|
-
* @returns Array of state difference objects
|
|
388
|
-
*/
|
|
389
|
-
getRecentChanges(limit?: number): Array<{
|
|
390
|
-
timestamp: number;
|
|
391
|
-
changedPaths: string[];
|
|
392
|
-
from: Partial<T>;
|
|
393
|
-
to: Partial<T>;
|
|
394
|
-
}>;
|
|
395
|
-
/**
|
|
396
|
-
* Creates a time-travel debug middleware that lets you undo/redo state changes
|
|
397
|
-
* @returns An object with undo/redo methods and state info
|
|
398
|
-
*/
|
|
399
|
-
createTimeTravel(): {
|
|
400
|
-
canUndo: () => boolean;
|
|
401
|
-
canRedo: () => boolean;
|
|
402
|
-
undo: () => void;
|
|
403
|
-
redo: () => void;
|
|
404
|
-
getHistoryLength: () => number;
|
|
405
|
-
clear: () => void;
|
|
406
|
-
};
|
|
407
|
-
/**
|
|
408
|
-
* Disconnects all event listeners and cleans up resources
|
|
409
|
-
*/
|
|
410
|
-
disconnect(): void;
|
|
411
|
-
}
|
|
1
|
+
import { SimplePersistence } from '@asaidimu/utils-persistence';
|
|
2
|
+
import { ObservabilityOptions, DeepPartial, BlockingMiddleware, Middleware, ReactiveDataStore, StoreObserver } from '@asaidimu/utils-store';
|
|
412
3
|
|
|
413
4
|
type StoreOptions<T> = ObservabilityOptions & {
|
|
414
5
|
enableMetrics?: boolean;
|
|
415
|
-
persistence?:
|
|
6
|
+
persistence?: SimplePersistence<T>;
|
|
416
7
|
};
|
|
417
8
|
type Action<T> = (state: T, ...args: any[]) => DeepPartial<T>;
|
|
418
9
|
type StoreSelector<T, S> = (state: T) => S;
|
|
@@ -462,7 +53,7 @@ declare function createStore<T extends Record<string, unknown>, R extends Action
|
|
|
462
53
|
/** Direct store instance access */
|
|
463
54
|
readonly store: ReactiveDataStore<T>;
|
|
464
55
|
/** Performance monitoring tools when enabled */
|
|
465
|
-
readonly observer:
|
|
56
|
+
readonly observer: StoreObserver<T> | undefined;
|
|
466
57
|
/** Selector creator for subscribing to specific state */
|
|
467
58
|
readonly select: <S>(selector: (state: T) => S) => S;
|
|
468
59
|
/** Action dispatchers */
|
|
@@ -474,304 +65,4 @@ declare function createStore<T extends Record<string, unknown>, R extends Action
|
|
|
474
65
|
readonly state: () => T;
|
|
475
66
|
};
|
|
476
67
|
|
|
477
|
-
|
|
478
|
-
* Configuration for external metrics destination
|
|
479
|
-
*/
|
|
480
|
-
interface RemoteDestination {
|
|
481
|
-
/** Unique identifier for this destination */
|
|
482
|
-
id: string;
|
|
483
|
-
/** Human-readable name of the destination */
|
|
484
|
-
name: string;
|
|
485
|
-
/** Function that handles sending metrics to the destination */
|
|
486
|
-
send: (payload: RemoteMetricsPayload) => Promise<void>;
|
|
487
|
-
/** Optional function to test connection to the destination */
|
|
488
|
-
testConnection?: () => Promise<boolean>;
|
|
489
|
-
/** Optional configuration for this destination */
|
|
490
|
-
config?: Record<string, any>;
|
|
491
|
-
}
|
|
492
|
-
/**
|
|
493
|
-
* Types of metrics that can be sent remotely
|
|
494
|
-
*/
|
|
495
|
-
type MetricType = 'counter' | 'gauge' | 'histogram' | 'log' | 'trace';
|
|
496
|
-
/**
|
|
497
|
-
* Structure for remote metric payloads
|
|
498
|
-
*/
|
|
499
|
-
interface RemoteMetricsPayload {
|
|
500
|
-
/** Timestamp when the metrics were collected */
|
|
501
|
-
timestamp: number;
|
|
502
|
-
/** Source identifier (app name, instance ID, etc.) */
|
|
503
|
-
source: string;
|
|
504
|
-
/** Collection of metrics being reported */
|
|
505
|
-
metrics: Array<{
|
|
506
|
-
/** Metric name (should follow naming conventions) */
|
|
507
|
-
name: string;
|
|
508
|
-
/** Type of metric */
|
|
509
|
-
type: MetricType;
|
|
510
|
-
/** Value of the metric */
|
|
511
|
-
value: number | string | object;
|
|
512
|
-
/** Optional units for the metric */
|
|
513
|
-
unit?: string;
|
|
514
|
-
/** Contextual labels/tags for the metric */
|
|
515
|
-
labels?: Record<string, string>;
|
|
516
|
-
/** For trace type, references to parent spans */
|
|
517
|
-
parentId?: string;
|
|
518
|
-
/** For trace type, trace ID for grouping spans */
|
|
519
|
-
traceId?: string;
|
|
520
|
-
}>;
|
|
521
|
-
}
|
|
522
|
-
/**
|
|
523
|
-
* Configuration options for RemoteObservability
|
|
524
|
-
*/
|
|
525
|
-
interface RemoteObservabilityOptions extends ObservabilityOptions {
|
|
526
|
-
/** Application/service name */
|
|
527
|
-
serviceName: string;
|
|
528
|
-
/** Environment (prod, staging, dev) */
|
|
529
|
-
environment: string;
|
|
530
|
-
/** Instance identifier (for distributed systems) */
|
|
531
|
-
instanceId?: string;
|
|
532
|
-
/** How frequently to send batched metrics (ms) */
|
|
533
|
-
reportingInterval?: number;
|
|
534
|
-
/** How many metrics to batch before sending */
|
|
535
|
-
batchSize?: number;
|
|
536
|
-
/** Whether to disable batching and send immediately */
|
|
537
|
-
immediateReporting?: boolean;
|
|
538
|
-
/** Which categories of metrics to collect */
|
|
539
|
-
collectCategories?: {
|
|
540
|
-
performance?: boolean;
|
|
541
|
-
errors?: boolean;
|
|
542
|
-
stateChanges?: boolean;
|
|
543
|
-
middleware?: boolean;
|
|
544
|
-
};
|
|
545
|
-
/** Whether to compress payloads */
|
|
546
|
-
compressPayloads?: boolean;
|
|
547
|
-
}
|
|
548
|
-
/**
|
|
549
|
-
* Remote observability extension for store monitoring
|
|
550
|
-
*/
|
|
551
|
-
declare class RemoteObservability<T extends object> extends StoreObservability<T> {
|
|
552
|
-
private destinations;
|
|
553
|
-
private metricsBatch;
|
|
554
|
-
private reportingInterval;
|
|
555
|
-
private batchSize;
|
|
556
|
-
private serviceName;
|
|
557
|
-
private environment;
|
|
558
|
-
private instanceId;
|
|
559
|
-
private immediateReporting;
|
|
560
|
-
private collectCategories;
|
|
561
|
-
private compressPayloads;
|
|
562
|
-
private reportingTimer;
|
|
563
|
-
private traceIdCounter;
|
|
564
|
-
private activeTraces;
|
|
565
|
-
/**
|
|
566
|
-
* Creates a new RemoteObservability instance
|
|
567
|
-
* @param store The ReactiveDataStore to observe
|
|
568
|
-
* @param options Configuration options
|
|
569
|
-
*/
|
|
570
|
-
constructor(store: DataStore<T>, options: RemoteObservabilityOptions);
|
|
571
|
-
/**
|
|
572
|
-
* Generate a unique instance ID if none provided
|
|
573
|
-
*/
|
|
574
|
-
private generateInstanceId;
|
|
575
|
-
/**
|
|
576
|
-
* Setup additional event listeners for remote reporting
|
|
577
|
-
*/
|
|
578
|
-
private setupRemoteEventListeners;
|
|
579
|
-
/**
|
|
580
|
-
* Begin a trace for performance tracking
|
|
581
|
-
* @param name Name of the trace
|
|
582
|
-
* @returns Trace ID
|
|
583
|
-
*/
|
|
584
|
-
beginTrace(name: string): string;
|
|
585
|
-
/**
|
|
586
|
-
* Begin a span within a trace
|
|
587
|
-
* @param traceId Parent trace ID
|
|
588
|
-
* @param name Span name
|
|
589
|
-
* @param labels Additional context labels
|
|
590
|
-
* @returns Span ID
|
|
591
|
-
*/
|
|
592
|
-
beginSpan(traceId: string, name: string, labels?: Record<string, string>): string;
|
|
593
|
-
/**
|
|
594
|
-
* End a span within a trace
|
|
595
|
-
* @param traceId Parent trace ID
|
|
596
|
-
* @param spanName Name of span to end (ends last matching span)
|
|
597
|
-
*/
|
|
598
|
-
endSpan(traceId: string, spanName: string): void;
|
|
599
|
-
/**
|
|
600
|
-
* End a trace and send it to remote destinations
|
|
601
|
-
* @param traceId Trace ID to end
|
|
602
|
-
*/
|
|
603
|
-
endTrace(traceId: string): void;
|
|
604
|
-
/**
|
|
605
|
-
* Add a remote destination for metrics
|
|
606
|
-
* @param destination Remote destination configuration
|
|
607
|
-
* @returns Boolean indicating if destination was added successfully
|
|
608
|
-
*/
|
|
609
|
-
addDestination(destination: RemoteDestination): boolean;
|
|
610
|
-
/**
|
|
611
|
-
* Remove a remote destination by ID
|
|
612
|
-
* @param id Destination ID to remove
|
|
613
|
-
* @returns Boolean indicating if destination was removed
|
|
614
|
-
*/
|
|
615
|
-
removeDestination(id: string): boolean;
|
|
616
|
-
/**
|
|
617
|
-
* Get list of configured destinations
|
|
618
|
-
* @returns Array of destination IDs and names
|
|
619
|
-
*/
|
|
620
|
-
getDestinations(): Array<{
|
|
621
|
-
id: string;
|
|
622
|
-
name: string;
|
|
623
|
-
}>;
|
|
624
|
-
/**
|
|
625
|
-
* Test connection to all destinations
|
|
626
|
-
* @returns Object mapping destination IDs to connection status
|
|
627
|
-
*/
|
|
628
|
-
testAllConnections(): Promise<Record<string, boolean>>;
|
|
629
|
-
/**
|
|
630
|
-
* Track a metric and add it to the batch
|
|
631
|
-
* @param metric Metric to track
|
|
632
|
-
*/
|
|
633
|
-
private trackMetric;
|
|
634
|
-
/**
|
|
635
|
-
* Flush metrics to all configured destinations
|
|
636
|
-
*/
|
|
637
|
-
private flushMetrics;
|
|
638
|
-
/**
|
|
639
|
-
* Start the periodic reporting cycle
|
|
640
|
-
*/
|
|
641
|
-
private startReportingCycle;
|
|
642
|
-
/**
|
|
643
|
-
* Change the reporting interval
|
|
644
|
-
* @param intervalMs New interval in milliseconds
|
|
645
|
-
*/
|
|
646
|
-
setReportingInterval(intervalMs: number): void;
|
|
647
|
-
/**
|
|
648
|
-
* Collect and report current store performance metrics
|
|
649
|
-
*/
|
|
650
|
-
reportCurrentMetrics(): void;
|
|
651
|
-
/**
|
|
652
|
-
* Override disconnect to also flush metrics
|
|
653
|
-
*/
|
|
654
|
-
disconnect(): void;
|
|
655
|
-
}
|
|
656
|
-
|
|
657
|
-
/**
|
|
658
|
-
* Create a destination that sends metrics to an OpenTelemetry collector
|
|
659
|
-
* @param config Configuration for the OpenTelemetry destination
|
|
660
|
-
*/
|
|
661
|
-
declare function createOpenTelemetryDestination(config: {
|
|
662
|
-
endpoint: string;
|
|
663
|
-
apiKey?: string;
|
|
664
|
-
resource?: Record<string, string>;
|
|
665
|
-
}): RemoteDestination;
|
|
666
|
-
/**
|
|
667
|
-
* Create a destination that sends metrics to Prometheus
|
|
668
|
-
* @param config Configuration for the Prometheus destination
|
|
669
|
-
*/
|
|
670
|
-
declare function createPrometheusDestination(config: {
|
|
671
|
-
pushgatewayUrl: string;
|
|
672
|
-
username?: string;
|
|
673
|
-
password?: string;
|
|
674
|
-
jobName: string;
|
|
675
|
-
}): RemoteDestination;
|
|
676
|
-
/**
|
|
677
|
-
* Create a destination that sends metrics to Grafana Cloud
|
|
678
|
-
* @param config Configuration for the Grafana Cloud destination
|
|
679
|
-
*/
|
|
680
|
-
declare function createGrafanaCloudDestination(config: {
|
|
681
|
-
url: string;
|
|
682
|
-
apiKey: string;
|
|
683
|
-
}): RemoteDestination;
|
|
684
|
-
/**
|
|
685
|
-
* Create a destination that sends metrics to a generic HTTP endpoint
|
|
686
|
-
* @param config Configuration for the HTTP destination
|
|
687
|
-
*/
|
|
688
|
-
declare function createHttpDestination(config: {
|
|
689
|
-
url: string;
|
|
690
|
-
headers?: Record<string, string>;
|
|
691
|
-
method?: 'POST' | 'PUT';
|
|
692
|
-
transformPayload?: (payload: RemoteMetricsPayload) => any;
|
|
693
|
-
}): RemoteDestination;
|
|
694
|
-
|
|
695
|
-
/**
|
|
696
|
-
* Hook to use the RemoteObservability instance with React
|
|
697
|
-
* @param store Store instance
|
|
698
|
-
* @param options Configuration options
|
|
699
|
-
*/
|
|
700
|
-
declare function useRemoteObservability<T extends object>(store: DataStore<T>, options: RemoteObservabilityOptions): {
|
|
701
|
-
remote: RemoteObservability<T>;
|
|
702
|
-
addOpenTelemetryDestination: (config: Parameters<typeof createOpenTelemetryDestination>[0]) => boolean;
|
|
703
|
-
addPrometheusDestination: (config: Parameters<typeof createPrometheusDestination>[0]) => boolean;
|
|
704
|
-
addGrafanaCloudDestination: (config: Parameters<typeof createGrafanaCloudDestination>[0]) => boolean;
|
|
705
|
-
addHttpDestination: (config: Parameters<typeof createHttpDestination>[0]) => boolean;
|
|
706
|
-
};
|
|
707
|
-
|
|
708
|
-
/**
|
|
709
|
-
* Implements DataStorePersistence using web storage (localStorage or sessionStorage).
|
|
710
|
-
* Supports cross-tab synchronization via an event bus and native storage events when using localStorage.
|
|
711
|
-
*/
|
|
712
|
-
declare class WebStoragePersistence<T extends object> implements DataStorePersistence<T> {
|
|
713
|
-
private readonly storageKey;
|
|
714
|
-
private readonly eventBus;
|
|
715
|
-
private readonly storage;
|
|
716
|
-
/**
|
|
717
|
-
* Initializes a new instance of WebStoragePersistence.
|
|
718
|
-
* @param storageKey The key under which data is stored in web storage (e.g., 'user-profile').
|
|
719
|
-
* @param session Optional flag; if true, uses sessionStorage instead of localStorage (default: false).
|
|
720
|
-
*/
|
|
721
|
-
constructor(storageKey: string, session?: boolean);
|
|
722
|
-
/**
|
|
723
|
-
* Initializes the event bus with configured settings.
|
|
724
|
-
* @returns Configured event bus instance.
|
|
725
|
-
*/
|
|
726
|
-
private initializeEventBus;
|
|
727
|
-
/**
|
|
728
|
-
* Sets up a listener for native storage events to synchronize across tabs (localStorage only).
|
|
729
|
-
*/
|
|
730
|
-
private setupStorageEventListener;
|
|
731
|
-
/**
|
|
732
|
-
* Persists the provided state to web storage and notifies subscribers.
|
|
733
|
-
* @param instanceId Unique identifier of the instance making the update.
|
|
734
|
-
* @param state The state to persist.
|
|
735
|
-
* @returns True if successful, false if an error occurs.
|
|
736
|
-
*/
|
|
737
|
-
set(instanceId: string, state: T): boolean;
|
|
738
|
-
/**
|
|
739
|
-
* Retrieves the current state from web storage.
|
|
740
|
-
* @returns The persisted state, or null if not found or invalid.
|
|
741
|
-
*/
|
|
742
|
-
get(): T | null;
|
|
743
|
-
/**
|
|
744
|
-
* Subscribes to updates in the persisted state, excluding the instance’s own changes.
|
|
745
|
-
* @param instanceId Unique identifier of the subscribing instance.
|
|
746
|
-
* @param onStateChange Callback to invoke with the updated state.
|
|
747
|
-
* @returns Function to unsubscribe from updates.
|
|
748
|
-
*/
|
|
749
|
-
subscribe(instanceId: string, onStateChange: (state: T) => void): () => void;
|
|
750
|
-
/**
|
|
751
|
-
* Removes the persisted state from web storage.
|
|
752
|
-
* @returns True if successful, false if an error occurs.
|
|
753
|
-
*/
|
|
754
|
-
clear(): boolean;
|
|
755
|
-
}
|
|
756
|
-
/**
|
|
757
|
-
* Alias for WebStoragePersistence, maintained for backward compatibility.
|
|
758
|
-
* @deprecated Use `WebStoragePersistence` instead, which supports both localStorage and sessionStorage.
|
|
759
|
-
*/
|
|
760
|
-
declare const LocalStoragePersistence: typeof WebStoragePersistence;
|
|
761
|
-
|
|
762
|
-
/**
|
|
763
|
-
* IndexedDB persistence adapter with storeId and ephemeral instance IDs
|
|
764
|
-
*/
|
|
765
|
-
declare class IndexedDBPersistence<T> implements DataStorePersistence<T> {
|
|
766
|
-
private cursorPromise;
|
|
767
|
-
private storeId;
|
|
768
|
-
private eventBus;
|
|
769
|
-
constructor(storeId: string);
|
|
770
|
-
set(id: string, state: T): Promise<boolean>;
|
|
771
|
-
get(): Promise<T | null>;
|
|
772
|
-
subscribe(id: string, callback: (state: T) => void): () => void;
|
|
773
|
-
clear(): Promise<boolean>;
|
|
774
|
-
static closeAll(): Promise<void>;
|
|
775
|
-
}
|
|
776
|
-
|
|
777
|
-
export { type Action, type Actions, type BlockingMiddleware, type DataStore, type DataStorePersistence, type DebugEvent, type DeepPartial, IndexedDBPersistence, LocalStoragePersistence, type MetricType, type Middleware, type MiddlewareExecution, type ObservabilityOptions, ReactiveDataStore, type RemoteDestination, type RemoteMetricsPayload, RemoteObservability, type RemoteObservabilityOptions, type StoreDefinition, type StoreEvent, type StoreExecutionState, type StoreMetrics, StoreObservability, type StoreOptions, type StoreSelector, WebStoragePersistence, createGrafanaCloudDestination, createHttpDestination, createOpenTelemetryDestination, createPrometheusDestination, createStore, useRemoteObservability };
|
|
68
|
+
export { type Action, type Actions, type StoreDefinition, type StoreOptions, type StoreSelector, createStore };
|