1
- import { VuexcellentOptions , VuexStore , VuexModule } from "@pderas/vuex-hydrate-state" ;
1
+ import { InitializedVuexStore , VuexcellentOptions , VuexStore , VuexModule , IPhaseLogger } from "@pderas/vuex-hydrate-state" ;
2
2
import { loggingMerge , objectMerge } from "./objectMerge" ;
3
3
import { mutantGenerator } from "./mutations" ;
4
- import { VuexcellentAutoCommitter } from "./committer" ;
4
+ import { VuexcellentAutoCommitter , autoCommitData } from "./committer" ;
5
5
import { AxiosInstance } from "axios" ;
6
6
import { createLogger } from './logger'
7
7
@@ -30,6 +30,7 @@ const defaultOptions = <VuexcellentOptions>{
30
30
31
31
export const hydrate = ( vuexState : VuexStore , options : VuexcellentOptions = defaultOptions ) => {
32
32
let __PHASE_STATE__ = globalBase . __PHASE_STATE__ ?? { }
33
+
33
34
options = {
34
35
...defaultOptions ,
35
36
...options
@@ -57,7 +58,6 @@ export const hydrate = (vuexState: VuexStore, options: VuexcellentOptions = defa
57
58
: mergedState ;
58
59
59
60
logger . info ( `[Phase] State Merged, Mutations Generated` , newState )
60
-
61
61
if ( options . axios && options . generateMutations && __BROWSER__ ) {
62
62
// prepare plugin
63
63
const VuexcellentPlugins = VuexcellentAutoCommitter (
@@ -74,9 +74,87 @@ export const hydrate = (vuexState: VuexStore, options: VuexcellentOptions = defa
74
74
75
75
} else if ( options . generateMutations ) {
76
76
logger . error (
77
- "[Phase ] It appears that auto-mutate could not be initialized.\nAn instance of axios could not be found. Make sure window.axios is available"
77
+ "[VuexHydrate ] It appears that auto-mutate could not be initialized.\nAn instance of axios could not be found. Make sure window.axios is available"
78
78
) ;
79
79
}
80
80
81
81
return newState ;
82
82
} ;
83
+
84
+ export const hydrateWatch = (
85
+ _this : any ,
86
+ store : InitializedVuexStore ,
87
+ options : VuexcellentOptions = defaultOptions ,
88
+ logger : IPhaseLogger
89
+ ) => {
90
+ const inertia = _this . $inertia . page . props ;
91
+ if ( inertia && inertia . $vuex ) {
92
+ let __PHASE_STATE__ = globalBase . __PHASE_STATE__ ?? { }
93
+
94
+ // Currently not running actions/mutations on page load
95
+ const { mutations, actions, ...phaseState } = __PHASE_STATE__
96
+ const vuexState = _this . $store . _modules . root . _rawModule ;
97
+
98
+ // merge incoming (store) options with window.__PHASE_STATE__
99
+ const mergedState = loggingMerge ( logger , vuexState , < VuexStore > phaseState ) ;
100
+
101
+ // generate mutations
102
+ const { createMutant, getMutation } = mutantGenerator ( options ) ;
103
+ const newState = options . generateMutations
104
+ ? createMutant ( mergedState )
105
+ : mergedState ;
106
+
107
+ autoMutateInertiaInterceptor ( inertia , _this . $store , newState , getMutation ) ;
108
+ }
109
+ }
110
+
111
+ /**
112
+ * Axios interceptor Generator to automatically call mutations
113
+ * and commit changed data. Sets up interceptor
114
+ *
115
+ * @param {Object } response the vuex response
116
+ * @param {Object } store initialized vuex store
117
+ * @param {Object } _state raw vuex starting data
118
+ * @param {Function } mutator function name generator
119
+ * @param {Object } options default options
120
+ *
121
+ * @return void
122
+ */
123
+ const autoMutateInertiaInterceptor = (
124
+ response : any ,
125
+ store : InitializedVuexStore ,
126
+ _state : VuexModule ,
127
+ mutator : any ,
128
+ options : VuexcellentOptions = defaultOptions ,
129
+ ) => {
130
+
131
+ const logger = options . logger = createLogger ( options . logLevel )
132
+
133
+ if ( ! response . $vuex ) {
134
+ logger . debug ( "[VuexHydrate] no vuex data detected. Skipping auto mutations." )
135
+ return response ;
136
+ }
137
+
138
+ logger . debug ( "[VuexHydrate] vuex data detected, attempting to auto-commit" )
139
+
140
+ try {
141
+ let $vuex = JSON . parse ( response . $vuex ) ;
142
+ // grab state & modules, if existing & auto-commit
143
+ autoCommitData ( { store, _state, mutator } , $vuex , logger ) ;
144
+
145
+ // user specified mutations
146
+ ( $vuex . mutations || [ ] ) . forEach (
147
+ ( [ mutation , value ] : [ string , any ?] ) => store . commit ( mutation , value )
148
+ ) ;
149
+
150
+ // user specified actions
151
+ ( $vuex . actions || [ ] ) . forEach (
152
+ ( [ action , value ] : [ string , any ?] ) => store . dispatch ( action , value )
153
+ ) ;
154
+ } catch ( err ) {
155
+ logger . error ( err ) ;
156
+ logger . warning (
157
+ `[@pderas/vuex-hydrate-state] An error occurred during the auto commit process.\nYour vuex state may not be what you expected.`
158
+ ) ;
159
+ }
160
+ } ;
0 commit comments