-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4d9b586
commit 9465029
Showing
13 changed files
with
468 additions
and
534 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/** | ||
* The api for all stores | ||
* The api is autogenerated once the module importer has been set | ||
* ``` | ||
* api.aStore.get.something => vuex magic string for vuex getter | ||
* ``` | ||
*/ | ||
export const api = {}; | ||
|
||
/** | ||
* Get subtree from map matching key | ||
* TODO -- verify -- | ||
*/ | ||
export function extractSubstoreApi(map, key) { | ||
const submodules = Object.keys(map).filter(k => k !== 'get' && k !== 'act' && k !== 'mutate'); | ||
const keyIsInMap = submodules.indexOf(key) >= 0; | ||
|
||
if (keyIsInMap) { | ||
return map[key]; | ||
} | ||
|
||
// TODO Speed up with some nice algorithm | ||
let result; | ||
submodules.forEach((submodule) => { | ||
const searchResult = extractSubstoreApi(map[submodule], key); | ||
if (searchResult) { | ||
result = searchResult; | ||
} | ||
}); | ||
|
||
return result; | ||
} | ||
|
||
|
||
export const getFullPath = (config) => { | ||
const suffix = config.instance ? '$' + config.instance : ''; | ||
const getterKey = config.subpath.match(/[a-zA-Z]*/)[0]; | ||
let localApi = api[config.vuexPlus.baseStoreName]; | ||
if (getterKey !== config.vuexPlus.baseStoreName) { | ||
localApi = extractSubstoreApi(api[config.vuexPlus.baseStoreName], getterKey + suffix); | ||
} | ||
|
||
if (!localApi) { | ||
const instance = config.subpath.split('/')[0] + '$' + config.instance; | ||
console.error('[Vuex+ warn]: Cant find substore instance "' + instance + '" in "' + config.container + '"'); | ||
return undefined; | ||
} | ||
|
||
const fullPath = localApi[config.method][config.key] | ||
.replace(config.vuexPlus.baseStoreName, config.vuexPlus.storeInstanceName); | ||
|
||
return fullPath; | ||
}; | ||
|
||
export function remapBaseStore(storeApi, baseStoreName, newStoreName) { | ||
newStoreName = newStoreName || baseStoreName; | ||
const result = {}; | ||
Object.keys(storeApi).forEach((type) => { | ||
if (type === 'get' || type === 'act' || type === 'mutate') { | ||
result[type] = {}; | ||
Object.keys(storeApi[type]).forEach((pathName) => { | ||
result[type][pathName] = storeApi[type][pathName].replace(baseStoreName, newStoreName); | ||
}); | ||
} else { | ||
result[type] = remapBaseStore(storeApi[type], baseStoreName, newStoreName); | ||
} | ||
}); | ||
|
||
return result; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import clone from 'clone'; | ||
import { api } from './api.js'; | ||
import { getStoreInstanceName } from '../common/helpers.js'; | ||
|
||
const getLocalPath = (path, context) => { | ||
const storeName = context.state['vuex+'].storeName; | ||
const instance = context.state['vuex+'].instance; | ||
return path.replace(storeName, getStoreInstanceName(storeName, instance)); | ||
}; | ||
|
||
/** | ||
* Method that returns a getter from the same instance. | ||
* @param {string} - Path as as string, usually from api. Eg. `api.example.get.something` | ||
* @param {Context} - Vuex context | ||
* @returns {any} - Value from Vuex getter | ||
*/ | ||
export default { | ||
get api() { | ||
return clone(api); | ||
}, | ||
|
||
get({ path, context, local }) { | ||
if (local) { | ||
const localPath = getLocalPath(path, context); | ||
return context.rootGetters[localPath]; | ||
} | ||
|
||
return context.rootGetters[path]; | ||
}, | ||
|
||
dispatch({ path, data, context, local }) { | ||
if (local) { | ||
const localPath = getLocalPath(path, context); | ||
return context.dispatch(localPath, data, { root: true }); | ||
} | ||
|
||
return context.dispatch(path, data, { root: true }); | ||
}, | ||
|
||
commit({ path, data, context, local }) { | ||
if (local) { | ||
const localPath = getLocalPath(path, context); | ||
return context.commit(localPath, data, { root: true }); | ||
} | ||
|
||
return context.commit(path, data, { root: true }); | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { getFullPath } from './api.js'; | ||
|
||
export default { | ||
getters(m) { | ||
const result = {}; | ||
Object.keys(m).forEach((key) => { | ||
result[key] = function get() { | ||
const path = getFullPath({ | ||
method: 'get', | ||
key, | ||
subpath: m[key], | ||
instance: this.instance, | ||
vuexPlus: this['$vuex+'], | ||
container: this.$parent.$vnode.componentOptions.tag, | ||
}); | ||
|
||
return this.$store.getters[path]; | ||
}; | ||
}); | ||
return result; | ||
}, | ||
|
||
actions(m) { | ||
const result = {}; | ||
Object.keys(m).forEach((key) => { | ||
result[key] = function dispatch(payload) { | ||
const path = getFullPath({ | ||
method: 'act', | ||
key, | ||
subpath: m[key], | ||
instance: this.instance, | ||
vuexPlus: this['$vuex+'], | ||
container: this.$parent.$vnode.componentOptions.tag, | ||
}); | ||
|
||
return this.$store.dispatch(path, payload); | ||
}; | ||
}); | ||
return result; | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import contextHmr from 'webpack-context-vuex-hmr'; | ||
|
||
import { setup } from './addStore.js'; | ||
import { hmrHandler, setStore } from '../common/hmrHandler.js'; | ||
|
||
let setupDone = false; | ||
export default { | ||
install(Vue) { | ||
Vue.mixin({ | ||
props: ['instance'], | ||
created() { | ||
if (!setupDone && this.$store) { | ||
setStore(this.$store); | ||
const importer = contextHmr.getNewInstance(); | ||
setup(importer); | ||
importer.getModules(); | ||
importer.setupHMR(hmrHandler); | ||
setupDone = true; | ||
} | ||
|
||
const findModuleName = (parent) => { | ||
if (!this['$vuex+'] && parent.$parent) { | ||
if (!parent.$parent['$vuex+']) { | ||
findModuleName(parent.$parent, '/'); | ||
} else { | ||
this['$vuex+'] = { | ||
baseStoreName: parent.$parent['$vuex+'].baseStoreName, | ||
storeInstanceName: parent.$parent['$vuex+'].storeInstanceName, | ||
}; | ||
} | ||
} | ||
}; | ||
|
||
findModuleName(this, '/'); | ||
}, | ||
}); | ||
}, | ||
}; |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import clone from 'clone'; | ||
|
||
export default (substore, instance) => { | ||
const result = clone(substore); | ||
Object.keys(result.api).forEach((type) => { | ||
if (type === 'get' || type === 'act' || type === 'mutate') { | ||
Object.keys(result.api[type]).forEach((key) => { | ||
result.api[type][key] = result.api[type][key].split('/')[0] + '$' + instance + '/' + key; | ||
}); | ||
} | ||
}); | ||
|
||
return result; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.