Skip to content

Commit

Permalink
make all assertions/warnings be dev only
Browse files Browse the repository at this point in the history
  • Loading branch information
ktsn authored and yyx990803 committed May 15, 2017
1 parent 45c471c commit 46a88b2
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 22 deletions.
4 changes: 2 additions & 2 deletions src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export const mapGetters = normalizeNamespace((namespace, getters) => {
if (namespace && !getModuleByNamespace(this.$store, 'mapGetters', namespace)) {
return
}
if (!(val in this.$store.getters)) {
if (process.env.NODE_ENV !== 'production' && !(val in this.$store.getters)) {
console.error(`[vuex] unknown getter: ${val}`)
return
}
Expand Down Expand Up @@ -90,7 +90,7 @@ function normalizeNamespace (fn) {

function getModuleByNamespace (store, helper, namespace) {
const module = store._modulesNamespaceMap[namespace]
if (!module) {
if (process.env.NODE_ENV !== 'production' && !module) {
console.error(`[vuex] module namespace not found in ${helper}(): ${namespace}`)
}
return module
Expand Down
13 changes: 10 additions & 3 deletions src/module/module-collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ export default class ModuleCollection {
}

register (path, rawModule, runtime = true) {
assertRawModule(path, rawModule)
if (process.env.NODE_ENV !== 'production') {
assertRawModule(path, rawModule)
}

const newModule = new Module(rawModule, runtime)
if (path.length === 0) {
Expand Down Expand Up @@ -54,15 +56,20 @@ export default class ModuleCollection {
}

function update (path, targetModule, newModule) {
assertRawModule(path, newModule)
if (process.env.NODE_ENV !== 'production') {
assertRawModule(path, newModule)
}

// update target module
targetModule.update(newModule)

// update nested modules
if (newModule.modules) {
for (const key in newModule.modules) {
if (!targetModule.getChild(key)) {
if (
process.env.NODE_ENV !== 'production' &&
!targetModule.getChild(key)
) {
console.warn(
`[vuex] trying to add a new module '${key}' on hot reloading, ` +
'manual reload is needed'
Expand Down
54 changes: 37 additions & 17 deletions src/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ let Vue // bind on install

export class Store {
constructor (options = {}) {
assert(Vue, `must call Vue.use(Vuex) before creating a store instance.`)
assert(typeof Promise !== 'undefined', `vuex requires a Promise polyfill in this browser.`)
assert(this instanceof Store, `Store must be called with the new operator.`)
if (process.env.NODE_ENV !== 'production') {
assert(Vue, `must call Vue.use(Vuex) before creating a store instance.`)
assert(typeof Promise !== 'undefined', `vuex requires a Promise polyfill in this browser.`)
assert(this instanceof Store, `Store must be called with the new operator.`)
}

const {
plugins = [],
Expand Down Expand Up @@ -64,7 +66,9 @@ export class Store {
}

set state (v) {
assert(false, `Use store.replaceState() to explicit replace store state.`)
if (process.env.NODE_ENV !== 'production') {
assert(false, `Use store.replaceState() to explicit replace store state.`)
}
}

commit (_type, _payload, _options) {
Expand All @@ -77,7 +81,7 @@ export class Store {

const mutation = { type, payload }
const entry = this._mutations[type]
if (!entry) {
if (process.env.NODE_ENV !== 'production' && !entry) {
console.error(`[vuex] unknown mutation type: ${type}`)
return
}
Expand All @@ -88,7 +92,10 @@ export class Store {
})
this._subscribers.forEach(sub => sub(mutation, this.state))

if (options && options.silent) {
if (
process.env.NODE_ENV !== 'production' &&
options && options.silent
) {
console.warn(
`[vuex] mutation type: ${type}. Silent option has been removed. ` +
'Use the filter functionality in the vue-devtools'
Expand All @@ -104,7 +111,7 @@ export class Store {
} = unifyObjectStyle(_type, _payload)

const entry = this._actions[type]
if (!entry) {
if (process.env.NODE_ENV !== 'production' && !entry) {
console.error(`[vuex] unknown action type: ${type}`)
return
}
Expand All @@ -127,7 +134,9 @@ export class Store {
}

watch (getter, cb, options) {
assert(typeof getter === 'function', `store.watch only accepts a function.`)
if (process.env.NODE_ENV !== 'production') {
assert(typeof getter === 'function', `store.watch only accepts a function.`)
}
return this._watcherVM.$watch(() => getter(this.state, this.getters), cb, options)
}

Expand All @@ -139,8 +148,11 @@ export class Store {

registerModule (path, rawModule) {
if (typeof path === 'string') path = [path]
assert(Array.isArray(path), `module path must be a string or an Array.`)
assert(path.length > 0, 'cannot register the root module by using registerModule.')

if (process.env.NODE_ENV !== 'production') {
assert(Array.isArray(path), `module path must be a string or an Array.`)
assert(path.length > 0, 'cannot register the root module by using registerModule.')
}

this._modules.register(path, rawModule)
installModule(this, this.state, path, this._modules.get(path))
Expand All @@ -150,7 +162,11 @@ export class Store {

unregisterModule (path) {
if (typeof path === 'string') path = [path]
assert(Array.isArray(path), `module path must be a string or an Array.`)

if (process.env.NODE_ENV !== 'production') {
assert(Array.isArray(path), `module path must be a string or an Array.`)
}

this._modules.unregister(path)
this._withCommit(() => {
const parentState = getNestedState(this.state, path.slice(0, -1))
Expand Down Expand Up @@ -285,7 +301,7 @@ function makeLocalContext (store, namespace, path) {

if (!options || !options.root) {
type = namespace + type
if (!store._actions[type]) {
if (process.env.NODE_ENV !== 'production' && !store._actions[type]) {
console.error(`[vuex] unknown local action type: ${args.type}, global type: ${type}`)
return
}
Expand All @@ -301,7 +317,7 @@ function makeLocalContext (store, namespace, path) {

if (!options || !options.root) {
type = namespace + type
if (!store._mutations[type]) {
if (process.env.NODE_ENV !== 'production' && !store._mutations[type]) {
console.error(`[vuex] unknown local mutation type: ${args.type}, global type: ${type}`)
return
}
Expand Down Expand Up @@ -383,7 +399,7 @@ function registerAction (store, type, handler, local) {
}

function registerGetter (store, type, rawGetter, local) {
if (store._wrappedGetters[type]) {
if (process.env.NODE_ENV !== 'production' && store._wrappedGetters[type]) {
console.error(`[vuex] duplicate getter key: ${type}`)
return
}
Expand All @@ -399,7 +415,9 @@ function registerGetter (store, type, rawGetter, local) {

function enableStrictMode (store) {
store._vm.$watch(function () { return this._data.$$state }, () => {
assert(store._committing, `Do not mutate vuex store state outside mutation handlers.`)
if (process.env.NODE_ENV !== 'production') {
assert(store._committing, `Do not mutate vuex store state outside mutation handlers.`)
}
}, { deep: true, sync: true })
}

Expand All @@ -416,13 +434,15 @@ function unifyObjectStyle (type, payload, options) {
type = type.type
}

assert(typeof type === 'string', `Expects string as the type, but found ${typeof type}.`)
if (process.env.NODE_ENV !== 'production') {
assert(typeof type === 'string', `Expects string as the type, but found ${typeof type}.`)
}

return { type, payload, options }
}

export function install (_Vue) {
if (Vue) {
if (process.env.NODE_ENV !== 'production' && Vue) {
console.error(
'[vuex] already installed. Vue.use(Vuex) should be called only once.'
)
Expand Down

0 comments on commit 46a88b2

Please sign in to comment.