@blac/core 2.0.0-rc-5 → 2.0.0-rc-6
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/README.md +42 -0
- package/dist/index.cjs.js +10 -1
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.d.ts +134 -8
- package/dist/index.es.js +446 -206
- package/dist/index.es.js.map +1 -1
- package/package.json +1 -1
- package/src/Blac.ts +48 -17
- package/src/BlacObserver.ts +33 -19
- package/src/Bloc.ts +82 -21
- package/src/BlocBase.ts +115 -47
- package/src/index.ts +7 -0
- package/src/testing.ts +239 -0
- package/src/types.ts +25 -2
- package/src/utils/uuid.ts +22 -0
package/src/types.ts
CHANGED
|
@@ -54,8 +54,31 @@ export type BlocConstructorParameters<B extends BlocBase<any>> =
|
|
|
54
54
|
BlocConstructor<B> extends new (...args: infer P) => B ? P : never;
|
|
55
55
|
|
|
56
56
|
/**
|
|
57
|
-
*
|
|
58
|
-
*
|
|
57
|
+
* Base interface for all Bloc events
|
|
58
|
+
* Events must be objects with a constructor to enable proper type matching
|
|
59
|
+
*/
|
|
60
|
+
export interface BlocEvent {
|
|
61
|
+
readonly type?: string;
|
|
62
|
+
readonly timestamp?: number;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Enhanced constraint for Bloc events - must be objects with proper constructor
|
|
67
|
+
*/
|
|
68
|
+
export type BlocEventConstraint = BlocEvent & object;
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Error boundary interface for Bloc error handling
|
|
72
|
+
*/
|
|
73
|
+
export interface BlocErrorBoundary<S, A extends BlocEventConstraint> {
|
|
74
|
+
onError: (error: Error, event: A, currentState: S, bloc: { name: string; id: string }) => void | Promise<void>;
|
|
75
|
+
shouldRethrow?: (error: Error, event: A) => boolean;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Function type for determining dependencies that trigger re-renders
|
|
80
|
+
* @template S The state type
|
|
81
|
+
* @returns Array of dependency arrays - if any dependency in any array changes, a re-render is triggered
|
|
59
82
|
*/
|
|
60
83
|
export type BlocHookDependencyArrayFn<S> = (
|
|
61
84
|
newState: S
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Cross-platform UUID v4 generator that works in all environments including React Native/Hermes
|
|
3
|
+
* Falls back to crypto.randomUUID() when available, otherwise uses Math.random()
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Generates a UUID v4 string compatible with all JavaScript environments
|
|
8
|
+
* @returns A UUID v4 string in the format xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx
|
|
9
|
+
*/
|
|
10
|
+
export function generateUUID(): string {
|
|
11
|
+
// Try to use crypto.randomUUID() if available (Node.js 16+, modern browsers)
|
|
12
|
+
if (typeof crypto !== 'undefined' && crypto.randomUUID) {
|
|
13
|
+
return crypto.randomUUID();
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
// Fallback implementation for React Native/Hermes and older environments
|
|
17
|
+
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
|
|
18
|
+
const r = Math.random() * 16 | 0;
|
|
19
|
+
const v = c === 'x' ? r : (r & 0x3 | 0x8);
|
|
20
|
+
return v.toString(16);
|
|
21
|
+
});
|
|
22
|
+
}
|