Spaces:
Build error
Build error
File size: 4,795 Bytes
c211499 |
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 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
'use strict';
Object.defineProperty(exports, '__esModule', {
value: true
});
exports.default = void 0;
var _FifoQueue = _interopRequireDefault(require('./FifoQueue'));
var _types = require('./types');
function _interopRequireDefault(obj) {
return obj && obj.__esModule ? obj : {default: obj};
}
function _defineProperty(obj, key, value) {
if (key in obj) {
Object.defineProperty(obj, key, {
value: value,
enumerable: true,
configurable: true,
writable: true
});
} else {
obj[key] = value;
}
return obj;
}
class Farm {
constructor(_numOfWorkers, _callback, options = {}) {
var _options$workerSchedu, _options$taskQueue;
_defineProperty(this, '_computeWorkerKey', void 0);
_defineProperty(this, '_workerSchedulingPolicy', void 0);
_defineProperty(this, '_cacheKeys', Object.create(null));
_defineProperty(this, '_locks', []);
_defineProperty(this, '_offset', 0);
_defineProperty(this, '_taskQueue', void 0);
this._numOfWorkers = _numOfWorkers;
this._callback = _callback;
this._computeWorkerKey = options.computeWorkerKey;
this._workerSchedulingPolicy =
(_options$workerSchedu = options.workerSchedulingPolicy) !== null &&
_options$workerSchedu !== void 0
? _options$workerSchedu
: 'round-robin';
this._taskQueue =
(_options$taskQueue = options.taskQueue) !== null &&
_options$taskQueue !== void 0
? _options$taskQueue
: new _FifoQueue.default();
}
doWork(method, ...args) {
const customMessageListeners = new Set();
const addCustomMessageListener = listener => {
customMessageListeners.add(listener);
return () => {
customMessageListeners.delete(listener);
};
};
const onCustomMessage = message => {
customMessageListeners.forEach(listener => listener(message));
};
const promise = new Promise( // Bind args to this function so it won't reference to the parent scope.
// This prevents a memory leak in v8, because otherwise the function will
// retaine args for the closure.
((args, resolve, reject) => {
const computeWorkerKey = this._computeWorkerKey;
const request = [_types.CHILD_MESSAGE_CALL, false, method, args];
let worker = null;
let hash = null;
if (computeWorkerKey) {
hash = computeWorkerKey.call(this, method, ...args);
worker = hash == null ? null : this._cacheKeys[hash];
}
const onStart = worker => {
if (hash != null) {
this._cacheKeys[hash] = worker;
}
};
const onEnd = (error, result) => {
customMessageListeners.clear();
if (error) {
reject(error);
} else {
resolve(result);
}
};
const task = {
onCustomMessage,
onEnd,
onStart,
request
};
if (worker) {
this._taskQueue.enqueue(task, worker.getWorkerId());
this._process(worker.getWorkerId());
} else {
this._push(task);
}
}).bind(null, args)
);
promise.UNSTABLE_onCustomMessage = addCustomMessageListener;
return promise;
}
_process(workerId) {
if (this._isLocked(workerId)) {
return this;
}
const task = this._taskQueue.dequeue(workerId);
if (!task) {
return this;
}
if (task.request[1]) {
throw new Error('Queue implementation returned processed task');
} // Reference the task object outside so it won't be retained by onEnd,
// and other properties of the task object, such as task.request can be
// garbage collected.
const taskOnEnd = task.onEnd;
const onEnd = (error, result) => {
taskOnEnd(error, result);
this._unlock(workerId);
this._process(workerId);
};
task.request[1] = true;
this._lock(workerId);
this._callback(
workerId,
task.request,
task.onStart,
onEnd,
task.onCustomMessage
);
return this;
}
_push(task) {
this._taskQueue.enqueue(task);
const offset = this._getNextWorkerOffset();
for (let i = 0; i < this._numOfWorkers; i++) {
this._process((offset + i) % this._numOfWorkers);
if (task.request[1]) {
break;
}
}
return this;
}
_getNextWorkerOffset() {
switch (this._workerSchedulingPolicy) {
case 'in-order':
return 0;
case 'round-robin':
return this._offset++;
}
}
_lock(workerId) {
this._locks[workerId] = true;
}
_unlock(workerId) {
this._locks[workerId] = false;
}
_isLocked(workerId) {
return this._locks[workerId];
}
}
exports.default = Farm;
|