@blac/core 2.0.0 → 2.0.1

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.
@@ -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
- }
@@ -1 +0,0 @@
1
- export * from './Persist';
package/src/index.ts DELETED
@@ -1,8 +0,0 @@
1
- export * from './Blac';
2
- export * from './BlacObserver';
3
- export * from './BlocBase';
4
- export * from './Cubit';
5
- export * from './Bloc';
6
- export * from './types';
7
- export * from './BlacPlugin';
8
- export * from './addons';
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[][];