|
| 1 | +/*! |
| 2 | + * vuex v3.4.0 |
| 3 | + * (c) 2020 Evan You |
| 4 | + * @license MIT |
| 5 | + */ |
| 6 | +(function (global, factory) { |
| 7 | + typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() : |
| 8 | + typeof define === 'function' && define.amd ? define(factory) : |
| 9 | + (global = global || self, global.Vuex = factory()); |
| 10 | +}(this, (function () { 'use strict'; |
| 11 | + |
| 12 | + /** |
| 13 | + * Get the first item that pass the test |
| 14 | + * by second argument function |
| 15 | + * |
| 16 | + * @param {Array} list |
| 17 | + * @param {Function} f |
| 18 | + * @return {*} |
| 19 | + */ |
| 20 | + function find (list, f) { |
| 21 | + return list.filter(f)[0] |
| 22 | + } |
| 23 | + |
| 24 | + /** |
| 25 | + * Deep copy the given object considering circular structure. |
| 26 | + * This function caches all nested objects and its copies. |
| 27 | + * If it detects circular structure, use cached copy to avoid infinite loop. |
| 28 | + * |
| 29 | + * @param {*} obj |
| 30 | + * @param {Array<Object>} cache |
| 31 | + * @return {*} |
| 32 | + */ |
| 33 | + function deepCopy (obj, cache) { |
| 34 | + if ( cache === void 0 ) cache = []; |
| 35 | + |
| 36 | + // just return if obj is immutable value |
| 37 | + if (obj === null || typeof obj !== 'object') { |
| 38 | + return obj |
| 39 | + } |
| 40 | + |
| 41 | + // if obj is hit, it is in circular structure |
| 42 | + var hit = find(cache, function (c) { return c.original === obj; }); |
| 43 | + if (hit) { |
| 44 | + return hit.copy |
| 45 | + } |
| 46 | + |
| 47 | + var copy = Array.isArray(obj) ? [] : {}; |
| 48 | + // put the copy into cache at first |
| 49 | + // because we want to refer it in recursive deepCopy |
| 50 | + cache.push({ |
| 51 | + original: obj, |
| 52 | + copy: copy |
| 53 | + }); |
| 54 | + |
| 55 | + Object.keys(obj).forEach(function (key) { |
| 56 | + copy[key] = deepCopy(obj[key], cache); |
| 57 | + }); |
| 58 | + |
| 59 | + return copy |
| 60 | + } |
| 61 | + |
| 62 | + // Credits: borrowed code from fcomb/redux-logger |
| 63 | + |
| 64 | + function createLogger (ref) { |
| 65 | + if ( ref === void 0 ) ref = {}; |
| 66 | + var collapsed = ref.collapsed; if ( collapsed === void 0 ) collapsed = true; |
| 67 | + var filter = ref.filter; if ( filter === void 0 ) filter = function (mutation, stateBefore, stateAfter) { return true; }; |
| 68 | + var transformer = ref.transformer; if ( transformer === void 0 ) transformer = function (state) { return state; }; |
| 69 | + var mutationTransformer = ref.mutationTransformer; if ( mutationTransformer === void 0 ) mutationTransformer = function (mut) { return mut; }; |
| 70 | + var actionFilter = ref.actionFilter; if ( actionFilter === void 0 ) actionFilter = function (action, state) { return true; }; |
| 71 | + var actionTransformer = ref.actionTransformer; if ( actionTransformer === void 0 ) actionTransformer = function (act) { return act; }; |
| 72 | + var logMutations = ref.logMutations; if ( logMutations === void 0 ) logMutations = true; |
| 73 | + var logActions = ref.logActions; if ( logActions === void 0 ) logActions = true; |
| 74 | + var logger = ref.logger; if ( logger === void 0 ) logger = console; |
| 75 | + |
| 76 | + return function (store) { |
| 77 | + var prevState = deepCopy(store.state); |
| 78 | + |
| 79 | + if (typeof logger === 'undefined') { |
| 80 | + return |
| 81 | + } |
| 82 | + |
| 83 | + if (logMutations) { |
| 84 | + store.subscribe(function (mutation, state) { |
| 85 | + var nextState = deepCopy(state); |
| 86 | + |
| 87 | + if (filter(mutation, prevState, nextState)) { |
| 88 | + var formattedTime = getFormattedTime(); |
| 89 | + var formattedMutation = mutationTransformer(mutation); |
| 90 | + var message = "mutation " + (mutation.type) + formattedTime; |
| 91 | + |
| 92 | + startMessage(logger, message, collapsed); |
| 93 | + logger.log('%c prev state', 'color: #9E9E9E; font-weight: bold', transformer(prevState)); |
| 94 | + logger.log('%c mutation', 'color: #03A9F4; font-weight: bold', formattedMutation); |
| 95 | + logger.log('%c next state', 'color: #4CAF50; font-weight: bold', transformer(nextState)); |
| 96 | + endMessage(logger); |
| 97 | + } |
| 98 | + |
| 99 | + prevState = nextState; |
| 100 | + }); |
| 101 | + } |
| 102 | + |
| 103 | + if (logActions) { |
| 104 | + store.subscribeAction(function (action, state) { |
| 105 | + if (actionFilter(action, state)) { |
| 106 | + var formattedTime = getFormattedTime(); |
| 107 | + var formattedAction = actionTransformer(action); |
| 108 | + var message = "action " + (action.type) + formattedTime; |
| 109 | + |
| 110 | + startMessage(logger, message, collapsed); |
| 111 | + logger.log('%c action', 'color: #03A9F4; font-weight: bold', formattedAction); |
| 112 | + endMessage(logger); |
| 113 | + } |
| 114 | + }); |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + function startMessage (logger, message, collapsed) { |
| 120 | + var startMessage = collapsed |
| 121 | + ? logger.groupCollapsed |
| 122 | + : logger.group; |
| 123 | + |
| 124 | + // render |
| 125 | + try { |
| 126 | + startMessage.call(logger, message); |
| 127 | + } catch (e) { |
| 128 | + logger.log(message); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + function endMessage (logger) { |
| 133 | + try { |
| 134 | + logger.groupEnd(); |
| 135 | + } catch (e) { |
| 136 | + logger.log('—— log end ——'); |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + function getFormattedTime () { |
| 141 | + var time = new Date(); |
| 142 | + return (" @ " + (pad(time.getHours(), 2)) + ":" + (pad(time.getMinutes(), 2)) + ":" + (pad(time.getSeconds(), 2)) + "." + (pad(time.getMilliseconds(), 3))) |
| 143 | + } |
| 144 | + |
| 145 | + function repeat (str, times) { |
| 146 | + return (new Array(times + 1)).join(str) |
| 147 | + } |
| 148 | + |
| 149 | + function pad (num, maxLength) { |
| 150 | + return repeat('0', maxLength - num.toString().length) + num |
| 151 | + } |
| 152 | + |
| 153 | + return createLogger; |
| 154 | + |
| 155 | +}))); |
0 commit comments