@xyo-network/react-node 2.45.0-rc.20 → 2.45.0-rc.22
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/dist/cjs/hooks/useModule.js +31 -13
- package/dist/cjs/hooks/useModule.js.map +1 -1
- package/dist/cjs/hooks/useProvidedNode.js +10 -2
- package/dist/cjs/hooks/useProvidedNode.js.map +1 -1
- package/dist/cjs/hooks/useWrappedModule.js +14 -4
- package/dist/cjs/hooks/useWrappedModule.js.map +1 -1
- package/dist/docs.json +122 -88
- package/dist/esm/hooks/useModule.js +32 -14
- package/dist/esm/hooks/useModule.js.map +1 -1
- package/dist/esm/hooks/useProvidedNode.js +11 -3
- package/dist/esm/hooks/useProvidedNode.js.map +1 -1
- package/dist/esm/hooks/useWrappedModule.js +15 -5
- package/dist/esm/hooks/useWrappedModule.js.map +1 -1
- package/dist/types/hooks/useModule.d.ts +2 -2
- package/dist/types/hooks/useModule.d.ts.map +1 -1
- package/dist/types/hooks/useProvidedNode.d.ts.map +1 -1
- package/dist/types/hooks/useWrappedModule.d.ts +1 -0
- package/dist/types/hooks/useWrappedModule.d.ts.map +1 -1
- package/package.json +6 -5
- package/src/hooks/useModule.tsx +37 -16
- package/src/hooks/useProvidedNode.tsx +13 -3
- package/src/hooks/useWrappedModule.tsx +21 -5
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import { useAsyncEffect } from '@xylabs/react-shared';
|
|
2
|
-
import { useState } from 'react';
|
|
2
|
+
import { useMemo, useState } from 'react';
|
|
3
3
|
import { useProvidedWrappedNode } from './useProvidedNode';
|
|
4
|
-
export const useModule = (
|
|
4
|
+
export const useModule = (nameOrAddressOrFilter) => {
|
|
5
|
+
const nameOrAddress = useMemo(() => (typeof nameOrAddressOrFilter === 'string' ? nameOrAddressOrFilter : undefined), [nameOrAddressOrFilter]);
|
|
6
|
+
const filter = useMemo(() => (typeof nameOrAddressOrFilter === 'object' ? nameOrAddressOrFilter : undefined), [nameOrAddressOrFilter]);
|
|
5
7
|
const [node, nodeError] = useProvidedWrappedNode();
|
|
6
8
|
const [module, setModule] = useState();
|
|
7
9
|
const [error, setError] = useState();
|
|
@@ -9,13 +11,6 @@ export const useModule = (nameOrAddress) => {
|
|
|
9
11
|
useAsyncEffect(
|
|
10
12
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
11
13
|
async (mounted) => {
|
|
12
|
-
const detachHandler = (args) => {
|
|
13
|
-
const eventModule = args.module;
|
|
14
|
-
if (eventModule.address === address && mounted()) {
|
|
15
|
-
setModule(undefined);
|
|
16
|
-
setError(undefined);
|
|
17
|
-
}
|
|
18
|
-
};
|
|
19
14
|
try {
|
|
20
15
|
if (nodeError) {
|
|
21
16
|
setError(nodeError);
|
|
@@ -23,16 +18,39 @@ export const useModule = (nameOrAddress) => {
|
|
|
23
18
|
}
|
|
24
19
|
else {
|
|
25
20
|
if (node) {
|
|
26
|
-
const
|
|
27
|
-
const
|
|
21
|
+
const attachEmitter = node.module;
|
|
22
|
+
const detachEmitter = node.module;
|
|
23
|
+
const attachHandler = (args) => {
|
|
24
|
+
const eventModule = args.module;
|
|
25
|
+
if (eventModule.address === nameOrAddress || (module?.config.name === nameOrAddress && mounted())) {
|
|
26
|
+
setModule(eventModule);
|
|
27
|
+
setError(undefined);
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
const detachHandler = (args) => {
|
|
31
|
+
const eventModule = args.module;
|
|
32
|
+
if (eventModule.address === address && mounted()) {
|
|
33
|
+
setModule(undefined);
|
|
34
|
+
setError(undefined);
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
const module = nameOrAddress
|
|
38
|
+
? await node.resolve(nameOrAddress)
|
|
39
|
+
: (await node.resolve(filter)).pop();
|
|
28
40
|
if (mounted()) {
|
|
29
|
-
|
|
41
|
+
if (module) {
|
|
42
|
+
detachEmitter.on('moduleDetached', detachHandler);
|
|
43
|
+
}
|
|
44
|
+
else {
|
|
45
|
+
attachEmitter.on('moduleAttached', attachHandler);
|
|
46
|
+
}
|
|
30
47
|
setModule(module);
|
|
31
48
|
setError(undefined);
|
|
32
49
|
}
|
|
33
50
|
return () => {
|
|
34
51
|
//remove the event handler on unmount
|
|
35
|
-
|
|
52
|
+
attachEmitter.on('moduleAttached', attachHandler, true);
|
|
53
|
+
detachEmitter.on('moduleDetached', detachHandler, true);
|
|
36
54
|
};
|
|
37
55
|
}
|
|
38
56
|
else {
|
|
@@ -50,7 +68,7 @@ export const useModule = (nameOrAddress) => {
|
|
|
50
68
|
setModule(undefined);
|
|
51
69
|
}
|
|
52
70
|
}
|
|
53
|
-
}, [nameOrAddress, node, nodeError, address]);
|
|
71
|
+
}, [nameOrAddress, node, nodeError, address, filter]);
|
|
54
72
|
return [module, error];
|
|
55
73
|
};
|
|
56
74
|
//# sourceMappingURL=useModule.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useModule.js","sourceRoot":"","sources":["../../../src/hooks/useModule.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAA;AAGrD,OAAO,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAA;
|
|
1
|
+
{"version":3,"file":"useModule.js","sourceRoot":"","sources":["../../../src/hooks/useModule.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAA;AAGrD,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAA;AAEzC,OAAO,EAAE,sBAAsB,EAAE,MAAM,mBAAmB,CAAA;AAE1D,MAAM,CAAC,MAAM,SAAS,GAAG,CACvB,qBAA6C,EACH,EAAE;IAC5C,MAAM,aAAa,GAAG,OAAO,CAAC,GAAG,EAAE,CAAC,CAAC,OAAO,qBAAqB,KAAK,QAAQ,CAAC,CAAC,CAAC,qBAAqB,CAAC,CAAC,CAAC,SAAS,CAAC,EAAE,CAAC,qBAAqB,CAAC,CAAC,CAAA;I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C,aAAa,EAAE,IAAI,EAAE,SAAS,EAAE,OAAO,EAAE,MAAM,CAAC,CAClD,CAAA;IAED,OAAO,CAAC,MAAM,EAAE,KAAK,CAAC,CAAA;AACxB,CAAC,CAAA"}
|
|
@@ -1,17 +1,25 @@
|
|
|
1
1
|
import { NodeWrapper } from '@xyo-network/node';
|
|
2
|
-
import {
|
|
2
|
+
import { useAccount } from '@xyo-network/react-wallet';
|
|
3
|
+
import { useContext, useEffect, useMemo, useState } from 'react';
|
|
3
4
|
import { NodeContext } from '../contexts';
|
|
4
5
|
export const useProvidedNode = () => {
|
|
5
6
|
const { node } = useContext(NodeContext);
|
|
6
7
|
return [node];
|
|
7
8
|
};
|
|
8
9
|
export const useProvidedWrappedNode = (account) => {
|
|
10
|
+
const [providedAccount] = useAccount();
|
|
9
11
|
const [node] = useProvidedNode();
|
|
10
12
|
const [wrappedNode, setWrappedNode] = useState();
|
|
11
13
|
const [error, setError] = useState();
|
|
14
|
+
const accountToUse = useMemo(() => account ?? providedAccount, [account, providedAccount]);
|
|
15
|
+
if (!accountToUse) {
|
|
16
|
+
const error = Error('useProvidedWrappedNode requires either an Account context or account parameter');
|
|
17
|
+
console.error(error.message);
|
|
18
|
+
setError(error);
|
|
19
|
+
}
|
|
12
20
|
useEffect(() => {
|
|
13
21
|
try {
|
|
14
|
-
if (node) {
|
|
22
|
+
if (node && accountToUse) {
|
|
15
23
|
setWrappedNode(NodeWrapper.wrap(node, account));
|
|
16
24
|
setError(undefined);
|
|
17
25
|
}
|
|
@@ -20,7 +28,7 @@ export const useProvidedWrappedNode = (account) => {
|
|
|
20
28
|
setWrappedNode(undefined);
|
|
21
29
|
setError(ex);
|
|
22
30
|
}
|
|
23
|
-
}, [node, account]);
|
|
31
|
+
}, [node, account, accountToUse]);
|
|
24
32
|
return [wrappedNode, error];
|
|
25
33
|
};
|
|
26
34
|
//# sourceMappingURL=useProvidedNode.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useProvidedNode.js","sourceRoot":"","sources":["../../../src/hooks/useProvidedNode.tsx"],"names":[],"mappings":"AACA,OAAO,EAAc,WAAW,EAAE,MAAM,mBAAmB,CAAA;AAC3D,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAA;
|
|
1
|
+
{"version":3,"file":"useProvidedNode.js","sourceRoot":"","sources":["../../../src/hooks/useProvidedNode.tsx"],"names":[],"mappings":"AACA,OAAO,EAAc,WAAW,EAAE,MAAM,mBAAmB,CAAA;AAC3D,OAAO,EAAE,UAAU,EAAE,MAAM,2BAA2B,CAAA;AACtD,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAA;AAEhE,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAA;AAEzC,MAAM,CAAC,MAAM,eAAe,GAAG,GAA6B,EAAE;IAC5D,MAAM,EAAE,IAAI,EAAE,GAAG,UAAU,CAAC,WAAW,CAAC,CAAA;IAExC,OAAO,CAAC,IAAI,CAAC,CAAA;AACf,CAAC,CAAA;AAED,MAAM,CAAC,MAAM,sBAAsB,GAAG,CAAC,OAAyB,EAAgD,EAAE;IAChH,MAAM,CAAC,eAAe,CAAC,GAAG,UAAU,EAAE,CAAA;IACtC,MAAM,CAAC,IAAI,CAAC,GAAG,eAAe,EAAE,CAAA;IAChC,MAAM,CAAC,WAAW,EAAE,cAAc,CAAC,GAAG,QAAQ,EAAe,CAAA;IAC7D,MAAM,CAAC,KAAK,EAAE,QAAQ,CAAC,GAAG,QAAQ,EAAS,CAAA;IAE3C,MAAM,YAAY,GAAG,OAAO,CAAC,GAAG,EAAE,CAAC,OAAO,IAAI,eAAe,EAAE,CAAC,OAAO,EAAE,eAAe,CAAC,CAAC,CAAA;IAE1F,IAAI,CAAC,YAAY,EAAE;QACjB,MAAM,KAAK,GAAG,KAAK,CAAC,gFAAgF,CAAC,CAAA;QACrG,OAAO,CAAC,KAAK,CAAC,KAAK,CAAC,OAAO,CAAC,CAAA;QAC5B,QAAQ,CAAC,KAAK,CAAC,CAAA;KAChB;IAED,SAAS,CAAC,GAAG,EAAE;QACb,IAAI;YACF,IAAI,IAAI,IAAI,YAAY,EAAE;gBACxB,cAAc,CAAC,WAAW,CAAC,IAAI,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC,CAAA;gBAC/C,QAAQ,CAAC,SAAS,CAAC,CAAA;aACpB;SACF;QAAC,OAAO,EAAE,EAAE;YACX,cAAc,CAAC,SAAS,CAAC,CAAA;YACzB,QAAQ,CAAC,EAAW,CAAC,CAAA;SACtB;IACH,CAAC,EAAE,CAAC,IAAI,EAAE,OAAO,EAAE,YAAY,CAAC,CAAC,CAAA;IAEjC,OAAO,CAAC,WAAW,EAAE,KAAK,CAAC,CAAA;AAC7B,CAAC,CAAA"}
|
|
@@ -1,15 +1,25 @@
|
|
|
1
1
|
import { ModuleWrapper } from '@xyo-network/module';
|
|
2
|
-
import {
|
|
2
|
+
import { useAccount } from '@xyo-network/react-wallet';
|
|
3
|
+
import { useEffect, useMemo, useState } from 'react';
|
|
3
4
|
import { useModule } from './useModule';
|
|
4
5
|
export const WrappedModuleHookFactory = (wrapperObject) => {
|
|
5
6
|
return (nameOrAddress, account) => {
|
|
6
|
-
const [
|
|
7
|
+
const [providedAccount] = useAccount();
|
|
8
|
+
const [module, moduleError] = useModule(nameOrAddress ?? {
|
|
9
|
+
query: [wrapperObject.requiredQueries],
|
|
10
|
+
});
|
|
7
11
|
const [wrapper, setWrapper] = useState();
|
|
8
12
|
const [error, setError] = useState();
|
|
13
|
+
const accountToUse = useMemo(() => account ?? providedAccount, [account, providedAccount]);
|
|
14
|
+
if (!accountToUse) {
|
|
15
|
+
const error = Error('Module hooks require either an Account context or account parameter');
|
|
16
|
+
console.error(error.message);
|
|
17
|
+
setError(error);
|
|
18
|
+
}
|
|
9
19
|
useEffect(() => {
|
|
10
|
-
if (module) {
|
|
20
|
+
if (module && accountToUse) {
|
|
11
21
|
try {
|
|
12
|
-
const wrapper = wrapperObject.wrap(module,
|
|
22
|
+
const wrapper = wrapperObject.wrap(module, accountToUse);
|
|
13
23
|
setWrapper(wrapper);
|
|
14
24
|
}
|
|
15
25
|
catch (ex) {
|
|
@@ -21,7 +31,7 @@ export const WrappedModuleHookFactory = (wrapperObject) => {
|
|
|
21
31
|
setWrapper(undefined);
|
|
22
32
|
setError(moduleError);
|
|
23
33
|
}
|
|
24
|
-
}, [module, account, moduleError]);
|
|
34
|
+
}, [module, account, moduleError, accountToUse]);
|
|
25
35
|
return [wrapper, error];
|
|
26
36
|
};
|
|
27
37
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useWrappedModule.js","sourceRoot":"","sources":["../../../src/hooks/useWrappedModule.tsx"],"names":[],"mappings":"AAEA,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAA;AACnD,OAAO,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAA;
|
|
1
|
+
{"version":3,"file":"useWrappedModule.js","sourceRoot":"","sources":["../../../src/hooks/useWrappedModule.tsx"],"names":[],"mappings":"AAEA,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAA;AACnD,OAAO,EAAE,UAAU,EAAE,MAAM,2BAA2B,CAAA;AACtD,OAAO,EAAE,SAAS,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAA;AAEpD,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAA;AAOvC,MAAM,CAAC,MAAM,wBAAwB,GAAG,CAAuD,aAA4C,EAAE,EAAE;IAC7I,OAAO,CAAC,aAAsB,EAAE,OAAyB,EAAmD,EAAE;QAC5G,MAAM,CAAC,eAAe,CAAC,GAAG,UAAU,EAAE,CAAA;QACtC,MAAM,CAAC,MAAM,EAAE,WAAW,CAAC,GAAG,SAAS,CACrC,aAAa,IAAI;YACf,KAAK,EAAE,CAAC,aAAa,CAAC,eAAe,CAAC;SACvC,CACF,CAAA;QAED,MAAM,CAAC,OAAO,EAAE,UAAU,CAAC,GAAG,QAAQ,EAAkB,CAAA;QACxD,MAAM,CAAC,KAAK,EAAE,QAAQ,CAAC,GAAG,QAAQ,EAAS,CAAA;QAE3C,MAAM,YAAY,GAAG,OAAO,CAAC,GAAG,EAAE,CAAC,OAAO,IAAI,eAAe,EAAE,CAAC,OAAO,EAAE,eAAe,CAAC,CAAC,CAAA;QAE1F,IAAI,CAAC,YAAY,EAAE;YACjB,MAAM,KAAK,GAAG,KAAK,CAAC,qEAAqE,CAAC,CAAA;YAC1F,OAAO,CAAC,KAAK,CAAC,KAAK,CAAC,OAAO,CAAC,CAAA;YAC5B,QAAQ,CAAC,KAAK,CAAC,CAAA;SAChB;QAED,SAAS,CAAC,GAAG,EAAE;YACb,IAAI,MAAM,IAAI,YAAY,EAAE;gBAC1B,IAAI;oBACF,MAAM,OAAO,GAAG,aAAa,CAAC,IAAI,CAAC,MAAM,EAAE,YAAY,CAAmB,CAAA;oBAC1E,UAAU,CAAC,OAAO,CAAC,CAAA;iBACpB;gBAAC,OAAO,EAAE,EAAE;oBACX,UAAU,CAAC,SAAS,CAAC,CAAA;oBACrB,QAAQ,CAAC,EAAW,CAAC,CAAA;iBACtB;aACF;iBAAM;gBACL,UAAU,CAAC,SAAS,CAAC,CAAA;gBACrB,QAAQ,CAAC,WAAW,CAAC,CAAA;aACtB;QACH,CAAC,EAAE,CAAC,MAAM,EAAE,OAAO,EAAE,WAAW,EAAE,YAAY,CAAC,CAAC,CAAA;QAEhD,OAAO,CAAC,OAAO,EAAE,KAAK,CAAC,CAAA;IACzB,CAAC,CAAA;AACH,CAAC,CAAA;AAED,MAAM,CAAC,MAAM,gBAAgB,GAAG,wBAAwB,CAAC,aAAa,CAAC,CAAA"}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Module } from '@xyo-network/module-model';
|
|
1
|
+
import { Module, ModuleFilter } from '@xyo-network/module-model';
|
|
2
2
|
export declare const useModule: <TModule extends Module<import("@xyo-network/payload-model").SchemaFields & import("@xyo-network/payload-model").PayloadFields & {
|
|
3
3
|
name?: string | undefined;
|
|
4
4
|
security?: {
|
|
@@ -19,5 +19,5 @@ export declare const useModule: <TModule extends Module<import("@xyo-network/pay
|
|
|
19
19
|
storeQueries?: boolean | undefined;
|
|
20
20
|
} & {
|
|
21
21
|
schema: string;
|
|
22
|
-
}>>(
|
|
22
|
+
}>>(nameOrAddressOrFilter?: string | ModuleFilter) => [TModule | undefined, Error | undefined];
|
|
23
23
|
//# sourceMappingURL=useModule.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useModule.d.ts","sourceRoot":"","sources":["../../../src/hooks/useModule.tsx"],"names":[],"mappings":"AACA,OAAO,EAAE,MAAM,EAAE,MAAM,2BAA2B,CAAA;
|
|
1
|
+
{"version":3,"file":"useModule.d.ts","sourceRoot":"","sources":["../../../src/hooks/useModule.tsx"],"names":[],"mappings":"AACA,OAAO,EAAE,MAAM,EAAE,YAAY,EAAE,MAAM,2BAA2B,CAAA;AAMhE,eAAO,MAAM,SAAS;;;;;;;;;;;;;;;;;;;;4BACI,MAAM,GAAG,YAAY,6CAuE9C,CAAA"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useProvidedNode.d.ts","sourceRoot":"","sources":["../../../src/hooks/useProvidedNode.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,4BAA4B,CAAA;AAC5D,OAAO,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAA;
|
|
1
|
+
{"version":3,"file":"useProvidedNode.d.ts","sourceRoot":"","sources":["../../../src/hooks/useProvidedNode.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,4BAA4B,CAAA;AAC5D,OAAO,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAA;AAM3D,eAAO,MAAM,eAAe,QAAO,CAAC,UAAU,GAAG,SAAS,CAIzD,CAAA;AAED,eAAO,MAAM,sBAAsB,aAAc,eAAe,KAAG,CAAC,WAAW,GAAG,SAAS,EAAE,KAAK,GAAG,SAAS,CA2B7G,CAAA"}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { AccountInstance } from '@xyo-network/account-model';
|
|
2
2
|
import { ModuleWrapper } from '@xyo-network/module';
|
|
3
3
|
export interface WrapperStatic<TModuleWrapper extends ModuleWrapper = ModuleWrapper> {
|
|
4
|
+
requiredQueries: string[];
|
|
4
5
|
wrap: (module?: TModuleWrapper['module'], account?: AccountInstance) => TModuleWrapper;
|
|
5
6
|
}
|
|
6
7
|
export declare const WrappedModuleHookFactory: <TModuleWrapper extends ModuleWrapper<import("@xyo-network/module").Module<import("@xyo-network/payload-model").SchemaFields & import("@xyo-network/payload-model").PayloadFields & {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useWrappedModule.d.ts","sourceRoot":"","sources":["../../../src/hooks/useWrappedModule.tsx"],"names":[],"mappings":"AACA,OAAO,EAAE,eAAe,EAAE,MAAM,4BAA4B,CAAA;AAC5D,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAA;
|
|
1
|
+
{"version":3,"file":"useWrappedModule.d.ts","sourceRoot":"","sources":["../../../src/hooks/useWrappedModule.tsx"],"names":[],"mappings":"AACA,OAAO,EAAE,eAAe,EAAE,MAAM,4BAA4B,CAAA;AAC5D,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAA;AAMnD,MAAM,WAAW,aAAa,CAAC,cAAc,SAAS,aAAa,GAAG,aAAa;IACjF,eAAe,EAAE,MAAM,EAAE,CAAA;IACzB,IAAI,EAAE,CAAC,MAAM,CAAC,EAAE,cAAc,CAAC,QAAQ,CAAC,EAAE,OAAO,CAAC,EAAE,eAAe,KAAK,cAAc,CAAA;CACvF;AAED,eAAO,MAAM,wBAAwB;;;;;;;;;;;;;;;;;;;;uEACX,MAAM,YAAY,eAAe,oDAoC1D,CAAA;AAED,eAAO,MAAM,gBAAgB,mBAtCH,MAAM,YAAY,eAAe;;;;;;;;;;mCAsCY,CAAA"}
|
package/package.json
CHANGED
|
@@ -10,14 +10,15 @@
|
|
|
10
10
|
"url": "https://github.com/XYOracleNetwork/sdk-xyo-react-js/issues"
|
|
11
11
|
},
|
|
12
12
|
"dependencies": {
|
|
13
|
-
"@mui/lab": "^5.0.0-alpha.
|
|
14
|
-
"@xylabs/react-flexbox": "^2.16.
|
|
15
|
-
"@xylabs/react-shared": "^2.16.
|
|
13
|
+
"@mui/lab": "^5.0.0-alpha.122",
|
|
14
|
+
"@xylabs/react-flexbox": "^2.16.9",
|
|
15
|
+
"@xylabs/react-shared": "^2.16.9",
|
|
16
16
|
"@xyo-network/account-model": "^2.51.4",
|
|
17
17
|
"@xyo-network/module": "^2.51.4",
|
|
18
18
|
"@xyo-network/module-model": "^2.51.4",
|
|
19
19
|
"@xyo-network/node": "^2.51.4",
|
|
20
|
-
"@xyo-network/react-shared": "^2.45.0-rc.
|
|
20
|
+
"@xyo-network/react-shared": "^2.45.0-rc.22",
|
|
21
|
+
"@xyo-network/react-wallet": "^2.45.0-rc.22"
|
|
21
22
|
},
|
|
22
23
|
"devDependencies": {
|
|
23
24
|
"@storybook/react": "^6.5.16",
|
|
@@ -78,6 +79,6 @@
|
|
|
78
79
|
},
|
|
79
80
|
"sideEffects": false,
|
|
80
81
|
"types": "dist/types/index.d.ts",
|
|
81
|
-
"version": "2.45.0-rc.
|
|
82
|
+
"version": "2.45.0-rc.22",
|
|
82
83
|
"stableVersion": "2.44.1"
|
|
83
84
|
}
|
package/src/hooks/useModule.tsx
CHANGED
|
@@ -1,41 +1,62 @@
|
|
|
1
1
|
import { useAsyncEffect } from '@xylabs/react-shared'
|
|
2
|
-
import { Module } from '@xyo-network/module-model'
|
|
3
|
-
import { ModuleDetachedEventArgs, ModuleDetachedEventEmitter } from '@xyo-network/node'
|
|
4
|
-
import { useState } from 'react'
|
|
2
|
+
import { Module, ModuleFilter } from '@xyo-network/module-model'
|
|
3
|
+
import { ModuleAttachedEventArgs, ModuleAttachedEventEmitter, ModuleDetachedEventArgs, ModuleDetachedEventEmitter } from '@xyo-network/node'
|
|
4
|
+
import { useMemo, useState } from 'react'
|
|
5
5
|
|
|
6
6
|
import { useProvidedWrappedNode } from './useProvidedNode'
|
|
7
7
|
|
|
8
|
-
export const useModule = <TModule extends Module = Module>(
|
|
8
|
+
export const useModule = <TModule extends Module = Module>(
|
|
9
|
+
nameOrAddressOrFilter?: string | ModuleFilter,
|
|
10
|
+
): [TModule | undefined, Error | undefined] => {
|
|
11
|
+
const nameOrAddress = useMemo(() => (typeof nameOrAddressOrFilter === 'string' ? nameOrAddressOrFilter : undefined), [nameOrAddressOrFilter])
|
|
12
|
+
const filter = useMemo(() => (typeof nameOrAddressOrFilter === 'object' ? nameOrAddressOrFilter : undefined), [nameOrAddressOrFilter])
|
|
9
13
|
const [node, nodeError] = useProvidedWrappedNode()
|
|
10
14
|
const [module, setModule] = useState<TModule>()
|
|
11
15
|
const [error, setError] = useState<Error>()
|
|
16
|
+
|
|
12
17
|
const address = module?.address
|
|
18
|
+
|
|
13
19
|
useAsyncEffect(
|
|
14
20
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
15
21
|
async (mounted) => {
|
|
16
|
-
const detachHandler = (args: ModuleDetachedEventArgs) => {
|
|
17
|
-
const eventModule = args.module
|
|
18
|
-
if (eventModule.address === address && mounted()) {
|
|
19
|
-
setModule(undefined)
|
|
20
|
-
setError(undefined)
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
22
|
try {
|
|
24
23
|
if (nodeError) {
|
|
25
24
|
setError(nodeError)
|
|
26
25
|
setModule(undefined)
|
|
27
26
|
} else {
|
|
28
27
|
if (node) {
|
|
29
|
-
const
|
|
30
|
-
const
|
|
28
|
+
const attachEmitter = node.module as ModuleAttachedEventEmitter
|
|
29
|
+
const detachEmitter = node.module as ModuleDetachedEventEmitter
|
|
30
|
+
const attachHandler = (args: ModuleAttachedEventArgs) => {
|
|
31
|
+
const eventModule = args.module
|
|
32
|
+
if (eventModule.address === nameOrAddress || (module?.config.name === nameOrAddress && mounted())) {
|
|
33
|
+
setModule(eventModule as TModule)
|
|
34
|
+
setError(undefined)
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
const detachHandler = (args: ModuleDetachedEventArgs) => {
|
|
38
|
+
const eventModule = args.module
|
|
39
|
+
if (eventModule.address === address && mounted()) {
|
|
40
|
+
setModule(undefined)
|
|
41
|
+
setError(undefined)
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
const module: TModule | undefined = nameOrAddress
|
|
45
|
+
? await node.resolve<TModule>(nameOrAddress)
|
|
46
|
+
: (await node.resolve<TModule>(filter)).pop()
|
|
31
47
|
if (mounted()) {
|
|
32
|
-
|
|
48
|
+
if (module) {
|
|
49
|
+
detachEmitter.on('moduleDetached', detachHandler)
|
|
50
|
+
} else {
|
|
51
|
+
attachEmitter.on('moduleAttached', attachHandler)
|
|
52
|
+
}
|
|
33
53
|
setModule(module)
|
|
34
54
|
setError(undefined)
|
|
35
55
|
}
|
|
36
56
|
return () => {
|
|
37
57
|
//remove the event handler on unmount
|
|
38
|
-
|
|
58
|
+
attachEmitter.on('moduleAttached', attachHandler, true)
|
|
59
|
+
detachEmitter.on('moduleDetached', detachHandler, true)
|
|
39
60
|
}
|
|
40
61
|
} else {
|
|
41
62
|
console.log('Setting All to undefined')
|
|
@@ -52,7 +73,7 @@ export const useModule = <TModule extends Module = Module>(nameOrAddress?: strin
|
|
|
52
73
|
}
|
|
53
74
|
}
|
|
54
75
|
},
|
|
55
|
-
[nameOrAddress, node, nodeError, address],
|
|
76
|
+
[nameOrAddress, node, nodeError, address, filter],
|
|
56
77
|
)
|
|
57
78
|
|
|
58
79
|
return [module, error]
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { AccountInstance } from '@xyo-network/account-model'
|
|
2
2
|
import { NodeModule, NodeWrapper } from '@xyo-network/node'
|
|
3
|
-
import {
|
|
3
|
+
import { useAccount } from '@xyo-network/react-wallet'
|
|
4
|
+
import { useContext, useEffect, useMemo, useState } from 'react'
|
|
4
5
|
|
|
5
6
|
import { NodeContext } from '../contexts'
|
|
6
7
|
|
|
@@ -11,13 +12,22 @@ export const useProvidedNode = (): [NodeModule | undefined] => {
|
|
|
11
12
|
}
|
|
12
13
|
|
|
13
14
|
export const useProvidedWrappedNode = (account?: AccountInstance): [NodeWrapper | undefined, Error | undefined] => {
|
|
15
|
+
const [providedAccount] = useAccount()
|
|
14
16
|
const [node] = useProvidedNode()
|
|
15
17
|
const [wrappedNode, setWrappedNode] = useState<NodeWrapper>()
|
|
16
18
|
const [error, setError] = useState<Error>()
|
|
17
19
|
|
|
20
|
+
const accountToUse = useMemo(() => account ?? providedAccount, [account, providedAccount])
|
|
21
|
+
|
|
22
|
+
if (!accountToUse) {
|
|
23
|
+
const error = Error('useProvidedWrappedNode requires either an Account context or account parameter')
|
|
24
|
+
console.error(error.message)
|
|
25
|
+
setError(error)
|
|
26
|
+
}
|
|
27
|
+
|
|
18
28
|
useEffect(() => {
|
|
19
29
|
try {
|
|
20
|
-
if (node) {
|
|
30
|
+
if (node && accountToUse) {
|
|
21
31
|
setWrappedNode(NodeWrapper.wrap(node, account))
|
|
22
32
|
setError(undefined)
|
|
23
33
|
}
|
|
@@ -25,7 +35,7 @@ export const useProvidedWrappedNode = (account?: AccountInstance): [NodeWrapper
|
|
|
25
35
|
setWrappedNode(undefined)
|
|
26
36
|
setError(ex as Error)
|
|
27
37
|
}
|
|
28
|
-
}, [node, account])
|
|
38
|
+
}, [node, account, accountToUse])
|
|
29
39
|
|
|
30
40
|
return [wrappedNode, error]
|
|
31
41
|
}
|
|
@@ -1,24 +1,40 @@
|
|
|
1
1
|
/* eslint-disable react-hooks/rules-of-hooks */
|
|
2
2
|
import { AccountInstance } from '@xyo-network/account-model'
|
|
3
3
|
import { ModuleWrapper } from '@xyo-network/module'
|
|
4
|
-
import {
|
|
4
|
+
import { useAccount } from '@xyo-network/react-wallet'
|
|
5
|
+
import { useEffect, useMemo, useState } from 'react'
|
|
5
6
|
|
|
6
7
|
import { useModule } from './useModule'
|
|
7
8
|
|
|
8
9
|
export interface WrapperStatic<TModuleWrapper extends ModuleWrapper = ModuleWrapper> {
|
|
10
|
+
requiredQueries: string[]
|
|
9
11
|
wrap: (module?: TModuleWrapper['module'], account?: AccountInstance) => TModuleWrapper
|
|
10
12
|
}
|
|
11
13
|
|
|
12
14
|
export const WrappedModuleHookFactory = <TModuleWrapper extends ModuleWrapper = ModuleWrapper>(wrapperObject: WrapperStatic<TModuleWrapper>) => {
|
|
13
15
|
return (nameOrAddress?: string, account?: AccountInstance): [TModuleWrapper | undefined, Error | undefined] => {
|
|
14
|
-
const [
|
|
16
|
+
const [providedAccount] = useAccount()
|
|
17
|
+
const [module, moduleError] = useModule<TModuleWrapper['module']>(
|
|
18
|
+
nameOrAddress ?? {
|
|
19
|
+
query: [wrapperObject.requiredQueries],
|
|
20
|
+
},
|
|
21
|
+
)
|
|
15
22
|
|
|
16
23
|
const [wrapper, setWrapper] = useState<TModuleWrapper>()
|
|
17
24
|
const [error, setError] = useState<Error>()
|
|
25
|
+
|
|
26
|
+
const accountToUse = useMemo(() => account ?? providedAccount, [account, providedAccount])
|
|
27
|
+
|
|
28
|
+
if (!accountToUse) {
|
|
29
|
+
const error = Error('Module hooks require either an Account context or account parameter')
|
|
30
|
+
console.error(error.message)
|
|
31
|
+
setError(error)
|
|
32
|
+
}
|
|
33
|
+
|
|
18
34
|
useEffect(() => {
|
|
19
|
-
if (module) {
|
|
35
|
+
if (module && accountToUse) {
|
|
20
36
|
try {
|
|
21
|
-
const wrapper = wrapperObject.wrap(module,
|
|
37
|
+
const wrapper = wrapperObject.wrap(module, accountToUse) as TModuleWrapper
|
|
22
38
|
setWrapper(wrapper)
|
|
23
39
|
} catch (ex) {
|
|
24
40
|
setWrapper(undefined)
|
|
@@ -28,7 +44,7 @@ export const WrappedModuleHookFactory = <TModuleWrapper extends ModuleWrapper =
|
|
|
28
44
|
setWrapper(undefined)
|
|
29
45
|
setError(moduleError)
|
|
30
46
|
}
|
|
31
|
-
}, [module, account, moduleError])
|
|
47
|
+
}, [module, account, moduleError, accountToUse])
|
|
32
48
|
|
|
33
49
|
return [wrapper, error]
|
|
34
50
|
}
|