@darwino/darwino
Version:
A set of Javascript classes and utilities
90 lines (69 loc) • 1.94 kB
JavaScript
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
/*
* (c) Copyright Darwino Inc. 2014-2017.
*/
export default class ArrayDataFetcher {
constructor(props) {
_defineProperty(this, "firstPage", 0);
_defineProperty(this, "pageSize", 1000000);
_defineProperty(this, "_array", []);
_defineProperty(this, "_eof", false);
_defineProperty(this, "_fetching", false);
_defineProperty(this, "_error", false);
_defineProperty(this, "_errorMsg", null);
Object.assign(this, props);
}
init() {
this._array = [];
this._eof = false;
this._error = false;
this._errorMsg = null;
this.loadMoreRows();
}
getArray() {
return this._array;
}
isFetching() {
return this._fetching;
}
isError() {
return this._error;
}
getErrorMessage() {
return this._errorMsg;
}
hasMoreRows() {
return !this._error && !this._eof;
}
getRow(i) {
return this._array[i];
}
getRows(skip, limit) {
return this._array.slice(skip, limit);
}
getRowCount() {
return this._array.length;
}
loadMoreRows() {
if (!this._eof) {
var num = this._array.length / this.pageSize + this.firstPage;
var pageSize = this.pageSize;
this.dataLoader(num, pageSize).then(data => {
this._array = this._array.concat(data);
this._eof = data.length < pageSize;
this._fetching = false;
this._error = false;
this._errorMsg = null; // Send an event for the content changed
if (this.onDataLoaded) this.onDataLoaded(num - this.firstPage, pageSize);
}, error => {
// Error
this._fetching = false;
this._error = true;
this._errorMsg = error.message; // Send an event for the content changed
if (this.onDataLoaded) this.onDataLoaded(0, 0);
});
this._fetching = true;
}
}
}
//# sourceMappingURL=ArrayDataFetcher.js.map