hermes-io 2.2.11 → 2.2.12
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/. quokka +0 -1
- package/hooks/index.js +1 -0
- package/hooks/useObserver.js +3 -3
- package/hooks/useProxy.js +32 -0
- package/index.js +0 -1
- package/package.json +1 -1
- package/components/WithActions.js +0 -3
- package/components/WithStore.js +0 -3
- package/components/index.js +0 -3
package/. quokka
CHANGED
package/hooks/index.js
CHANGED
package/hooks/useObserver.js
CHANGED
|
@@ -3,7 +3,7 @@ import React, { useEffect } from "react";
|
|
|
3
3
|
export const useObserver = (props = {}) => {
|
|
4
4
|
useEffect(() => {
|
|
5
5
|
const { observer, listener, contexts = [] } = props;
|
|
6
|
-
function
|
|
6
|
+
function subscriber(payload, resolve) {
|
|
7
7
|
const hasfromList = contexts.length !== 0;
|
|
8
8
|
const hasValidList = hasfromList && contexts.find((ctx) => ctx.id === payload?.context?.id);
|
|
9
9
|
if (hasValidList) {
|
|
@@ -11,7 +11,7 @@ export const useObserver = (props = {}) => {
|
|
|
11
11
|
listener?.(payload, resolve);
|
|
12
12
|
}
|
|
13
13
|
}
|
|
14
|
-
observer
|
|
15
|
-
return () => observer
|
|
14
|
+
observer?.subscribe?.(subscriber);
|
|
15
|
+
return () => observer?.unsubscribe?.(subscriber);
|
|
16
16
|
}, [props]);
|
|
17
17
|
};
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import React, { useEffect, useState, useRef } from "react";
|
|
2
|
+
import { useObserver } from './useObserver';
|
|
3
|
+
|
|
4
|
+
export const useProxy = (target, options) => {
|
|
5
|
+
const proxyRef = useRef({ ...target });
|
|
6
|
+
const [value, setValue] = useState(target);
|
|
7
|
+
useObserver({
|
|
8
|
+
observer: options?.observer,
|
|
9
|
+
contexts: options?.contexts,
|
|
10
|
+
listener: (payload) => value[payload.value],
|
|
11
|
+
})
|
|
12
|
+
useEffect(() => {
|
|
13
|
+
function setup() {
|
|
14
|
+
const handler = {
|
|
15
|
+
get: function (_target, prop) {
|
|
16
|
+
return value[prop];
|
|
17
|
+
},
|
|
18
|
+
set: function (target, prop, newValue) {
|
|
19
|
+
target[prop] = newValue;
|
|
20
|
+
setValue({ ...target });
|
|
21
|
+
options?.contexts?.forEach?.((context) =>
|
|
22
|
+
options?.observer?.notify?.({ value: proxyRef, context })
|
|
23
|
+
);
|
|
24
|
+
return true;
|
|
25
|
+
},
|
|
26
|
+
};
|
|
27
|
+
proxyRef.current = new Proxy(target, handler);
|
|
28
|
+
}
|
|
29
|
+
setup();
|
|
30
|
+
}, [target, setValue, proxyRef, value, options]);
|
|
31
|
+
return proxyRef;
|
|
32
|
+
};
|
package/index.js
CHANGED
package/package.json
CHANGED
package/components/WithStore.js
DELETED
package/components/index.js
DELETED