vscroll
Version:
Virtual scroll engine
211 lines • 8.03 kB
JavaScript
import { __read, __spreadArray } from "tslib";
import { CommonProcess, AdapterProcess, ProcessStatus as Status } from '../processes/index';
var Logger = /** @class */ (function () {
function Logger(scroller, packageInfo, adapter) {
var _this = this;
this.logs = [];
this.logAdapterMethod = function (methodName, args, add) {
if (!_this.debug) {
return;
}
var params = (args === void 0 ? [] : (Array.isArray(args) ? args : [args]))
.map(function (arg) {
if (typeof arg === 'function') {
return 'func';
}
else if (typeof arg !== 'object' || !arg) {
return arg;
}
else if (Array.isArray(arg)) {
return "[of ".concat(arg.length, "]");
}
return '{ ' + Object.keys(arg).join(', ') + ' }';
})
.join(', ');
_this.log("adapter: ".concat(methodName, "(").concat(params || '', ")").concat(add || ''));
};
var settings = scroller.settings;
this.debug = settings.debug;
this.immediateLog = settings.immediateLog;
this.logTime = settings.logTime;
this.getTime = function () {
return scroller.state && " // time: ".concat(scroller.state.time);
};
this.getStat = function () {
var buffer = scroller.buffer, viewport = scroller.viewport;
var first = buffer.getFirstVisibleItem();
var last = buffer.getLastVisibleItem();
return 'pos: ' + viewport.scrollPosition + ', ' +
'size: ' + viewport.getScrollableSize() + ', ' +
'bwd_p: ' + viewport.paddings.backward.size + ', ' +
'fwd_p: ' + viewport.paddings.forward.size + ', ' +
'default: ' + (buffer.defaultSize || 'no') + ', ' +
'items: ' + buffer.getVisibleItemsCount() + ', ' +
'range: ' + (first && last ? "[".concat(first.$index, "..").concat(last.$index, "]") : 'no');
};
this.getFetchRange = function () {
var _a = scroller.state.fetch, first = _a.first.index, last = _a.last.index;
return !Number.isNaN(first) && !Number.isNaN(last)
? "[".concat(first, "..").concat(last, "]")
: 'no';
};
this.getLoopId = function () { return scroller.state.cycle.loopId; };
this.getLoopIdNext = function () { return scroller.state.cycle.loopIdNext; };
this.getWorkflowCycleData = function () {
return "".concat(settings.instanceIndex, "-").concat(scroller.state.cycle.count);
};
this.getScrollPosition = function () { return scroller.routines.getScrollPosition(); };
this.log(function () {
return 'vscroll Workflow has been started, ' +
"core: ".concat(packageInfo.core.name, " v").concat(packageInfo.core.version, ", ") +
"consumer: ".concat(packageInfo.consumer.name, " v").concat(packageInfo.consumer.version, ", ") +
"scroller instance: ".concat(settings.instanceIndex, ", adapter ") +
(!adapter ? 'is not instantiated' : "instance: ".concat(adapter.id));
});
}
Logger.prototype.object = function (str, obj, stringify) {
this.log(function () { return [
str,
stringify
? JSON.stringify(obj, function (k, v) {
if (Number.isNaN(v)) {
return 'NaN';
}
if (v === Infinity) {
return 'Infinity';
}
if (v === -Infinity) {
return '-Infinity';
}
if (v instanceof Element) {
return 'HTMLElement';
}
if (v instanceof HTMLDocument) {
return 'HTMLDocument';
}
if (typeof v === 'function') {
return 'Function';
}
return v;
})
.replace(/"/g, '')
.replace(/(\{|:|,)/g, '$1 ')
.replace(/(\})/g, ' $1')
: obj
]; });
};
Logger.prototype.stat = function (str) {
var _this = this;
if (this.debug) {
var logStyles_1 = [
'color: #888; border: dashed #888 0; border-bottom-width: 0px',
'color: #000; border-width: 0'
];
this.log(function () { return __spreadArray(['%cstat' + (str ? " ".concat(str) : '') + ',%c ' + _this.getStat()], __read(logStyles_1), false); });
}
};
Logger.prototype.fetch = function (str) {
var _this = this;
if (this.debug) {
var _text_1 = 'fetch interval' + (str ? " ".concat(str) : '');
var logStyles_2 = ['color: #888', 'color: #000'];
this.log(function () { return __spreadArray(["%c".concat(_text_1, ": %c").concat(_this.getFetchRange())], __read(logStyles_2), false); });
}
};
Logger.prototype.prepareForLog = function (data) {
return data instanceof Event && data.target
? this.getScrollPosition()
: data;
};
Logger.prototype.logProcess = function (data) {
if (!this.debug) {
return;
}
var process = data.process, status = data.status, payload = data.payload;
// inner loop start-end log
var loopLog = [];
if (process === CommonProcess.init && status === Status.next) {
loopLog.push("%c---=== loop ".concat(this.getLoopIdNext(), " start"));
}
else if (process === CommonProcess.end) {
loopLog.push("%c---=== loop ".concat(this.getLoopId(), " done"));
var parent_1 = payload && payload.process;
if (status === Status.next && (parent_1 !== AdapterProcess.reset && parent_1 !== AdapterProcess.reload)) {
loopLog[0] += ", loop ".concat(this.getLoopIdNext(), " start");
}
}
if (loopLog.length) {
this.log(function () { return __spreadArray(__spreadArray([], __read(loopLog), false), ['color: #006600;'], false); });
}
};
Logger.prototype.logCycle = function (start) {
if (start === void 0) { start = true; }
var logData = this.getWorkflowCycleData();
var border = start ? '1px 0 0 1px' : '0 0 1px 1px';
var logStyles = "color: #0000aa; border: solid #555 1px; border-width: ".concat(border, "; margin-left: -2px");
this.log(function () { return ["%c ~~~ WF Cycle ".concat(logData, " ").concat(start ? 'STARTED' : 'FINALIZED', " ~~~ "), logStyles]; });
};
Logger.prototype.logError = function (str) {
var _this = this;
if (this.debug) {
var logStyles_3 = ['color: #a00;', 'color: #000'];
this.log(function () { return __spreadArray(['error:%c' + (str ? " ".concat(str) : '') + "%c (loop ".concat(_this.getLoopIdNext(), ")")], __read(logStyles_3), false); });
}
};
Logger.prototype.log = function () {
var _this = this;
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i] = arguments[_i];
}
if (this.debug) {
if (typeof args[0] === 'function') {
args = args[0]();
if (!Array.isArray(args)) {
args = [args];
}
}
if (args.every(function (item) { return item === void 0; })) {
return;
}
if (this.logTime) {
args = __spreadArray(__spreadArray([], __read(args), false), [this.getTime()], false);
}
args = args.map(function (arg) { return _this.prepareForLog(arg); });
if (this.immediateLog) {
console.log.apply(this, args);
}
else {
this.logs.push(args);
}
}
};
// logNow(...args: unknown[]) {
// const immediateLog = this.immediateLog;
// const debug = this.debug;
// (this as any).debug = true;
// (this as any).immediateLog = true;
// this.log.apply(this, args);
// (this as any).debug = debug;
// (this as any).immediateLog = immediateLog;
// }
Logger.prototype.logForce = function () {
var _this = this;
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i] = arguments[_i];
}
if (this.debug) {
if (!this.immediateLog && this.logs.length) {
this.logs.forEach(function (logArgs) { return console.log.apply(_this, logArgs); });
this.logs = [];
}
if (args.length) {
console.log.apply(this, args);
}
}
};
return Logger;
}());
export { Logger };
//# sourceMappingURL=logger.js.map