-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1762 from silx-kit/data-hooks
Export and document context access hooks
- Loading branch information
Showing
20 changed files
with
264 additions
and
125 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { | ||
type ArrayShape, | ||
type Dataset, | ||
type ScalarShape, | ||
} from '@h5web/shared/hdf5-models'; | ||
|
||
import { useDataContext } from '../providers/DataProvider'; | ||
import { getSliceSelection } from '../vis-packs/core/utils'; | ||
import { type DimensionMapping } from './models'; | ||
|
||
export function useValuesInCache( | ||
...datasets: (Dataset<ScalarShape | ArrayShape> | undefined)[] | ||
): (dimMapping: DimensionMapping) => boolean { | ||
const { valuesStore } = useDataContext(); | ||
return (dimMapping) => { | ||
const selection = getSliceSelection(dimMapping); | ||
return datasets.every( | ||
(dataset) => !dataset || valuesStore.has({ dataset, selection }), | ||
); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import { assertDatasetValue, isDefined } from '@h5web/shared/guards'; | ||
import { | ||
type ArrayShape, | ||
type Dataset, | ||
type ProvidedEntity, | ||
type ScalarShape, | ||
type Value, | ||
} from '@h5web/shared/hdf5-models'; | ||
|
||
import { useDataContext } from './providers/DataProvider'; | ||
|
||
export function useEntity(path: string): ProvidedEntity { | ||
const { entitiesStore } = useDataContext(); | ||
return entitiesStore.get(path); | ||
} | ||
|
||
export function usePrefetchValues( | ||
datasets: (Dataset<ScalarShape | ArrayShape> | undefined)[], | ||
selection?: string, | ||
): void { | ||
const { valuesStore } = useDataContext(); | ||
datasets.filter(isDefined).forEach((dataset) => { | ||
valuesStore.prefetch({ dataset, selection }); | ||
}); | ||
} | ||
|
||
export function useDatasetValue<D extends Dataset<ArrayShape | ScalarShape>>( | ||
dataset: D, | ||
selection?: string, | ||
): Value<D>; | ||
|
||
export function useDatasetValue<D extends Dataset<ArrayShape | ScalarShape>>( | ||
dataset: D | undefined, | ||
selection?: string, | ||
): Value<D> | undefined; | ||
|
||
export function useDatasetValue<D extends Dataset<ArrayShape | ScalarShape>>( | ||
dataset: D | undefined, | ||
selection?: string, | ||
): Value<D> | undefined { | ||
const { valuesStore } = useDataContext(); | ||
|
||
if (!dataset) { | ||
return undefined; | ||
} | ||
|
||
// If `selection` is undefined, the entire dataset will be fetched | ||
const value = valuesStore.get({ dataset, selection }); | ||
|
||
assertDatasetValue(value, dataset); | ||
return value; | ||
} | ||
|
||
export function useDatasetsValues<D extends Dataset<ArrayShape | ScalarShape>>( | ||
datasets: D[], | ||
selection?: string, | ||
): Value<D>[]; | ||
|
||
export function useDatasetsValues<D extends Dataset<ArrayShape | ScalarShape>>( | ||
datasets: (D | undefined)[], | ||
selection?: string, | ||
): (Value<D> | undefined)[]; | ||
|
||
export function useDatasetsValues<D extends Dataset<ArrayShape | ScalarShape>>( | ||
datasets: (D | undefined)[], | ||
selection?: string, | ||
): (Value<D> | undefined)[] { | ||
const { valuesStore } = useDataContext(); | ||
|
||
return datasets.map((dataset) => { | ||
if (!dataset) { | ||
return undefined; | ||
} | ||
|
||
const value = valuesStore.get({ dataset, selection }); | ||
assertDatasetValue(value, dataset); | ||
return value; | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.