This repository was archived by the owner on Dec 9, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathBulkLoader.js
191 lines (191 loc) · 7.18 KB
/
BulkLoader.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
var __extends = (this && this.__extends) || (function () {
var extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
(function (factory) {
if (typeof module === "object" && typeof module.exports === "object") {
var v = factory(require, exports);
if (v !== undefined) module.exports = v;
}
else if (typeof define === "function" && define.amd) {
define(["require", "exports", "../event/EventDispatcher", "../event/LoaderEvent", "../event/BulkLoaderEvent", "../model/Collection"], factory);
}
})(function (require, exports) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var EventDispatcher_1 = require("../event/EventDispatcher");
var LoaderEvent_1 = require("../event/LoaderEvent");
var BulkLoaderEvent_1 = require("../event/BulkLoaderEvent");
var Collection_1 = require("../model/Collection");
/**
* A class to help with loading IDataStore's.
*
* @class BulkLoader
* @extends EventDispatcher
* @module StructureJS
* @submodule util
* @requires Extend
* @requires LoaderEvent
* @constructor
* @author Robert S. (www.codeBelt.com)
*/
var BulkLoader = (function (_super) {
__extends(BulkLoader, _super);
function BulkLoader() {
var _this = _super.call(this) || this;
/**
* A collection to store all the IDataStore's.
*
* @property _dataStores
* @type {Collection}
* @protected
*/
_this._dataStores = null;
/**
* The total number of items that have been loaded.
*
* @property _totalComplete
* @type {number}
* @protected
*/
_this._totalComplete = 0;
/**
* A queue of IDataStore's that need to be loaded still.
*
* @property _queue
* @type {Array<{key:any, value:any}>}
* @protected
*/
_this._queue = [];
/**
* Set the maximum number of simultaneous connections (default is 3).
*
* @property maxConnections
* @type [number=3]
* @public
*/
_this.maxConnections = 3;
_this._dataStores = new Collection_1.default();
return _this;
}
/**
* Helper method to add IDataStore's.
*
* @method addFile
* @param dataStore {IDataStore}
* @param key [string=null]
* @public
*/
BulkLoader.prototype.addFile = function (dataStore, key) {
if (key === void 0) { key = null; }
if (key === null) {
key = String(dataStore.sjsId);
}
var model = {
key: key,
value: dataStore
};
this._dataStores.add(model);
};
/**
* Helper method to get IDataStore's.
*
* @method getFile
* @param key {string}
* @public
*/
BulkLoader.prototype.getFile = function (key) {
var model = this._dataStores.findBy({ key: key })[0];
return model.value;
};
/**
* Helper method to start the loading process.
*
* @method start
* @public
*/
BulkLoader.prototype.start = function () {
var dataStore;
this._queue = this._dataStores.models.slice(0);
var length = (this.maxConnections > this._queue.length) ? this._queue.length : this.maxConnections;
for (var i = 0; i < length; i++) {
dataStore = this._queue.shift().value;
dataStore.addEventListenerOnce(LoaderEvent_1.default.COMPLETE, this._onComplete, this);
dataStore.load();
}
return this;
};
/**
* Helper method to stop/clear the loader.
*
* @method clear
* @public
*/
BulkLoader.prototype.clear = function () {
this._queue = [];
var dataStore;
for (var i = 0; i < this._dataStores.length; i++) {
dataStore = this._dataStores.get(i).value;
dataStore.removeEventListener(LoaderEvent_1.default.COMPLETE, this._onComplete, this);
}
this._totalComplete = 0;
this._dataStores = new Collection_1.default();
};
/**
* Event handler method called every time a IDataStore's is completely loaded.
*
* @method _onComplete
* @param event {LoaderEvent}
* @protected
*/
BulkLoader.prototype._onComplete = function (event) {
var dataStore = event.target;
this._totalComplete++;
var bulkLoaderEvent = new BulkLoaderEvent_1.default(BulkLoaderEvent_1.default.COMPLETE);
bulkLoaderEvent.total = this._dataStores.length;
bulkLoaderEvent.totalComplete = this._totalComplete;
bulkLoaderEvent.percentComplete = (this._totalComplete / this._dataStores.length) * 100;
bulkLoaderEvent.data = dataStore;
// Dispatch the IDataStore that was just completed.
this.dispatchEvent(bulkLoaderEvent);
if (this._queue.length !== 0) {
var dataStore_1 = this._queue.shift().value;
dataStore_1.addEventListenerOnce(LoaderEvent_1.default.COMPLETE, this._onComplete, this);
dataStore_1.load();
}
if (this._totalComplete === this._dataStores.length) {
this._onLoadComplete();
}
};
/**
* Event handler method called once all IDataStore's are completely loaded.
*
* @method _onLoadComplete
* @protected
*/
BulkLoader.prototype._onLoadComplete = function () {
var model;
var dataStoreList = [];
for (var i = 0; i < this._dataStores.length; i++) {
model = this._dataStores.get(i);
dataStoreList[i] = model.value;
}
// Add the whole list because all are completed.
var bulkLoaderEvent = new BulkLoaderEvent_1.default(BulkLoaderEvent_1.default.LOAD_COMPLETE);
bulkLoaderEvent.total = this._dataStores.length;
bulkLoaderEvent.totalComplete = this._totalComplete;
bulkLoaderEvent.percentComplete = (this._totalComplete / this._dataStores.length) * 100;
bulkLoaderEvent.data = dataStoreList;
// Only dispatch when all the IDataStore are load complete.
this.dispatchEvent(bulkLoaderEvent);
};
return BulkLoader;
}(EventDispatcher_1.default));
exports.default = BulkLoader;
});