Passed
Push — trunk ( 5d9bf1...024f41 )
by Christian
34:40 queued 22:07
created

src/Administration/Resources/app/administration/src/app/init-pre/state.init.ts   A

Complexity

Total Complexity 3
Complexity/F 1

Size

Lines of Code 51
Function Count 3

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 38
dl 0
loc 51
rs 10
c 0
b 0
f 0
wmc 3
mnd 0
bc 0
fnc 3
bpm 0
cpm 1
noi 0

3 Functions

Rating   Name   Duplication   Size   Complexity  
A state.init.ts ➔ initVuexModules 0 4 1
A state.init.ts ➔ initState 0 8 1
A state.init.ts ➔ initVuexState 0 25 1
1
/**
2
 * @package admin
3
 */
4
5
import Vue from 'vue';
6
import Vuex from 'vuex';
7
import type { Module } from 'vuex';
8
import VuexModules from 'src/app/state/index';
9
import type { FullState } from 'src/core/factory/state.factory';
10
11
// eslint-disable-next-line sw-deprecation-rules/private-feature-declarations
12
export default function initState() {
13
    initVuexState(Shopware.State);
14
    // @ts-expect-error - all vuex modules are defined
15
    initVuexModules(VuexModules, Shopware.State);
16
17
    return true;
18
}
19
20
function initVuexState(state: FullState) {
21
    Vue.use(Vuex);
22
23
    const store = new Vuex.Store<VuexRootState>({
24
        modules: {},
25
        strict: false,
26
    });
27
28
    // eslint-disable-next-line max-len
29
    /* eslint-disable @typescript-eslint/no-unsafe-return, @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-argument, max-len */
30
    state._registerPrivateProperty('_store', store);
31
    state._registerProperty('list', () => Object.keys(store.state));
32
    state._registerProperty('get', <N extends keyof VuexRootState>(name: N) => store.state[name]);
33
    state._registerGetterMethod('getters', () => store.getters);
34
    state._registerProperty('commit', (...args: Parameters<typeof store.commit>) => store.commit(...args));
35
    state._registerProperty('dispatch', (...args: Parameters<typeof store.dispatch>) => store.dispatch(...args));
36
    state._registerProperty('watch', (...args: Parameters<typeof store.watch>) => store.watch(...args));
37
    state._registerProperty('subscribe', (...args: Parameters<typeof store.subscribe>) => store.subscribe(...args));
38
    state._registerProperty('subscribeAction', (...args: Parameters<typeof store.subscribeAction>) => store.subscribeAction(...args));
39
    state._registerProperty('registerModule', (...args: Parameters<typeof store.registerModule>) => store.registerModule(...args));
40
    state._registerProperty('unregisterModule', (...args: Parameters<typeof store.unregisterModule>) => store.unregisterModule(...args));
41
    /* eslint-enable @typescript-eslint/no-unsafe-return, @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-argument, max-len */
42
43
    return state;
44
}
45
46
function initVuexModules(modules: { [moduleName: string]: Module<keyof VuexRootState, VuexRootState> }, state: FullState) {
47
    Object.entries(modules).forEach(([moduleName, module]) => {
48
        state.registerModule(moduleName, module);
49
    });
50
}
51