All files / platform/core/src/utils addProxyFields.js

72.72% Statements 8/11
33.33% Branches 2/6
50% Functions 1/2
72.72% Lines 8/11

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        73x             73x                                           37x       37x 111x     111x                   37x   37x        
/**
 * fieldProxies has the set of fields which are proxied on a per-field basis
 * to the child element.
 */
export const fieldProxies = [
  'ImagePositionPatient',
  'ImageOrientationPatient',
  'PixelSpacing',
  // more fields as necessary
];
 
const METADATA_PROXY_FLAG = Symbol('isMetadataProxy');
 
/**
 * Adds proxy properties for a limited number of values which are sometimes
 * defined in multiframe instance data.
 *
 * If a requested field is in `metadataFieldsToWrap`:
 *   - It first tries to return the value from the current instance.
 *   - If not found, it attempts to retrieve the value from the `_parentInstance`.
 *   - If still not found, it checks `_parentInstance._shared`.
 *   - If none exist, it returns `undefined`.
 *
 * For all other properties, it behaves like a regular property access.
 *
 * This allows graceful fallback access for DICOM metadata values that might be spread
 * across nested or shared metadata structures.
 *
 * @param {Object} instance - The target instance object to wrap.
 * @returns {Proxy} A proxy-wrapped instance with custom field resolution behavior.
 */
export function addProxyFields(instance) {
  // Skip wrapping if already wrapped
  Iif (!instance || instance[METADATA_PROXY_FLAG]) {
    return instance;
  }
 
  for(const fieldProxy of fieldProxies) {
    Iif( fieldProxy in instance) {
      continue;
    }
    Object.defineProperty(instance,fieldProxy, {
      configurable: true,
      enumerable: true,
      get: () => {
        return instance._parentInstance?.[fieldProxy] ?? instance._parentInstance?._shared?.[fieldProxy];
      }
    });
  }
 
  // Mark this proxy to avoid double wrapping
  Object.defineProperty(instance,METADATA_PROXY_FLAG, { value: true });
 
  return instance;
}
 
export default addProxyFields;