Skip to content

Instantly share code, notes, and snippets.

@heygambo
Created October 12, 2016 12:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save heygambo/c6ad82192134bdbcbbbef6f0d7dcf124 to your computer and use it in GitHub Desktop.
Save heygambo/c6ad82192134bdbcbbbef6f0d7dcf124 to your computer and use it in GitHub Desktop.
Vuex 1.0.0 logger.js
'use strict';
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) {
return typeof obj;
} : function (obj) {
return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
};
var asyncGenerator = function () {
function AwaitValue(value) {
this.value = value;
}
function AsyncGenerator(gen) {
var front, back;
function send(key, arg) {
return new Promise(function (resolve, reject) {
var request = {
key: key,
arg: arg,
resolve: resolve,
reject: reject,
next: null
};
if (back) {
back = back.next = request;
} else {
front = back = request;
resume(key, arg);
}
});
}
function resume(key, arg) {
try {
var result = gen[key](arg);
var value = result.value;
if (value instanceof AwaitValue) {
Promise.resolve(value.value).then(function (arg) {
resume("next", arg);
}, function (arg) {
resume("throw", arg);
});
} else {
settle(result.done ? "return" : "normal", result.value);
}
} catch (err) {
settle("throw", err);
}
}
function settle(type, value) {
switch (type) {
case "return":
front.resolve({
value: value,
done: true
});
break;
case "throw":
front.reject(value);
break;
default:
front.resolve({
value: value,
done: false
});
break;
}
front = front.next;
if (front) {
resume(front.key, front.arg);
} else {
back = null;
}
}
this._invoke = send;
if (typeof gen.return !== "function") {
this.return = undefined;
}
}
if (typeof Symbol === "function" && Symbol.asyncIterator) {
AsyncGenerator.prototype[Symbol.asyncIterator] = function () {
return this;
};
}
AsyncGenerator.prototype.next = function (arg) {
return this._invoke("next", arg);
};
AsyncGenerator.prototype.throw = function (arg) {
return this._invoke("throw", arg);
};
AsyncGenerator.prototype.return = function (arg) {
return this._invoke("return", arg);
};
return {
wrap: function (fn) {
return function () {
return new AsyncGenerator(fn.apply(this, arguments));
};
},
await: function (value) {
return new AwaitValue(value);
}
};
}();
/**
* Get the first item that pass the test
* by second argument function
*
* @param {Array} list
* @param {Function} f
* @return {*}
*/
function find(list, f) {
return list.filter(f)[0];
}
/**
* Deep copy the given object considering circular structure.
* This function caches all nested objects and its copies.
* If it detects circular structure, use cached copy to avoid infinite loop.
*
* @param {*} obj
* @param {Array<Object>} cache
* @return {*}
*/
function deepCopy(obj) {
var cache = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
// just return if obj is immutable value
if (obj === null || (typeof obj === 'undefined' ? 'undefined' : _typeof(obj)) !== 'object') {
return obj;
}
// if obj is hit, it is in circular structure
var hit = find(cache, function (c) {
return c.original === obj;
});
if (hit) {
return hit.copy;
}
var copy = Array.isArray(obj) ? [] : {};
// put the copy into cache at first
// because we want to refer it in recursive deepCopy
cache.push({
original: obj,
copy: copy
});
Object.keys(obj).forEach(function (key) {
copy[key] = deepCopy(obj[key], cache);
});
return copy;
}
function createLogger() {
var _ref = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
var _ref$collapsed = _ref.collapsed;
var collapsed = _ref$collapsed === undefined ? true : _ref$collapsed;
var _ref$transformer = _ref.transformer;
var transformer = _ref$transformer === undefined ? function (state) {
return state;
} : _ref$transformer;
var _ref$mutationTransfor = _ref.mutationTransformer;
var mutationTransformer = _ref$mutationTransfor === undefined ? function (mut) {
return mut;
} : _ref$mutationTransfor;
return function (store) {
var prevState = deepCopy(store.state);
store.subscribe(function (mutation, state) {
if (typeof console === 'undefined') {
return;
}
var nextState = deepCopy(state);
var time = new Date();
var formattedTime = ' @ ' + pad(time.getHours(), 2) + ':' + pad(time.getMinutes(), 2) + ':' + pad(time.getSeconds(), 2) + '.' + pad(time.getMilliseconds(), 3);
var formattedMutation = mutationTransformer(mutation);
var message = 'mutation ' + mutation.type + formattedTime;
var startMessage = collapsed ? console.groupCollapsed : console.group;
// render
try {
startMessage.call(console, message);
} catch (e) {
console.log(message);
}
console.log('%c prev state', 'color: #9E9E9E; font-weight: bold', transformer(prevState));
console.log('%c mutation', 'color: #03A9F4; font-weight: bold', formattedMutation);
console.log('%c next state', 'color: #4CAF50; font-weight: bold', transformer(nextState));
try {
console.groupEnd();
} catch (e) {
console.log('—— log end ——');
}
prevState = nextState;
});
};
}
function repeat(str, times) {
return new Array(times + 1).join(str);
}
function pad(num, maxLength) {
return repeat('0', maxLength - num.toString().length) + num;
}
module.exports = createLogger;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment