Skip to content

Commit

Permalink
Global api hmr and local trigger
Browse files Browse the repository at this point in the history
  • Loading branch information
presidenten committed Apr 26, 2017
1 parent 32b7aa9 commit 8a53508
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 46 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "vuex-plus",
"version": "0.5.2",
"version": "0.5.3",
"description": "Opinionated library that handles module instances in Vuex",
"main": "dist/vuex+.js",
"scripts": {
Expand Down
6 changes: 6 additions & 0 deletions src/instanceHandling/addStore.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import newStore from './newStore.js';
import { api, remapBaseStore } from './api.js';
import { getStoreInstanceName, toCamelCase } from './helpers.js';
import { registerForHMR, unregisterForHMR } from './hmrHandler.js';

Expand Down Expand Up @@ -45,6 +46,9 @@ export function add(baseStoreName) {
if (!this.$store._modules.root._children[this['$vuex+'].storeInstanceName]) { // eslint-disable-line
this.$store.registerModule(this['$vuex+'].storeInstanceName, store);

const remappedApi = remapBaseStore(store.$api, this['$vuex+'].baseStoreName, this['$vuex+'].storeInstanceName);
api[this['$vuex+'].storeInstanceName] = remappedApi;

if (module.hot) {
this.$hmrHandler = new HmrHandler(this['$vuex+'].storeInstanceName, getNewInstanceStore);
registerForHMR(this.$hmrHandler, baseStoreName, this['$vuex+'].storeInstanceName);
Expand All @@ -58,6 +62,8 @@ export function add(baseStoreName) {
if (!this.preserve && counter[this['$vuex+'].storeInstanceName] === 0) {
this.$store.unregisterModule(this['$vuex+'].storeInstanceName);

// delete api[this['$vuex+'].storeInstanceName];

if (module.hot) {
unregisterForHMR(this.$hmrHandler);
}
Expand Down
58 changes: 46 additions & 12 deletions src/instanceHandling/api.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
import { toCamelCase } from './helpers.js';

let importer;

/**
* The api for all stores
* The api is autogenerated once the module importer has been set
Expand All @@ -11,14 +7,52 @@ let importer;
*/
export const api = {};

// /**
// * Set the importer that can read all stores via require.context
// */
//
// export const generateAPI = (newImporter) => {
// importer = newImporter;
// const modules = importer.getModules();
// Object.keys(modules).forEach((module) => {
// const camelCasedName = toCamelCase(modules[module].name);
// api[camelCasedName] = modules[module].$api;
// });
// };

/**
* Set the importer that can read all stores via require.context
* Private method that modifies magics strings to contain their parents
*/
export const generateAPI = (newImporter) => {
importer = newImporter;
const modules = importer.getModules();
Object.keys(modules).forEach((module) => {
const camelCasedName = toCamelCase(modules[module].name);
api[camelCasedName] = modules[module].$api;
export function addModuleToNames(name, subapi) {
const result = {};
Object.keys(subapi).forEach((type) => {
if (type === 'get' || type === 'act' || type === 'mutate') {
result[type] = {};
Object.keys(subapi[type]).forEach((pathName) => {
const path = subapi[type][pathName];
result[type][pathName] = name + '/' + path;
});
} else {
result[type] = addModuleToNames(name, subapi[type]);
}
});
};

return result;
}

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;
}
5 changes: 5 additions & 0 deletions src/instanceHandling/hmrHandler.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { api, remapBaseStore } from './api.js';
import { toCamelCase } from './helpers.js';

let handlers = [];
Expand Down Expand Up @@ -27,6 +28,10 @@ export const hmrHandler = (updatedModules) => {
.forEach((handler) => {
modules[handler.storeInstanceName] = handler.newStore(updatedModules[key]);
});

Object.keys(modules).forEach((m) => {
api[m] = remapBaseStore(modules[m].$api, modules[m].name, m);
});
store.hotUpdate({ modules });
});
};
19 changes: 1 addition & 18 deletions src/instanceHandling/storeWrapper.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,7 @@
import clone from 'clone';
import { toCamelCase } from './helpers.js';
import { addModuleToNames } from './api.js';

/**
* Private method that modifies magics strings to contain their parents
*/
function addModuleToNames(name, subapi) {
const result = {};
Object.keys(subapi).forEach((type) => {
if (type === 'get' || type === 'act' || type === 'mutate') {
result[type] = {};
Object.keys(subapi[type]).forEach((pathName) => {
const path = subapi[type][pathName];
result[type][pathName] = name + '/' + path;
});
} else {
result[type] = addModuleToNames(name, subapi[type]);
}
});
return result;
}

/**
* Modify Vuex Module to contain an api with magic strings
Expand Down
39 changes: 24 additions & 15 deletions src/vuex+.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import storeWrapper from './instanceHandling/storeWrapper.js';
import { hmrHandler, setStore } from './instanceHandling/hmrHandler.js';
import { getLocalPath } from './instanceHandling/helpers.js';
import { add, setup } from './instanceHandling/addStore.js';
import * as apiManager from './instanceHandling/api.js';
import { api } from './instanceHandling/api.js';

export const addStore = add;

Expand All @@ -32,10 +32,9 @@ function searchDeeper(map, key) {
const getFullPath = (config) => {
const suffix = config.instance ? '$' + config.instance : '';
const getterKey = config.subpath.match(/[a-zA-Z]*/)[0];
let localApi = apiManager.api[config.vuexPlus.baseStoreName];

let localApi = api[config.vuexPlus.baseStoreName];
if (getterKey !== config.vuexPlus.baseStoreName) {
localApi = searchDeeper(apiManager.api[config.vuexPlus.baseStoreName], getterKey + suffix);
localApi = searchDeeper(api[config.vuexPlus.baseStoreName], getterKey + suffix);
}

if (!localApi) {
Expand Down Expand Up @@ -145,24 +144,35 @@ export const newInstance = function newInstance(substore, instance) {
* @returns {any} - Value from Vuex getter
*/
export const global = {
api: apiManager.api,
get api() {
return clone(api);
},

get({ path, context }) {
const localPath = getLocalPath(path, context);
get({ path, context, local }) {
if (local) {
const localPath = getLocalPath(path, context);
return context.rootGetters[localPath];
}

return context.rootGetters[localPath];
return context.rootGetters[path];
},

dispatch({ path, data, context }) {
const localPath = getLocalPath(path, context);
dispatch({ path, data, context, local }) {
if (local) {
const localPath = getLocalPath(path, context);
return context.dispatch(localPath, data, { root: true });
}

return context.dispatch(localPath, data, { root: true });
return context.dispatch(path, data, { root: true });
},

commit({ path, data, context }) {
const localPath = getLocalPath(path, context);
commit({ path, data, context, local }) {
if (local) {
const localPath = getLocalPath(path, context);
return context.commit(localPath, data, { root: true });
}

return context.commit(localPath, data, { root: true });
return context.commit(path, data, { root: true });
},
};

Expand All @@ -175,7 +185,6 @@ export default {
if (!setupDone && this.$store) {
setStore(this.$store);
const importer = contextHmr.getNewInstance();
apiManager.generateAPI(importer);
setup(importer);
importer.getModules();
importer.setupHMR(hmrHandler);
Expand Down

0 comments on commit 8a53508

Please sign in to comment.