Skip to content

Commit a81ab98

Browse files
committed
[build] 2.5.0
1 parent 35ebfcf commit a81ab98

File tree

5 files changed

+186
-80
lines changed

5 files changed

+186
-80
lines changed

dist/logger.js

+10-9
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,13 @@ function createLogger (ref) {
6666
var filter = ref.filter; if ( filter === void 0 ) filter = function (mutation, stateBefore, stateAfter) { return true; };
6767
var transformer = ref.transformer; if ( transformer === void 0 ) transformer = function (state) { return state; };
6868
var mutationTransformer = ref.mutationTransformer; if ( mutationTransformer === void 0 ) mutationTransformer = function (mut) { return mut; };
69+
var logger = ref.logger; if ( logger === void 0 ) logger = console;
6970

7071
return function (store) {
7172
var prevState = deepCopy(store.state);
7273

7374
store.subscribe(function (mutation, state) {
74-
if (typeof console === 'undefined') {
75+
if (typeof logger === 'undefined') {
7576
return
7677
}
7778
var nextState = deepCopy(state);
@@ -82,24 +83,24 @@ function createLogger (ref) {
8283
var formattedMutation = mutationTransformer(mutation);
8384
var message = "mutation " + (mutation.type) + formattedTime;
8485
var startMessage = collapsed
85-
? console.groupCollapsed
86-
: console.group;
86+
? logger.groupCollapsed
87+
: logger.group;
8788

8889
// render
8990
try {
90-
startMessage.call(console, message);
91+
startMessage.call(logger, message);
9192
} catch (e) {
9293
console.log(message);
9394
}
9495

95-
console.log('%c prev state', 'color: #9E9E9E; font-weight: bold', transformer(prevState));
96-
console.log('%c mutation', 'color: #03A9F4; font-weight: bold', formattedMutation);
97-
console.log('%c next state', 'color: #4CAF50; font-weight: bold', transformer(nextState));
96+
logger.log('%c prev state', 'color: #9E9E9E; font-weight: bold', transformer(prevState));
97+
logger.log('%c mutation', 'color: #03A9F4; font-weight: bold', formattedMutation);
98+
logger.log('%c next state', 'color: #4CAF50; font-weight: bold', transformer(nextState));
9899

99100
try {
100-
console.groupEnd();
101+
logger.groupEnd();
101102
} catch (e) {
102-
console.log('—— log end ——');
103+
logger.log('—— log end ——');
103104
}
104105
}
105106

dist/vuex.common.js

+58-23
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* vuex v2.4.1
2+
* vuex v2.5.0
33
* (c) 2017 Evan You
44
* @license MIT
55
*/
@@ -246,26 +246,44 @@ function update (path, targetModule, newModule) {
246246
}
247247
}
248248

249+
var functionAssert = {
250+
assert: function (value) { return typeof value === 'function'; },
251+
expected: 'function'
252+
};
253+
254+
var objectAssert = {
255+
assert: function (value) { return typeof value === 'function' ||
256+
(typeof value === 'object' && typeof value.handler === 'function'); },
257+
expected: 'function or object with "handler" function'
258+
};
259+
260+
var assertTypes = {
261+
getters: functionAssert,
262+
mutations: functionAssert,
263+
actions: objectAssert
264+
};
265+
249266
function assertRawModule (path, rawModule) {
250-
['getters', 'actions', 'mutations'].forEach(function (key) {
267+
Object.keys(assertTypes).forEach(function (key) {
251268
if (!rawModule[key]) { return }
252269

270+
var assertOptions = assertTypes[key];
271+
253272
forEachValue(rawModule[key], function (value, type) {
254273
assert(
255-
typeof value === 'function',
256-
makeAssertionMessage(path, key, type, value)
274+
assertOptions.assert(value),
275+
makeAssertionMessage(path, key, type, value, assertOptions.expected)
257276
);
258277
});
259278
});
260279
}
261280

262-
function makeAssertionMessage (path, key, type, value) {
263-
var buf = key + " should be function but \"" + key + "." + type + "\"";
281+
function makeAssertionMessage (path, key, type, value, expected) {
282+
var buf = key + " should be " + expected + " but \"" + key + "." + type + "\"";
264283
if (path.length > 0) {
265284
buf += " in module \"" + (path.join('.')) + "\"";
266285
}
267286
buf += " is " + (JSON.stringify(value)) + ".";
268-
269287
return buf
270288
}
271289

@@ -293,12 +311,13 @@ var Store = function Store (options) {
293311

294312
var state = options.state; if ( state === void 0 ) state = {};
295313
if (typeof state === 'function') {
296-
state = state();
314+
state = state() || {};
297315
}
298316

299317
// store internal state
300318
this._committing = false;
301319
this._actions = Object.create(null);
320+
this._actionSubscribers = [];
302321
this._mutations = Object.create(null);
303322
this._wrappedGetters = Object.create(null);
304323
this._modules = new ModuleCollection(options);
@@ -386,34 +405,35 @@ Store.prototype.commit = function commit (_type, _payload, _options) {
386405
};
387406

388407
Store.prototype.dispatch = function dispatch (_type, _payload) {
408+
var this$1 = this;
409+
389410
// check object-style dispatch
390411
var ref = unifyObjectStyle(_type, _payload);
391412
var type = ref.type;
392413
var payload = ref.payload;
393414

415+
var action = { type: type, payload: payload };
394416
var entry = this._actions[type];
395417
if (!entry) {
396418
if (process.env.NODE_ENV !== 'production') {
397419
console.error(("[vuex] unknown action type: " + type));
398420
}
399421
return
400422
}
423+
424+
this._actionSubscribers.forEach(function (sub) { return sub(action, this$1.state); });
425+
401426
return entry.length > 1
402427
? Promise.all(entry.map(function (handler) { return handler(payload); }))
403428
: entry[0](payload)
404429
};
405430

406431
Store.prototype.subscribe = function subscribe (fn) {
407-
var subs = this._subscribers;
408-
if (subs.indexOf(fn) < 0) {
409-
subs.push(fn);
410-
}
411-
return function () {
412-
var i = subs.indexOf(fn);
413-
if (i > -1) {
414-
subs.splice(i, 1);
415-
}
416-
}
432+
return genericSubscribe(fn, this._subscribers)
433+
};
434+
435+
Store.prototype.subscribeAction = function subscribeAction (fn) {
436+
return genericSubscribe(fn, this._actionSubscribers)
417437
};
418438

419439
Store.prototype.watch = function watch (getter, cb, options) {
@@ -433,7 +453,9 @@ Store.prototype.replaceState = function replaceState (state) {
433453
});
434454
};
435455

436-
Store.prototype.registerModule = function registerModule (path, rawModule) {
456+
Store.prototype.registerModule = function registerModule (path, rawModule, options) {
457+
if ( options === void 0 ) options = {};
458+
437459
if (typeof path === 'string') { path = [path]; }
438460

439461
if (process.env.NODE_ENV !== 'production') {
@@ -442,7 +464,7 @@ Store.prototype.registerModule = function registerModule (path, rawModule) {
442464
}
443465

444466
this._modules.register(path, rawModule);
445-
installModule(this, this.state, path, this._modules.get(path));
467+
installModule(this, this.state, path, this._modules.get(path), options.preserveState);
446468
// reset store to update getters...
447469
resetStoreVM(this, this.state);
448470
};
@@ -478,6 +500,18 @@ Store.prototype._withCommit = function _withCommit (fn) {
478500

479501
Object.defineProperties( Store.prototype, prototypeAccessors );
480502

503+
function genericSubscribe (fn, subs) {
504+
if (subs.indexOf(fn) < 0) {
505+
subs.push(fn);
506+
}
507+
return function () {
508+
var i = subs.indexOf(fn);
509+
if (i > -1) {
510+
subs.splice(i, 1);
511+
}
512+
}
513+
}
514+
481515
function resetStore (store, hot) {
482516
store._actions = Object.create(null);
483517
store._mutations = Object.create(null);
@@ -562,8 +596,9 @@ function installModule (store, rootState, path, module, hot) {
562596
});
563597

564598
module.forEachAction(function (action, key) {
565-
var namespacedType = namespace + key;
566-
registerAction(store, namespacedType, action, local);
599+
var type = action.root ? key : namespace + key;
600+
var handler = action.handler || action;
601+
registerAction(store, type, handler, local);
567602
});
568603

569604
module.forEachGetter(function (getter, key) {
@@ -886,7 +921,7 @@ function getModuleByNamespace (store, helper, namespace) {
886921
var index = {
887922
Store: Store,
888923
install: install,
889-
version: '2.4.1',
924+
version: '2.5.0',
890925
mapState: mapState,
891926
mapMutations: mapMutations,
892927
mapGetters: mapGetters,

0 commit comments

Comments
 (0)