hermes-io 1.0.0

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.
@@ -0,0 +1,3 @@
1
+ import React from 'react';
2
+
3
+ export const withActions = (actions, Component) => (props = {}) => <Component actions={actions} {...props}/>;
@@ -0,0 +1,3 @@
1
+ import React from 'react';
2
+
3
+ export const withStore = (store, Component) => (props = {}) => <Component store={store} {...props}/>;
@@ -0,0 +1,3 @@
1
+ export * from './WithActions';
2
+ export * from './WithStore';
3
+
package/hooks/index.js ADDED
@@ -0,0 +1 @@
1
+ export * from './useObserver';
@@ -0,0 +1,16 @@
1
+ import { useEffect } from "react";
2
+
3
+ export const useObserver = (props = {}) => {
4
+ useEffect(() => {
5
+ const { observer, listener, from = [] } = props;
6
+ function handleAction(value) {
7
+ const hasfromList = from.length !== 0;
8
+ const hasValidList = hasfromList && from.includes(value.from);
9
+ if (hasValidList) {
10
+ listener?.(value);
11
+ }
12
+ }
13
+ observer.subscribe(handleAction);
14
+ return () => observer.unsubscribe(handleAction);
15
+ }, [props]);
16
+ };
package/index.js ADDED
@@ -0,0 +1,3 @@
1
+ export * from "./hooks";
2
+ export * from "./components";
3
+ export * from "./main";
package/main.js ADDED
@@ -0,0 +1,12 @@
1
+ export class Observer {
2
+ subscriptors = [];
3
+ subscribe(callback) {
4
+ this.subscriptors.push(callback);
5
+ }
6
+ unsubscribe(callback) {
7
+ this.subscriptors.splice(this.subscriptors.findIndex(cb => cb === callback), 1);
8
+ }
9
+ notify(args = {}) {
10
+ this.subscriptors.forEach(callback => callback(args));
11
+ }
12
+ }
package/package.json ADDED
@@ -0,0 +1,15 @@
1
+ {
2
+ "name": "hermes-io",
3
+ "version": "1.0.0",
4
+ "description": "A lightweight javascript library that allows communication between Reactjs components by using the observer pattern and the hook api",
5
+ "main": "index.js",
6
+ "scripts": {
7
+ "test": "echo \"Error: no test specified\" && exit 1"
8
+ },
9
+ "keywords": [
10
+ "hermes-io",
11
+ "maxtermax"
12
+ ],
13
+ "author": "maxtermax",
14
+ "license": "MIT"
15
+ }