Skip to content

Commit

Permalink
Fetch attribute values on demand in resolvePath
Browse files Browse the repository at this point in the history
  • Loading branch information
axelboc committed Nov 16, 2021
1 parent 565fc38 commit a19925b
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 188 deletions.
18 changes: 18 additions & 0 deletions packages/app/src/__tests__/NexusPack.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,24 @@ test('visualize NXdata group with "rgb-image" interpretation', async () => {
expect(tabs[0]).toHaveTextContent(NexusVis.NxRGB);
});

test('visualize NXdata group with unknown interpretation', async () => {
await renderApp();
await selectExplorerNode('nexus_entry/unknown');

const tabs = await findVisSelectorTabs();
expect(tabs).toHaveLength(1);
expect(tabs[0]).toHaveTextContent(NexusVis.NxImage);
});

test('visualize NXdata group with "rgb-image" interpretation but incompatible signal', async () => {
await renderApp();
await selectExplorerNode('nexus_entry/rgb_incompatible');

const tabs = await findVisSelectorTabs();
expect(tabs).toHaveLength(1);
expect(tabs[0]).toHaveTextContent(NexusVis.NxSpectrum);
});

test('visualize NXentry group with implicit default child NXdata group', async () => {
await renderApp();
await selectExplorerNode('nexus_no_default');
Expand Down
4 changes: 2 additions & 2 deletions packages/app/src/visualizer/Visualizer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ interface Props {
function Visualizer(props: Props) {
const { path } = props;

const { entitiesStore } = useContext(ProviderContext);
const { entitiesStore, attrValuesStore } = useContext(ProviderContext);

function getEntity(entityPath: string): Entity {
return handleError(
Expand All @@ -25,7 +25,7 @@ function Visualizer(props: Props) {
);
}

const resolution = resolvePath(path, getEntity);
const resolution = resolvePath(path, getEntity, attrValuesStore);

if (!resolution) {
return (
Expand Down
174 changes: 0 additions & 174 deletions packages/app/src/visualizer/utils.test.ts

This file was deleted.

34 changes: 22 additions & 12 deletions packages/app/src/visualizer/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Entity } from '@h5web/shared';
import type { AttributeValues, Entity } from '@h5web/shared';
import {
assertGroupWithChildren,
hasComplexType,
Expand All @@ -10,8 +10,8 @@ import {
buildEntityPath,
} from '@h5web/shared';
import { NxInterpretation } from '@h5web/shared/src/models-nexus';
import type { FetchStore } from 'react-suspense-fetch';

import { getAttributeValue } from '../utils';
import type { CoreVisDef } from '../vis-packs/core/visualizations';
import { Vis, CORE_VIS } from '../vis-packs/core/visualizations';
import type { VisDef } from '../vis-packs/models';
Expand All @@ -24,38 +24,45 @@ import { NexusVis, NEXUS_VIS } from '../vis-packs/nexus/visualizations';

export function resolvePath(
path: string,
getEntity: (path: string) => Entity
getEntity: (path: string) => Entity,
attrValueStore: FetchStore<AttributeValues, Entity>
): { entity: Entity; supportedVis: VisDef[] } | undefined {
const entity = getEntity(path);

const supportedVis = findSupportedVis(entity);
const supportedVis = findSupportedVis(entity, attrValueStore);
if (supportedVis.length > 0) {
return { entity, supportedVis };
}

const nxDefaultPath = getNxDefaultPath(entity);
const nxDefaultPath = getNxDefaultPath(entity, attrValueStore);
if (nxDefaultPath) {
return resolvePath(nxDefaultPath, getEntity);
return resolvePath(nxDefaultPath, getEntity, attrValueStore);
}

return undefined;
}

function findSupportedVis(entity: Entity): VisDef[] {
const nxVis = getSupportedNxVis(entity);
function findSupportedVis(
entity: Entity,
attrValueStore: FetchStore<AttributeValues, Entity>
): VisDef[] {
const nxVis = getSupportedNxVis(entity, attrValueStore);
if (nxVis) {
return [nxVis];
}

return getSupportedCoreVis(entity);
}

function getNxDefaultPath(entity: Entity): string | undefined {
function getNxDefaultPath(
entity: Entity,
attrValueStore: FetchStore<AttributeValues, Entity>
): string | undefined {
if (!isGroup(entity)) {
return undefined;
}

const defaultPath = getAttributeValue(entity, 'default');
const { default: defaultPath } = attrValueStore.get(entity);

if (defaultPath) {
assertStr(defaultPath, `Expected 'default' attribute to be a string`);
Expand All @@ -79,15 +86,18 @@ export function getSupportedCoreVis(entity: Entity): CoreVisDef[] {
: supportedVis;
}

export function getSupportedNxVis(entity: Entity): VisDef | undefined {
export function getSupportedNxVis(
entity: Entity,
attrValueStore: FetchStore<AttributeValues, Entity>
): VisDef | undefined {
if (!isGroup(entity) || !isNxDataGroup(entity)) {
return undefined;
}

assertGroupWithChildren(entity);
const dataset = findSignalDataset(entity);
const isCplx = hasComplexType(dataset);
const interpretation = getAttributeValue(dataset, 'interpretation');
const { interpretation } = attrValueStore.get(dataset);

if (
interpretation === NxInterpretation.RGB &&
Expand Down
10 changes: 10 additions & 0 deletions packages/shared/src/mock/metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,16 @@ export const mockMetadata = makeNxGroup(mockFilepath, 'NXroot', {
})
),
}),
makeNxDataGroup('unknown', {
signal: makeNxDataset('fourD', intType, [3, 9, 20, 41], {
interpretation: 'unknown',
}),
}),
makeNxDataGroup('rgb_incompatible', {
signal: makeNxDataset('oneD', intType, [41], {
interpretation: 'rgb-image',
}),
}),
],
}),
makeNxGroup('nexus_no_default', 'NXprocess', {
Expand Down

0 comments on commit a19925b

Please sign in to comment.