Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposal: Global action enhancers - WIP #929

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions src/store.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import applyMixin from './mixin'
import devtoolPlugin from './plugins/devtool'
import ModuleCollection from './module/module-collection'
import { forEachValue, isObject, isPromise, assert } from './util'
import { compose, forEachValue, isObject, isPromise, assert } from './util'

let Vue // bind on install

Expand All @@ -21,6 +21,7 @@ export class Store {
}

const {
actionEnhancers = [],
plugins = [],
strict = false
} = options
Expand All @@ -42,9 +43,14 @@ export class Store {
this._subscribers = []
this._watcherVM = new Vue()

// bind commit and dispatch to self
const store = this
const { dispatch, commit } = this
const { commit } = this
let { dispatch } = this

const actionEnhancerChain = getActionEnhancerChain(store, state, actionEnhancers)
dispatch = compose(...actionEnhancerChain)(dispatch.bind(store))

// bind commit and dispatch to self
this.dispatch = function boundDispatch (type, payload) {
return dispatch.call(store, type, payload)
}
Expand Down Expand Up @@ -438,6 +444,15 @@ function enableStrictMode (store) {
}, { deep: true, sync: true })
}

function getActionEnhancerChain (store, state, actionEnhancers) {
return actionEnhancers.map(actionEnhancer => actionEnhancer({
dispatch: store.dispatch,
commit: store.commit,
getters: store.getters,
state
}))
}

function getNestedState (state, path) {
return path.length
? path.reduce((state, key) => state[key], state)
Expand Down
23 changes: 23 additions & 0 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,26 @@ export function isPromise (val) {
export function assert (condition, msg) {
if (!condition) throw new Error(`[vuex] ${msg}`)
}

/**
* Taken from Redux repo
* Composes single-argument functions from right to left. The rightmost
* function can take multiple arguments as it provides the signature for
* the resulting composite function.
*
* @param {...Function} funcs The functions to compose.
* @returns {Function} A function obtained by composing the argument functions
* from right to left. For example, compose(f, g, h) is identical to doing
* (...args) => f(g(h(...args))).
*/
export function compose (...funcs) {
if (funcs.length === 0) {
return arg => arg
}

if (funcs.length === 1) {
return funcs[0]
}

return funcs.reduce((a, b) => (...args) => a(b(...args)))
}