Skip to content

Commit

Permalink
Updated build
Browse files Browse the repository at this point in the history
  • Loading branch information
presidenten committed Apr 26, 2017
1 parent 8a53508 commit 4d9b586
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 33 deletions.
102 changes: 70 additions & 32 deletions dist/vuex+.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,28 @@ var getLocalPath = function (path, context) {
return path.replace(storeName, getStoreInstanceName(storeName, instance));
};

/**
* 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
* ```
*/
var 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;
// });
// };

/**
* Private method that modifies magics strings to contain their parents
*/
Expand All @@ -32,6 +54,24 @@ function addModuleToNames(name, subapi) {
result[type] = addModuleToNames(name, subapi[type]);
}
});

return result;
}

function remapBaseStore(storeApi, baseStoreName, newStoreName) {
newStoreName = newStoreName || baseStoreName;
var result = {};
Object.keys(storeApi).forEach(function (type) {
if (type === 'get' || type === 'act' || type === 'mutate') {
result[type] = {};
Object.keys(storeApi[type]).forEach(function (pathName) {
result[type][pathName] = storeApi[type][pathName].replace(baseStoreName, newStoreName);
});
} else {
result[type] = remapBaseStore(storeApi[type], baseStoreName, newStoreName);
}
});

return result;
}

Expand Down Expand Up @@ -110,6 +150,10 @@ var hmrHandler = function (updatedModules) {
.forEach(function (handler) {
modules[handler.storeInstanceName] = handler.newStore(updatedModules[key]);
});

Object.keys(modules).forEach(function (m) {
api[m] = remapBaseStore(modules[m].$api, modules[m].name, m);
});
store$1.hotUpdate({ modules: modules });
});
};
Expand Down Expand Up @@ -201,6 +245,9 @@ function add(baseStoreName) {
if (!this.$store._modules.root._children[this['$vuex+'].storeInstanceName]) { // eslint-disable-line
this.$store.registerModule(this['$vuex+'].storeInstanceName, store);

var 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 @@ -214,6 +261,8 @@ 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 All @@ -223,29 +272,6 @@ function add(baseStoreName) {
};
}

var importer$1;

/**
* 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
* ```
*/
var api = {};

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

var addStore = add;

function searchDeeper(map, key) {
Expand All @@ -272,7 +298,6 @@ var getFullPath = function (config) {
var suffix = config.instance ? '$' + config.instance : '';
var getterKey = config.subpath.match(/[a-zA-Z]*/)[0];
var localApi = api[config.vuexPlus.baseStoreName];

if (getterKey !== config.vuexPlus.baseStoreName) {
localApi = searchDeeper(api[config.vuexPlus.baseStoreName], getterKey + suffix);
}
Expand Down Expand Up @@ -384,35 +409,49 @@ var newInstance = function newInstance(substore, instance) {
* @returns {any} - Value from Vuex getter
*/
var global = {
api: api,
get api() {
return clone(api);
},

get: function get(ref) {
var path = ref.path;
var context = ref.context;
var local = ref.local;

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

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

dispatch: function dispatch(ref) {
var path = ref.path;
var data = ref.data;
var context = ref.context;
var local = ref.local;

var localPath = getLocalPath(path, context);
if (local) {
var 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: function commit(ref) {
var path = ref.path;
var data = ref.data;
var context = ref.context;
var local = ref.local;

var localPath = getLocalPath(path, context);
if (local) {
var 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 @@ -427,7 +466,6 @@ var vuex_ = {
if (!setupDone && this.$store) {
setStore(this.$store);
var importer = contextHmr.getNewInstance();
generateAPI(importer);
setup(importer);
importer.getModules();
importer.setupHMR(hmrHandler);
Expand Down
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.3",
"version": "0.5.4",
"description": "Opinionated library that handles module instances in Vuex",
"main": "dist/vuex+.js",
"scripts": {
Expand Down

0 comments on commit 4d9b586

Please sign in to comment.