hermes-io 2.9.74 → 2.9.75
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 +46 -32
- package/lib/hooks/index.js +1 -1
- package/lib/hooks/useObservableStore.js +1 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -3,22 +3,42 @@ A lightweight React library that allows communication between components by usin
|
|
|
3
3
|
|
|
4
4
|
## Usage
|
|
5
5
|
```javascript
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
6
|
+
import { MicroStore } from "hermes-io";
|
|
7
|
+
|
|
8
|
+
export const store = new MicroStore();
|
|
9
|
+
export const storeId = 'counter-id';
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
```javascript
|
|
13
|
+
export const actions = {
|
|
14
|
+
INCREMENT: "INCREMENT",
|
|
15
|
+
DECREMENT: "DECREMENT"
|
|
16
|
+
};
|
|
14
17
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
18
|
+
export default function reducer(store, action) {
|
|
19
|
+
const actionsMap = {
|
|
20
|
+
[actions.INCREMENT]: () => {
|
|
21
|
+
store.state.count += 1;
|
|
22
|
+
},
|
|
23
|
+
[actions.DECREMENT]: () => {
|
|
24
|
+
store.state.count -= 1;
|
|
25
|
+
},
|
|
21
26
|
};
|
|
27
|
+
return actionsMap[action.payload.type]();
|
|
28
|
+
};
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
```javascript
|
|
32
|
+
import { useObservableStore } from "hermes-io";
|
|
33
|
+
import { store, storeId } from "@/store";
|
|
34
|
+
import reducer, { actions } from "@/reducer";
|
|
35
|
+
|
|
36
|
+
export default function App() {
|
|
37
|
+
useObservableStore(storeId, { count: 0 }, reducer, store);
|
|
38
|
+
|
|
39
|
+
const increment = () => store.mutate({ type: events.INCREMENT });
|
|
40
|
+
|
|
41
|
+
const decrement = () => store.mutate({ type: events.DECREMENT });
|
|
22
42
|
|
|
23
43
|
return (
|
|
24
44
|
<div>
|
|
@@ -29,32 +49,26 @@ function App({ notify }) {
|
|
|
29
49
|
</div>
|
|
30
50
|
);
|
|
31
51
|
};
|
|
32
|
-
export default withNotify(App, {
|
|
33
|
-
context: CounterContext,
|
|
34
|
-
observer: CounterObserver
|
|
35
|
-
});
|
|
36
52
|
```
|
|
37
53
|
|
|
38
54
|
```javascript
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
const { value = {} } = event;
|
|
43
|
-
const { type } = value;
|
|
44
|
-
if (type === INCREMENT) setCount((prevValue) => prevValue + 1);
|
|
45
|
-
if (type === DECREMENT) setCount((prevValue) => prevValue - 1);
|
|
46
|
-
};
|
|
55
|
+
import { useMutations } from "hermes-io";
|
|
56
|
+
import { store, storeId } from "@/store";
|
|
57
|
+
import { actions } from "@/reducer";
|
|
47
58
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
59
|
+
export function Counter() {
|
|
60
|
+
const { state, onEvent } = useMutations({
|
|
61
|
+
initialState: { count: 0 },
|
|
62
|
+
id: storeId,
|
|
63
|
+
store,
|
|
52
64
|
});
|
|
65
|
+
onEvent(actions.INCREMENT, () => ({ count: state.count });
|
|
66
|
+
onEvent(actions.DECREMENT, () => ({ count: state.count });
|
|
53
67
|
|
|
54
|
-
return <h1>Counter: {count}</h1>;
|
|
68
|
+
return <h1>Counter: {state.count}</h1>;
|
|
55
69
|
}
|
|
56
70
|
```
|
|
57
|
-
<img src="https://
|
|
71
|
+
<img src="https://miro.medium.com/v2/resize:fit:1400/format:webp/1*VhOkr1735qdrHHyuJszqvQ.gif" />
|
|
58
72
|
|
|
59
73
|
## Documentation
|
|
60
74
|
See: https://hermes-io-docs.vercel.app/
|
package/lib/hooks/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export*from"./useObserver";export*from"./useMutations";export*from"./useStore";export*from"./
|
|
1
|
+
export*from"./useObserver";export*from"./useMutations";export*from"./useStore";export*from"./useObservableStore";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{useStore as r}from"./useStore";import{Context as e}from"../context/context";import{Observer as o}from"../observer/observer";import{Store as t}from"../store/store";export var useObservableStore=function(s,n,m,i){var c={store:new t({id:s,context:new e("Context_"+s),observer:new o}),reducer:m,data:n};return i&&(c.microStore=i),{store:r(c).store}};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "hermes-io",
|
|
3
|
-
"version": "2.9.
|
|
3
|
+
"version": "2.9.75",
|
|
4
4
|
"description": "A lightweight React library that allows communication between Reactjs components by using the observer pattern and the hook api",
|
|
5
5
|
"main": "./lib/index.js",
|
|
6
6
|
"module": "./lib/index.js",
|