hermes-io 2.8.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 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
- function App({ notify }) {
7
- const increment = () => {
8
- notify({
9
- value: {
10
- type: INCREMENT,
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
- const decrement = () => {
16
- notify({
17
- value: {
18
- type: DECREMENT,
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
- export function Counter() {
40
- const [count, setCount] = useState(0);
41
- const handleCounterNotification = (event) => {
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
- useObserver({
49
- contexts: [CounterContext],
50
- observer: CounterObserver,
51
- listener: handleCounterNotification,
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://raw.githubusercontent.com/Maxtermax/hermes-io-counter-demo/master/src/assets/optimized.gif" />
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/
@@ -1 +1 @@
1
- export*from"./useObserver";export*from"./useMutations";export*from"./useStore";export*from"./useStoreFactory";
1
+ export*from"./useObserver";export*from"./useMutations";export*from"./useStore";export*from"./useObservableStore";
@@ -1 +1 @@
1
- function _array_like_to_array(r,e){(null==e||e>r.length)&&(e=r.length);for(var t=0,n=Array(e);t<e;t++)n[t]=r[t];return n}function _extends(){return(_extends=Object.assign||function(r){for(var e=1;e<arguments.length;e++){var t=arguments[e];for(var n in t)Object.prototype.hasOwnProperty.call(t,n)&&(r[n]=t[n])}return r}).apply(this,arguments)}function _create_for_of_iterator_helper_loose(r,e){var t="undefined"!=typeof Symbol&&r[Symbol.iterator]||r["@@iterator"];if(t)return(t=t.call(r)).next.bind(t);if(Array.isArray(r)||(t=function(r,e){if(r){if("string"==typeof r)return _array_like_to_array(r,void 0);var t=Object.prototype.toString.call(r).slice(8,-1);if("Object"===t&&r.constructor&&(t=r.constructor.name),"Map"===t||"Set"===t)return Array.from(t);if("Arguments"===t||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t))return _array_like_to_array(r,void 0)}}(r))||e&&r&&"number"==typeof r.length){t&&(r=t);var n=0;return function(){return n>=r.length?{done:!0}:{done:!1,value:r[n++]}}}throw TypeError("Invalid attempt to iterate non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}import{useState as r,useRef as e}from"react";import{useObserver as t}from"./useObserver";var randomId=function(){var r,e;return(null==(e=crypto)?void 0:null==(r=e.randomUUID)?void 0:r.call(e))||Math.random().toString(36).substring(2,16)};export var useMutations=function(n){void 0===n&&(n={});var o=n.events,a=void 0===o?[]:o,l=n.onChange,i=n.store,u=n.id,v=n.initialState,c=r(randomId()),d=(c[0],c[1]),s=e({state:_extends({},void 0===v?{}:v),events:[],onEvent:function(r,e){return s.current.events=[].concat(s.current.events,[{event:r,onChange:e}])}}),f=function(r){return n.noUpdate=r},_=function(r,e,t,o){var a=!r,l=!1;if(null==r||null==(i=r.forEach)||i.call(r,function(r){if(r===n.id){l=!0;var a,i=null!=(a=null==o?void 0:o(e,t,f,s.current.state))?a:{};n.noUpdate&&(i={}),s.current.state=_extends({},s.current.state,i)}}),a){var i,u,v=null!=(u=null==o?void 0:o(e,t,f,s.current.state))?u:{};n.noUpdate&&(v={}),s.current.state=_extends({},s.current.state,v)}!0!==n.noUpdate&&!1!==l&&d(randomId())};return t({id:u,microStore:i,listener:function(r,e){var t=null==(u=r.value)?void 0:null==(i=u.payload)?void 0:i.value,n=null==(v=r.value)?void 0:v.targets,o=null==(c=s.current)?void 0:c.events;if(o.length>0){for(var i,u,v,c,d,f=_create_for_of_iterator_helper_loose(o);!(d=f()).done;){var p=d.value;r.value.type===p.event&&_(n,t,e,p.onChange)}return}for(var y,m=_create_for_of_iterator_helper_loose(a);!(y=m()).done;){var h=y.value;r.value.type===h&&_(n,t,e,l)}},contexts:[null==i?void 0:i.context],observer:null==i?void 0:i.observer}),s.current};
1
+ function _array_like_to_array(r,e){(null==e||e>r.length)&&(e=r.length);for(var t=0,n=Array(e);t<e;t++)n[t]=r[t];return n}function _extends(){return(_extends=Object.assign||function(r){for(var e=1;e<arguments.length;e++){var t=arguments[e];for(var n in t)Object.prototype.hasOwnProperty.call(t,n)&&(r[n]=t[n])}return r}).apply(this,arguments)}function _create_for_of_iterator_helper_loose(r,e){var t="undefined"!=typeof Symbol&&r[Symbol.iterator]||r["@@iterator"];if(t)return(t=t.call(r)).next.bind(t);if(Array.isArray(r)||(t=function(r,e){if(r){if("string"==typeof r)return _array_like_to_array(r,void 0);var t=Object.prototype.toString.call(r).slice(8,-1);if("Object"===t&&r.constructor&&(t=r.constructor.name),"Map"===t||"Set"===t)return Array.from(t);if("Arguments"===t||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t))return _array_like_to_array(r,void 0)}}(r))||e&&r&&"number"==typeof r.length){t&&(r=t);var n=0;return function(){return n>=r.length?{done:!0}:{done:!1,value:r[n++]}}}throw TypeError("Invalid attempt to iterate non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}import{useState as r,useRef as e}from"react";import{useObserver as t}from"./useObserver";var randomId=function(){var r,e;return(null==(e=crypto)?void 0:null==(r=e.randomUUID)?void 0:r.call(e))||Math.random().toString(36).substring(2,16)};export var useMutations=function(n){void 0===n&&(n={});var o=n.events,a=void 0===o?[]:o,i=n.onChange,l=n.store,u=n.id,v=n.initialState,c=r(randomId()),d=(c[0],c[1]),s=e({state:_extends({},void 0===v?{}:v),events:[],onEvent:function(r,e){var t=s.current.events;t.some(function(e){return e.event===r})||t.push({event:r,onChange:e})}}),f=function(r){return n.noUpdate=r},_=function(r,e,t,o){var a=!r,i=!1;if(null==r||null==(l=r.forEach)||l.call(r,function(r){if(r===n.id){i=!0;var a,l=null!=(a=null==o?void 0:o(e,t,f,s.current.state))?a:{};n.noUpdate&&(l={}),s.current.state=_extends({},s.current.state,l)}}),a){var l,u,v=null!=(u=null==o?void 0:o(e,t,f,s.current.state))?u:{};n.noUpdate&&(v={}),s.current.state=_extends({},s.current.state,v)}!0!==n.noUpdate&&!1!==i&&d(randomId())};return t({id:u,microStore:l,listener:function(r,e){var t=null==(u=r.value)?void 0:null==(l=u.payload)?void 0:l.value,n=null==(v=r.value)?void 0:v.targets,o=null==(c=s.current)?void 0:c.events;if(o.length>0){for(var l,u,v,c,d,f=_create_for_of_iterator_helper_loose(o);!(d=f()).done;){var p=d.value;r.value.type===p.event&&_(n,t,e,p.onChange)}return}for(var y,m=_create_for_of_iterator_helper_loose(a);!(y=m()).done;){var h=y.value;r.value.type===h&&_(n,t,e,i)}},contexts:[null==l?void 0:l.context],observer:null==l?void 0:l.observer}),s.current};
@@ -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.8.74",
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",