@blac/core 2.0.0 → 2.0.3
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 +137 -177
- package/dist/StateContainerRegistry.cjs.map +1 -0
- package/dist/StateContainerRegistry.js.map +1 -0
- package/dist/debug.cjs +27 -0
- package/dist/debug.cjs.map +1 -0
- package/dist/debug.js +19 -0
- package/dist/debug.js.map +1 -0
- package/dist/dependency-manager.cjs.map +1 -0
- package/dist/dependency-manager.js.map +1 -0
- package/dist/index.cjs +346 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +794 -0
- package/dist/index.d.ts +675 -386
- package/dist/index.js +310 -0
- package/dist/index.js.map +1 -0
- package/dist/management.cjs.map +1 -0
- package/dist/management.js.map +1 -0
- package/dist/plugins.cjs +4 -0
- package/dist/plugins.js +3 -0
- package/dist/tracking/dependency-manager.d.ts +37 -0
- package/dist/tracking/index.d.ts +12 -0
- package/dist/tracking/path-utils.d.ts +52 -0
- package/dist/tracking/tracked.d.ts +70 -0
- package/dist/tracking/tracking-proxy.d.ts +197 -0
- package/dist/tracking-proxy.cjs.map +1 -0
- package/dist/tracking-proxy.js.map +1 -0
- package/dist/tracking.cjs +19 -0
- package/dist/tracking.d.cts +9 -0
- package/dist/tracking.d.ts +9 -0
- package/dist/tracking.js +4 -0
- package/dist/types.cjs +14 -0
- package/dist/types.cjs.map +1 -0
- package/dist/types.d.cts +8 -0
- package/dist/types.d.ts +8 -0
- package/dist/types.js +13 -0
- package/dist/types.js.map +1 -0
- package/dist/watch-entry.cjs +111 -0
- package/dist/watch-entry.cjs.map +1 -0
- package/dist/watch-entry.js +107 -0
- package/dist/watch-entry.js.map +1 -0
- package/dist/watch.cjs.map +1 -0
- package/dist/watch.js.map +1 -0
- package/package.json +122 -19
- package/dist/index.cjs.js +0 -2
- package/dist/index.cjs.js.map +0 -1
- package/dist/index.es.js +0 -670
- package/dist/index.es.js.map +0 -1
- package/src/Blac.ts +0 -483
- package/src/BlacEvent.ts +0 -5
- package/src/BlacObservable.ts +0 -88
- package/src/BlacObserver.ts +0 -129
- package/src/BlacPlugin.ts +0 -13
- package/src/Bloc.ts +0 -109
- package/src/BlocBase.ts +0 -248
- package/src/Cubit.ts +0 -69
- package/src/addons/BlacAddon.ts +0 -17
- package/src/addons/Persist.ts +0 -105
- package/src/addons/index.ts +0 -1
- package/src/index.ts +0 -8
- package/src/types.ts +0 -63
package/src/addons/Persist.ts
DELETED
|
@@ -1,105 +0,0 @@
|
|
|
1
|
-
import { BlocBase, BlocInstanceId } from '../BlocBase';
|
|
2
|
-
import BlacAddon, { BlacAddonEmit, BlacAddonInit } from './BlacAddon';
|
|
3
|
-
|
|
4
|
-
type StorageType = 'localStorage' | 'sessionStorage';
|
|
5
|
-
|
|
6
|
-
function getStorage(type: StorageType): Storage {
|
|
7
|
-
switch (type) {
|
|
8
|
-
case 'localStorage':
|
|
9
|
-
return localStorage;
|
|
10
|
-
case 'sessionStorage':
|
|
11
|
-
return sessionStorage;
|
|
12
|
-
default:
|
|
13
|
-
return localStorage;
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* Persist addon
|
|
19
|
-
*
|
|
20
|
-
* @param options
|
|
21
|
-
* @returns BlacAddon
|
|
22
|
-
*/
|
|
23
|
-
export function Persist(
|
|
24
|
-
options: {
|
|
25
|
-
/**
|
|
26
|
-
* @default 'blac'
|
|
27
|
-
*/
|
|
28
|
-
keyPrefix?: string;
|
|
29
|
-
/**
|
|
30
|
-
* @default the bloc's id
|
|
31
|
-
*/
|
|
32
|
-
keyName?: string;
|
|
33
|
-
/**
|
|
34
|
-
* Used when the value is not found in storage
|
|
35
|
-
*/
|
|
36
|
-
defaultValue?: unknown;
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* @default 'localStorage'
|
|
40
|
-
* @see StorageType
|
|
41
|
-
*/
|
|
42
|
-
storageType?: StorageType;
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* @default false
|
|
46
|
-
*/
|
|
47
|
-
onError?: (e: unknown) => void;
|
|
48
|
-
} = {},
|
|
49
|
-
): BlacAddon {
|
|
50
|
-
const {
|
|
51
|
-
keyPrefix = 'blac',
|
|
52
|
-
keyName,
|
|
53
|
-
defaultValue,
|
|
54
|
-
storageType = 'localStorage',
|
|
55
|
-
} = options;
|
|
56
|
-
|
|
57
|
-
const fullKey = (id: string | BlocInstanceId) => `${keyPrefix}:${String(id)}`;
|
|
58
|
-
|
|
59
|
-
const getFromLocalStorage = (id: string | BlocInstanceId): unknown => {
|
|
60
|
-
try {
|
|
61
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access
|
|
62
|
-
const value = getStorage(storageType).getItem(fullKey(id));
|
|
63
|
-
if (typeof value !== 'string') {
|
|
64
|
-
return defaultValue;
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
const p = JSON.parse(value) as { v: unknown };
|
|
68
|
-
if (typeof p.v !== 'undefined') {
|
|
69
|
-
return p.v;
|
|
70
|
-
} else {
|
|
71
|
-
return defaultValue;
|
|
72
|
-
}
|
|
73
|
-
} catch (e) {
|
|
74
|
-
options.onError?.(e);
|
|
75
|
-
return defaultValue;
|
|
76
|
-
}
|
|
77
|
-
};
|
|
78
|
-
|
|
79
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
80
|
-
const onInit: BlacAddonInit = (e: BlocBase<any>) => {
|
|
81
|
-
const id = keyName ?? e._id;
|
|
82
|
-
|
|
83
|
-
const value = getFromLocalStorage(id);
|
|
84
|
-
e._pushState(value, null);
|
|
85
|
-
};
|
|
86
|
-
|
|
87
|
-
let currentCachedValue = '';
|
|
88
|
-
const onEmit: BlacAddonEmit = ({ newState, cubit }) => {
|
|
89
|
-
const id = keyName ?? cubit._id;
|
|
90
|
-
|
|
91
|
-
const newValue = JSON.stringify({ v: newState });
|
|
92
|
-
|
|
93
|
-
if (newValue !== currentCachedValue) {
|
|
94
|
-
// eslint-disable-next-line @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-member-access
|
|
95
|
-
getStorage(storageType).setItem(fullKey(id), newValue);
|
|
96
|
-
currentCachedValue = newValue;
|
|
97
|
-
}
|
|
98
|
-
};
|
|
99
|
-
|
|
100
|
-
return {
|
|
101
|
-
name: 'Persist',
|
|
102
|
-
onInit,
|
|
103
|
-
onEmit,
|
|
104
|
-
};
|
|
105
|
-
}
|
package/src/addons/index.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export * from './Persist';
|
package/src/index.ts
DELETED
package/src/types.ts
DELETED
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
|
-
import { Bloc } from './Bloc';
|
|
3
|
-
import { BlocBase } from './BlocBase';
|
|
4
|
-
import { Cubit } from './Cubit';
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Represents a constructor type for a Bloc that takes no parameters
|
|
8
|
-
* @template B - The type of the Bloc instance
|
|
9
|
-
*/
|
|
10
|
-
export type BlocClassNoParams<B> = new (args: never[]) => B;
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* Represents the abstract base types for Bloc and Cubit
|
|
14
|
-
*/
|
|
15
|
-
export type BlocBaseAbstract =
|
|
16
|
-
| typeof Bloc<any, any, any>
|
|
17
|
-
| typeof Cubit<any, any>;
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* Represents a constructor type for a Bloc that can take any parameters
|
|
21
|
-
* @template B - The type of the Bloc instance
|
|
22
|
-
*/
|
|
23
|
-
export type BlocConstructor<B> = new (...args: any) => B;
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* Extracts the state type from a BlocBase instance
|
|
27
|
-
* @template B - The BlocBase type to extract the state from
|
|
28
|
-
*/
|
|
29
|
-
export type ValueType<B extends BlocBase<any>> =
|
|
30
|
-
B extends BlocBase<infer U> ? U : never;
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* Extracts the state type from either a Bloc or Cubit
|
|
34
|
-
* @template T - The Bloc or Cubit type to extract the state from
|
|
35
|
-
*/
|
|
36
|
-
export type BlocState<T> = T extends BlocBase<infer S> ? S : never;
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* Extracts the props type from either a Bloc or Cubit
|
|
40
|
-
* @template T - The Bloc or Cubit type to extract the props from
|
|
41
|
-
*/
|
|
42
|
-
export type InferPropsFromGeneric<T> =
|
|
43
|
-
T extends Bloc<any, any, infer P>
|
|
44
|
-
? P
|
|
45
|
-
: T extends Cubit<any, infer P>
|
|
46
|
-
? P
|
|
47
|
-
: never;
|
|
48
|
-
|
|
49
|
-
/**
|
|
50
|
-
* Extracts the constructor parameters type from a BlocBase
|
|
51
|
-
* @template B - The BlocBase type to extract the constructor parameters from
|
|
52
|
-
*/
|
|
53
|
-
export type BlocConstructorParameters<B extends BlocBase<any>> =
|
|
54
|
-
BlocConstructor<B> extends new (...args: infer P) => B ? P : never;
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* Represents a function type for determining hook dependencies based on state changes
|
|
58
|
-
* @template B - The BlocGeneric type
|
|
59
|
-
*/
|
|
60
|
-
export type BlocHookDependencyArrayFn<S> = (
|
|
61
|
-
newState: S,
|
|
62
|
-
oldState: S,
|
|
63
|
-
) => unknown[][];
|