All files / extensions/cornerstone/src/stores useSynchronizersStore.ts

87.5% Statements 7/8
50% Branches 1/2
75% Functions 3/4
85.71% Lines 6/7

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            34x           34x                                                                                 34x         36x 36x                                       34x          
import { create } from 'zustand';
import { devtools } from 'zustand/middleware';
 
/**
 * Identifier for the synchronizers store type.
 */
const PRESENTATION_TYPE_ID = 'synchronizersStoreId';
 
/**
 * Flag to enable or disable debug mode for the store.
 * Set to `true` to enable zustand devtools.
 */
const DEBUG_STORE = false;
 
/**
 * Information about a single synchronizer.
 */
type SynchronizerInfo = {
  id: string;
  type: string;
  sourceViewports: Array<{ viewportId: string; renderingEngineId: string }>;
  targetViewports: Array<{ viewportId: string; renderingEngineId: string }>;
};
 
/**
 * State shape for the Synchronizers store.
 */
type SynchronizersState = {
  /**
   * Stores synchronizer information indexed by a unique key.
   */
  synchronizersStore: Record<string, SynchronizerInfo[]>;
 
  /**
   * Sets the synchronizers for a specific viewport.
   *
   * @param viewportId - The ID of the viewport.
   * @param synchronizers - An array of SynchronizerInfo.
   */
  setSynchronizers: (viewportId: string, synchronizers: SynchronizerInfo[]) => void;
 
  /**
   * Clears the entire synchronizers store.
   */
  clearSynchronizersStore: () => void;
};
 
/**
 * Creates the Synchronizers store.
 *
 * @param set - The zustand set function.
 * @returns The synchronizers store state and actions.
 */
const createSynchronizersStore = (set): SynchronizersState => ({
  synchronizersStore: {},
  type: PRESENTATION_TYPE_ID,
 
  setSynchronizers: (viewportId: string, synchronizers: SynchronizerInfo[]) => {
    set(
      state => ({
        synchronizersStore: {
          ...state.synchronizersStore,
          [viewportId]: synchronizers,
        },
      }),
      false,
      'setSynchronizers'
    );
  },
 
  clearSynchronizersStore: () => {
    set({ synchronizersStore: {} }, false, 'clearSynchronizersStore');
  },
});
 
/**
 * Zustand store for managing synchronizers.
 * Applies devtools middleware when DEBUG_STORE is enabled.
 */
export const useSynchronizersStore = create<SynchronizersState>()(
  DEBUG_STORE
    ? devtools(createSynchronizersStore, { name: 'SynchronizersStore' })
    : createSynchronizersStore
);