@@ -6,17 +6,8 @@ let Vue // bind on install
66
77class Store {
88 constructor ( options = { } ) {
9- if ( ! Vue ) {
10- throw new Error (
11- '[vuex] must call Vue.use(Vuex) before creating a store instance.'
12- )
13- }
14-
15- if ( typeof Promise === 'undefined' ) {
16- throw new Error (
17- '[vuex] vuex requires a Promise polyfill in this browser.'
18- )
19- }
9+ assert ( Vue , `must call Vue.use(Vuex) before creating a store instance.` )
10+ assert ( typeof Promise !== 'undefined' , `vuex requires a Promise polyfill in this browser.` )
2011
2112 const {
2213 state = { } ,
@@ -61,7 +52,7 @@ class Store {
6152 }
6253
6354 set state ( v ) {
64- throw new Error ( '[vuex] Use store.replaceState() to explicit replace store state.' )
55+ assert ( false , ` Use store.replaceState() to explicit replace store state.` )
6556 }
6657
6758 replaceState ( state ) {
@@ -72,9 +63,7 @@ class Store {
7263
7364 module ( path , module , hot ) {
7465 if ( typeof path === 'string' ) path = [ path ]
75- if ( ! Array . isArray ( path ) ) {
76- throw new Error ( '[vuex] module path must be a string or an Array.' )
77- }
66+ assert ( Array . isArray ( path ) , `module path must be a string or an Array.` )
7867
7968 const isRoot = ! path . length
8069 const {
@@ -170,7 +159,6 @@ class Store {
170159 dispatch ( type , payload ) {
171160 const entry = this . _actions [ type ]
172161 if ( ! entry ) {
173- debugger
174162 console . error ( `[vuex] unknown action type: ${ type } ` )
175163 return
176164 }
@@ -192,6 +180,11 @@ class Store {
192180 }
193181 }
194182
183+ watch ( getter , cb , options ) {
184+ assert ( typeof getter === 'function' , `store.watch only accepts a function.` )
185+ return this . _vm . $watch ( ( ) => getter ( this . state ) , cb , options )
186+ }
187+
195188 hotUpdate ( newOptions ) {
196189 this . _actions = Object . create ( null )
197190 this . _mutations = Object . create ( null )
@@ -227,6 +220,10 @@ class Store {
227220 }
228221}
229222
223+ function assert ( condition , msg ) {
224+ if ( ! condition ) throw new Error ( `[vuex] ${ msg } ` )
225+ }
226+
230227function initStoreState ( store , state , getters ) {
231228 // bind getters
232229 store . getters = { }
@@ -276,11 +273,7 @@ function extractModuleGetters (getters = {}, modules = {}, path = []) {
276273
277274function enableStrictMode ( store ) {
278275 store . _vm . $watch ( 'state' , ( ) => {
279- if ( ! store . _committing ) {
280- throw new Error (
281- '[vuex] Do not mutate vuex store state outside mutation handlers.'
282- )
283- }
276+ assert ( store . _committing , `Do not mutate vuex store state outside mutation handlers.` )
284277 } , { deep : true , sync : true } )
285278}
286279
0 commit comments