1
1
/**
2
- * vuex v3.1.3
2
+ * vuex v4.0.0-alpha.1
3
3
* (c) 2020 Evan You
4
4
* @license MIT
5
5
*/
6
6
'use strict' ;
7
7
8
- function applyMixin ( Vue ) {
9
- var version = Number ( Vue . version . split ( '.' ) [ 0 ] ) ;
8
+ var vue = require ( 'vue' ) ;
10
9
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
+ }
26
17
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
+ }
40
31
}
41
- }
32
+ } ) ;
42
33
}
43
34
44
35
var target = typeof window !== 'undefined'
@@ -291,21 +282,28 @@ function makeAssertionMessage (path, key, type, value, expected) {
291
282
return buf
292
283
}
293
284
294
- var Vue ; // bind on install
285
+ // let Vue // bind on install
286
+
287
+ function createStore ( options ) {
288
+ return new Store ( options )
289
+ }
295
290
296
291
var Store = function Store ( options ) {
297
292
var this$1 = this ;
298
293
if ( options === void 0 ) options = { } ;
299
294
295
+ // TODO: Bring back this one if needed.
296
+ //
300
297
// Auto install if it is not done yet and `window` has `Vue`.
301
298
// To allow users to avoid auto-installation in some cases,
302
299
// 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
+ // }
306
303
307
304
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.`)
309
307
assert ( typeof Promise !== 'undefined' , "vuex requires a Promise polyfill in this browser." ) ;
310
308
assert ( this instanceof Store , "store must be called with the new operator." ) ;
311
309
}
@@ -322,7 +320,6 @@ var Store = function Store (options) {
322
320
this . _modules = new ModuleCollection ( options ) ;
323
321
this . _modulesNamespaceMap = Object . create ( null ) ;
324
322
this . _subscribers = [ ] ;
325
- this . _watcherVM = new Vue ( ) ;
326
323
this . _makeLocalGettersCache = Object . create ( null ) ;
327
324
328
325
// bind commit and dispatch to self
@@ -354,14 +351,31 @@ var Store = function Store (options) {
354
351
// apply plugins
355
352
plugins . forEach ( function ( plugin ) { return plugin ( this$1 ) ; } ) ;
356
353
357
- var useDevtools = options . devtools !== undefined ? options . devtools : Vue . config . devtools ;
354
+ var useDevtools = options . devtools !== undefined ? options . devtools : /* Vue.config.devtools */ true ;
358
355
if ( useDevtools ) {
359
356
devtoolPlugin ( this ) ;
360
357
}
361
358
} ;
362
359
363
360
var prototypeAccessors$1 = { state : { configurable : true } } ;
364
361
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
+
365
379
prototypeAccessors$1 . state . get = function ( ) {
366
380
return this . _vm . _data . $$state
367
381
} ;
@@ -467,13 +481,13 @@ Store.prototype.subscribeAction = function subscribeAction (fn) {
467
481
return genericSubscribe ( subs , this . _actionSubscribers )
468
482
} ;
469
483
470
- Store . prototype . watch = function watch ( getter , cb , options ) {
484
+ Store . prototype . watch = function watch$1 ( getter , cb , options ) {
471
485
var this$1 = this ;
472
486
473
487
if ( process . env . NODE_ENV !== 'production' ) {
474
488
assert ( typeof getter === 'function' , "store.watch only accepts a function." ) ;
475
489
}
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 ) )
477
491
} ;
478
492
479
493
Store . prototype . replaceState = function replaceState ( state ) {
@@ -512,7 +526,7 @@ Store.prototype.unregisterModule = function unregisterModule (path) {
512
526
this . _modules . unregister ( path ) ;
513
527
this . _withCommit ( function ( ) {
514
528
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 ] ] ;
516
530
} ) ;
517
531
resetStore ( this ) ;
518
532
} ;
@@ -563,30 +577,42 @@ function resetStoreVM (store, state, hot) {
563
577
// reset local getters cache
564
578
store . _makeLocalGettersCache = Object . create ( null ) ;
565
579
var wrappedGetters = store . _wrappedGetters ;
566
- var computed = { } ;
580
+ var computedObj = { } ;
567
581
forEachValue ( wrappedGetters , function ( fn , key ) {
582
+ // TODO: Refactor following code and comment. We can simplify many things
583
+ // using computed function.
584
+ //
568
585
// use computed to leverage its lazy-caching mechanism
569
586
// direct inline function use will lead to closure preserving oldVm.
570
587
// using partial to return function with only arguments preserved in closure environment.
571
- computed [ key ] = partial ( fn , store ) ;
588
+ computedObj [ key ] = partial ( fn , store ) ;
572
589
Object . defineProperty ( store . getters , key , {
573
- get : function ( ) { return store . _vm [ key ] ; } ,
590
+ get : function ( ) { return vue . computed ( function ( ) { return computedObj [ key ] ( ) ; } ) . value ; } ,
574
591
enumerable : true // for local getters
575
592
} ) ;
576
593
} ) ;
577
594
595
+ // TODO: Bring back this if it's still needed.
596
+ //
578
597
// use a Vue instance to store the state tree
579
598
// suppress warnings just in case the user has added
580
599
// 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 : {
585
609
$$state : state
586
- } ,
587
- computed : computed
610
+ }
588
611
} ) ;
589
- Vue . config . silent = silent ;
612
+
613
+ // TODO: Bring back maybe?
614
+ //
615
+ // Vue.config.silent = silent
590
616
591
617
// enable strict mode for new vm
592
618
if ( store . strict ) {
@@ -601,7 +627,8 @@ function resetStoreVM (store, state, hot) {
601
627
oldVm . _data . $$state = null ;
602
628
} ) ;
603
629
}
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())
605
632
}
606
633
}
607
634
@@ -629,7 +656,7 @@ function installModule (store, rootState, path, module, hot) {
629
656
) ;
630
657
}
631
658
}
632
- Vue . set ( parentState , moduleName , module . state ) ;
659
+ parentState [ moduleName ] = module . state ;
633
660
} ) ;
634
661
}
635
662
@@ -790,11 +817,11 @@ function registerGetter (store, type, rawGetter, local) {
790
817
}
791
818
792
819
function enableStrictMode ( store ) {
793
- store . _vm . $ watch( function ( ) { return this . _data . $$state } , function ( ) {
820
+ vue . watch ( function ( ) { return store . _vm . _data . $$state ; } , function ( ) {
794
821
if ( process . env . NODE_ENV !== 'production' ) {
795
822
assert ( store . _committing , "do not mutate vuex store state outside mutation handlers." ) ;
796
823
}
797
- } , { deep : true , sync : true } ) ;
824
+ } , { deep : true , flush : 'sync' } ) ;
798
825
}
799
826
800
827
function getNestedState ( state , path ) {
@@ -815,19 +842,6 @@ function unifyObjectStyle (type, payload, options) {
815
842
return { type : type , payload : payload , options : options }
816
843
}
817
844
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
-
831
845
/**
832
846
* Reduce the code which written in Vue.js for getting the state.
833
847
* @param {String } [namespace] - Module's namespace
@@ -1039,9 +1053,10 @@ function getModuleByNamespace (store, helper, namespace) {
1039
1053
}
1040
1054
1041
1055
var index = {
1056
+ version : '4.0.0-alpha.1' ,
1057
+ createStore : createStore ,
1042
1058
Store : Store ,
1043
- install : install ,
1044
- version : '3.1.3' ,
1059
+ useStore : useStore ,
1045
1060
mapState : mapState ,
1046
1061
mapMutations : mapMutations ,
1047
1062
mapGetters : mapGetters ,
0 commit comments