Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 | 277x 277x 277x 1414x 1141x 273x 273x 273x 1414x 1414x 1414x 1414x 287616x 287616x 287616x 5x 5x 5x 12x 27x 12x 3215x 3215x 18750x 18750x 18750x 3215x 273x | import {
Enums,
metaData,
utilities as csMetadataUtilities,
registerDisplaySetProviders,
} from '@cornerstonejs/metadata';
import { DisplaySet } from '../../types';
const DISPLAY_SET_MODULE = Enums.MetadataModules.DISPLAY_SET;
/**
* Storage adapter that keeps OHIF display sets in the `@cornerstonejs/metadata`
* DISPLAY_SET typed cache (keyed by `displaySetInstanceUID`) instead of an
* OHIF-private map, so cornerstone-native consumers can resolve the same
* display sets through `metaData.getTyped(Enums.MetadataModules.DISPLAY_SET, uid)`.
*
* The typed cache has no enumeration API, so a module-level UID index is kept
* for iteration. The index is module-level (like the previous module-level
* `displaySetCache` map) so all service instances share the same store.
*
* Writes use `setCacheData` for exact `Map.set` overwrite semantics — the
* `metaData.addTyped` ingest path warns and keeps the OLD value on duplicate
* adds, which would break the re-add flows (invalidation, `addInstances`
* merges, SR re-hydration).
*
* ### Lifetime
*
* Display sets live for the duration of a mode and are discarded on mode exit.
* Two things clear them, and both are intended:
*
* - `DisplaySetService.onModeExit()` calls {@link clearDisplaySets}, which
* removes this store's own UIDs;
* - `@ohif/extension-cornerstone`'s `onModeExit` calls
* `utilities.clearCacheData()`, releasing the whole typed metadata registry
* (naturalized instances hold Part 10 buffers outside the size-capped image
* cache). That takes the DISPLAY_SET module with it.
*
* The ordering is safe by construction: `mode.onModeExit` runs before either,
* so a mode that wants to carry display sets across a mode change snapshots
* them there and re-adds them with `addDisplaySets` on re-entry.
*
* A display set is therefore never expected to outlive its mode. Code that
* needs one later must hold its own reference — not a `displaySetInstanceUID`
* to look up afterwards.
*
* ### Planned: a true store
*
* This module is an adapter today, not a zustand store. The name is kept
* because a later change replaces it with the vanilla zustand
* `displaySetStore` of `@cornerstonejs/metadata` (cornerstonejs/cornerstone3D#3059).
* That store keeps display sets as immutable records: changes go through
* `update` / `modify` calls, and in-place writes to a display set stop
* working.
*/
const displaySetUIDs = new Set<string>();
let providersRegistered = false;
/**
* Registers the DISPLAY_SET typed providers plus the typed-provider bridge so
* `metaData.getTyped(DISPLAY_SET, uid)` resolves even when
* `registerDefaultProviders()` has not run (e.g. in tests). Guarded so it
* only runs once; `addProvider` dedupes the bridge by function identity, so
* this cannot double-register relative to `registerDefaultProviders()`.
*/
export function ensureDisplaySetMetadataProviders(): void {
if (providersRegistered) {
return;
}
providersRegistered = true;
metaData.addProvider(metaData.metadataModuleProvider, -1000);
registerDisplaySetProviders();
}
/**
* Stores a display set, overwriting any previous value for the same
* `displaySetInstanceUID` (matching `Map.set` semantics).
*/
export function setDisplaySet(displaySet: DisplaySet): void {
ensureDisplaySetMetadataProviders();
const uid = displaySet.displaySetInstanceUID;
csMetadataUtilities.setCacheData(DISPLAY_SET_MODULE, uid, displaySet);
displaySetUIDs.add(uid);
}
/**
* Retrieves a display set by UID. Prunes the UID index when the underlying
* typed cache entry has been cleared externally — by the cornerstone
* extension's mode-exit `utilities.clearCacheData()`, or by a loader
* re-initialization — so a wiped entry reads as absent rather than as a stale
* UID this store still believes in.
*/
export function getDisplaySet(uid: string): DisplaySet | undefined {
const value = csMetadataUtilities.getCacheData(DISPLAY_SET_MODULE, uid) as DisplaySet | undefined;
Iif (value === undefined && displaySetUIDs.has(uid)) {
displaySetUIDs.delete(uid);
}
return value;
}
export function hasDisplaySet(uid: string): boolean {
return getDisplaySet(uid) !== undefined;
}
/** Removes a display set from the store. */
export function deleteDisplaySet(uid: string): boolean {
const existed = displaySetUIDs.delete(uid);
csMetadataUtilities.clearTypedCacheData(DISPLAY_SET_MODULE, uid);
return existed;
}
/**
* Removes every display set owned by this store. Clears per-UID rather than
* clearing the whole DISPLAY_SET module so cornerstone-native entries written
* by other code survive.
*
* Called from `DisplaySetService.onModeExit()`. The cornerstone extension's
* own mode-exit `clearCacheData()` is broader and may get there first; either
* order leaves the store empty, which is the intent.
*/
export function clearDisplaySets(): void {
for (const uid of displaySetUIDs) {
csMetadataUtilities.clearTypedCacheData(DISPLAY_SET_MODULE, uid);
}
displaySetUIDs.clear();
}
/** All stored display sets in insertion order (matching Map iteration order). */
export function getAllDisplaySets(): DisplaySet[] {
const result: DisplaySet[] = [];
for (const uid of [...displaySetUIDs]) {
const displaySet = getDisplaySet(uid);
if (displaySet) {
result.push(displaySet);
}
}
return result;
}
/**
* A snapshot map of the stored display sets, keyed by UID. Returned by the
* deprecated `DisplaySetService.getDisplaySetCache()` — mutating the snapshot
* has no effect on the store.
*/
export function getDisplaySetSnapshot(): Map<string, DisplaySet> {
return new Map(getAllDisplaySets().map(ds => [ds.displaySetInstanceUID, ds]));
}
|