vscroll
Version:
Virtual scroll engine
101 lines • 4.66 kB
JavaScript
import { __extends } from "tslib";
import { DatasourceGeneric, makeDatasource } from './classes/datasource';
import { Settings } from './classes/settings';
import { Logger } from './classes/logger';
import { Routines } from './classes/domRoutines';
import { Viewport } from './classes/viewport';
import { Buffer } from './classes/buffer';
import { State } from './classes/state';
import { Adapter } from './classes/adapter';
import { validate, DATASOURCE } from './inputs/index';
import core from './version';
export var INVALID_DATASOURCE_PREFIX = 'Invalid datasource:';
var instanceCount = 0;
var Scroller = /** @class */ (function () {
function Scroller(_a) {
var datasource = _a.datasource, consumer = _a.consumer, element = _a.element, workflow = _a.workflow, CustomRoutines = _a.Routines, scroller = _a.scroller;
var get = validate(datasource, DATASOURCE).params.get;
if (!get.isValid) {
throw new Error("".concat(INVALID_DATASOURCE_PREFIX, " ").concat(get.errors[0]));
}
var packageInfo = scroller ? scroller.state.packageInfo : { consumer: consumer, core: core };
element = scroller ? scroller.routines.element : element;
workflow = scroller ? scroller.workflow : workflow;
// In general, custom Routines must extend the original Routines. If not, we provide implicit extending.
// This is undocumented feature. It should be removed in vscroll v2.
if (CustomRoutines && !(CustomRoutines.prototype instanceof Routines)) {
var __Routines_1 = /** @class */ (function (_super) {
__extends(__Routines, _super);
function __Routines() {
return _super !== null && _super.apply(this, arguments) || this;
}
return __Routines;
}(Routines));
Object.getOwnPropertyNames(CustomRoutines.prototype)
.filter(function (method) { return method !== 'constructor'; })
.forEach(function (method) {
return __Routines_1.prototype[method] = CustomRoutines === null || CustomRoutines === void 0 ? void 0 : CustomRoutines.prototype[method];
});
CustomRoutines = __Routines_1;
}
this.workflow = workflow;
this.settings = new Settings(datasource.settings, datasource.devSettings, ++instanceCount);
this.logger = new Logger(this, packageInfo, datasource.adapter);
this.routines = new (CustomRoutines || Routines)(element, this.settings);
this.state = new State(packageInfo, this.settings, scroller ? scroller.state : void 0);
this.buffer = new Buffer(this.settings, workflow.onDataChanged, this.logger);
this.viewport = new Viewport(this.settings, this.routines, this.state, this.logger);
this.logger.object('vscroll settings object', this.settings, true);
this.initDatasource(datasource, scroller);
}
Scroller.prototype.initDatasource = function (datasource, scroller) {
var _this = this;
if (scroller) { // scroller re-instantiating case
this.datasource = datasource;
this.adapter = scroller.adapter;
// todo: what about (this.settings.adapter !== scroller.setting.adapter) case?
return;
}
// scroller is being instantiated for the first time
var constructed = datasource instanceof DatasourceGeneric;
var mockAdapter = !constructed && !this.settings.adapter;
if (constructed) { // datasource is already instantiated
this.datasource = datasource;
}
else { // datasource as POJO
var DS = makeDatasource(function () { return ({ mock: mockAdapter }); });
this.datasource = new DS(datasource);
if (this.settings.adapter) {
datasource.adapter = this.datasource.adapter;
}
}
var publicContext = !mockAdapter ? this.datasource.adapter : null;
this.adapter = new Adapter(publicContext, function () { return _this.workflow; }, this.logger);
};
Scroller.prototype.init = function (adapterRun$) {
var _this = this;
this.viewport.reset(this.buffer.startIndex);
this.logger.stat('initialization');
this.adapter.initialize({
buffer: this.buffer,
state: this.state,
viewport: this.viewport,
logger: this.logger,
adapterRun$: adapterRun$,
getWorkflow: function () { return _this.workflow; }
});
};
Scroller.prototype.dispose = function (forever) {
this.logger.log(function () { return 'disposing scroller' + (forever ? ' (forever)' : ''); });
if (forever) { // Adapter is not re-instantiated on reset
this.adapter.dispose();
}
this.buffer.dispose();
this.state.dispose();
};
Scroller.prototype.finalize = function () {
};
return Scroller;
}());
export { Scroller };
//# sourceMappingURL=scroller.js.map