react-native-global-state-hooks 2.0.2 → 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.advance.md +77 -32
- package/README.md +23 -16
- package/lib/GlobalStore.d.ts +4 -4
- package/lib/GlobalStore.d.ts.map +1 -1
- package/lib/GlobalStoreTypes.d.ts +4 -10
- package/lib/GlobalStoreTypes.d.ts.map +1 -1
- package/package.json +1 -1
package/README.advance.md
CHANGED
|
@@ -1,61 +1,105 @@
|
|
|
1
|
-
|
|
2
|
-
## Customize persist storage
|
|
3
|
-
|
|
4
|
-
Let suppose you don't like **async-storage** or you also want to implement some kind of encrypt-process. You could easily extend the **GlobalStore** Class, and customize your persist store implementation.
|
|
5
|
-
|
|
6
1
|
## Creating hooks with reusable actions
|
|
7
2
|
|
|
8
|
-
Let's say you want to have a STATE with a specific set of actions that you could be reused. With this library is pretty easy to accomplish. Let's create **increase** and **decrease** actions to our
|
|
3
|
+
Let's say you want to have a STATE with a specific set of actions that you could be reused. With this library is pretty easy to accomplish. Let's create **increase** and **decrease** actions to our count-store. **useCountGlobal.ts**:
|
|
9
4
|
|
|
10
5
|
```JSX
|
|
11
|
-
import
|
|
12
|
-
|
|
6
|
+
import {
|
|
7
|
+
IActionCollectionConfig,
|
|
8
|
+
IActionCollectionResult,
|
|
9
|
+
StateSetter,
|
|
10
|
+
} from 'react-native-global-state-hooks/lib/GlobalStoreTypes';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* When using a custom api, the getHook and getHookDecoupled will not longer return directly the setter,
|
|
14
|
+
* intead they will return and api with the specific actions and mutations defined for the store
|
|
15
|
+
* Creating a configuration object for our api
|
|
16
|
+
*/
|
|
17
|
+
const countActionsApi: IActionCollectionConfig<number> = {
|
|
18
|
+
/* Decrease the value of the count */
|
|
19
|
+
decrease(decrease: number) {
|
|
20
|
+
/**
|
|
21
|
+
* We need to return the async function that is gonna take care of the state mutation or actions
|
|
22
|
+
*/
|
|
23
|
+
return async (setter: StateSetter<number>, state: number) => {
|
|
24
|
+
/**
|
|
25
|
+
* Next, we perfom whatever modification we want on top of the store
|
|
26
|
+
*/
|
|
27
|
+
return setter(state - decrease);
|
|
28
|
+
};
|
|
29
|
+
},
|
|
13
30
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
increase: (increase: number) => (setter: IGlobalState.StateSetter<number>, state: number) => Promise<void>,
|
|
17
|
-
}
|
|
31
|
+
//* Lets add a new action to increase the value of the count */
|
|
32
|
+
increase(increase: number) {
|
|
18
33
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
}
|
|
34
|
+
return async (setter: StateSetter<number>, state: number) => {
|
|
35
|
+
return setter(state + increase);
|
|
36
|
+
};
|
|
37
|
+
},
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Now our getHook and getHookDecoupled are gonna return our custom api instead of the StateSetter,
|
|
42
|
+
* This will allow us to have more control over our store since the mutations of the same are gonna be limitated
|
|
43
|
+
*/
|
|
44
|
+
const countStore = new GlobalStore(0, countActionsApi);
|
|
45
|
+
```
|
|
23
46
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
47
|
+
If we remove all the explanatory comments the code will look like this:
|
|
48
|
+
|
|
49
|
+
```TS
|
|
50
|
+
const countStore = new GlobalStore(0, {
|
|
51
|
+
decrease(decrease: number) {
|
|
52
|
+
return (setter: StateSetter<number>, state: number) =>
|
|
53
|
+
setter(state - decrease);
|
|
27
54
|
},
|
|
28
|
-
|
|
29
|
-
|
|
55
|
+
|
|
56
|
+
increase(increase: number) {
|
|
57
|
+
return (setter: StateSetter<number>, state: number) =>
|
|
58
|
+
setter(state + increase);
|
|
30
59
|
},
|
|
31
|
-
};
|
|
60
|
+
} as IActionCollectionConfig<number>);
|
|
61
|
+
```
|
|
32
62
|
|
|
33
|
-
|
|
63
|
+
Now lets get our new global hook with specific API
|
|
34
64
|
|
|
35
|
-
|
|
65
|
+
```TS
|
|
66
|
+
export interface ICountActions
|
|
67
|
+
extends IActionCollectionResult<number, IActionCollectionConfig<number>> {
|
|
68
|
+
decrease: (decrease: number) => Promise<number>;
|
|
69
|
+
increase: (increase: number) => Promise<number>;
|
|
70
|
+
}
|
|
36
71
|
|
|
72
|
+
/**
|
|
73
|
+
* The ICountActions interface is optional but it allow you yo get more accurate results for the typescript autocompletes and validations, ignore this if you are not using TS
|
|
74
|
+
*/
|
|
75
|
+
export const useCountGlobal = countStore.getHook<ICountActions>();
|
|
37
76
|
```
|
|
38
77
|
|
|
39
|
-
|
|
78
|
+
And that's it! the result of our useCountGlobal will return our actions instead of a simple setter... Let's see how that will look:
|
|
40
79
|
|
|
41
80
|
```JSX
|
|
42
81
|
import { useCountGlobal } from './useCountGlobal'
|
|
43
82
|
|
|
44
|
-
|
|
45
|
-
const [count,
|
|
83
|
+
const MyComponent: Reac.FC = () => {
|
|
84
|
+
const [count, countActions] = useCountGlobal();
|
|
46
85
|
|
|
47
|
-
|
|
48
|
-
const
|
|
86
|
+
// this functions are strongly typed
|
|
87
|
+
const increaseClick = () => countActions.increase(1);
|
|
88
|
+
const decreaseClick = () => countActions.decrease(1);
|
|
49
89
|
|
|
50
90
|
return (<>
|
|
51
|
-
<
|
|
52
|
-
<
|
|
53
|
-
<
|
|
91
|
+
<Text>{count}<Text/>
|
|
92
|
+
<Button onPress={increaseClick} title={'increase'} />
|
|
93
|
+
<Button onPress={decreaseClick} title={'decrease'} />
|
|
54
94
|
</>);
|
|
55
95
|
}
|
|
56
96
|
|
|
57
97
|
```
|
|
58
98
|
|
|
99
|
+
## Customize persist storage
|
|
100
|
+
|
|
101
|
+
Let suppose you don't like **async-storage** or you also want to implement some kind of encrypt-process. You could easily extend the **GlobalStore** Class, and customize your persist store implementation.
|
|
102
|
+
|
|
59
103
|
```JSX
|
|
60
104
|
import GlobalState from 'react-native-global-state-hooks';
|
|
61
105
|
import secureStorage from 'react-native-secure-storage';
|
|
@@ -70,6 +114,7 @@ export class SecureGlobalState<
|
|
|
70
114
|
|
|
71
115
|
protected asyncStorageGetItem = () => secureStorage.getItem(this.persistStoreAs as string, config);
|
|
72
116
|
|
|
117
|
+
/** value is a json string*/
|
|
73
118
|
protected asyncStorageSetItem = (value: string) => secureStorage.setItem(this.persistStoreAs as string, value, config);
|
|
74
119
|
|
|
75
120
|
}
|
package/README.md
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
# react-native-global-state-hooks
|
|
2
|
-
This is a package to easily handling global-state across your react-native-components **No-redux
|
|
2
|
+
This is a package to easily handling global-state across your react-native-components **No-redux**, **No-context**
|
|
3
3
|
|
|
4
|
-
This utility follows the same style as the default **useState** hook, with a subscription pattern and HOFs to create a more intuitive, atomic and easy way of sharing state between components
|
|
4
|
+
This utility follows the same style as the default **useState** hook, with a subscription pattern and **HOFs** to create a more intuitive, atomic and easy way of sharing state between components
|
|
5
5
|
|
|
6
6
|
**after version 1.0.4, we migrated to @react-native-async-storage/async-storage, because @react-native-async-storage/async-storage has been deprecated!!**
|
|
7
7
|
|
|
@@ -16,14 +16,14 @@ import GlobalStore from 'react-native-global-state-hooks';
|
|
|
16
16
|
// initialize your store with the default value of the same.
|
|
17
17
|
const countStore = new GlobalStore(0);
|
|
18
18
|
|
|
19
|
-
//
|
|
19
|
+
// you'll use this function the same way you'll use the **useState**
|
|
20
20
|
export const useCountGlobal = countStore.getHook();
|
|
21
21
|
|
|
22
22
|
// That's it, that's a global store... Strongly typed, with a global-hook that we could reuse cross all our react-components.
|
|
23
23
|
```
|
|
24
24
|
|
|
25
25
|
## Implementing your global hook into your components
|
|
26
|
-
Let's say we have two components **
|
|
26
|
+
Let's say we have two components **MyFirstComponent**, **MySecondComponent**, in order to use our global hook they will look just like:
|
|
27
27
|
|
|
28
28
|
```JSX
|
|
29
29
|
import { useCountGlobal } from './useCountGlobal'
|
|
@@ -32,14 +32,14 @@ const MyFirstComponent: React.FC = () => {
|
|
|
32
32
|
const [count, setter] = useCountGlobal();
|
|
33
33
|
const onClickAddOne = () => setter(count + 1);
|
|
34
34
|
|
|
35
|
-
return (<
|
|
35
|
+
return (<Button title={`count: ${count}`} onPress={onClickAddOne} />);
|
|
36
36
|
}
|
|
37
37
|
|
|
38
38
|
const MySecondComponent: React.FC = () => {
|
|
39
39
|
const [count, setter] = useCountGlobal();
|
|
40
40
|
const onClickAddTwo = () => setter(count + 2);
|
|
41
41
|
|
|
42
|
-
return (<
|
|
42
|
+
return (<Button title={`count: ${count}`} onPress={onClickAddOne} />);
|
|
43
43
|
}
|
|
44
44
|
|
|
45
45
|
// Just like that! You are now using a global state!!
|
|
@@ -53,11 +53,11 @@ const countStore = new GlobalStore(0);
|
|
|
53
53
|
|
|
54
54
|
## Persisted store
|
|
55
55
|
|
|
56
|
-
You could persist the state with **@react-native-async-storage** by just adding the **key
|
|
56
|
+
You could persist the state with **@react-native-async-storage** by just adding the **storage-key** to the constructor of your global-store, for example:
|
|
57
57
|
|
|
58
58
|
```JSX
|
|
59
59
|
// The FIRST parameter is the initial value of the state
|
|
60
|
-
// The Second parameter is an API to restrict access to the state, will talk about that later.
|
|
60
|
+
// The Second parameter is an API to restrict access to the state, will talk about that later on [README]:./README.advance.md
|
|
61
61
|
// The Third parameter is the key that will be used on the async-storage
|
|
62
62
|
const countStore = new GlobalStore(0, null, 'GLOBAL_COUNT');
|
|
63
63
|
```
|
|
@@ -77,7 +77,7 @@ const MyComponent: React.FC = () => {
|
|
|
77
77
|
*/
|
|
78
78
|
const countLabel = isCountReady ? `count: ${count}` : 'Loading async storage...';
|
|
79
79
|
|
|
80
|
-
return (<
|
|
80
|
+
return (<Button title={countLabel} onPress={onClickAddOne} />);
|
|
81
81
|
}
|
|
82
82
|
```
|
|
83
83
|
|
|
@@ -109,7 +109,7 @@ import { useCountGlobal, setCountGlobalValue } from './useCountGlobal'
|
|
|
109
109
|
const CountDisplayerComponent: React.FC = () => {
|
|
110
110
|
const [count] = useCountGlobal();
|
|
111
111
|
|
|
112
|
-
return (<
|
|
112
|
+
return (<Text>{count}<Text/>);
|
|
113
113
|
}
|
|
114
114
|
|
|
115
115
|
// Stage2 does not need to be updated once the global count changes
|
|
@@ -118,17 +118,24 @@ const CountManagerComponent: React.FC = () => {
|
|
|
118
118
|
const decreaseClick = () => setCountGlobalValue(count => count - 1);
|
|
119
119
|
|
|
120
120
|
return (<>
|
|
121
|
-
<
|
|
122
|
-
<
|
|
121
|
+
<Button onPress={increaseClick} title={'increase'} />
|
|
122
|
+
<Button onPress={decreaseClick} title={'decrease'}/>
|
|
123
123
|
</>);
|
|
124
124
|
}
|
|
125
125
|
```
|
|
126
126
|
|
|
127
|
+
## Advance Config
|
|
128
|
+
Here you can see more information how to create more complex services for your global stores.
|
|
129
|
+
[README]:./README.advance.md
|
|
130
|
+
|
|
127
131
|
## Advantages:
|
|
128
132
|
1. Using REACT's simplest and default way to deal with the state.
|
|
129
|
-
|
|
133
|
+
2. Adding partial state designations (This is not on useState default functionality)
|
|
134
|
+
3. Added availability to create actions and decoupled access to the states, no more connects, and dispatches, just call your actions as a normal service of whatever other libraries.
|
|
135
|
+
4. This library is already taking care of avoiding re-renders if the new state does not have changes
|
|
136
|
+
5. This tool also take care for you to avoid **async-storage*** data to lose the data types that you stored. For example when you are using datetimes
|
|
130
137
|
|
|
131
|
-
|
|
138
|
+
# Finallly notes:
|
|
139
|
+
Are concern about performance? this library is for you, instead of handling huge complex stores with options like redux, or by passing the setState to a context Provider (because of the limitations that the context has)... You should just use this library, we are using the more atomic and 'native' way that REACT gives to handle the state, and that is the hook **useState**...
|
|
132
140
|
|
|
133
|
-
|
|
134
|
-
[README]:./README.advance.md
|
|
141
|
+
This utility is just including the implementation of the use state into a subscriber pattern, to enable you to create hooks that will be subscribed to specific store changes, does how we'll be creating a global state hook.
|
package/lib/GlobalStore.d.ts
CHANGED
|
@@ -18,10 +18,10 @@ export declare class GlobalStore<IState, IPersist extends string | null = null,
|
|
|
18
18
|
protected asyncStorageSetItem(valueToStore: string): Promise<void>;
|
|
19
19
|
protected setAsyncStoreItem(): Promise<void>;
|
|
20
20
|
protected getStateCopy: () => IState;
|
|
21
|
-
getHook: <IApi extends IGlobalStore.
|
|
22
|
-
getHookDecoupled: <IApi extends IGlobalStore.
|
|
21
|
+
getHook: <IApi extends IGlobalStore.IActionCollectionResult<IState, IActions> | null = IActions extends null ? null : IGlobalStore.IActionCollectionResult<IState, IActions>>() => () => [IState, IGlobalStore.IHookResult<IState, IActions, IApi>, IsPersist extends true ? boolean : null];
|
|
22
|
+
getHookDecoupled: <IApi extends IGlobalStore.IActionCollectionResult<IState, IActions> | null = IActions extends null ? null : IGlobalStore.IActionCollectionResult<IState, IActions>>() => [() => IPersist extends string ? Promise<IState> : IState, IGlobalStore.IHookResult<IState, IActions, IApi>];
|
|
23
23
|
private _stateOrchestrator;
|
|
24
|
-
protected get stateOrchestrator(): IGlobalStore.StateSetter<IState> | IGlobalStore.
|
|
24
|
+
protected get stateOrchestrator(): IGlobalStore.StateSetter<IState> | IGlobalStore.IActionCollectionResult<IState, IActions>;
|
|
25
25
|
/**
|
|
26
26
|
** [subscriber-update-callback, hook]
|
|
27
27
|
*/
|
|
@@ -34,7 +34,7 @@ export declare class GlobalStore<IState, IPersist extends string | null = null,
|
|
|
34
34
|
* React native cannot use unstable_batchedUpdates, it does not have any effect
|
|
35
35
|
*/
|
|
36
36
|
static ExecutePendingBatches: import("lodash").DebouncedFunc<() => void>;
|
|
37
|
-
protected getActions: <IApi extends IGlobalStore.
|
|
37
|
+
protected getActions: <IApi extends IGlobalStore.IActionCollectionResult<IState, IGlobalStore.IActionCollectionConfig<IState>>>() => IApi;
|
|
38
38
|
}
|
|
39
39
|
export default GlobalStore;
|
|
40
40
|
//# sourceMappingURL=GlobalStore.d.ts.map
|
package/lib/GlobalStore.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"GlobalStore.d.ts","sourceRoot":"","sources":["../src/GlobalStore.ts"],"names":[],"mappings":";AAKA,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BAAmB,CAAC,YAAY,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;cAIxD,iBAAiB,IAAI,OAAO,CAAC,IAAI,CAAC;IAUlD,SAAS,CAAC,YAAY,QAAO,MAAM,CAAyC;IAErE,OAAO,sRA2BZ;IAEK,gBAAgB,mLAGV,QAAQ,SAAS,MAAM,GAAG,QAAQ,MAAM,CAAC,GAAG,MAAM,oDAO7D;IAEF,OAAO,CAAC,kBAAkB,CAA0G;IAEpI,SAAS,KAAK,iBAAiB,IAAI,YAAY,CAAC,WAAW,CAAC,MAAM,CAAC,GAAG,YAAY,CAAC,uBAAuB,CAAC,MAAM,EAAE,QAAQ,CAAC,CAY3H;IAED;;MAEE;IACF,SAAS,CAAC,MAAM,CAAC,cAAc,EAAE,CAAC,MAAM,IAAI,EAAE,MAAM,CAAC,EAAE,CAAM;IAE7D,SAAS,CAAC,YAAY,6BAA8B,MAAM,KAAK,MAAM,aAAa,MAAM,IAAI,UAqB1F;IAEF,SAAS,CAAC,iBAAiB,6BAAoC,MAAM,KAAK,MAAM,MAC9E,QAAQ,MAAM,CAAC,CAAmF;IAEpG,SAAS,CAAC,+BAA+B,6BAAoC,MAAM,KAAK,MAAM,MAAI,QAAQ,MAAM,CAAC,CAK/G;IAEF,MAAM,CAAC,8BAA8B,EAAE,CAAC,MAAM,IAAI,CAAC,EAAE,CAAM;IAE3D;;MAEE;IACF,MAAM,CAAC,qBAAqB,6CAQtB;IAEN,SAAS,CAAC,UAAU,sHA0BlB;CAEH;AAED,eAAe,WAAW,CAAC"}
|
|
@@ -14,21 +14,15 @@ export interface IActionCollectionConfig<IState> {
|
|
|
14
14
|
[key: string]: IActionConfig<IState>;
|
|
15
15
|
}
|
|
16
16
|
/**
|
|
17
|
-
* This should be the API format of the hook (if you passed an API as a parameter)
|
|
18
|
-
*/
|
|
19
|
-
export interface IActionCollectionResult {
|
|
20
|
-
[key: string]: (...params: any[]) => Promise<unknown>;
|
|
21
|
-
}
|
|
22
|
-
/**
|
|
23
17
|
* This is the API result of the hook (if you passed an API as a parameter)
|
|
24
18
|
*/
|
|
25
|
-
export declare type
|
|
19
|
+
export declare type IActionCollectionResult<IState, IActions extends IActionCollectionConfig<IState> | null> = {
|
|
26
20
|
[key in keyof IActions]: (...params: any[]) => unknown;
|
|
27
21
|
};
|
|
28
22
|
/**
|
|
29
23
|
* Hook result, if you passed an API as a parameter it will be returned in the second position of the hook invoke.
|
|
30
24
|
*/
|
|
31
|
-
export declare type IHookResult<IState, IActions extends IActionCollectionConfig<IState> | null = null, IApi extends
|
|
25
|
+
export declare type IHookResult<IState, IActions extends IActionCollectionConfig<IState> | null = null, IApi extends IActionCollectionResult<IState, IActions> | null = IActions extends null ? null : IActionCollectionResult<IState, IActions>> = IApi extends null ? StateSetter<IState> : IActions extends IActionCollectionConfig<IState> ? IApi extends IActionCollectionResult<IState, IActions> ? IApi : StateSetter<IState> : StateSetter<IState>;
|
|
32
26
|
/**
|
|
33
27
|
* This is a class to create global-store objects
|
|
34
28
|
* @template IState
|
|
@@ -49,7 +43,7 @@ export interface IGlobalState<IState, IPersist extends string | null = null, IsP
|
|
|
49
43
|
* Returns a global hook that will share information across components by subscribing them to a specific store.
|
|
50
44
|
* @return [currentState, GlobalState.IHookResult<IState, IActions, IApi>, initialStatePersistStorage | null, isUpdatedPersistStorage | null]
|
|
51
45
|
*/
|
|
52
|
-
getHook: <IApi extends IActions extends
|
|
46
|
+
getHook: <IApi extends IActions extends IActionCollectionResult<IState, IActions> ? IActionCollectionResult<IState, IActions> : null>() => () => [
|
|
53
47
|
IState,
|
|
54
48
|
IHookResult<IState, IActions, IApi>,
|
|
55
49
|
IsPersist extends true ? boolean : null
|
|
@@ -59,7 +53,7 @@ export interface IGlobalState<IState, IPersist extends string | null = null, IsP
|
|
|
59
53
|
* THIS IS NOT A REACT-HOOK, so you could use it everywhere example other hooks, and services.
|
|
60
54
|
* @return [currentState, GlobalState.IHookResult<IState, IActions, IApi>]
|
|
61
55
|
*/
|
|
62
|
-
getHookDecoupled: <IApi extends IActions extends
|
|
56
|
+
getHookDecoupled: <IApi extends IActions extends IActionCollectionResult<IState, IActions> ? IActionCollectionResult<IState, IActions> : null>() => [
|
|
63
57
|
() => IPersist extends string ? Promise<IState> : IState,
|
|
64
58
|
IHookResult<IState, IActions, IApi>
|
|
65
59
|
];
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"GlobalStoreTypes.d.ts","sourceRoot":"","sources":["../src/GlobalStoreTypes.ts"],"names":[],"mappings":"AAAA;;;EAGE;AACF,oBAAY,WAAW,CAAC,MAAM,IAAI,CAChC,MAAM,EAAE,MAAM,GAAG,CAAC,CAAC,KAAK,EAAE,MAAM,KAAK,MAAM,CAAC,KACzC,OAAO,CAAC,MAAM,CAAC,CAAC;AAErB;;EAEE;AACF,oBAAY,aAAa,CAAC,MAAM,IAAI,CAElC,GAAG,MAAM,EAAE,GAAG,EAAE,KACb,CAAC,MAAM,EAAE,WAAW,CAAC,MAAM,CAAC,EAAE,YAAY,EAAE,MAAM,KAAK,OAAO,CAAC,OAAO,CAAC,CAAC;AAE7E;;EAEE;AACF,MAAM,WAAW,uBAAuB,CAAC,MAAM;IAC7C,CAAC,GAAG,EAAE,MAAM,GAAG,aAAa,CAAC,MAAM,CAAC,CAAC;CACtC;AAED;;EAEE;AACF,
|
|
1
|
+
{"version":3,"file":"GlobalStoreTypes.d.ts","sourceRoot":"","sources":["../src/GlobalStoreTypes.ts"],"names":[],"mappings":"AAAA;;;EAGE;AACF,oBAAY,WAAW,CAAC,MAAM,IAAI,CAChC,MAAM,EAAE,MAAM,GAAG,CAAC,CAAC,KAAK,EAAE,MAAM,KAAK,MAAM,CAAC,KACzC,OAAO,CAAC,MAAM,CAAC,CAAC;AAErB;;EAEE;AACF,oBAAY,aAAa,CAAC,MAAM,IAAI,CAElC,GAAG,MAAM,EAAE,GAAG,EAAE,KACb,CAAC,MAAM,EAAE,WAAW,CAAC,MAAM,CAAC,EAAE,YAAY,EAAE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