hermes-io 2.2.4 → 2.2.5
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
CHANGED
|
@@ -1,5 +1,15 @@
|
|
|
1
1
|
# hermes-io
|
|
2
|
-
A lightweight javascript library that allows communication between
|
|
2
|
+
A lightweight javascript library that allows communication between components by using the observer pattern and the hook api.
|
|
3
|
+
|
|
4
|
+
# Summary
|
|
5
|
+
- [Installation](#installation)
|
|
6
|
+
- [Get started](#get-started)
|
|
7
|
+
- [Observers](#observers)
|
|
8
|
+
- [Contexts](#contexts)
|
|
9
|
+
- [useObserver](#useobserver-hook)
|
|
10
|
+
- [notify](#notify)
|
|
11
|
+
- Devtools (TODO)
|
|
12
|
+
|
|
3
13
|
|
|
4
14
|
# Installation
|
|
5
15
|
```
|
|
@@ -7,63 +17,153 @@ npm i hermes-io --save
|
|
|
7
17
|
```
|
|
8
18
|
|
|
9
19
|
# Get started
|
|
10
|
-
|
|
11
|
-
|
|
20
|
+
`hermes-io` is a set of toolkits that combined allows communication between components let's explore every tool by following the `sneaker store example`:
|
|
21
|
+
|
|
22
|
+
# Observers
|
|
23
|
+
`hermes-io` provide an `Observer` class to create instances that can be `subscribable` that means many subscribers can listen for notifications on the instance by using the method `subscribe`, check the following example:
|
|
24
|
+
|
|
25
|
+
We are exporting an `object` with two instances of the class `Observer` each key of the object has invidual propuses one for handle notifications about `add` a product and the other for `remove` a product.
|
|
26
|
+
|
|
27
|
+
```javascript
|
|
28
|
+
// observers/products.js
|
|
29
|
+
import { Observer } from "hermes-io";
|
|
30
|
+
|
|
31
|
+
export default {
|
|
32
|
+
add: new Observer(),
|
|
33
|
+
remove: new Observer(),
|
|
34
|
+
};
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
# Contexts
|
|
38
|
+
`NOTICE` this concept has nothing to do with the `react context api`.
|
|
39
|
+
|
|
40
|
+
`hermes-io` provide a `Context` class to create instances that can be used to create `notification context` that means that only notification
|
|
41
|
+
submited on on specific context will be listened otherwise will be ignored, you can think on this like a `whitelist` let's analyze the following example:
|
|
42
|
+
|
|
43
|
+
In our sneaker store we have a `product list` and a `shopping car` the user can `add` a `product` to the `shopping car`
|
|
44
|
+
and also can `remove` a `product`, in both cases the one component can talk to the other by using `notifications`
|
|
45
|
+
on one specific observer and update the `ui`, this leads us to any part of the code with access to the observers can trigger `unexpected behaviors`,
|
|
46
|
+
there is when the concept of a `context` comes in, the context constrains the observer by telling which `notifications` must listen.
|
|
47
|
+
|
|
48
|
+
```javascript
|
|
49
|
+
import { Context } from 'hermes-io';
|
|
50
|
+
|
|
51
|
+
export const products = new Context('Product');
|
|
52
|
+
export const shoppingCar = new Context('ShoppingCar');
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
```javascript
|
|
57
|
+
const sneakerList = [
|
|
58
|
+
{
|
|
59
|
+
id: '1',
|
|
60
|
+
name: 'Jordan',
|
|
61
|
+
image: '/assets/images/jordan_3.webp',
|
|
62
|
+
description: 'Air Jordan 3 Retro OG',
|
|
63
|
+
price: '250'
|
|
64
|
+
},
|
|
65
|
+
{
|
|
66
|
+
id: '2',
|
|
67
|
+
image: '/assets/images/addidas.webp',
|
|
68
|
+
description: 'Bad Bunny Forum Buckle Low sneakers',
|
|
69
|
+
name: 'Adidas Forum',
|
|
70
|
+
price: '200'
|
|
71
|
+
},
|
|
72
|
+
{
|
|
73
|
+
id: '3',
|
|
74
|
+
image: '/assets/images/addidas.webp',
|
|
75
|
+
description: 'Bad Bunny Forum Buckle Low sneakers',
|
|
76
|
+
name: 'Adidas Forum',
|
|
77
|
+
price: '200'
|
|
78
|
+
}
|
|
79
|
+
]
|
|
80
|
+
|
|
81
|
+
const productsStore = new Map();
|
|
82
|
+
productsStore.set('collection', sneakerList);
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
# useObserver (hook)
|
|
86
|
+
`hermes-io` provide a `react custom hook` to integrate `Observer` with `Context`, this hook can be used to subscribe listeners and receive `notifications` under cetains contrains provided by the `notification context`, let's analize this in detail.
|
|
87
|
+
|
|
88
|
+
| key | value | required | description |
|
|
89
|
+
|----------|-------------------|----------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------|
|
|
90
|
+
| observer | Observer instance | true | An instance of the class Observer |
|
|
91
|
+
| listener | Function | true | A standar javascript function |
|
|
92
|
+
| contexts | Array<Context> | true | An array of instances of the class Context, when a notification comes and is not signed with any of the contexts in the array the listener never will be called |
|
|
12
93
|
|
|
13
94
|
```javascript
|
|
14
|
-
|
|
15
|
-
export const AddProductObserver = new Observer();
|
|
16
|
-
export const RemoveProductObserver = new Observer();
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
// App.js
|
|
20
|
-
import { useState } from 'react';
|
|
21
|
-
import { useObserver, Observer } from 'hermes-io';
|
|
22
|
-
import { RemoveProductObserver, AddProductObserver } from './observers';
|
|
23
|
-
import { Products } from './components/Products';
|
|
24
|
-
import { ShoppingCar } from './components/ShoppingCar';
|
|
25
|
-
|
|
26
|
-
export const App = (props = {}) => {
|
|
27
|
-
const [productsToBy, setProductsToBy] = useState([]);
|
|
28
|
-
const productsStore = useProductStore(); // get products from some store
|
|
29
|
-
const products = productsStore.get();
|
|
95
|
+
import { useObserver } from "hermes-io";
|
|
30
96
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
97
|
+
useObserver({
|
|
98
|
+
observer: ProductsObservers.add,
|
|
99
|
+
listener: handleAddProduct,
|
|
100
|
+
contexts: [contexts.products],
|
|
101
|
+
});
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
```javascript
|
|
105
|
+
import React, { useState } from "react";
|
|
106
|
+
import Products from "./components/Products/Products"
|
|
107
|
+
import ShoppingCar from "./components/ShoppingCar/ShoppingCar";
|
|
108
|
+
import { useObserver } from "hermes-io";
|
|
109
|
+
import ProductsObservers from './observers/products';
|
|
110
|
+
import theme from '@theme';
|
|
111
|
+
import * as contexts from './contexts';
|
|
112
|
+
|
|
113
|
+
const filterSelectes = (collection) => collection.filter((item) => item.selected); //filter selectes products
|
|
114
|
+
|
|
115
|
+
function App() {
|
|
116
|
+
const [products, setProducts] = useState(ProductsStore.get('collection'));
|
|
117
|
+
|
|
118
|
+
const handleRemoveProduct = ({ value: product = {} }) => {
|
|
119
|
+
product.selected = false;
|
|
120
|
+
setProducts([...ProductsStore.get('collection')]);
|
|
35
121
|
};
|
|
36
122
|
|
|
37
|
-
const handleAddProduct = (product = {}) => {
|
|
38
|
-
|
|
39
|
-
|
|
123
|
+
const handleAddProduct = ({ value: product = {} }) => {
|
|
124
|
+
product.selected = true;
|
|
125
|
+
setProducts([...ProductsStore.get('collection')]);
|
|
40
126
|
};
|
|
41
127
|
|
|
42
128
|
useObserver({
|
|
43
|
-
observer:
|
|
129
|
+
observer: ProductsObservers.add,
|
|
44
130
|
listener: handleAddProduct,
|
|
45
|
-
|
|
131
|
+
contexts: [contexts.products],
|
|
46
132
|
});
|
|
47
133
|
|
|
48
134
|
useObserver({
|
|
49
|
-
observer:
|
|
135
|
+
observer: ProductsObservers.remove,
|
|
50
136
|
listener: handleRemoveProduct,
|
|
51
|
-
|
|
137
|
+
contexts: [contexts.shoppingCar, contexts.products],
|
|
52
138
|
});
|
|
53
|
-
|
|
54
|
-
return <div>
|
|
55
|
-
<Products data={products} />
|
|
56
|
-
<ShoppingCar data={productsToBy}/>
|
|
57
|
-
</div>
|
|
58
|
-
};
|
|
59
139
|
|
|
140
|
+
return (
|
|
141
|
+
<>
|
|
142
|
+
<ShoppingCar data={filterSelectes(products)} />
|
|
143
|
+
<Products data={products} />
|
|
144
|
+
</>
|
|
145
|
+
);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
export default App;
|
|
60
149
|
|
|
150
|
+
```
|
|
151
|
+
# Notify
|
|
152
|
+
Is a method that allows sending notifications to the `subscribers` of a specific `observer` signed with a `context` that way we create a `notification context`, let's see this in details:
|
|
153
|
+
|
|
154
|
+
| key | value | required | description |
|
|
155
|
+
|---------|---------|----------|-----------------------------------|
|
|
156
|
+
| value | any | true | Payload with business information |
|
|
157
|
+
| context | context | true | A context instance |
|
|
158
|
+
|
|
159
|
+
```javascript
|
|
61
160
|
// ShoppingCar.js
|
|
62
|
-
import
|
|
161
|
+
import ProductsObservers from './observers/products';
|
|
162
|
+
import * as contexts from '../contexts';
|
|
63
163
|
|
|
64
164
|
export const ShoppingCar = (props = {}) => {
|
|
65
165
|
const handleRemoveProduct = (product = {}) => {
|
|
66
|
-
|
|
166
|
+
ProductsObservers.remove.notify({ value: product, context: contexts.shoppingCar });
|
|
67
167
|
};
|
|
68
168
|
return <div>
|
|
69
169
|
<ul>
|
|
@@ -80,15 +180,17 @@ export const ShoppingCar = (props = {}) => {
|
|
|
80
180
|
</ul>
|
|
81
181
|
</div>
|
|
82
182
|
};
|
|
83
|
-
|
|
183
|
+
```
|
|
184
|
+
```javascript
|
|
84
185
|
// Products.js
|
|
85
|
-
import
|
|
186
|
+
import ProductsObservers from './observers/products';
|
|
187
|
+
import * as contexts from '../contexts';
|
|
86
188
|
|
|
87
189
|
export const Products = (props = {}) => {
|
|
88
190
|
const { data = [] } = props;
|
|
89
191
|
|
|
90
192
|
const handleAddProduct = (product = {}) => {
|
|
91
|
-
|
|
193
|
+
ProductsObservers.add.notify({ value: product, context: contexts.products });
|
|
92
194
|
};
|
|
93
195
|
|
|
94
196
|
return <ul>
|
|
@@ -104,7 +206,6 @@ export const Products = (props = {}) => {
|
|
|
104
206
|
}
|
|
105
207
|
</ul>
|
|
106
208
|
};
|
|
107
|
-
|
|
108
209
|
```
|
|
109
210
|
|
|
110
211
|
|
|
@@ -20,15 +20,10 @@ const Products = (props = {}) => {
|
|
|
20
20
|
};
|
|
21
21
|
|
|
22
22
|
const handleRemoveProduct = (product = {}) => {
|
|
23
|
-
console.log(1, product);
|
|
24
|
-
console.log(2, product);
|
|
25
|
-
console.log(3, product);
|
|
26
23
|
ProductsObserver.remove.notify({
|
|
27
24
|
value: product,
|
|
28
25
|
context: contexts.products,
|
|
29
26
|
});
|
|
30
|
-
console.log(4, product);
|
|
31
|
-
console.log(5, product);
|
|
32
27
|
};
|
|
33
28
|
|
|
34
29
|
return (
|
package/package.json
CHANGED