All files / app/codeCharta/state/store state.manager.ts

100% Statements 28/28
100% Branches 10/10
100% Functions 4/4
100% Lines 28/28

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 7142x 42x 42x 42x 42x     42x 42x   42x             42x               42x 42x 202x 525x 30x 30x   495x     42x                   42x 312x 2156x 81x     2075x 1x     2074x 2074x   2074x           312x       2075x    
import { appSettings, defaultAppSettings } from "./appSettings/appSettings.reducer"
import { defaultFileSettings, fileSettings } from "./fileSettings/fileSettings.reducer"
import { defaultDynamicSettings, dynamicSettings } from "./dynamicSettings/dynamicSettings.reducer"
import { defaultFiles, files } from "./files/files.reducer"
import { appStatus, defaultAppStatus } from "./appStatus/appStatus.reducer"
import { ActionReducer } from "@ngrx/store"
import { CcState } from "../../codeCharta.model"
import { isSetStateAction } from "./state.actions"
import { clone } from "../../util/clone"
 
export const appReducers = {
    fileSettings,
    appSettings,
    dynamicSettings,
    files,
    appStatus
}
export const defaultState: CcState = {
    fileSettings: defaultFileSettings,
    appSettings: defaultAppSettings,
    dynamicSettings: defaultDynamicSettings,
    files: defaultFiles,
    appStatus: defaultAppStatus
}
 
export const setStateMiddleware =
    (reducer: ActionReducer<CcState>): ActionReducer<CcState> =>
    (state, action) => {
        if (isSetStateAction(action)) {
            const newState = clone(state)
            return _applyPartialState(newState, action.value)
        }
        return reducer(state, action)
    }
 
const objectWithDynamicKeysInStore = new Set([
    "fileSettings.attributeTypes",
    "fileSettings.attributeDescriptors",
    "fileSettings.blacklist",
    "fileSettings.edges",
    "fileSettings.markedPackages",
    "dynamicSettings.focusedNodePath",
    "files" // ToDo; this should be a Map with an unique id
])
 
export function _applyPartialState<T>(applyTo: T, toBeApplied: unknown, composedPath = []): T {
    for (const [key, value] of Object.entries(toBeApplied)) {
        if (value === null || value === undefined) {
            continue
        }
 
        if (!isKeyOf(applyTo, key)) {
            continue
        }
 
        const newComposedPath = [...composedPath, key]
        const composedJoinedPath = newComposedPath.join(".")
 
        applyTo[key] =
            typeof value !== "object" || objectWithDynamicKeysInStore.has(composedJoinedPath)
                ? value
                : _applyPartialState(applyTo[key], value, newComposedPath)
    }
 
    return applyTo
}
 
function isKeyOf<T>(of: T, key: PropertyKey): key is keyof T {
    return Object.prototype.hasOwnProperty.call(of, key)
}