Skip to content

Commit 2598b22

Browse files
committed
[build] 4.0.0-alpha.1
1 parent 382cde3 commit 2598b22

File tree

6 files changed

+349
-291
lines changed

6 files changed

+349
-291
lines changed

dist/vuex.common.js

Lines changed: 86 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,35 @@
11
/**
2-
* vuex v3.1.3
2+
* vuex v4.0.0-alpha.1
33
* (c) 2020 Evan You
44
* @license MIT
55
*/
66
'use strict';
77

8-
function applyMixin (Vue) {
9-
var version = Number(Vue.version.split('.')[0]);
8+
var vue = require('vue');
109

11-
if (version >= 2) {
12-
Vue.mixin({ beforeCreate: vuexInit });
13-
} else {
14-
// override init and inject vuex init procedure
15-
// for 1.x backwards compatibility.
16-
var _init = Vue.prototype._init;
17-
Vue.prototype._init = function (options) {
18-
if ( options === void 0 ) options = {};
19-
20-
options.init = options.init
21-
? [vuexInit].concat(options.init)
22-
: vuexInit;
23-
_init.call(this, options);
24-
};
25-
}
10+
var storeKey = 'store';
11+
12+
function useStore (key) {
13+
if ( key === void 0 ) key = null;
14+
15+
return vue.inject(key !== null ? key : storeKey)
16+
}
2617

27-
/**
28-
* Vuex init hook, injected into each instances init hooks list.
29-
*/
30-
31-
function vuexInit () {
32-
var options = this.$options;
33-
// store injection
34-
if (options.store) {
35-
this.$store = typeof options.store === 'function'
36-
? options.store()
37-
: options.store;
38-
} else if (options.parent && options.parent.$store) {
39-
this.$store = options.parent.$store;
18+
function applyMixin (app, store, injectKey) {
19+
app.provide(injectKey || storeKey, store);
20+
21+
// TODO: Refactor this to use `provide/inject`. It's currently
22+
// not possible because Vue 3 doesn't work with `$` prefixed
23+
// `provide/inject` at the moment.
24+
app.mixin({
25+
beforeCreate: function beforeCreate () {
26+
if (!this.parent) {
27+
this.$store = typeof store === 'function' ? store() : store;
28+
} else {
29+
this.$store = this.parent.$options.$store;
30+
}
4031
}
41-
}
32+
});
4233
}
4334

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

294-
var Vue; // bind on install
285+
// let Vue // bind on install
286+
287+
function createStore (options) {
288+
return new Store(options)
289+
}
295290

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

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

307304
if (process.env.NODE_ENV !== 'production') {
308-
assert(Vue, "must call Vue.use(Vuex) before creating a store instance.");
305+
// TODO: Maybe we can remove this depending on the new implementation.
306+
// assert(Vue, `must call Vue.use(Vuex) before creating a store instance.`)
309307
assert(typeof Promise !== 'undefined', "vuex requires a Promise polyfill in this browser.");
310308
assert(this instanceof Store, "store must be called with the new operator.");
311309
}
@@ -322,7 +320,6 @@ var Store = function Store (options) {
322320
this._modules = new ModuleCollection(options);
323321
this._modulesNamespaceMap = Object.create(null);
324322
this._subscribers = [];
325-
this._watcherVM = new Vue();
326323
this._makeLocalGettersCache = Object.create(null);
327324

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

357-
var useDevtools = options.devtools !== undefined ? options.devtools : Vue.config.devtools;
354+
var useDevtools = options.devtools !== undefined ? options.devtools : /* Vue.config.devtools */ true;
358355
if (useDevtools) {
359356
devtoolPlugin(this);
360357
}
361358
};
362359

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

362+
Store.prototype.install = function install (app, injectKey) {
363+
// TODO: Removing double install check for now. Maybe we can bring this
364+
// feature back again if needed.
365+
//
366+
// if (Vue && _Vue === Vue) {
367+
// if (process.env.NODE_ENV !== 'production') {
368+
// console.error(
369+
// '[vuex] already installed. Vue.use(Vuex) should be called only once.'
370+
// )
371+
// }
372+
// return
373+
// }
374+
// Vue = _Vue
375+
376+
applyMixin(app, this, injectKey);
377+
};
378+
365379
prototypeAccessors$1.state.get = function () {
366380
return this._vm._data.$$state
367381
};
@@ -467,13 +481,13 @@ Store.prototype.subscribeAction = function subscribeAction (fn) {
467481
return genericSubscribe(subs, this._actionSubscribers)
468482
};
469483

470-
Store.prototype.watch = function watch (getter, cb, options) {
484+
Store.prototype.watch = function watch$1 (getter, cb, options) {
471485
var this$1 = this;
472486

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

479493
Store.prototype.replaceState = function replaceState (state) {
@@ -512,7 +526,7 @@ Store.prototype.unregisterModule = function unregisterModule (path) {
512526
this._modules.unregister(path);
513527
this._withCommit(function () {
514528
var parentState = getNestedState(this$1.state, path.slice(0, -1));
515-
Vue.delete(parentState, path[path.length - 1]);
529+
delete parentState[path[path.length - 1]];
516530
});
517531
resetStore(this);
518532
};
@@ -563,30 +577,42 @@ function resetStoreVM (store, state, hot) {
563577
// reset local getters cache
564578
store._makeLocalGettersCache = Object.create(null);
565579
var wrappedGetters = store._wrappedGetters;
566-
var computed = {};
580+
var computedObj = {};
567581
forEachValue(wrappedGetters, function (fn, key) {
582+
// TODO: Refactor following code and comment. We can simplify many things
583+
// using computed function.
584+
//
568585
// use computed to leverage its lazy-caching mechanism
569586
// direct inline function use will lead to closure preserving oldVm.
570587
// using partial to return function with only arguments preserved in closure environment.
571-
computed[key] = partial(fn, store);
588+
computedObj[key] = partial(fn, store);
572589
Object.defineProperty(store.getters, key, {
573-
get: function () { return store._vm[key]; },
590+
get: function () { return vue.computed(function () { return computedObj[key](); }).value; },
574591
enumerable: true // for local getters
575592
});
576593
});
577594

595+
// TODO: Bring back this if it's still needed.
596+
//
578597
// use a Vue instance to store the state tree
579598
// suppress warnings just in case the user has added
580599
// some funky global mixins
581-
var silent = Vue.config.silent;
582-
Vue.config.silent = true;
583-
store._vm = new Vue({
584-
data: {
600+
// const silent = Vue.config.silent
601+
// Vue.config.silent = true
602+
603+
// TODO: Refactor the code and remove this comment.
604+
//
605+
// New impl with reactive. Defining redundunt keys to make it as close as
606+
// the old impl api.
607+
store._vm = vue.reactive({
608+
_data: {
585609
$$state: state
586-
},
587-
computed: computed
610+
}
588611
});
589-
Vue.config.silent = silent;
612+
613+
// TODO: Bring back maybe?
614+
//
615+
// Vue.config.silent = silent
590616

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

@@ -629,7 +656,7 @@ function installModule (store, rootState, path, module, hot) {
629656
);
630657
}
631658
}
632-
Vue.set(parentState, moduleName, module.state);
659+
parentState[moduleName] = module.state;
633660
});
634661
}
635662

@@ -790,11 +817,11 @@ function registerGetter (store, type, rawGetter, local) {
790817
}
791818

792819
function enableStrictMode (store) {
793-
store._vm.$watch(function () { return this._data.$$state }, function () {
820+
vue.watch(function () { return store._vm._data.$$state; }, function () {
794821
if (process.env.NODE_ENV !== 'production') {
795822
assert(store._committing, "do not mutate vuex store state outside mutation handlers.");
796823
}
797-
}, { deep: true, sync: true });
824+
}, { deep: true, flush: 'sync' });
798825
}
799826

800827
function getNestedState (state, path) {
@@ -815,19 +842,6 @@ function unifyObjectStyle (type, payload, options) {
815842
return { type: type, payload: payload, options: options }
816843
}
817844

818-
function install (_Vue) {
819-
if (Vue && _Vue === Vue) {
820-
if (process.env.NODE_ENV !== 'production') {
821-
console.error(
822-
'[vuex] already installed. Vue.use(Vuex) should be called only once.'
823-
);
824-
}
825-
return
826-
}
827-
Vue = _Vue;
828-
applyMixin(Vue);
829-
}
830-
831845
/**
832846
* Reduce the code which written in Vue.js for getting the state.
833847
* @param {String} [namespace] - Module's namespace
@@ -1039,9 +1053,10 @@ function getModuleByNamespace (store, helper, namespace) {
10391053
}
10401054

10411055
var index = {
1056+
version: '4.0.0-alpha.1',
1057+
createStore: createStore,
10421058
Store: Store,
1043-
install: install,
1044-
version: '3.1.3',
1059+
useStore: useStore,
10451060
mapState: mapState,
10461061
mapMutations: mapMutations,
10471062
mapGetters: mapGetters,

0 commit comments

Comments
 (0)