@xyo-network/react-node 7.5.7 → 7.5.11
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/browser/components/render/ModuleDescriptionBox.d.ts.map +1 -1
- package/dist/browser/components/render/RenderModule.d.ts +0 -5
- package/dist/browser/components/render/RenderModule.d.ts.map +1 -1
- package/dist/browser/hooks/getModuleFilterOptions.d.ts +1 -1
- package/dist/browser/hooks/getWeakModuleFilterOptions.d.ts +1 -1
- package/dist/browser/index.mjs +156 -282
- package/dist/browser/index.mjs.map +1 -1
- package/package.json +121 -42
- package/src/components/Node.stories.tsx +0 -67
- package/src/components/Node.tsx +0 -20
- package/src/components/NodeDescriptionBox.tsx +0 -20
- package/src/components/NodeDrawer.stories.tsx +0 -45
- package/src/components/NodeDrawer.tsx +0 -35
- package/src/components/NodeModulesBox.tsx +0 -23
- package/src/components/TestModule.ts +0 -20
- package/src/components/index.ts +0 -2
- package/src/components/render/ModuleDescriptionBox.tsx +0 -41
- package/src/components/render/RenderModule.tsx +0 -64
- package/src/components/render/index.ts +0 -2
- package/src/hooks/ModuleFromNodeConfig.ts +0 -12
- package/src/hooks/WeakModuleFromNodeConfig.ts +0 -12
- package/src/hooks/getModuleFilterOptions.ts +0 -10
- package/src/hooks/getWeakModuleFilterOptions.ts +0 -10
- package/src/hooks/index.ts +0 -17
- package/src/hooks/provided/index.ts +0 -2
- package/src/hooks/provided/useProvidedNode.tsx +0 -8
- package/src/hooks/provided/useWeakProvidedNode.tsx +0 -9
- package/src/hooks/useModuleFromNode.ts +0 -51
- package/src/hooks/useModulesFromNode.ts +0 -39
- package/src/hooks/useNode.ts +0 -18
- package/src/hooks/useNodeConfigNodeField.ts +0 -10
- package/src/hooks/useNodeDescription.ts +0 -16
- package/src/hooks/useNodeFromNode.tsx +0 -20
- package/src/hooks/useNodesFromNode.tsx +0 -26
- package/src/hooks/useWeakModuleFromNode.ts +0 -51
- package/src/hooks/useWeakModulesFromNode.ts +0 -42
- package/src/hooks/useWeakNode.ts +0 -20
- package/src/hooks/useWeakNodeConfigNodeField.ts +0 -12
- package/src/hooks/useWeakNodeDescription.ts +0 -16
- package/src/hooks/useWeakNodeFromNode.tsx +0 -12
- package/src/hooks/useWeakNodesFromNode.tsx +0 -26
- package/src/index.ts +0 -4
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import { AbstractModuleInstance } from '@xyo-network/module-abstract'
|
|
2
|
-
import type { ModuleConfig, ModuleParams } from '@xyo-network/module-model'
|
|
3
|
-
import {
|
|
4
|
-
asSchema, type Query, type Schema,
|
|
5
|
-
} from '@xyo-network/payload-model'
|
|
6
|
-
|
|
7
|
-
export const TestModuleConfigSchema = asSchema('network.xyo.test.module', true)
|
|
8
|
-
export type TestModuleConfigSchema = typeof TestModuleConfigSchema
|
|
9
|
-
|
|
10
|
-
export class TestModule extends AbstractModuleInstance<ModuleParams<ModuleConfig<{ schema: TestModuleConfigSchema }>>> {
|
|
11
|
-
static override readonly configSchemas: Schema[] = [...super.configSchemas, TestModuleConfigSchema]
|
|
12
|
-
static override readonly defaultConfigSchema: Schema = TestModuleConfigSchema
|
|
13
|
-
get _queryAccountPaths(): Record<Query['schema'], string> {
|
|
14
|
-
return {}
|
|
15
|
-
}
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
export const init = async () => {
|
|
19
|
-
await TestModule.create({ config: { name: 'TestModule', schema: TestModuleConfigSchema } })
|
|
20
|
-
}
|
package/src/components/index.ts
DELETED
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
import { ChevronRight as ChevronRightIcon, ExpandMore as ExpandMoreIcon } from '@mui/icons-material'
|
|
2
|
-
import { styled, Typography } from '@mui/material'
|
|
3
|
-
import { SimpleTreeView } from '@mui/x-tree-view'
|
|
4
|
-
import type { FlexBoxProps } from '@xylabs/react-flexbox'
|
|
5
|
-
import type { ModuleInstance } from '@xyo-network/module-model'
|
|
6
|
-
import React, { useRef } from 'react'
|
|
7
|
-
|
|
8
|
-
import { RenderModule } from './RenderModule.tsx'
|
|
9
|
-
|
|
10
|
-
export interface ModuleDescriptionProps extends FlexBoxProps {
|
|
11
|
-
mod?: WeakRef<ModuleInstance> | null
|
|
12
|
-
}
|
|
13
|
-
export const ModuleDescriptionBox: React.FC<ModuleDescriptionProps> = ({ mod }) => {
|
|
14
|
-
const idRef = useRef<{ idIncrementor: number; ids: string[] }>({ idIncrementor: 0, ids: [] })
|
|
15
|
-
|
|
16
|
-
return (
|
|
17
|
-
<>
|
|
18
|
-
{mod
|
|
19
|
-
? (
|
|
20
|
-
<>
|
|
21
|
-
<Typography variant="h4">Node Configuration</Typography>
|
|
22
|
-
<StyledTreeView
|
|
23
|
-
aria-label="file system navigator"
|
|
24
|
-
slots={{ collapseIcon: ExpandMoreIcon, expandIcon: ChevronRightIcon }}
|
|
25
|
-
sx={{ flexGrow: 1 }}
|
|
26
|
-
>
|
|
27
|
-
<RenderModule mod={mod} idRef={idRef} />
|
|
28
|
-
</StyledTreeView>
|
|
29
|
-
</>
|
|
30
|
-
)
|
|
31
|
-
: mod === null
|
|
32
|
-
? <Typography variant="h4">Node loading...</Typography>
|
|
33
|
-
: <Typography variant="h4">Node not found</Typography>}
|
|
34
|
-
</>
|
|
35
|
-
)
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
const StyledTreeView = styled(SimpleTreeView, { name: 'StyledTreeView' })(() => ({
|
|
39
|
-
height: 'auto',
|
|
40
|
-
maxWidth: 'auto',
|
|
41
|
-
}))
|
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
import { styled } from '@mui/material'
|
|
2
|
-
import { TreeItem } from '@mui/x-tree-view'
|
|
3
|
-
import { useAsyncEffect } from '@xylabs/react-async-effect'
|
|
4
|
-
import type { ModuleInstance } from '@xyo-network/module-model'
|
|
5
|
-
import type { RefObject } from 'react'
|
|
6
|
-
import React, { useState } from 'react'
|
|
7
|
-
|
|
8
|
-
interface RenderModuleProps {
|
|
9
|
-
idRef: RefObject<{
|
|
10
|
-
idIncrementor: number
|
|
11
|
-
ids: string[]
|
|
12
|
-
}>
|
|
13
|
-
mod: WeakRef<ModuleInstance>
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
export const RenderModule: React.FC<RenderModuleProps> = ({ mod, idRef }) => {
|
|
17
|
-
const [childModules, setChildModules] = useState<WeakRef<ModuleInstance>[]>()
|
|
18
|
-
|
|
19
|
-
useAsyncEffect(
|
|
20
|
-
|
|
21
|
-
async (mounted) => {
|
|
22
|
-
const moduleInstance = mod.deref()
|
|
23
|
-
const { address } = moduleInstance ?? {}
|
|
24
|
-
if (moduleInstance) {
|
|
25
|
-
const children = (await moduleInstance.resolve('*')).filter(childModule => childModule.address !== address)
|
|
26
|
-
if (mounted()) {
|
|
27
|
-
setChildModules(children.map(childModule => new WeakRef(childModule)))
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
},
|
|
31
|
-
[mod],
|
|
32
|
-
)
|
|
33
|
-
|
|
34
|
-
const increment = () => {
|
|
35
|
-
const newId = (idRef.current.idIncrementor++).toString()
|
|
36
|
-
idRef.current.ids.push(newId)
|
|
37
|
-
return newId
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
const moduleInstance = mod.deref()
|
|
41
|
-
const { queries, address } = moduleInstance ?? {}
|
|
42
|
-
|
|
43
|
-
return (
|
|
44
|
-
<StyledAddressTreeItem itemId={increment()} label={`address: ${address}`}>
|
|
45
|
-
{queries?.map((query, index) => {
|
|
46
|
-
return <TreeItem key={query} itemId={increment()} label={`query : ${query}`} sx={{ mb: index === queries.length - 1 ? 1.5 : 0.5 }} />
|
|
47
|
-
})}
|
|
48
|
-
{childModules && childModules.length > 0
|
|
49
|
-
? (
|
|
50
|
-
<TreeItem itemId={increment()} label="children" sx={{ mb: 0.5 }}>
|
|
51
|
-
{childModules.map((childModuleRef) => {
|
|
52
|
-
const childModule = childModuleRef.deref()
|
|
53
|
-
return childModule ? <RenderModule key={childModule?.address} mod={childModuleRef} idRef={idRef} /> : null
|
|
54
|
-
})}
|
|
55
|
-
</TreeItem>
|
|
56
|
-
)
|
|
57
|
-
: null}
|
|
58
|
-
</StyledAddressTreeItem>
|
|
59
|
-
)
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
const StyledAddressTreeItem = styled(TreeItem, { name: 'StyledAddressTreeItem' })(({ theme }) => (
|
|
63
|
-
{ '& .MuiTreeItem-content': { marginBottom: theme.spacing(0.25) } }
|
|
64
|
-
))
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import type { Logger } from '@xylabs/sdk-js'
|
|
2
|
-
import type { ModuleFilterOptions, ModuleInstance } from '@xyo-network/module-model'
|
|
3
|
-
import type { NodeInstance } from '@xyo-network/node-model'
|
|
4
|
-
|
|
5
|
-
export type ModuleFromNodeConfig<T extends ModuleInstance = ModuleInstance> = ModuleFilterOptions<T> & {
|
|
6
|
-
logger?: Logger
|
|
7
|
-
node?: NodeInstance | string
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
export const getModuleFromNodeConfigLogger = (config?: ModuleFromNodeConfig) => {
|
|
11
|
-
return config?.logger
|
|
12
|
-
}
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import type { Logger } from '@xylabs/sdk-js'
|
|
2
|
-
import type { ModuleFilterOptions } from '@xyo-network/module-model'
|
|
3
|
-
import type { NodeInstance } from '@xyo-network/node-model'
|
|
4
|
-
|
|
5
|
-
export type WeakModuleFromNodeConfig = ModuleFilterOptions & {
|
|
6
|
-
logger?: Logger
|
|
7
|
-
node?: WeakRef<NodeInstance> | string
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
export const getWeakModuleFromNodeConfigLogger = (config?: WeakModuleFromNodeConfig) => {
|
|
11
|
-
return config?.logger
|
|
12
|
-
}
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import type { ModuleFromNodeConfig } from './ModuleFromNodeConfig.ts'
|
|
2
|
-
|
|
3
|
-
export const getModuleFilterOptions = (config?: ModuleFromNodeConfig) => {
|
|
4
|
-
const {
|
|
5
|
-
direction, identity, maxDepth,
|
|
6
|
-
} = config ?? {}
|
|
7
|
-
return {
|
|
8
|
-
direction, identity, maxDepth,
|
|
9
|
-
}
|
|
10
|
-
}
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import type { WeakModuleFromNodeConfig } from './WeakModuleFromNodeConfig.ts'
|
|
2
|
-
|
|
3
|
-
export const getWeakModuleFilterOptions = (config?: WeakModuleFromNodeConfig) => {
|
|
4
|
-
const {
|
|
5
|
-
direction, identity, maxDepth,
|
|
6
|
-
} = config ?? {}
|
|
7
|
-
return {
|
|
8
|
-
direction, identity, maxDepth,
|
|
9
|
-
}
|
|
10
|
-
}
|
package/src/hooks/index.ts
DELETED
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
export * from './getModuleFilterOptions.ts'
|
|
2
|
-
export * from './getWeakModuleFilterOptions.ts'
|
|
3
|
-
export * from './ModuleFromNodeConfig.ts'
|
|
4
|
-
export * from './provided/index.ts'
|
|
5
|
-
export * from './useModuleFromNode.ts'
|
|
6
|
-
export * from './useModulesFromNode.ts'
|
|
7
|
-
export * from './useNode.ts'
|
|
8
|
-
export * from './useNodeConfigNodeField.ts'
|
|
9
|
-
export * from './useNodeDescription.ts'
|
|
10
|
-
export * from './useNodeFromNode.tsx'
|
|
11
|
-
export * from './useNodesFromNode.tsx'
|
|
12
|
-
export * from './useWeakModuleFromNode.ts'
|
|
13
|
-
export * from './useWeakModulesFromNode.ts'
|
|
14
|
-
export * from './useWeakNodeConfigNodeField.ts'
|
|
15
|
-
export * from './useWeakNodeDescription.ts'
|
|
16
|
-
export * from './useWeakNodeFromNode.tsx'
|
|
17
|
-
export * from './WeakModuleFromNodeConfig.ts'
|
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import type { NodeInstance } from '@xyo-network/node-model'
|
|
2
|
-
import { NodeContext } from '@xyo-network/react-node-context'
|
|
3
|
-
import { use } from 'react'
|
|
4
|
-
|
|
5
|
-
export const useProvidedNode = (): [NodeInstance | null | undefined] => {
|
|
6
|
-
const { node } = use(NodeContext)
|
|
7
|
-
return [node]
|
|
8
|
-
}
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import type { NodeInstance } from '@xyo-network/node-model'
|
|
2
|
-
import { NodeContext } from '@xyo-network/react-node-context'
|
|
3
|
-
import { use, useMemo } from 'react'
|
|
4
|
-
|
|
5
|
-
export const useWeakProvidedNode = (): [WeakRef<NodeInstance> | null | undefined] => {
|
|
6
|
-
const { node } = use(NodeContext)
|
|
7
|
-
const weakNode = useMemo(() => (node ? new WeakRef(node) : null), [node])
|
|
8
|
-
return [weakNode]
|
|
9
|
-
}
|
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
import { usePromise } from '@xylabs/react-promise'
|
|
2
|
-
import type { TypeCheck } from '@xylabs/sdk-js'
|
|
3
|
-
import type { ModuleInstance } from '@xyo-network/module-model'
|
|
4
|
-
import { isModuleInstance } from '@xyo-network/module-model'
|
|
5
|
-
import { useState } from 'react'
|
|
6
|
-
|
|
7
|
-
import { getModuleFilterOptions } from './getModuleFilterOptions.ts'
|
|
8
|
-
import type { ModuleFromNodeConfig } from './ModuleFromNodeConfig.ts'
|
|
9
|
-
import { getModuleFromNodeConfigLogger } from './ModuleFromNodeConfig.ts'
|
|
10
|
-
import { useNode } from './useNode.ts'
|
|
11
|
-
|
|
12
|
-
/** @deprecated use useWeakModuleFromNode */
|
|
13
|
-
export const useModuleFromNode = <T extends ModuleInstance | void = void>(
|
|
14
|
-
nameOrAddressOrInstance: string | (T extends ModuleInstance ? T : ModuleInstance) | undefined = undefined,
|
|
15
|
-
config?: T extends ModuleInstance ? ModuleFromNodeConfig<T> : ModuleFromNodeConfig | undefined,
|
|
16
|
-
): [(T extends ModuleInstance ? T : ModuleInstance) | undefined, Error | undefined] => {
|
|
17
|
-
const [node, nodeError] = useNode(config)
|
|
18
|
-
const logger = getModuleFromNodeConfigLogger(config)
|
|
19
|
-
const filterOptions = getModuleFilterOptions(config)
|
|
20
|
-
const [result, setResult] = useState<(T extends ModuleInstance ? T : ModuleInstance) | undefined>()
|
|
21
|
-
const [, error] = usePromise(async () => {
|
|
22
|
-
logger?.debug('useModuleFromNode: resolving')
|
|
23
|
-
const identity: TypeCheck<T extends ModuleInstance ? T : ModuleInstance> = (config?.identity ?? isModuleInstance) as TypeCheck<
|
|
24
|
-
T extends ModuleInstance ? T : ModuleInstance
|
|
25
|
-
>
|
|
26
|
-
if (node && nameOrAddressOrInstance) {
|
|
27
|
-
node.on('moduleAttached', ({ mod }) => {
|
|
28
|
-
logger?.debug(`useModuleFromNode: moduleAttached [${mod.config.name ?? mod.address}]`)
|
|
29
|
-
if (mod.address === nameOrAddressOrInstance || mod.config?.name === nameOrAddressOrInstance) {
|
|
30
|
-
setResult(identity(mod) ? (mod as T extends ModuleInstance ? T : ModuleInstance) : undefined)
|
|
31
|
-
}
|
|
32
|
-
})
|
|
33
|
-
node.on('moduleDetached', ({ mod }) => {
|
|
34
|
-
logger?.debug(`useModuleFromNode: moduleDetached [${mod.config.name ?? mod.address}]`)
|
|
35
|
-
if (mod.address === nameOrAddressOrInstance || mod.config?.name === nameOrAddressOrInstance) {
|
|
36
|
-
setResult(undefined)
|
|
37
|
-
}
|
|
38
|
-
})
|
|
39
|
-
if (identity(nameOrAddressOrInstance)) {
|
|
40
|
-
setResult(nameOrAddressOrInstance)
|
|
41
|
-
} else {
|
|
42
|
-
const result = await node.resolve(nameOrAddressOrInstance, filterOptions)
|
|
43
|
-
logger?.debug(`Result: ${result?.address}`)
|
|
44
|
-
setResult(identity(result) ? (result as T extends ModuleInstance ? T : ModuleInstance) : undefined)
|
|
45
|
-
}
|
|
46
|
-
return result
|
|
47
|
-
}
|
|
48
|
-
logger?.debug('Result: No Node')
|
|
49
|
-
}, [node, nameOrAddressOrInstance])
|
|
50
|
-
return [result, nodeError ?? error]
|
|
51
|
-
}
|
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
import { usePromise } from '@xylabs/react-promise'
|
|
2
|
-
import { exists } from '@xylabs/sdk-js'
|
|
3
|
-
import type { ModuleIdentifier, ModuleInstance } from '@xyo-network/module-model'
|
|
4
|
-
import { useState } from 'react'
|
|
5
|
-
|
|
6
|
-
import { getModuleFilterOptions } from './getModuleFilterOptions.ts'
|
|
7
|
-
import type { ModuleFromNodeConfig } from './ModuleFromNodeConfig.ts'
|
|
8
|
-
import { useNode } from './useNode.ts'
|
|
9
|
-
|
|
10
|
-
/** @deprecated use useWeakModulesFromNode */
|
|
11
|
-
export const useModulesFromNode = (ids?: ModuleIdentifier[], config?: ModuleFromNodeConfig): [ModuleInstance[] | undefined, Error | undefined] => {
|
|
12
|
-
const [node, nodeError] = useNode(config)
|
|
13
|
-
const logger = config?.logger
|
|
14
|
-
const filterOptions = getModuleFilterOptions()
|
|
15
|
-
const [result, setResult] = useState<ModuleInstance[] | undefined>()
|
|
16
|
-
const [, error] = usePromise(async () => {
|
|
17
|
-
logger?.debug('useModuleFromNode: resolving')
|
|
18
|
-
if (node) {
|
|
19
|
-
const refreshModules = async () => {
|
|
20
|
-
const moduleInstances = ids
|
|
21
|
-
? (await Promise.all(ids.map(id => node.resolve(id, filterOptions)))).filter(exists)
|
|
22
|
-
: await node.resolve('*', filterOptions)
|
|
23
|
-
setResult(moduleInstances)
|
|
24
|
-
return moduleInstances
|
|
25
|
-
}
|
|
26
|
-
node.on('moduleAttached', async ({ mod }) => {
|
|
27
|
-
logger?.debug(`useModuleFromNode: moduleAttached [${mod.config.name ?? mod.address}]`)
|
|
28
|
-
await refreshModules()
|
|
29
|
-
})
|
|
30
|
-
node.on('moduleDetached', async ({ mod }) => {
|
|
31
|
-
logger?.debug(`useModuleFromNode: moduleDetached [${mod.config.name ?? mod.address}]`)
|
|
32
|
-
await refreshModules()
|
|
33
|
-
})
|
|
34
|
-
return await refreshModules()
|
|
35
|
-
}
|
|
36
|
-
console.log('Result: No Node')
|
|
37
|
-
}, [node, ids])
|
|
38
|
-
return [result, nodeError ?? error]
|
|
39
|
-
}
|
package/src/hooks/useNode.ts
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import { usePromise } from '@xylabs/react-promise'
|
|
2
|
-
import type { NodeInstance } from '@xyo-network/node-model'
|
|
3
|
-
import { asNodeInstance } from '@xyo-network/node-model'
|
|
4
|
-
|
|
5
|
-
import type { ModuleFromNodeConfig } from './ModuleFromNodeConfig.ts'
|
|
6
|
-
import { useProvidedNode } from './provided/index.ts'
|
|
7
|
-
import { useNodeConfigNodeField } from './useNodeConfigNodeField.ts'
|
|
8
|
-
|
|
9
|
-
export const useNode = (config?: ModuleFromNodeConfig | undefined): [NodeInstance | undefined, Error | undefined] => {
|
|
10
|
-
const [nodeAddress, nodeInstance] = useNodeConfigNodeField(config)
|
|
11
|
-
const [providedNode] = useProvidedNode()
|
|
12
|
-
const [nodeAddressNode, error] = usePromise(async () => {
|
|
13
|
-
if (providedNode && nodeAddress) {
|
|
14
|
-
return asNodeInstance(await providedNode.resolve(nodeAddress), 'Module is not a node')
|
|
15
|
-
}
|
|
16
|
-
}, [providedNode, nodeAddress])
|
|
17
|
-
return [nodeAddressNode ?? nodeInstance ?? providedNode ?? undefined, error]
|
|
18
|
-
}
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import type { NodeInstance } from '@xyo-network/node-model'
|
|
2
|
-
import { useMemo } from 'react'
|
|
3
|
-
|
|
4
|
-
import type { ModuleFromNodeConfig } from './ModuleFromNodeConfig.ts'
|
|
5
|
-
|
|
6
|
-
export const useNodeConfigNodeField = (config?: ModuleFromNodeConfig | undefined): [string | undefined, NodeInstance | undefined] => {
|
|
7
|
-
const nodeAddress = useMemo(() => (typeof config?.node === 'string' ? config?.node : undefined), [config?.node])
|
|
8
|
-
const nodeInstance = useMemo(() => (typeof config?.node === 'object' ? config?.node : undefined), [config?.node])
|
|
9
|
-
return [nodeAddress, nodeInstance]
|
|
10
|
-
}
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import { usePromise } from '@xylabs/react-promise'
|
|
2
|
-
import type { ModuleDescription, ModuleDescriptionPayload } from '@xyo-network/module-model'
|
|
3
|
-
import { ModuleDescriptionSchema } from '@xyo-network/module-model'
|
|
4
|
-
import { isPayloadOfSchemaType } from '@xyo-network/payload-model'
|
|
5
|
-
|
|
6
|
-
import type { ModuleFromNodeConfig } from './ModuleFromNodeConfig.ts'
|
|
7
|
-
import { useNode } from './useNode.ts'
|
|
8
|
-
|
|
9
|
-
export const useNodeDescription = (config?: ModuleFromNodeConfig | undefined): [ModuleDescription | undefined, Error | undefined] => {
|
|
10
|
-
const [activeNode, nodeError] = useNode(config)
|
|
11
|
-
const [description, error] = usePromise(async () => {
|
|
12
|
-
const state = await activeNode?.state()
|
|
13
|
-
return state?.find<ModuleDescriptionPayload>(isPayloadOfSchemaType(ModuleDescriptionSchema))
|
|
14
|
-
}, [activeNode])
|
|
15
|
-
return [description, nodeError ?? error]
|
|
16
|
-
}
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import type { NodeInstance } from '@xyo-network/node-model'
|
|
2
|
-
import { asNodeInstance } from '@xyo-network/node-model'
|
|
3
|
-
|
|
4
|
-
import type { ModuleFromNodeConfig } from './ModuleFromNodeConfig.ts'
|
|
5
|
-
import { useModuleFromNode } from './useModuleFromNode.ts'
|
|
6
|
-
|
|
7
|
-
/** @deprecated use useWeakNodeFromNode instead */
|
|
8
|
-
export const useNodeFromNode = (
|
|
9
|
-
nameOrAddressOrInstance?: string | NodeInstance,
|
|
10
|
-
config?: ModuleFromNodeConfig,
|
|
11
|
-
): [NodeInstance | undefined, Error | undefined] => {
|
|
12
|
-
const [mod, error] = useModuleFromNode(nameOrAddressOrInstance, config)
|
|
13
|
-
const instance = asNodeInstance(mod)
|
|
14
|
-
if (mod && !instance) {
|
|
15
|
-
const error = new Error(`Resolved module is not a NodeInstance [${mod.config?.schema}:${mod.config?.name}:${mod.address}]`)
|
|
16
|
-
console.error(error.message)
|
|
17
|
-
return [undefined, error]
|
|
18
|
-
}
|
|
19
|
-
return [instance, error]
|
|
20
|
-
}
|
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
import type { ModuleIdentifier } from '@xyo-network/module-model'
|
|
2
|
-
import type { NodeInstance } from '@xyo-network/node-model'
|
|
3
|
-
import { isNodeInstance } from '@xyo-network/node-model'
|
|
4
|
-
|
|
5
|
-
import type { ModuleFromNodeConfig } from './ModuleFromNodeConfig.ts'
|
|
6
|
-
import { useModulesFromNode } from './useModulesFromNode.ts'
|
|
7
|
-
|
|
8
|
-
/** @deprecated use useWeakNodesFromNode */
|
|
9
|
-
export const useNodesFromNode = (ids?: ModuleIdentifier[], config?: ModuleFromNodeConfig): [NodeInstance[] | null | undefined, Error | undefined] => {
|
|
10
|
-
const [modules, error] = useModulesFromNode(ids, config)
|
|
11
|
-
if (error) {
|
|
12
|
-
return [null, error]
|
|
13
|
-
}
|
|
14
|
-
return modules
|
|
15
|
-
? [
|
|
16
|
-
// eslint-disable-next-line unicorn/no-array-reduce
|
|
17
|
-
modules.reduce<NodeInstance[]>((prev, mod) => {
|
|
18
|
-
if (isNodeInstance(mod)) {
|
|
19
|
-
prev.push(mod)
|
|
20
|
-
}
|
|
21
|
-
return prev
|
|
22
|
-
}, []),
|
|
23
|
-
undefined,
|
|
24
|
-
]
|
|
25
|
-
: [modules, error]
|
|
26
|
-
}
|
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
import { usePromise } from '@xylabs/react-promise'
|
|
2
|
-
import type { TypeCheck } from '@xylabs/sdk-js'
|
|
3
|
-
import type { ModuleInstance } from '@xyo-network/module-model'
|
|
4
|
-
import { isModuleInstance } from '@xyo-network/module-model'
|
|
5
|
-
import { useState } from 'react'
|
|
6
|
-
|
|
7
|
-
import { getWeakModuleFilterOptions } from './getWeakModuleFilterOptions.ts'
|
|
8
|
-
import { useWeakNode } from './useWeakNode.ts'
|
|
9
|
-
import type { WeakModuleFromNodeConfig } from './WeakModuleFromNodeConfig.ts'
|
|
10
|
-
import { getWeakModuleFromNodeConfigLogger } from './WeakModuleFromNodeConfig.ts'
|
|
11
|
-
|
|
12
|
-
export const useWeakModuleFromNode = <T extends ModuleInstance | void = void>(
|
|
13
|
-
nameOrAddressOrInstance: string | (T extends ModuleInstance ? T : ModuleInstance) | undefined = undefined,
|
|
14
|
-
config?: WeakModuleFromNodeConfig,
|
|
15
|
-
): [WeakRef<T extends ModuleInstance ? T : ModuleInstance> | undefined, Error | undefined] => {
|
|
16
|
-
const [node, nodeError] = useWeakNode(config)
|
|
17
|
-
const logger = getWeakModuleFromNodeConfigLogger(config)
|
|
18
|
-
const filterOptions = getWeakModuleFilterOptions(config)
|
|
19
|
-
const [result, setResult] = useState<WeakRef<T extends ModuleInstance ? T : ModuleInstance> | undefined>()
|
|
20
|
-
const [, error] = usePromise(async () => {
|
|
21
|
-
logger?.debug('useModuleFromNode: resolving')
|
|
22
|
-
const identity: TypeCheck<T extends ModuleInstance ? T : ModuleInstance> = (config?.identity ?? isModuleInstance) as TypeCheck<
|
|
23
|
-
T extends ModuleInstance ? T : ModuleInstance
|
|
24
|
-
>
|
|
25
|
-
const nodeInstance = node?.deref()
|
|
26
|
-
if (nodeInstance && nameOrAddressOrInstance) {
|
|
27
|
-
nodeInstance.on('moduleAttached', ({ mod }) => {
|
|
28
|
-
logger?.debug(`useModuleFromNode: moduleAttached [${mod.config.name ?? mod.address}]`)
|
|
29
|
-
if (mod.address === nameOrAddressOrInstance || mod.config?.name === nameOrAddressOrInstance) {
|
|
30
|
-
setResult(identity(mod) ? new WeakRef(mod as T extends ModuleInstance ? T : ModuleInstance) : undefined)
|
|
31
|
-
}
|
|
32
|
-
})
|
|
33
|
-
nodeInstance.on('moduleDetached', ({ mod }) => {
|
|
34
|
-
logger?.debug(`useModuleFromNode: moduleDetached [${mod.config.name ?? mod.address}]`)
|
|
35
|
-
if (mod.address === nameOrAddressOrInstance || mod.config?.name === nameOrAddressOrInstance) {
|
|
36
|
-
setResult(undefined)
|
|
37
|
-
}
|
|
38
|
-
})
|
|
39
|
-
if (identity(nameOrAddressOrInstance)) {
|
|
40
|
-
setResult(new WeakRef(nameOrAddressOrInstance as T extends ModuleInstance ? T : ModuleInstance))
|
|
41
|
-
} else {
|
|
42
|
-
const result = await nodeInstance.resolve(nameOrAddressOrInstance, filterOptions)
|
|
43
|
-
logger?.debug(`Result: ${result?.address}`)
|
|
44
|
-
setResult(identity(result) ? new WeakRef(result as T extends ModuleInstance ? T : ModuleInstance) : undefined)
|
|
45
|
-
}
|
|
46
|
-
return result
|
|
47
|
-
}
|
|
48
|
-
logger?.debug('Result: No Node')
|
|
49
|
-
}, [node, nameOrAddressOrInstance])
|
|
50
|
-
return [result, nodeError ?? error]
|
|
51
|
-
}
|
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
import { usePromise } from '@xylabs/react-promise'
|
|
2
|
-
import { exists } from '@xylabs/sdk-js'
|
|
3
|
-
import type { ModuleIdentifier, ModuleInstance } from '@xyo-network/module-model'
|
|
4
|
-
import { useState } from 'react'
|
|
5
|
-
|
|
6
|
-
import { getModuleFilterOptions } from './getModuleFilterOptions.ts'
|
|
7
|
-
import type { ModuleFromNodeConfig } from './ModuleFromNodeConfig.ts'
|
|
8
|
-
import { useNode } from './useNode.ts'
|
|
9
|
-
|
|
10
|
-
export const useWeakModulesFromNode = (
|
|
11
|
-
ids?: ModuleIdentifier[],
|
|
12
|
-
config?: ModuleFromNodeConfig,
|
|
13
|
-
): [WeakRef<ModuleInstance>[] | undefined, Error | undefined] => {
|
|
14
|
-
const [node, nodeError] = useNode(config)
|
|
15
|
-
const logger = config?.logger
|
|
16
|
-
const filterOptions = getModuleFilterOptions()
|
|
17
|
-
const [result, setResult] = useState<WeakRef<ModuleInstance>[] | undefined>()
|
|
18
|
-
const [, error] = usePromise(async () => {
|
|
19
|
-
logger?.debug('useModuleFromNode: resolving')
|
|
20
|
-
const nodeInstance = node
|
|
21
|
-
if (nodeInstance) {
|
|
22
|
-
const refreshModules = async () => {
|
|
23
|
-
const moduleInstances = ids
|
|
24
|
-
? (await Promise.all(ids.map(id => node.resolve(id, filterOptions)))).filter(exists)
|
|
25
|
-
: await node.resolve('*', filterOptions)
|
|
26
|
-
setResult(moduleInstances.map(mod => new WeakRef(mod)))
|
|
27
|
-
return moduleInstances
|
|
28
|
-
}
|
|
29
|
-
nodeInstance.on('moduleAttached', async ({ mod }) => {
|
|
30
|
-
logger?.debug(`useModuleFromNode: moduleAttached [${mod.config.name ?? mod.address}]`)
|
|
31
|
-
await refreshModules()
|
|
32
|
-
})
|
|
33
|
-
nodeInstance.on('moduleDetached', async ({ mod }) => {
|
|
34
|
-
logger?.debug(`useModuleFromNode: moduleDetached [${mod.config.name ?? mod.address}]`)
|
|
35
|
-
await refreshModules()
|
|
36
|
-
})
|
|
37
|
-
return await refreshModules()
|
|
38
|
-
}
|
|
39
|
-
console.log('Result: No Node')
|
|
40
|
-
}, [node, ids])
|
|
41
|
-
return [result, nodeError ?? error]
|
|
42
|
-
}
|
package/src/hooks/useWeakNode.ts
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import { usePromise } from '@xylabs/react-promise'
|
|
2
|
-
import { isDefined } from '@xylabs/sdk-js'
|
|
3
|
-
import type { NodeInstance } from '@xyo-network/node-model'
|
|
4
|
-
import { asNodeInstance } from '@xyo-network/node-model'
|
|
5
|
-
|
|
6
|
-
import { useWeakProvidedNode } from './provided/index.ts'
|
|
7
|
-
import { useWeakNodeConfigNodeField } from './useWeakNodeConfigNodeField.ts'
|
|
8
|
-
import type { WeakModuleFromNodeConfig } from './WeakModuleFromNodeConfig.ts'
|
|
9
|
-
|
|
10
|
-
export const useWeakNode = (config?: WeakModuleFromNodeConfig | undefined): [WeakRef<NodeInstance> | undefined, Error | undefined] => {
|
|
11
|
-
const [nodeAddress, nodeInstance] = useWeakNodeConfigNodeField(config)
|
|
12
|
-
const [providedNode] = useWeakProvidedNode()
|
|
13
|
-
const [nodeAddressNode, error] = usePromise(async () => {
|
|
14
|
-
const providedNodeInstance = providedNode?.deref()
|
|
15
|
-
if (providedNodeInstance && isDefined(nodeAddress)) {
|
|
16
|
-
return new WeakRef(asNodeInstance(await providedNodeInstance.resolve(nodeAddress), () => 'Module is not a node', { required: true }))
|
|
17
|
-
}
|
|
18
|
-
}, [providedNode, nodeAddress])
|
|
19
|
-
return [nodeAddressNode ?? nodeInstance ?? providedNode ?? undefined, error]
|
|
20
|
-
}
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import type { NodeInstance } from '@xyo-network/node-model'
|
|
2
|
-
import { useMemo } from 'react'
|
|
3
|
-
|
|
4
|
-
import type { WeakModuleFromNodeConfig } from './WeakModuleFromNodeConfig.ts'
|
|
5
|
-
|
|
6
|
-
export const useWeakNodeConfigNodeField = (
|
|
7
|
-
config?: WeakModuleFromNodeConfig | undefined,
|
|
8
|
-
): [string | undefined, WeakRef<NodeInstance> | undefined] => {
|
|
9
|
-
const nodeAddress = useMemo(() => (typeof config?.node === 'string' ? config?.node : undefined), [config?.node])
|
|
10
|
-
const nodeInstance = useMemo(() => (typeof config?.node === 'object' ? config?.node : undefined), [config?.node])
|
|
11
|
-
return [nodeAddress, nodeInstance]
|
|
12
|
-
}
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import { usePromise } from '@xylabs/react-promise'
|
|
2
|
-
import type { ModuleDescription, ModuleDescriptionPayload } from '@xyo-network/module-model'
|
|
3
|
-
import { ModuleDescriptionSchema } from '@xyo-network/module-model'
|
|
4
|
-
import { isPayloadOfSchemaType } from '@xyo-network/payload-model'
|
|
5
|
-
|
|
6
|
-
import { useWeakNode } from './useWeakNode.ts'
|
|
7
|
-
import type { WeakModuleFromNodeConfig } from './WeakModuleFromNodeConfig.ts'
|
|
8
|
-
|
|
9
|
-
export const useWeakNodeDescription = (config?: WeakModuleFromNodeConfig | undefined): [ModuleDescription | undefined, Error | undefined] => {
|
|
10
|
-
const [activeNode, nodeError] = useWeakNode(config)
|
|
11
|
-
const [description, error] = usePromise(async () => {
|
|
12
|
-
const state = await activeNode?.deref()?.state()
|
|
13
|
-
return state?.find<ModuleDescriptionPayload>(isPayloadOfSchemaType(ModuleDescriptionSchema))
|
|
14
|
-
}, [activeNode])
|
|
15
|
-
return [description, nodeError ?? error]
|
|
16
|
-
}
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import type { NodeInstance } from '@xyo-network/node-model'
|
|
2
|
-
import { isNodeInstance } from '@xyo-network/node-model'
|
|
3
|
-
|
|
4
|
-
import { useWeakModuleFromNode } from './useWeakModuleFromNode.ts'
|
|
5
|
-
import type { WeakModuleFromNodeConfig } from './WeakModuleFromNodeConfig.ts'
|
|
6
|
-
|
|
7
|
-
export const useWeakNodeFromNode = (
|
|
8
|
-
nameOrAddressOrInstance?: string | NodeInstance,
|
|
9
|
-
config?: WeakModuleFromNodeConfig,
|
|
10
|
-
): [WeakRef<NodeInstance> | undefined, Error | undefined] => {
|
|
11
|
-
return useWeakModuleFromNode<NodeInstance>(nameOrAddressOrInstance, { identity: isNodeInstance, ...config })
|
|
12
|
-
}
|
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
import { exists } from '@xylabs/sdk-js'
|
|
2
|
-
import type { ModuleIdentifier } from '@xyo-network/module-model'
|
|
3
|
-
import type { NodeInstance } from '@xyo-network/node-model'
|
|
4
|
-
import { asNodeInstance } from '@xyo-network/node-model'
|
|
5
|
-
|
|
6
|
-
import type { ModuleFromNodeConfig } from './ModuleFromNodeConfig.ts'
|
|
7
|
-
import { useWeakModulesFromNode } from './useWeakModulesFromNode.ts'
|
|
8
|
-
|
|
9
|
-
export const useWeakNodesFromNode = (
|
|
10
|
-
ids?: ModuleIdentifier[],
|
|
11
|
-
config?: ModuleFromNodeConfig,
|
|
12
|
-
): [WeakRef<NodeInstance>[] | null | undefined, Error | undefined] => {
|
|
13
|
-
const [modules, error] = useWeakModulesFromNode(ids, config)
|
|
14
|
-
if (error) {
|
|
15
|
-
return [null, error]
|
|
16
|
-
}
|
|
17
|
-
return modules
|
|
18
|
-
? [
|
|
19
|
-
modules
|
|
20
|
-
.map(mod => asNodeInstance(mod?.deref()))
|
|
21
|
-
.filter(exists)
|
|
22
|
-
.map(mod => new WeakRef(mod)),
|
|
23
|
-
undefined,
|
|
24
|
-
]
|
|
25
|
-
: [modules, error]
|
|
26
|
-
}
|
package/src/index.ts
DELETED