react-state-bucket 1.0.4 → 1.0.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 +175 -119
- package/dist/index.d.ts +16 -6
- package/dist/index.js +72 -78
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,181 +1,237 @@
|
|
|
1
|
-
|
|
1
|
+
<p align="center">
|
|
2
|
+
<a href="https://mui.com/core/" rel="noopener" target="_blank"><img width="120" src="https://api.mypiebd.com/uploads/2024/11/26/images/5c31a5fc731a89c82fd0e846dd69a99378243.png" alt="Material UI logo"></a>
|
|
3
|
+
</p>
|
|
2
4
|
|
|
3
|
-
|
|
5
|
+
<h1 align="center">React State Bucket</h1>
|
|
4
6
|
|
|
5
|
-
## Features
|
|
6
7
|
|
|
7
|
-
-
|
|
8
|
-
- Supports three storage options: memory, session storage, and URL.
|
|
9
|
-
- Fully TypeScript-supported API.
|
|
10
|
-
- React hook-based implementation for seamless integration.
|
|
11
|
-
- Ideal for both small and large-scale React applications.
|
|
8
|
+
`react-state-bucket` is a lightweight and powerful package designed to manage states globally in React applications. It provides CRUD operations for your state data with ease, enabling developers to handle complex state management scenarios without the need for heavy libraries.
|
|
12
9
|
|
|
13
|
-
|
|
10
|
+
This package supports multiple storage options, including:
|
|
14
11
|
|
|
15
|
-
|
|
12
|
+
- Memory (default)
|
|
13
|
+
- Session Storage
|
|
14
|
+
- Local Storage
|
|
15
|
+
- URL Query Parameters
|
|
16
|
+
|
|
17
|
+
## Installation
|
|
16
18
|
|
|
17
19
|
```bash
|
|
18
20
|
npm install react-state-bucket
|
|
19
21
|
```
|
|
20
22
|
|
|
21
|
-
|
|
23
|
+
## Features
|
|
22
24
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
25
|
+
- **Global State Management**: Easily manage state across your entire React application.
|
|
26
|
+
- **CRUD Operations**: Create, Read, Update, and Delete state values effortlessly.
|
|
27
|
+
- **Multiple Storage Options**: Choose where to store your data ("memory", "session", "local", or "url").
|
|
28
|
+
- **Reactivity**: Automatically update components when the state changes.
|
|
29
|
+
- **Custom Hooks**: Seamlessly integrate with React’s functional components.
|
|
26
30
|
|
|
27
31
|
## Usage
|
|
28
32
|
|
|
29
|
-
###
|
|
30
|
-
|
|
31
|
-
The package is designed for use with React's client-side components. Start by importing the required functionality:
|
|
33
|
+
### Basic Example
|
|
32
34
|
|
|
33
|
-
```
|
|
35
|
+
```jsx
|
|
34
36
|
"use client";
|
|
35
|
-
import { createBucket } from "react-state-bucket";
|
|
36
|
-
```
|
|
37
37
|
|
|
38
|
-
|
|
38
|
+
import React from "react";
|
|
39
|
+
import createBucket from "react-state-bucket";
|
|
39
40
|
|
|
40
|
-
|
|
41
|
+
// Create a bucket with initial state
|
|
42
|
+
const useGlobalState = createBucket({ count: 0, user: "Guest" });
|
|
41
43
|
|
|
42
|
-
|
|
43
|
-
const
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
}
|
|
44
|
+
function App() {
|
|
45
|
+
const globalState = useGlobalState();
|
|
46
|
+
|
|
47
|
+
return (
|
|
48
|
+
<div>
|
|
49
|
+
<h1>Global State Management</h1>
|
|
50
|
+
<p>Count: {globalState.get("count")}</p>
|
|
51
|
+
<button onClick={() => globalState.set("count", globalState.get("count") + 1)}>Increment</button>
|
|
52
|
+
<button onClick={() => globalState.delete("count")}>Reset Count</button>
|
|
53
|
+
<pre>{JSON.stringify(globalState.getState(), null, 2)}</pre>
|
|
54
|
+
</div>
|
|
55
|
+
);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export default App;
|
|
49
59
|
```
|
|
50
60
|
|
|
51
|
-
### Using
|
|
61
|
+
### Using Multiple Buckets
|
|
62
|
+
|
|
63
|
+
```jsx
|
|
64
|
+
const useUserBucket = createBucket({ name: "", age: 0 });
|
|
65
|
+
const useSettingsBucket = createBucket({ theme: "light", notifications: true });
|
|
66
|
+
|
|
67
|
+
function Profile() {
|
|
68
|
+
const userBucket = useUserBucket();
|
|
69
|
+
const settingsBucket = useSettingsBucket();
|
|
70
|
+
|
|
71
|
+
return (
|
|
72
|
+
<div>
|
|
73
|
+
<h1>User Profile</h1>
|
|
74
|
+
<button onClick={() => userBucket.set("name", "John Doe")}>Set Name</button>
|
|
75
|
+
<button onClick={() => settingsBucket.set("theme", "dark")}>Change Theme</button>
|
|
76
|
+
<pre>User State: {JSON.stringify(userBucket.getState(), null, 2)}</pre>
|
|
77
|
+
<p>Current Theme: {settingsBucket.get("theme")}</p>
|
|
78
|
+
</div>
|
|
79
|
+
);
|
|
80
|
+
}
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
### Storage Options Example
|
|
52
84
|
|
|
53
|
-
|
|
85
|
+
```jsx
|
|
86
|
+
const usePersistentBucket = createBucket(
|
|
87
|
+
{ token: "", language: "en" },
|
|
88
|
+
{ store: "local" }
|
|
89
|
+
);
|
|
54
90
|
|
|
55
|
-
|
|
91
|
+
function PersistentExample() {
|
|
92
|
+
const persistentBucket = usePersistentBucket();
|
|
93
|
+
|
|
94
|
+
return (
|
|
95
|
+
<div>
|
|
96
|
+
<h1>Persistent Bucket</h1>
|
|
97
|
+
<button onClick={() => persistentBucket.set("token", "abc123")}>Set Token</button>
|
|
98
|
+
<p>Token: {persistentBucket.get("token")}</p>
|
|
99
|
+
</div>
|
|
100
|
+
);
|
|
101
|
+
}
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
### Reusing State Across Components
|
|
105
|
+
|
|
106
|
+
```jsx
|
|
107
|
+
"use client";
|
|
56
108
|
|
|
57
|
-
```javascript
|
|
58
109
|
import React from "react";
|
|
110
|
+
import createBucket from "react-state-bucket";
|
|
111
|
+
|
|
112
|
+
// Create a global bucket
|
|
113
|
+
const useGlobalState = createBucket({ count: 0, user: "Guest" });
|
|
59
114
|
|
|
60
115
|
function Counter() {
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
116
|
+
const globalState = useGlobalState(); // Access bucket functions and trigger re-render on state updates
|
|
117
|
+
|
|
118
|
+
return (
|
|
119
|
+
<div>
|
|
120
|
+
<h2>Counter Component</h2>
|
|
121
|
+
<p>Count: {globalState.get("count")}</p>
|
|
122
|
+
<button onClick={() => globalState.set("count", globalState.get("count") + 1)}>
|
|
123
|
+
Increment Count
|
|
124
|
+
</button>
|
|
125
|
+
</div>
|
|
126
|
+
);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
function UserDisplay() {
|
|
130
|
+
const globalState = useGlobalState(); // Reuse the same global bucket for reactivity
|
|
131
|
+
|
|
132
|
+
return (
|
|
133
|
+
<div>
|
|
134
|
+
<h2>User Component</h2>
|
|
135
|
+
<p>Current User: {globalState.get("user")}</p>
|
|
136
|
+
<button onClick={() => globalState.set("user", "John Doe")}>
|
|
137
|
+
Set User to John Doe
|
|
138
|
+
</button>
|
|
139
|
+
</div>
|
|
140
|
+
);
|
|
72
141
|
}
|
|
73
142
|
|
|
74
143
|
function App() {
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
144
|
+
|
|
145
|
+
return (
|
|
146
|
+
<div>
|
|
147
|
+
<h1>Global State Example</h1>
|
|
148
|
+
<Counter />
|
|
149
|
+
<UserDisplay />
|
|
150
|
+
<pre>Global State: {JSON.stringify(useGlobalState().getState(), null, 2)}</pre>
|
|
151
|
+
</div>
|
|
152
|
+
);
|
|
80
153
|
}
|
|
81
154
|
|
|
82
155
|
export default App;
|
|
83
156
|
```
|
|
84
157
|
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
When you call the hook returned by `createBucket`, you gain access to a set of utility methods:
|
|
158
|
+
## Direct Usage of Functions
|
|
88
159
|
|
|
89
|
-
|
|
90
|
-
| ----------------- | ---------------------------------------------------------- |
|
|
91
|
-
| `set(key, value)` | Sets the value of a specific key in the state. |
|
|
92
|
-
| `get(key)` | Gets the value of a specific key from the state. |
|
|
93
|
-
| `delete(key)` | Deletes a specific key from the state. |
|
|
94
|
-
| `clear()` | Clears all keys from the state. |
|
|
95
|
-
| `getState()` | Retrieves the entire state object. |
|
|
96
|
-
| `setState(state)` | Updates the state with a partial object. |
|
|
97
|
-
| `isChange(key)` | Checks if a specific key has changed since the last clear. |
|
|
98
|
-
| `getChanges()` | Retrieves an object representing all changed keys. |
|
|
99
|
-
| `clearChanges()` | Clears the record of changes. |
|
|
160
|
+
The `createBucket` function provides direct access to the state management methods like `get`, `set`, `delete`, and others. For components that require re-rendering when the state changes, call the custom hook returned by `createBucket` within the component.
|
|
100
161
|
|
|
101
|
-
###
|
|
162
|
+
### Example
|
|
102
163
|
|
|
103
|
-
|
|
164
|
+
```jsx
|
|
165
|
+
const useGlobalState = createBucket({ count: 0, user: "Guest" });
|
|
104
166
|
|
|
105
|
-
|
|
106
|
-
2. **Session:** Persists the state in `sessionStorage` across page reloads.
|
|
107
|
-
3. **URL:** Stores the state in the URL's query parameters, enabling sharable states.
|
|
108
|
-
|
|
109
|
-
#### Example with URL Storage:
|
|
167
|
+
function Counter() {
|
|
110
168
|
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
169
|
+
return (
|
|
170
|
+
<div>
|
|
171
|
+
<p>Count: {useGlobalState.get("count")}</p>
|
|
172
|
+
<button onClick={() => useGlobalState.set("count", useGlobalState.get("count") + 1)}>Increment</button>
|
|
173
|
+
</div>
|
|
174
|
+
);
|
|
175
|
+
}
|
|
117
176
|
|
|
118
|
-
function
|
|
119
|
-
|
|
177
|
+
function App() {
|
|
178
|
+
useGlobalState(); // Will automatically re-render when state updates
|
|
120
179
|
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
180
|
+
return (
|
|
181
|
+
<div>
|
|
182
|
+
<Counter />
|
|
183
|
+
</div>
|
|
184
|
+
);
|
|
126
185
|
}
|
|
186
|
+
|
|
187
|
+
export default App;
|
|
127
188
|
```
|
|
128
189
|
|
|
129
|
-
## Real-World Example
|
|
130
190
|
|
|
131
|
-
### Managing a Multi-Step Form
|
|
132
191
|
|
|
133
|
-
|
|
134
|
-
const useFormState = createBucket({
|
|
135
|
-
step: 1,
|
|
136
|
-
formData: {},
|
|
137
|
-
});
|
|
192
|
+
## API Reference
|
|
138
193
|
|
|
139
|
-
|
|
140
|
-
const state = useFormState();
|
|
194
|
+
### `createBucket(initial: object, option?: BucketOptions)`
|
|
141
195
|
|
|
142
|
-
|
|
143
|
-
const prevStep = () => state.set("step", state.get("step") - 1);
|
|
196
|
+
Creates a new bucket for managing the global state.
|
|
144
197
|
|
|
145
|
-
|
|
146
|
-
<div>
|
|
147
|
-
<h2>Step {state.get("step")}</h2>
|
|
148
|
-
<button onClick={prevStep} disabled={state.get("step") === 1}>Back</button>
|
|
149
|
-
<button onClick={nextStep}>Next</button>
|
|
150
|
-
</div>
|
|
151
|
-
);
|
|
152
|
-
}
|
|
153
|
-
```
|
|
198
|
+
#### Parameters
|
|
154
199
|
|
|
155
|
-
|
|
200
|
+
| Name | Type | Description |
|
|
201
|
+
| --------- | --------- | ----------------------------------------- |
|
|
202
|
+
| `initial` | `object` | Initial state values. |
|
|
203
|
+
| `option` | `object?` | Optional configuration (default: memory). |
|
|
156
204
|
|
|
157
|
-
|
|
158
|
-
- Use `session` storage for states that should persist across page reloads but not sessions.
|
|
159
|
-
- Use `url` storage for sharable states, like filters or query parameters.
|
|
205
|
+
#### `BucketOptions`
|
|
160
206
|
|
|
161
|
-
|
|
207
|
+
`BucketOptions` allows you to configure how and where the state is stored. It includes:
|
|
162
208
|
|
|
163
|
-
|
|
164
|
-
|
|
209
|
+
| Property | Type | Description |
|
|
210
|
+
| -------- | ------------------------------------- | ---------------------------------------------- |
|
|
211
|
+
| `store` | `'memory', 'session', 'local', 'url'` | Specifies the storage mechanism for the state. |
|
|
165
212
|
|
|
166
|
-
###
|
|
167
|
-
An error will be thrown, ensuring state integrity.
|
|
213
|
+
### Returned Functions
|
|
168
214
|
|
|
169
|
-
|
|
170
|
-
Yes, `react-state-bucket` fully supports TypeScript with type-safe APIs.
|
|
215
|
+
#### State Management
|
|
171
216
|
|
|
172
|
-
|
|
217
|
+
| Function | Description |
|
|
218
|
+
| ----------------- | ----------------------------------------------------- |
|
|
219
|
+
| `set(key, value)` | Sets the value of a specific key in the state. |
|
|
220
|
+
| `get(key)` | Retrieves the value of a specific key from the state. |
|
|
221
|
+
| `delete(key)` | Removes a key-value pair from the state. |
|
|
222
|
+
| `clear()` | Clears all state values. |
|
|
223
|
+
| `getState()` | Returns the entire state as an object. |
|
|
224
|
+
| `setState(state)` | Updates the state with a partial object. |
|
|
173
225
|
|
|
174
|
-
|
|
175
|
-
[https://github.com/devnax/react-state-bucket.git](https://github.com/devnax/react-state-bucket.git)
|
|
226
|
+
#### Change Detection
|
|
176
227
|
|
|
177
|
-
|
|
228
|
+
| Function | Description |
|
|
229
|
+
| ---------------- | ------------------------------------------- |
|
|
230
|
+
| `isChange(key)` | Checks if a specific key has been modified. |
|
|
231
|
+
| `getChanges()` | Returns an array of keys that have changed. |
|
|
232
|
+
| `clearChanges()` | Resets the change detection for all keys. |
|
|
178
233
|
|
|
179
234
|
## License
|
|
180
235
|
|
|
181
|
-
MIT
|
|
236
|
+
This project is licensed under the [MIT License](https://opensource.org/licenses/MIT).
|
|
237
|
+
|
package/dist/index.d.ts
CHANGED
|
@@ -1,17 +1,27 @@
|
|
|
1
|
-
type
|
|
2
|
-
store?: "memory" | "session" | "url";
|
|
1
|
+
export type BucketOptions = {
|
|
2
|
+
store?: "memory" | "session" | "local" | "url";
|
|
3
3
|
};
|
|
4
4
|
export declare const createBucket: <IT extends {
|
|
5
5
|
[key: string]: any;
|
|
6
|
-
}>(initial: IT, option?:
|
|
6
|
+
}>(initial: IT, option?: BucketOptions) => {
|
|
7
|
+
(): {
|
|
8
|
+
set: <T extends keyof IT>(key: T, value: IT[T]) => void;
|
|
9
|
+
get: <T extends keyof IT>(key: T) => IT[T];
|
|
10
|
+
delete: <T extends keyof IT>(key: T) => void;
|
|
11
|
+
clear: () => void;
|
|
12
|
+
getState: () => IT;
|
|
13
|
+
setState: (state: Partial<IT>) => void;
|
|
14
|
+
isChange: <T extends keyof IT>(key: T) => boolean | undefined;
|
|
15
|
+
getChanges: () => string[];
|
|
16
|
+
clearChanges: () => void;
|
|
17
|
+
};
|
|
7
18
|
set: <T extends keyof IT>(key: T, value: IT[T]) => void;
|
|
8
19
|
get: <T extends keyof IT>(key: T) => IT[T];
|
|
9
20
|
delete: <T extends keyof IT>(key: T) => void;
|
|
10
21
|
clear: () => void;
|
|
11
22
|
getState: () => IT;
|
|
12
23
|
setState: (state: Partial<IT>) => void;
|
|
13
|
-
isChange: <T extends keyof IT>(key: T) => boolean;
|
|
14
|
-
getChanges: () =>
|
|
24
|
+
isChange: <T extends keyof IT>(key: T) => boolean | undefined;
|
|
25
|
+
getChanges: () => string[];
|
|
15
26
|
clearChanges: () => void;
|
|
16
27
|
};
|
|
17
|
-
export {};
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
"use client";
|
|
2
|
-
import { useEffect, useId, useState } from "react";
|
|
2
|
+
import { useEffect, useId, useMemo, useState } from "react";
|
|
3
3
|
export const createBucket = (initial, option) => {
|
|
4
4
|
const hooks = new Map();
|
|
5
5
|
let data = new Map();
|
|
@@ -8,22 +8,23 @@ export const createBucket = (initial, option) => {
|
|
|
8
8
|
store: "memory",
|
|
9
9
|
...option,
|
|
10
10
|
};
|
|
11
|
+
for (let key in initial) {
|
|
12
|
+
let value = initial[key];
|
|
13
|
+
data.set(key, value);
|
|
14
|
+
changes.set(key, true);
|
|
15
|
+
}
|
|
11
16
|
const handleStorage = (isLoaded = true) => {
|
|
12
17
|
if (typeof window !== 'undefined') {
|
|
13
18
|
let url = new URL(window.location.href);
|
|
14
|
-
if (_option.store === 'session') {
|
|
19
|
+
if (_option.store === 'session' || _option.store === 'local') {
|
|
20
|
+
let storage = _option.store === "session" ? sessionStorage : localStorage;
|
|
15
21
|
for (let key in initial) {
|
|
16
|
-
let has =
|
|
22
|
+
let has = storage.getItem(key) !== null;
|
|
17
23
|
if (isLoaded || !has) {
|
|
18
|
-
|
|
19
|
-
sessionStorage.setItem(key, data.get(key));
|
|
20
|
-
}
|
|
21
|
-
else {
|
|
22
|
-
sessionStorage.removeItem(key);
|
|
23
|
-
}
|
|
24
|
+
data.has(key) ? storage.setItem(key, data.get(key)) : storage.removeItem(key);
|
|
24
25
|
}
|
|
25
26
|
else if (has) {
|
|
26
|
-
data.set(key,
|
|
27
|
+
data.set(key, storage.getItem(key));
|
|
27
28
|
}
|
|
28
29
|
}
|
|
29
30
|
}
|
|
@@ -31,12 +32,7 @@ export const createBucket = (initial, option) => {
|
|
|
31
32
|
for (let key in initial) {
|
|
32
33
|
let has = url.searchParams.has(key);
|
|
33
34
|
if (isLoaded || !has) {
|
|
34
|
-
|
|
35
|
-
url.searchParams.set(key, data.get(key));
|
|
36
|
-
}
|
|
37
|
-
else {
|
|
38
|
-
url.searchParams.delete(key);
|
|
39
|
-
}
|
|
35
|
+
data.has(key) ? url.searchParams.set(key, data.get(key)) : url.searchParams.delete(key);
|
|
40
36
|
}
|
|
41
37
|
else if (has) {
|
|
42
38
|
data.set(key, url.searchParams.get(key));
|
|
@@ -46,11 +42,6 @@ export const createBucket = (initial, option) => {
|
|
|
46
42
|
}
|
|
47
43
|
}
|
|
48
44
|
};
|
|
49
|
-
for (let key in initial) {
|
|
50
|
-
let value = initial[key];
|
|
51
|
-
data.set(key, value);
|
|
52
|
-
changes.set(key, true);
|
|
53
|
-
}
|
|
54
45
|
handleStorage(false);
|
|
55
46
|
let dispatch = () => {
|
|
56
47
|
hooks.forEach(d => {
|
|
@@ -59,75 +50,78 @@ export const createBucket = (initial, option) => {
|
|
|
59
50
|
}
|
|
60
51
|
catch (error) { }
|
|
61
52
|
});
|
|
53
|
+
handleStorage();
|
|
54
|
+
};
|
|
55
|
+
const set = (key, value) => {
|
|
56
|
+
if (!(key in initial))
|
|
57
|
+
throw new Error(`(${key}) Invalid key provided in the set function. Please verify the structure of the initial state data.`);
|
|
58
|
+
data.set(key, value);
|
|
59
|
+
changes.set(key, true);
|
|
60
|
+
dispatch();
|
|
61
|
+
};
|
|
62
|
+
const get = (key) => data.get(key);
|
|
63
|
+
const _delete = (key) => {
|
|
64
|
+
data.delete(key);
|
|
65
|
+
changes.set(key, true);
|
|
66
|
+
dispatch();
|
|
62
67
|
};
|
|
68
|
+
const clear = () => {
|
|
69
|
+
for (let key in initial) {
|
|
70
|
+
data.delete(key);
|
|
71
|
+
changes.set(key, true);
|
|
72
|
+
}
|
|
73
|
+
dispatch();
|
|
74
|
+
};
|
|
75
|
+
const getState = () => {
|
|
76
|
+
let d = {};
|
|
77
|
+
for (let key in initial) {
|
|
78
|
+
d[key] = data.get(key);
|
|
79
|
+
}
|
|
80
|
+
return d;
|
|
81
|
+
};
|
|
82
|
+
const setState = (state) => {
|
|
83
|
+
for (let key in state) {
|
|
84
|
+
if (!(key in initial))
|
|
85
|
+
throw new Error(`(${key}) Invalid key provided in the setState function. Please verify the structure of the initial state data.`);
|
|
86
|
+
data.set(key, state[key]);
|
|
87
|
+
changes.set(key, true);
|
|
88
|
+
}
|
|
89
|
+
dispatch();
|
|
90
|
+
};
|
|
91
|
+
const isChange = (key) => changes.get(key);
|
|
92
|
+
const getChanges = () => Array.from(changes.keys()).filter((key) => changes.get(key));
|
|
93
|
+
const clearChanges = () => Array.from(changes.keys()).forEach((key) => changes.set(key, false));
|
|
63
94
|
const useHook = () => {
|
|
64
95
|
const id = useId();
|
|
65
|
-
const [, setUp] = useState(0);
|
|
96
|
+
const [d, setUp] = useState(0);
|
|
66
97
|
useEffect(() => {
|
|
67
98
|
hooks.set(id, () => setUp(Math.random()));
|
|
68
99
|
return () => {
|
|
69
100
|
hooks.delete(id);
|
|
70
101
|
};
|
|
71
102
|
}, []);
|
|
103
|
+
const state = useMemo(() => getState(), [d]);
|
|
72
104
|
return {
|
|
73
|
-
set
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
return data.get(key);
|
|
83
|
-
},
|
|
84
|
-
delete: (key) => {
|
|
85
|
-
data.delete(key);
|
|
86
|
-
changes.set(key, true);
|
|
87
|
-
dispatch();
|
|
88
|
-
handleStorage();
|
|
89
|
-
},
|
|
90
|
-
clear: () => {
|
|
91
|
-
for (let key in initial) {
|
|
92
|
-
data.delete(key);
|
|
93
|
-
changes.set(key, true);
|
|
94
|
-
}
|
|
95
|
-
dispatch();
|
|
96
|
-
handleStorage();
|
|
97
|
-
},
|
|
98
|
-
getState: () => {
|
|
99
|
-
let d = {};
|
|
100
|
-
for (let key in initial) {
|
|
101
|
-
d[key] = data.get(key);
|
|
102
|
-
}
|
|
103
|
-
return d;
|
|
104
|
-
},
|
|
105
|
-
setState: (state) => {
|
|
106
|
-
for (let key in state) {
|
|
107
|
-
if (!(key in initial))
|
|
108
|
-
throw new Error(`(${key}) Invalid key provided in the setState function. Please verify the structure of the initial state data.`);
|
|
109
|
-
data.set(key, state[key]);
|
|
110
|
-
changes.set(key, true);
|
|
111
|
-
}
|
|
112
|
-
dispatch();
|
|
113
|
-
handleStorage();
|
|
114
|
-
},
|
|
115
|
-
isChange: (key) => Boolean(changes.get(key)),
|
|
116
|
-
getChanges: () => {
|
|
117
|
-
let _changes = {};
|
|
118
|
-
for (let key of changes.keys()) {
|
|
119
|
-
if (changes.get(key))
|
|
120
|
-
_changes[key] = true;
|
|
121
|
-
}
|
|
122
|
-
return _changes;
|
|
123
|
-
},
|
|
124
|
-
clearChanges: () => {
|
|
125
|
-
for (let key of changes.keys()) {
|
|
126
|
-
changes.set(key, false);
|
|
127
|
-
}
|
|
128
|
-
}
|
|
105
|
+
set,
|
|
106
|
+
get,
|
|
107
|
+
delete: _delete,
|
|
108
|
+
clear,
|
|
109
|
+
getState: () => state,
|
|
110
|
+
setState,
|
|
111
|
+
isChange,
|
|
112
|
+
getChanges,
|
|
113
|
+
clearChanges,
|
|
129
114
|
};
|
|
130
115
|
};
|
|
116
|
+
useHook.set = set;
|
|
117
|
+
useHook.get = get;
|
|
118
|
+
useHook.delete = _delete;
|
|
119
|
+
useHook.clear = clear;
|
|
120
|
+
useHook.getState = getState;
|
|
121
|
+
useHook.setState = setState;
|
|
122
|
+
useHook.isChange = isChange;
|
|
123
|
+
useHook.getChanges = getChanges;
|
|
124
|
+
useHook.clearChanges = clearChanges;
|
|
131
125
|
return useHook;
|
|
132
126
|
};
|
|
133
127
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,YAAY,CAAA;AACZ,OAAO,EAAE,SAAS,EAAE,KAAK,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAA;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,YAAY,CAAA;AACZ,OAAO,EAAE,SAAS,EAAE,KAAK,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAA;AAM3D,MAAM,CAAC,MAAM,YAAY,GAAG,CAAoC,OAAW,EAAE,MAAsB,EAAE,EAAE;IACnG,MAAM,KAAK,GAAG,IAAI,GAAG,EAAoB,CAAA;IACzC,IAAI,IAAI,GAAG,IAAI,GAAG,EAAY,CAAA;IAC9B,IAAI,OAAO,GAAG,IAAI,GAAG,EAAmB,CAAA;IAExC,IAAI,OAAO,GAAkB;QACzB,KAAK,EAAE,QAAQ;QACf,GAAG,MAAM;KACZ,CAAA;IAED,KAAK,IAAI,GAAG,IAAI,OAAO,EAAE,CAAC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