logger.js 4.9 KB

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