|
| 1 | +import { DevtoolHook, StateTree, Store } from './types' |
| 2 | + |
| 3 | +const target = |
| 4 | + typeof window !== 'undefined' |
| 5 | + ? window |
| 6 | + : typeof global !== 'undefined' |
| 7 | + ? global |
| 8 | + : { __VUE_DEVTOOLS_GLOBAL_HOOK__: undefined } |
| 9 | + |
| 10 | +// eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 11 | +const devtoolHook: DevtoolHook | undefined = target.__VUE_DEVTOOLS_GLOBAL_HOOK__ |
| 12 | + |
| 13 | +interface RootState { |
| 14 | + _devtoolHook: DevtoolHook |
| 15 | + _vm: { $options: { computed: {} } } |
| 16 | + _mutations: {} |
| 17 | + // we neeed to store modules names |
| 18 | + _modulesNamespaceMap: Record<string, boolean> |
| 19 | + _modules: { |
| 20 | + // we only need this specific method to let devtools retrieve the module name |
| 21 | + get(name: string): boolean |
| 22 | + } |
| 23 | + state: Record<string, StateTree> |
| 24 | + |
| 25 | + replaceState: Function |
| 26 | + registerModule: Function |
| 27 | + unregisterModule: Function |
| 28 | +} |
| 29 | + |
| 30 | +let rootStore: RootState |
| 31 | + |
| 32 | +export function devtoolPlugin<S extends StateTree>(store: Store<S>) { |
| 33 | + if (!devtoolHook) return |
| 34 | + |
| 35 | + if (!rootStore) { |
| 36 | + rootStore = { |
| 37 | + _devtoolHook: devtoolHook, |
| 38 | + _vm: { $options: { computed: {} } }, |
| 39 | + _mutations: {}, |
| 40 | + // we neeed to store modules names |
| 41 | + _modulesNamespaceMap: {}, |
| 42 | + _modules: { |
| 43 | + // we only need this specific method to let devtools retrieve the module name |
| 44 | + get(name: string) { |
| 45 | + return name in rootStore._modulesNamespaceMap |
| 46 | + }, |
| 47 | + }, |
| 48 | + state: {}, |
| 49 | + |
| 50 | + replaceState: () => { |
| 51 | + // we handle replacing per store so we do nothing here |
| 52 | + }, |
| 53 | + // these are used by the devtools |
| 54 | + registerModule: () => {}, |
| 55 | + unregisterModule: () => {}, |
| 56 | + } |
| 57 | + devtoolHook.emit('vuex:init', rootStore) |
| 58 | + } |
| 59 | + |
| 60 | + rootStore.state[store.name] = store.state |
| 61 | + |
| 62 | + // tell the devtools we added a module |
| 63 | + rootStore.registerModule(store.name, store) |
| 64 | + |
| 65 | + Object.defineProperty(rootStore.state, store.name, { |
| 66 | + get: () => store.state, |
| 67 | + set: state => store.replaceState(state), |
| 68 | + }) |
| 69 | + |
| 70 | + // Vue.set(rootStore.state, store.name, store.state) |
| 71 | + // the trailing slash is removed by the devtools |
| 72 | + rootStore._modulesNamespaceMap[store.name + '/'] = true |
| 73 | + |
| 74 | + devtoolHook.on('vuex:travel-to-state', targetState => { |
| 75 | + store.replaceState(targetState[store.name] as S) |
| 76 | + }) |
| 77 | + |
| 78 | + store.subscribe((mutation, state) => { |
| 79 | + rootStore.state[store.name] = state |
| 80 | + devtoolHook.emit( |
| 81 | + 'vuex:mutation', |
| 82 | + { |
| 83 | + ...mutation, |
| 84 | + type: `[${mutation.storeName}] ${mutation.type}`, |
| 85 | + }, |
| 86 | + rootStore.state |
| 87 | + ) |
| 88 | + }) |
| 89 | +} |
0 commit comments