hermes-io 2.2.18 → 2.2.20
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 +12 -157
- package/package.json +2 -2
- package/utils/README.md +63 -0
- package/README2.md +0 -8
package/README.md
CHANGED
|
@@ -1,102 +1,7 @@
|
|
|
1
|
-
# hermes-io ⚡
|
|
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
|
-
- [Observer](#observer)
|
|
8
|
-
- [Context](#context)
|
|
9
|
-
- [useObserver](#useobserver-hook)
|
|
10
|
-
- [notify](#notify)
|
|
11
|
-
- [Devtools](https://github.com/Maxtermax/hermes-io-devtools)
|
|
12
|
-
|
|
13
|
-

|
|
14
|
-
|
|
15
|
-
# Installation
|
|
16
|
-
```
|
|
17
|
-
npm i hermes-io --save
|
|
18
|
-
```
|
|
19
|
-
|
|
20
|
-
# Get started
|
|
21
|
-
`hermes-io` is a set of toolkits that combined allows communication between components let's explore every tool by following the [sneaker store example ](https://sneaker-store-1.vercel.app/) ⭐
|
|
22
|
-
[source code](https://github.com/Maxtermax/sneaker-store)
|
|
23
|
-
|
|
24
|
-
# Observer
|
|
25
|
-
`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:
|
|
26
|
-
|
|
27
|
-
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.
|
|
28
|
-
|
|
29
|
-
```javascript
|
|
30
|
-
// observers/products.js
|
|
31
|
-
import { Observer } from "hermes-io";
|
|
32
|
-
|
|
33
|
-
export default {
|
|
34
|
-
add: new Observer(),
|
|
35
|
-
remove: new Observer(),
|
|
36
|
-
};
|
|
37
|
-
```
|
|
38
|
-
|
|
39
|
-
# Context
|
|
40
|
-
`NOTICE` this concept has nothing to do with the `react context api`.
|
|
41
|
-
|
|
42
|
-
`hermes-io` provide a `Context` class to create instances that can be used to create `notification context` that means that only notification
|
|
43
|
-
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:
|
|
44
|
-
|
|
45
|
-
In our sneaker store we have a `product list` and a `shopping car` the user can `add` a `product` to the `shopping car`
|
|
46
|
-
and also can `remove` a `product`, in both cases the one component can talk to the other by using `notifications`
|
|
47
|
-
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`,
|
|
48
|
-
there is when the concept of a `context` comes in, the context constrains the observer by telling which `notifications` must listen.
|
|
49
|
-
|
|
50
|
-
```javascript
|
|
51
|
-
import { Context } from 'hermes-io';
|
|
52
|
-
|
|
53
|
-
export const products = new Context('Product');
|
|
54
|
-
export const shoppingCar = new Context('ShoppingCar');
|
|
55
|
-
```
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
```javascript
|
|
59
|
-
const sneakerList = [
|
|
60
|
-
{
|
|
61
|
-
id: '1',
|
|
62
|
-
name: 'Jordan',
|
|
63
|
-
image: '/assets/images/jordan_3.webp',
|
|
64
|
-
description: 'Air Jordan 3 Retro OG',
|
|
65
|
-
price: '250'
|
|
66
|
-
},
|
|
67
|
-
{
|
|
68
|
-
id: '2',
|
|
69
|
-
image: '/assets/images/addidas.webp',
|
|
70
|
-
description: 'Bad Bunny Forum Buckle Low sneakers',
|
|
71
|
-
name: 'Adidas Forum',
|
|
72
|
-
price: '200'
|
|
73
|
-
}
|
|
74
|
-
]
|
|
75
|
-
|
|
76
|
-
const productsStore = new Map();
|
|
77
|
-
productsStore.set('collection', sneakerList);
|
|
78
|
-
```
|
|
79
|
-
|
|
80
|
-
# useObserver (hook)
|
|
81
|
-
`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.
|
|
82
|
-
|
|
83
|
-
| key | value | required | description |
|
|
84
|
-
|----------|-------------------|----------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------|
|
|
85
|
-
| observer | Observer instance | true | An instance of the class Observer |
|
|
86
|
-
| listener | Function | true | A standar javascript function |
|
|
87
|
-
| contexts | 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 |
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
```javascript
|
|
91
|
-
import { useObserver } from "hermes-io";
|
|
92
|
-
|
|
93
|
-
useObserver({
|
|
94
|
-
observer: ProductsObservers.add,
|
|
95
|
-
listener: handleAddProduct,
|
|
96
|
-
contexts: [contexts.products],
|
|
97
|
-
});
|
|
98
|
-
```
|
|
1
|
+
# hermes-io ⚡
|
|
2
|
+
A lightweight javascript library that allows communication between components by using the observer pattern and the hook api.
|
|
99
3
|
|
|
4
|
+
## Usage:
|
|
100
5
|
```javascript
|
|
101
6
|
import React, { useState } from "react";
|
|
102
7
|
import Products from "./components/Products/Products"
|
|
@@ -144,65 +49,15 @@ function App() {
|
|
|
144
49
|
export default App;
|
|
145
50
|
|
|
146
51
|
```
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
| key | value | required | description |
|
|
151
|
-
|---------|---------|----------|-----------------------------------|
|
|
152
|
-
| value | any | true | Payload with business information |
|
|
153
|
-
| context | context | true | A context instance |
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
```javascript
|
|
157
|
-
// ShoppingCar.js
|
|
158
|
-
import ProductsObservers from './observers/products';
|
|
159
|
-
import * as contexts from '../contexts';
|
|
52
|
+
## Chrome extension
|
|
53
|
+
Install from chrome web store [here](https://chrome.google.com/webstore/detail/hermes-io/pjdkgcpikfmkncldipldmimanfkpeedm?hl=en)
|
|
160
54
|
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
return <div>
|
|
166
|
-
<ul>
|
|
167
|
-
{
|
|
168
|
-
data.map((product = {}) =>
|
|
169
|
-
<li key={product.id}>
|
|
170
|
-
<span>{product.name}</<span>
|
|
171
|
-
<p>{product.description}</p>
|
|
172
|
-
<small>${product.price}</<small>
|
|
173
|
-
<button onClick={() => handleRemoveProduct(product)}>Remove</<button>
|
|
174
|
-
</li>
|
|
175
|
-
))
|
|
176
|
-
}
|
|
177
|
-
</ul>
|
|
178
|
-
</div>
|
|
179
|
-
};
|
|
180
|
-
```
|
|
181
|
-
```javascript
|
|
182
|
-
// Products.js
|
|
183
|
-
import ProductsObservers from './observers/products';
|
|
184
|
-
import * as contexts from '../contexts';
|
|
55
|
+

|
|
56
|
+
|
|
57
|
+
## Documentation:
|
|
58
|
+
https://github.com/Maxtermax/hermes-io#readme
|
|
185
59
|
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
const handleAddProduct = (product = {}) => {
|
|
190
|
-
ProductsObservers.add.notify({ value: product, context: contexts.products });
|
|
191
|
-
};
|
|
192
|
-
|
|
193
|
-
return <ul>
|
|
194
|
-
{
|
|
195
|
-
data.map((product = {}) =>
|
|
196
|
-
<li key={product.id}>
|
|
197
|
-
<span>{product.name}</<span>
|
|
198
|
-
<p>{product.description}</p>
|
|
199
|
-
<small>${product.price}</<small>
|
|
200
|
-
<button disabled={product.selected} onClick={() => handleAddProduct(product)}>Add to car</<button>
|
|
201
|
-
</li>
|
|
202
|
-
))
|
|
203
|
-
}
|
|
204
|
-
</ul>
|
|
205
|
-
};
|
|
206
|
-
```
|
|
60
|
+
## Contributing:
|
|
61
|
+
Feel free to open a PR explaining your changes, this library is open to suggestions and improvements.
|
|
207
62
|
|
|
208
|
-
|
|
63
|
+
https://github.com/Maxtermax/hermes-io
|
package/package.json
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "hermes-io",
|
|
3
|
-
"version": "2.2.
|
|
3
|
+
"version": "2.2.20",
|
|
4
4
|
"description": "A lightweight javascript 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",
|
|
7
7
|
"scripts": {
|
|
8
8
|
"test": "echo \"Error: no test specified\" && exit 1",
|
|
9
|
-
"prepare": "swc ./src -d ./lib &&
|
|
9
|
+
"prepare": "swc ./src -d ./lib && cp ./utils/README.md ./README.md"
|
|
10
10
|
},
|
|
11
11
|
"exports": {
|
|
12
12
|
".": {
|
package/utils/README.md
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
# hermes-io ⚡
|
|
2
|
+
A lightweight javascript library that allows communication between components by using the observer pattern and the hook api.
|
|
3
|
+
|
|
4
|
+
## Usage:
|
|
5
|
+
```javascript
|
|
6
|
+
import React, { useState } from "react";
|
|
7
|
+
import Products from "./components/Products/Products"
|
|
8
|
+
import ShoppingCar from "./components/ShoppingCar/ShoppingCar";
|
|
9
|
+
import { useObserver } from "hermes-io";
|
|
10
|
+
import ProductsObservers from './observers/products';
|
|
11
|
+
import theme from '@theme';
|
|
12
|
+
import * as contexts from './contexts';
|
|
13
|
+
|
|
14
|
+
const filterSelectes = (collection) => collection.filter((item) => item.selected); //filter selectes products
|
|
15
|
+
|
|
16
|
+
function App() {
|
|
17
|
+
const [products, setProducts] = useState(ProductsStore.get('collection'));
|
|
18
|
+
|
|
19
|
+
const handleRemoveProduct = ({ value: product = {} }) => {
|
|
20
|
+
product.selected = false;
|
|
21
|
+
setProducts([...ProductsStore.get('collection')]);
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
const handleAddProduct = ({ value: product = {} }) => {
|
|
25
|
+
product.selected = true;
|
|
26
|
+
setProducts([...ProductsStore.get('collection')]);
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
useObserver({
|
|
30
|
+
observer: ProductsObservers.add,
|
|
31
|
+
listener: handleAddProduct,
|
|
32
|
+
contexts: [contexts.products],
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
useObserver({
|
|
36
|
+
observer: ProductsObservers.remove,
|
|
37
|
+
listener: handleRemoveProduct,
|
|
38
|
+
contexts: [contexts.shoppingCar, contexts.products],
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
return (
|
|
42
|
+
<>
|
|
43
|
+
<ShoppingCar data={filterSelectes(products)} />
|
|
44
|
+
<Products data={products} />
|
|
45
|
+
</>
|
|
46
|
+
);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export default App;
|
|
50
|
+
|
|
51
|
+
```
|
|
52
|
+
## Chrome extension
|
|
53
|
+
Install from chrome web store [here](https://chrome.google.com/webstore/detail/hermes-io/pjdkgcpikfmkncldipldmimanfkpeedm?hl=en)
|
|
54
|
+
|
|
55
|
+

|
|
56
|
+
|
|
57
|
+
## Documentation:
|
|
58
|
+
https://github.com/Maxtermax/hermes-io#readme
|
|
59
|
+
|
|
60
|
+
## Contributing:
|
|
61
|
+
Feel free to open a PR explaining your changes, this library is open to suggestions and improvements.
|
|
62
|
+
|
|
63
|
+
https://github.com/Maxtermax/hermes-io
|
package/README2.md
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
# hermes-io ⚡
|
|
2
|
-
A lightweight javascript library that allows communication between components by using the observer pattern and the hook api.
|
|
3
|
-
|
|
4
|
-

|
|
5
|
-
|
|
6
|
-
## Documentation:
|
|
7
|
-
|
|
8
|
-
https://github.com/Maxtermax/hermes-io#readme
|