@umituz/react-native-firebase 2.5.1 → 2.6.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/package.json +1 -1
- package/src/domains/firestore/index.ts +11 -0
- package/src/shared/domain/utils/calculation.util.ts +17 -305
- package/src/shared/domain/utils/index.ts +0 -5
- package/src/shared/infrastructure/config/state/FirebaseClientState.ts +34 -12
- package/src/shared/infrastructure/config/base/ClientStateManager.ts +0 -82
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@umituz/react-native-firebase",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.6.0",
|
|
4
4
|
"description": "Unified Firebase package for React Native apps - Auth and Firestore services using Firebase JS SDK (no native modules).",
|
|
5
5
|
"main": "./src/index.ts",
|
|
6
6
|
"types": "./src/index.ts",
|
|
@@ -100,5 +100,16 @@ export type { UseFirestoreQueryOptions } from './presentation/hooks/useFirestore
|
|
|
100
100
|
export type { UseFirestoreMutationOptions } from './presentation/hooks/useFirestoreMutation';
|
|
101
101
|
export type { UseFirestoreSnapshotOptions } from './presentation/hooks/useFirestoreSnapshot';
|
|
102
102
|
|
|
103
|
+
// Re-export commonly used Firebase Firestore types
|
|
104
|
+
export type {
|
|
105
|
+
Timestamp,
|
|
106
|
+
DocumentSnapshot,
|
|
107
|
+
QuerySnapshot,
|
|
108
|
+
DocumentReference,
|
|
109
|
+
CollectionReference,
|
|
110
|
+
Query,
|
|
111
|
+
Transaction,
|
|
112
|
+
} from 'firebase/firestore';
|
|
113
|
+
|
|
103
114
|
// Firebase types are available from the 'firebase' package directly
|
|
104
115
|
// Import them in your app: import { Timestamp } from 'firebase/firestore';
|
|
@@ -1,24 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Calculation Utilities
|
|
3
|
-
* Common mathematical operations used across the codebase
|
|
4
|
-
* Optimized for performance with minimal allocations
|
|
5
3
|
*/
|
|
6
4
|
|
|
7
|
-
/**
|
|
8
|
-
* Safely calculates percentage (0-1 range)
|
|
9
|
-
* Optimized: Guards against division by zero
|
|
10
|
-
*
|
|
11
|
-
* @param current - Current value
|
|
12
|
-
* @param limit - Maximum value
|
|
13
|
-
* @returns Percentage between 0 and 1
|
|
14
|
-
*
|
|
15
|
-
* @example
|
|
16
|
-
* ```typescript
|
|
17
|
-
* const percentage = calculatePercentage(750, 1000); // 0.75
|
|
18
|
-
* const percentage = calculatePercentage(1200, 1000); // 1.0 (capped)
|
|
19
|
-
* const percentage = calculatePercentage(0, 1000); // 0.0
|
|
20
|
-
* ```
|
|
21
|
-
*/
|
|
22
5
|
export function calculatePercentage(current: number, limit: number): number {
|
|
23
6
|
if (limit <= 0) return 0;
|
|
24
7
|
if (current <= 0) return 0;
|
|
@@ -26,327 +9,56 @@ export function calculatePercentage(current: number, limit: number): number {
|
|
|
26
9
|
return current / limit;
|
|
27
10
|
}
|
|
28
11
|
|
|
29
|
-
/**
|
|
30
|
-
* Calculates remaining quota
|
|
31
|
-
* Optimized: Single Math.max call
|
|
32
|
-
*
|
|
33
|
-
* @param current - Current usage
|
|
34
|
-
* @param limit - Maximum limit
|
|
35
|
-
* @returns Remaining amount (minimum 0)
|
|
36
|
-
*
|
|
37
|
-
* @example
|
|
38
|
-
* ```typescript
|
|
39
|
-
* const remaining = calculateRemaining(250, 1000); // 750
|
|
40
|
-
* const remaining = calculateRemaining(1200, 1000); // 0 (capped)
|
|
41
|
-
* ```
|
|
42
|
-
*/
|
|
43
12
|
export function calculateRemaining(current: number, limit: number): number {
|
|
44
13
|
return Math.max(0, limit - current);
|
|
45
14
|
}
|
|
46
15
|
|
|
47
|
-
/**
|
|
48
|
-
* Safe floor with minimum value
|
|
49
|
-
* Optimized: Single comparison
|
|
50
|
-
*
|
|
51
|
-
* @param value - Value to floor
|
|
52
|
-
* @param min - Minimum allowed value
|
|
53
|
-
* @returns Floored value, at least min
|
|
54
|
-
*
|
|
55
|
-
* @example
|
|
56
|
-
* ```typescript
|
|
57
|
-
* const result = safeFloor(5.7, 1); // 5
|
|
58
|
-
* const result = safeFloor(0.3, 1); // 1 (min enforced)
|
|
59
|
-
* const result = safeFloor(-2.5, 0); // 0 (min enforced)
|
|
60
|
-
* ```
|
|
61
|
-
*/
|
|
62
16
|
export function safeFloor(value: number, min: number): number {
|
|
63
17
|
const floored = Math.floor(value);
|
|
64
18
|
return floored < min ? min : floored;
|
|
65
19
|
}
|
|
66
20
|
|
|
67
|
-
/**
|
|
68
|
-
* Safe ceil with maximum value
|
|
69
|
-
* Optimized: Single comparison
|
|
70
|
-
*
|
|
71
|
-
* @param value - Value to ceil
|
|
72
|
-
* @param max - Maximum allowed value
|
|
73
|
-
* @returns Ceiled value, at most max
|
|
74
|
-
*
|
|
75
|
-
* @example
|
|
76
|
-
* ```typescript
|
|
77
|
-
* const result = safeCeil(5.2, 10); // 6
|
|
78
|
-
* const result = safeCeil(9.8, 10); // 10 (max enforced)
|
|
79
|
-
* const result = safeCeil(12.1, 10); // 10 (max enforced)
|
|
80
|
-
* ```
|
|
81
|
-
*/
|
|
82
|
-
export function safeCeil(value: number, max: number): number {
|
|
83
|
-
const ceiled = Math.ceil(value);
|
|
84
|
-
return ceiled > max ? max : ceiled;
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
/**
|
|
88
|
-
* Clamp value between min and max
|
|
89
|
-
* Optimized: Efficient without branching
|
|
90
|
-
*
|
|
91
|
-
* @param value - Value to clamp
|
|
92
|
-
* @param min - Minimum value
|
|
93
|
-
* @param max - Maximum value
|
|
94
|
-
* @returns Clamped value
|
|
95
|
-
*
|
|
96
|
-
* @example
|
|
97
|
-
* ```typescript
|
|
98
|
-
* const result = clamp(5, 0, 10); // 5
|
|
99
|
-
* const result = clamp(-5, 0, 10); // 0
|
|
100
|
-
* const result = clamp(15, 0, 10); // 10
|
|
101
|
-
* ```
|
|
102
|
-
*/
|
|
103
|
-
export function clamp(value: number, min: number, max: number): number {
|
|
104
|
-
return Math.max(min, Math.min(value, max));
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
/**
|
|
108
|
-
* Calculate milliseconds between two dates
|
|
109
|
-
* Optimized: Direct subtraction without Date object creation
|
|
110
|
-
*
|
|
111
|
-
* @param date1 - First date (timestamp or Date)
|
|
112
|
-
* @param date2 - Second date (timestamp or Date)
|
|
113
|
-
* @returns Difference in milliseconds (date1 - date2)
|
|
114
|
-
*
|
|
115
|
-
* @example
|
|
116
|
-
* ```typescript
|
|
117
|
-
* const diff = diffMs(Date.now(), Date.now() - 3600000); // 3600000
|
|
118
|
-
* ```
|
|
119
|
-
*/
|
|
120
21
|
export function diffMs(date1: number | Date, date2: number | Date): number {
|
|
121
|
-
const
|
|
122
|
-
const
|
|
123
|
-
return
|
|
22
|
+
const d1 = typeof date1 === 'number' ? date1 : date1.getTime();
|
|
23
|
+
const d2 = typeof date2 === 'number' ? date2 : date2.getTime();
|
|
24
|
+
return Math.abs(d1 - d2);
|
|
124
25
|
}
|
|
125
26
|
|
|
126
|
-
/**
|
|
127
|
-
* Calculate minutes difference between two dates
|
|
128
|
-
* Optimized: Single Math.floor call
|
|
129
|
-
*
|
|
130
|
-
* @param date1 - First date
|
|
131
|
-
* @param date2 - Second date
|
|
132
|
-
* @returns Difference in minutes (floored)
|
|
133
|
-
*
|
|
134
|
-
* @example
|
|
135
|
-
* ```typescript
|
|
136
|
-
* const diff = diffMinutes(Date.now(), Date.now() - 180000); // 3
|
|
137
|
-
* ```
|
|
138
|
-
*/
|
|
139
27
|
export function diffMinutes(date1: number | Date, date2: number | Date): number {
|
|
140
|
-
|
|
141
|
-
return Math.floor(msDiff / 60_000);
|
|
28
|
+
return Math.floor(diffMs(date1, date2) / 60000);
|
|
142
29
|
}
|
|
143
30
|
|
|
144
|
-
/**
|
|
145
|
-
* Calculate hours difference between two dates
|
|
146
|
-
* Optimized: Single Math.floor call
|
|
147
|
-
*
|
|
148
|
-
* @param date1 - First date
|
|
149
|
-
* @param date2 - Second date
|
|
150
|
-
* @returns Difference in hours (floored)
|
|
151
|
-
*
|
|
152
|
-
* @example
|
|
153
|
-
* ```typescript
|
|
154
|
-
* const diff = diffHours(Date.now(), Date.now() - 7200000); // 2
|
|
155
|
-
* ```
|
|
156
|
-
*/
|
|
157
31
|
export function diffHours(date1: number | Date, date2: number | Date): number {
|
|
158
|
-
|
|
159
|
-
return Math.floor(minsDiff / 60);
|
|
32
|
+
return Math.floor(diffMs(date1, date2) / 3600000);
|
|
160
33
|
}
|
|
161
34
|
|
|
162
|
-
/**
|
|
163
|
-
* Calculate days difference between two dates
|
|
164
|
-
* Optimized: Single Math.floor call
|
|
165
|
-
*
|
|
166
|
-
* @param date1 - First date
|
|
167
|
-
* @param date2 - Second date
|
|
168
|
-
* @returns Difference in days (floored)
|
|
169
|
-
*
|
|
170
|
-
* @example
|
|
171
|
-
* ```typescript
|
|
172
|
-
* const diff = diffDays(Date.now(), Date.now() - 172800000); // 2
|
|
173
|
-
* ```
|
|
174
|
-
*/
|
|
175
35
|
export function diffDays(date1: number | Date, date2: number | Date): number {
|
|
176
|
-
|
|
177
|
-
return Math.floor(hoursDiff / 24);
|
|
36
|
+
return Math.floor(diffMs(date1, date2) / 86400000);
|
|
178
37
|
}
|
|
179
38
|
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
* @param start - Start index (inclusive)
|
|
186
|
-
* @param end - End index (exclusive)
|
|
187
|
-
* @returns Sliced array
|
|
188
|
-
*
|
|
189
|
-
* @example
|
|
190
|
-
* ```typescript
|
|
191
|
-
* const items = [1, 2, 3, 4, 5];
|
|
192
|
-
* const sliced = safeSlice(items, 1, 3); // [2, 3]
|
|
193
|
-
* const sliced = safeSlice(items, -5, 10); // [1, 2, 3, 4, 5] (bounds checked)
|
|
194
|
-
* ```
|
|
195
|
-
*/
|
|
196
|
-
export function safeSlice<T>(array: T[], start: number, end?: number): T[] {
|
|
197
|
-
const len = array.length;
|
|
198
|
-
|
|
199
|
-
// Clamp start index
|
|
200
|
-
const safeStart = start < 0 ? 0 : (start >= len ? len : start);
|
|
201
|
-
|
|
202
|
-
// Clamp end index
|
|
203
|
-
const safeEnd = end === undefined
|
|
204
|
-
? len
|
|
205
|
-
: (end < 0 ? 0 : (end >= len ? len : end));
|
|
206
|
-
|
|
207
|
-
// Only slice if valid range
|
|
208
|
-
if (safeStart >= safeEnd) {
|
|
209
|
-
return [];
|
|
39
|
+
export function chunkArray<T>(array: readonly T[], chunkSize: number): T[][] {
|
|
40
|
+
if (chunkSize <= 0) return [];
|
|
41
|
+
const result: T[][] = [];
|
|
42
|
+
for (let i = 0; i < array.length; i += chunkSize) {
|
|
43
|
+
result.push(array.slice(i, i + chunkSize) as T[]);
|
|
210
44
|
}
|
|
45
|
+
return result;
|
|
46
|
+
}
|
|
211
47
|
|
|
212
|
-
|
|
48
|
+
export function safeSlice<T>(array: T[], start: number, end?: number): T[] {
|
|
49
|
+
if (start < 0) return array.slice(0, end);
|
|
50
|
+
if (start >= array.length) return [];
|
|
51
|
+
return array.slice(start, end);
|
|
213
52
|
}
|
|
214
53
|
|
|
215
|
-
/**
|
|
216
|
-
* Calculate fetch limit for pagination (pageLimit + 1)
|
|
217
|
-
* Optimized: Simple addition
|
|
218
|
-
*
|
|
219
|
-
* @param pageLimit - Requested page size
|
|
220
|
-
* @returns Fetch limit (pageLimit + 1 for hasMore detection)
|
|
221
|
-
*
|
|
222
|
-
* @example
|
|
223
|
-
* ```typescript
|
|
224
|
-
* const fetchLimit = getFetchLimit(10); // 11
|
|
225
|
-
* ```
|
|
226
|
-
*/
|
|
227
54
|
export function getFetchLimit(pageLimit: number): number {
|
|
228
55
|
return pageLimit + 1;
|
|
229
56
|
}
|
|
230
57
|
|
|
231
|
-
/**
|
|
232
|
-
* Calculate if hasMore based on items length and page limit
|
|
233
|
-
* Optimized: Direct comparison
|
|
234
|
-
*
|
|
235
|
-
* @param itemsLength - Total items fetched
|
|
236
|
-
* @param pageLimit - Requested page size
|
|
237
|
-
* @returns true if there are more items
|
|
238
|
-
*
|
|
239
|
-
* @example
|
|
240
|
-
* ```typescript
|
|
241
|
-
* const hasMore = hasMore(11, 10); // true
|
|
242
|
-
* const hasMore = hasMore(10, 10); // false
|
|
243
|
-
* ```
|
|
244
|
-
*/
|
|
245
58
|
export function hasMore(itemsLength: number, pageLimit: number): boolean {
|
|
246
59
|
return itemsLength > pageLimit;
|
|
247
60
|
}
|
|
248
61
|
|
|
249
|
-
/**
|
|
250
|
-
* Calculate result items count (min of itemsLength and pageLimit)
|
|
251
|
-
* Optimized: Single Math.min call
|
|
252
|
-
*
|
|
253
|
-
* @param itemsLength - Total items fetched
|
|
254
|
-
* @param pageLimit - Requested page size
|
|
255
|
-
* @returns Number of items to return
|
|
256
|
-
*
|
|
257
|
-
* @example
|
|
258
|
-
* ```typescript
|
|
259
|
-
* const count = getResultCount(11, 10); // 10
|
|
260
|
-
* const count = getResultCount(8, 10); // 8
|
|
261
|
-
* ```
|
|
262
|
-
*/
|
|
263
62
|
export function getResultCount(itemsLength: number, pageLimit: number): number {
|
|
264
63
|
return Math.min(itemsLength, pageLimit);
|
|
265
64
|
}
|
|
266
|
-
|
|
267
|
-
/**
|
|
268
|
-
* Chunk array into smaller arrays
|
|
269
|
-
* Optimized: Pre-allocated chunks when size is known
|
|
270
|
-
*
|
|
271
|
-
* @param array - Array to chunk
|
|
272
|
-
* @param chunkSize - Size of each chunk
|
|
273
|
-
* @returns Array of chunks
|
|
274
|
-
*
|
|
275
|
-
* @example
|
|
276
|
-
* ```typescript
|
|
277
|
-
* const chunks = chunkArray([1, 2, 3, 4, 5], 2); // [[1, 2], [3, 4], [5]]
|
|
278
|
-
* ```
|
|
279
|
-
*/
|
|
280
|
-
export function chunkArray<T>(array: readonly T[], chunkSize: number): T[][] {
|
|
281
|
-
if (chunkSize <= 0) {
|
|
282
|
-
throw new Error('chunkSize must be greater than 0');
|
|
283
|
-
}
|
|
284
|
-
|
|
285
|
-
const chunks: T[][] = [];
|
|
286
|
-
const len = array.length;
|
|
287
|
-
|
|
288
|
-
for (let i = 0; i < len; i += chunkSize) {
|
|
289
|
-
const end = Math.min(i + chunkSize, len);
|
|
290
|
-
chunks.push(array.slice(i, end) as T[]);
|
|
291
|
-
}
|
|
292
|
-
|
|
293
|
-
return chunks;
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
/**
|
|
297
|
-
* Sum array of numbers
|
|
298
|
-
* Optimized: Direct for-loop (faster than reduce)
|
|
299
|
-
*
|
|
300
|
-
* @param numbers - Array of numbers to sum
|
|
301
|
-
* @returns Sum of all numbers
|
|
302
|
-
*
|
|
303
|
-
* @example
|
|
304
|
-
* ```typescript
|
|
305
|
-
* const sum = sumArray([1, 2, 3, 4, 5]); // 15
|
|
306
|
-
* ```
|
|
307
|
-
*/
|
|
308
|
-
export function sumArray(numbers: number[]): number {
|
|
309
|
-
let sum = 0;
|
|
310
|
-
for (let i = 0; i < numbers.length; i++) {
|
|
311
|
-
const num = numbers[i];
|
|
312
|
-
if (num !== undefined && num !== null) {
|
|
313
|
-
sum += num;
|
|
314
|
-
}
|
|
315
|
-
}
|
|
316
|
-
return sum;
|
|
317
|
-
}
|
|
318
|
-
|
|
319
|
-
/**
|
|
320
|
-
* Average of array of numbers
|
|
321
|
-
* Optimized: Single-pass calculation
|
|
322
|
-
*
|
|
323
|
-
* @param numbers - Array of numbers
|
|
324
|
-
* @returns Average value
|
|
325
|
-
*
|
|
326
|
-
* @example
|
|
327
|
-
* ```typescript
|
|
328
|
-
* const avg = averageArray([1, 2, 3, 4, 5]); // 3
|
|
329
|
-
* ```
|
|
330
|
-
*/
|
|
331
|
-
export function averageArray(numbers: number[]): number {
|
|
332
|
-
if (numbers.length === 0) return 0;
|
|
333
|
-
return sumArray(numbers) / numbers.length;
|
|
334
|
-
}
|
|
335
|
-
|
|
336
|
-
/**
|
|
337
|
-
* Round to decimal places
|
|
338
|
-
* Optimized: Efficient rounding without string conversion
|
|
339
|
-
*
|
|
340
|
-
* @param value - Value to round
|
|
341
|
-
* @param decimals - Number of decimal places
|
|
342
|
-
* @returns Rounded value
|
|
343
|
-
*
|
|
344
|
-
* @example
|
|
345
|
-
* ```typescript
|
|
346
|
-
* const rounded = roundToDecimals(3.14159, 2); // 3.14
|
|
347
|
-
* ```
|
|
348
|
-
*/
|
|
349
|
-
export function roundToDecimals(value: number, decimals: number): number {
|
|
350
|
-
const multiplier = Math.pow(10, decimals);
|
|
351
|
-
return Math.round(value * multiplier) / multiplier;
|
|
352
|
-
}
|
|
@@ -37,8 +37,6 @@ export {
|
|
|
37
37
|
calculatePercentage,
|
|
38
38
|
calculateRemaining,
|
|
39
39
|
safeFloor,
|
|
40
|
-
safeCeil,
|
|
41
|
-
clamp,
|
|
42
40
|
diffMs,
|
|
43
41
|
diffMinutes,
|
|
44
42
|
diffHours,
|
|
@@ -48,7 +46,4 @@ export {
|
|
|
48
46
|
hasMore,
|
|
49
47
|
getResultCount,
|
|
50
48
|
chunkArray,
|
|
51
|
-
sumArray,
|
|
52
|
-
averageArray,
|
|
53
|
-
roundToDecimals,
|
|
54
49
|
} from './calculation.util';
|
|
@@ -1,20 +1,42 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Firebase Client State Manager
|
|
3
|
-
* Manages the state of Firebase initialization
|
|
4
|
-
*
|
|
5
|
-
* Single Responsibility: Only manages initialization state
|
|
6
|
-
* Uses generic ClientStateManager for shared functionality
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
1
|
import type { FirebaseApp } from 'firebase/app';
|
|
10
|
-
import { ClientStateManager } from '../base/ClientStateManager';
|
|
11
2
|
|
|
12
|
-
export class FirebaseClientState
|
|
3
|
+
export class FirebaseClientState {
|
|
4
|
+
private app: FirebaseApp | null = null;
|
|
5
|
+
private initializationError: string | null = null;
|
|
6
|
+
private isInitializedFlag = false;
|
|
7
|
+
|
|
13
8
|
getApp(): FirebaseApp | null {
|
|
14
|
-
return this.
|
|
9
|
+
return this.app;
|
|
15
10
|
}
|
|
16
11
|
|
|
17
12
|
setApp(app: FirebaseApp | null): void {
|
|
18
|
-
this.
|
|
13
|
+
this.app = app;
|
|
14
|
+
this.isInitializedFlag = app !== null;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
setInstance(app: FirebaseApp | null): void {
|
|
18
|
+
this.setApp(app);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
getInstance(): FirebaseApp | null {
|
|
22
|
+
return this.getApp();
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
isInitialized(): boolean {
|
|
26
|
+
return this.isInitializedFlag;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
getInitializationError(): string | null {
|
|
30
|
+
return this.initializationError;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
setInitializationError(error: string | null): void {
|
|
34
|
+
this.initializationError = error;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
reset(): void {
|
|
38
|
+
this.app = null;
|
|
39
|
+
this.initializationError = null;
|
|
40
|
+
this.isInitializedFlag = false;
|
|
19
41
|
}
|
|
20
42
|
}
|
|
@@ -1,82 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Client State Manager
|
|
3
|
-
*
|
|
4
|
-
* Generic state management for Firebase service clients.
|
|
5
|
-
* Provides centralized state tracking for initialization status, errors, and instances.
|
|
6
|
-
*
|
|
7
|
-
* @template TInstance - The service instance type (e.g., FirebaseApp, Firestore, Auth)
|
|
8
|
-
*/
|
|
9
|
-
|
|
10
|
-
interface ClientState<TInstance> {
|
|
11
|
-
instance: TInstance | null;
|
|
12
|
-
initializationError: string | null;
|
|
13
|
-
isInitialized: boolean;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* Generic client state manager
|
|
18
|
-
* Handles initialization state, error tracking, and instance management
|
|
19
|
-
*/
|
|
20
|
-
export class ClientStateManager<TInstance> {
|
|
21
|
-
private state: ClientState<TInstance>;
|
|
22
|
-
|
|
23
|
-
constructor() {
|
|
24
|
-
this.state = {
|
|
25
|
-
instance: null,
|
|
26
|
-
initializationError: null,
|
|
27
|
-
isInitialized: false,
|
|
28
|
-
};
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* Get the current instance
|
|
33
|
-
*/
|
|
34
|
-
getInstance(): TInstance | null {
|
|
35
|
-
return this.state.instance;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* Set the instance
|
|
40
|
-
*/
|
|
41
|
-
setInstance(instance: TInstance | null): void {
|
|
42
|
-
this.state.instance = instance;
|
|
43
|
-
this.state.isInitialized = instance !== null;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
* Check if the service is initialized
|
|
48
|
-
*/
|
|
49
|
-
isInitialized(): boolean {
|
|
50
|
-
return this.state.isInitialized;
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
/**
|
|
54
|
-
* Get the initialization error if any
|
|
55
|
-
*/
|
|
56
|
-
getInitializationError(): string | null {
|
|
57
|
-
return this.state.initializationError;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
/**
|
|
61
|
-
* Set the initialization error
|
|
62
|
-
*/
|
|
63
|
-
setInitializationError(error: string | null): void {
|
|
64
|
-
this.state.initializationError = error;
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
/**
|
|
68
|
-
* Reset the state
|
|
69
|
-
*/
|
|
70
|
-
reset(): void {
|
|
71
|
-
this.state.instance = null;
|
|
72
|
-
this.state.initializationError = null;
|
|
73
|
-
this.state.isInitialized = false;
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
/**
|
|
77
|
-
* Get the current state (read-only)
|
|
78
|
-
*/
|
|
79
|
-
getState(): Readonly<ClientState<TInstance>> {
|
|
80
|
-
return this.state;
|
|
81
|
-
}
|
|
82
|
-
}
|