Skip to content

Commit

Permalink
[build] 4.0.0-alpha.1
Browse files Browse the repository at this point in the history
  • Loading branch information
ktsn committed Mar 15, 2020
1 parent 382cde3 commit 2598b22
Show file tree
Hide file tree
Showing 6 changed files with 349 additions and 291 deletions.
157 changes: 86 additions & 71 deletions dist/vuex.common.js
Original file line number Diff line number Diff line change
@@ -1,44 +1,35 @@
/**
* vuex v3.1.3
* vuex v4.0.0-alpha.1
* (c) 2020 Evan You
* @license MIT
*/
'use strict';

function applyMixin (Vue) {
var version = Number(Vue.version.split('.')[0]);
var vue = require('vue');

if (version >= 2) {
Vue.mixin({ beforeCreate: vuexInit });
} else {
// override init and inject vuex init procedure
// for 1.x backwards compatibility.
var _init = Vue.prototype._init;
Vue.prototype._init = function (options) {
if ( options === void 0 ) options = {};

options.init = options.init
? [vuexInit].concat(options.init)
: vuexInit;
_init.call(this, options);
};
}
var storeKey = 'store';

function useStore (key) {
if ( key === void 0 ) key = null;

return vue.inject(key !== null ? key : storeKey)
}

/**
* Vuex init hook, injected into each instances init hooks list.
*/

function vuexInit () {
var options = this.$options;
// store injection
if (options.store) {
this.$store = typeof options.store === 'function'
? options.store()
: options.store;
} else if (options.parent && options.parent.$store) {
this.$store = options.parent.$store;
function applyMixin (app, store, injectKey) {
app.provide(injectKey || storeKey, store);

// TODO: Refactor this to use `provide/inject`. It's currently
// not possible because Vue 3 doesn't work with `$` prefixed
// `provide/inject` at the moment.
app.mixin({
beforeCreate: function beforeCreate () {
if (!this.parent) {
this.$store = typeof store === 'function' ? store() : store;
} else {
this.$store = this.parent.$options.$store;
}
}
}
});
}

var target = typeof window !== 'undefined'
Expand Down Expand Up @@ -291,21 +282,28 @@ function makeAssertionMessage (path, key, type, value, expected) {
return buf
}

var Vue; // bind on install
// let Vue // bind on install

function createStore (options) {
return new Store(options)
}

var Store = function Store (options) {
var this$1 = this;
if ( options === void 0 ) options = {};

// TODO: Bring back this one if needed.
//
// Auto install if it is not done yet and `window` has `Vue`.
// To allow users to avoid auto-installation in some cases,
// this code should be placed here. See #731
if (!Vue && typeof window !== 'undefined' && window.Vue) {
install(window.Vue);
}
// if (!Vue && typeof window !== 'undefined' && window.Vue) {
// install(window.Vue)
// }

if (process.env.NODE_ENV !== 'production') {
assert(Vue, "must call Vue.use(Vuex) before creating a store instance.");
// TODO: Maybe we can remove this depending on the new implementation.
// 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.");
}
Expand All @@ -322,7 +320,6 @@ var Store = function Store (options) {
this._modules = new ModuleCollection(options);
this._modulesNamespaceMap = Object.create(null);
this._subscribers = [];
this._watcherVM = new Vue();
this._makeLocalGettersCache = Object.create(null);

// bind commit and dispatch to self
Expand Down Expand Up @@ -354,14 +351,31 @@ var Store = function Store (options) {
// apply plugins
plugins.forEach(function (plugin) { return plugin(this$1); });

var useDevtools = options.devtools !== undefined ? options.devtools : Vue.config.devtools;
var useDevtools = options.devtools !== undefined ? options.devtools : /* Vue.config.devtools */ true;
if (useDevtools) {
devtoolPlugin(this);
}
};

var prototypeAccessors$1 = { state: { configurable: true } };

Store.prototype.install = function install (app, injectKey) {
// TODO: Removing double install check for now. Maybe we can bring this
// feature back again if needed.
//
// if (Vue && _Vue === Vue) {
// if (process.env.NODE_ENV !== 'production') {
// console.error(
// '[vuex] already installed. Vue.use(Vuex) should be called only once.'
// )
// }
// return
// }
// Vue = _Vue

applyMixin(app, this, injectKey);
};

prototypeAccessors$1.state.get = function () {
return this._vm._data.$$state
};
Expand Down Expand Up @@ -467,13 +481,13 @@ Store.prototype.subscribeAction = function subscribeAction (fn) {
return genericSubscribe(subs, this._actionSubscribers)
};

Store.prototype.watch = function watch (getter, cb, options) {
Store.prototype.watch = function watch$1 (getter, cb, options) {
var this$1 = this;

if (process.env.NODE_ENV !== 'production') {
assert(typeof getter === 'function', "store.watch only accepts a function.");
}
return this._watcherVM.$watch(function () { return getter(this$1.state, this$1.getters); }, cb, options)
return vue.watch(function () { return getter(this$1.state, this$1.getters); }, cb, Object.assign({}, options))
};

Store.prototype.replaceState = function replaceState (state) {
Expand Down Expand Up @@ -512,7 +526,7 @@ Store.prototype.unregisterModule = function unregisterModule (path) {
this._modules.unregister(path);
this._withCommit(function () {
var parentState = getNestedState(this$1.state, path.slice(0, -1));
Vue.delete(parentState, path[path.length - 1]);
delete parentState[path[path.length - 1]];
});
resetStore(this);
};
Expand Down Expand Up @@ -563,30 +577,42 @@ function resetStoreVM (store, state, hot) {
// reset local getters cache
store._makeLocalGettersCache = Object.create(null);
var wrappedGetters = store._wrappedGetters;
var computed = {};
var computedObj = {};
forEachValue(wrappedGetters, function (fn, key) {
// TODO: Refactor following code and comment. We can simplify many things
// using computed function.
//
// use computed to leverage its lazy-caching mechanism
// direct inline function use will lead to closure preserving oldVm.
// using partial to return function with only arguments preserved in closure environment.
computed[key] = partial(fn, store);
computedObj[key] = partial(fn, store);
Object.defineProperty(store.getters, key, {
get: function () { return store._vm[key]; },
get: function () { return vue.computed(function () { return computedObj[key](); }).value; },
enumerable: true // for local getters
});
});

// TODO: Bring back this if it's still needed.
//
// use a Vue instance to store the state tree
// suppress warnings just in case the user has added
// some funky global mixins
var silent = Vue.config.silent;
Vue.config.silent = true;
store._vm = new Vue({
data: {
// const silent = Vue.config.silent
// Vue.config.silent = true

// TODO: Refactor the code and remove this comment.
//
// New impl with reactive. Defining redundunt keys to make it as close as
// the old impl api.
store._vm = vue.reactive({
_data: {
$$state: state
},
computed: computed
}
});
Vue.config.silent = silent;

// TODO: Bring back maybe?
//
// Vue.config.silent = silent

// enable strict mode for new vm
if (store.strict) {
Expand All @@ -601,7 +627,8 @@ function resetStoreVM (store, state, hot) {
oldVm._data.$$state = null;
});
}
Vue.nextTick(function () { return oldVm.$destroy(); });
// TODO: I think we don't need this anymore since we're not using vm?
// Vue.nextTick(() => oldVm.$destroy())
}
}

Expand Down Expand Up @@ -629,7 +656,7 @@ function installModule (store, rootState, path, module, hot) {
);
}
}
Vue.set(parentState, moduleName, module.state);
parentState[moduleName] = module.state;
});
}

Expand Down Expand Up @@ -790,11 +817,11 @@ function registerGetter (store, type, rawGetter, local) {
}

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

function getNestedState (state, path) {
Expand All @@ -815,19 +842,6 @@ function unifyObjectStyle (type, payload, options) {
return { type: type, payload: payload, options: options }
}

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

/**
* Reduce the code which written in Vue.js for getting the state.
* @param {String} [namespace] - Module's namespace
Expand Down Expand Up @@ -1039,9 +1053,10 @@ function getModuleByNamespace (store, helper, namespace) {
}

var index = {
version: '4.0.0-alpha.1',
createStore: createStore,
Store: Store,
install: install,
version: '3.1.3',
useStore: useStore,
mapState: mapState,
mapMutations: mapMutations,
mapGetters: mapGetters,
Expand Down

0 comments on commit 2598b22

Please sign in to comment.