Skip to content

Commit

Permalink
Refactor to optimize memory usage, step 3
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon Grondin committed May 8, 2019
1 parent 0e397c1 commit 89db784
Show file tree
Hide file tree
Showing 8 changed files with 349 additions and 352 deletions.
270 changes: 141 additions & 129 deletions es5.js

Large diffs are not rendered by default.

58 changes: 27 additions & 31 deletions lib/Bottleneck.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,7 @@ Bottleneck = function () {
class Bottleneck {
constructor(options = {}, ...invalid) {
var storeInstanceOptions, storeOptions;
this._drainOne = this._drainOne.bind(this);
this.submit = this.submit.bind(this);
this.schedule = this.schedule.bind(this);
this.updateSettings = this.updateSettings.bind(this);
this.incrementReservoir = this.incrementReservoir.bind(this);
this._addToQueue = this._addToQueue.bind(this);

this._validateOptions(options, invalid);

Expand Down Expand Up @@ -351,7 +347,7 @@ Bottleneck = function () {
return waitForExecuting(1);
});

this._addToQueue = function (job) {
this._receive = function (job) {
return job._reject(new Bottleneck.prototype.BottleneckError(options.enqueueErrorMessage));
};

Expand All @@ -365,25 +361,17 @@ Bottleneck = function () {
_addToQueue(job) {
var _this2 = this;

var args, options;
args = job.args;
options = job.options;

if (!job.doReceive()) {
return false;
}

return this._submitLock.schedule(
/*#__PURE__*/
_asyncToGenerator(function* () {
var blocked, error, reachedHWM, shifted, strategy;
return _asyncToGenerator(function* () {
var args, blocked, error, options, reachedHWM, shifted, strategy;
args = job.args;
options = job.options;

try {
var _ref3 = yield _this2._store.__submit__(_this2.queued(), options.weight);
var _ref2 = yield _this2._store.__submit__(_this2.queued(), options.weight);

reachedHWM = _ref3.reachedHWM;
blocked = _ref3.blocked;
strategy = _ref3.strategy;
reachedHWM = _ref2.reachedHWM;
blocked = _ref2.blocked;
strategy = _ref2.strategy;
} catch (error1) {
error = error1;

Expand Down Expand Up @@ -424,21 +412,29 @@ Bottleneck = function () {

yield _this2._drainAll();
return reachedHWM;
}));
})();
}

_receive(job) {
if (job.doReceive()) {
return this._submitLock.schedule(this._addToQueue, job);
} else {
return false;
}
}

submit(...args) {
var cb, fn, job, options, ref, ref1, task;

if (typeof args[0] === "function") {
var _ref4, _ref5, _splice$call, _splice$call2;
var _ref3, _ref4, _splice$call, _splice$call2;

ref = args, (_ref4 = ref, _ref5 = _toArray(_ref4), fn = _ref5[0], args = _ref5.slice(1), _ref4), (_splice$call = splice.call(args, -1), _splice$call2 = _slicedToArray(_splice$call, 1), cb = _splice$call2[0], _splice$call);
ref = args, (_ref3 = ref, _ref4 = _toArray(_ref3), fn = _ref4[0], args = _ref4.slice(1), _ref3), (_splice$call = splice.call(args, -1), _splice$call2 = _slicedToArray(_splice$call, 1), cb = _splice$call2[0], _splice$call);
options = parser.load({}, this.jobDefaults);
} else {
var _ref6, _ref7, _splice$call3, _splice$call4;
var _ref5, _ref6, _splice$call3, _splice$call4;

ref1 = args, (_ref6 = ref1, _ref7 = _toArray(_ref6), options = _ref7[0], fn = _ref7[1], args = _ref7.slice(2), _ref6), (_splice$call3 = splice.call(args, -1), _splice$call4 = _slicedToArray(_splice$call3, 1), cb = _splice$call4[0], _splice$call3);
ref1 = args, (_ref5 = ref1, _ref6 = _toArray(_ref5), options = _ref6[0], fn = _ref6[1], args = _ref6.slice(2), _ref5), (_splice$call3 = splice.call(args, -1), _splice$call4 = _slicedToArray(_splice$call3, 1), cb = _splice$call4[0], _splice$call3);
options = parser.load(options, this.jobDefaults);
}

Expand All @@ -460,7 +456,7 @@ Bottleneck = function () {
return typeof cb === "function" ? cb(args) : void 0;
}
});
return this._addToQueue(job);
return this._receive(job);
}

schedule(...args) {
Expand All @@ -486,20 +482,20 @@ Bottleneck = function () {

job = new Job(task, args, options, this.jobDefaults, this.rejectOnDrop, this.Events, this._states, this.Promise);

this._addToQueue(job);
this._receive(job);

return job.promise;
}

wrap(fn) {
var schedule, wrapped;
schedule = this.schedule;
schedule = this.schedule.bind(this);

wrapped = function wrapped(...args) {
return schedule(fn.bind(this), ...args);
};

wrapped.withOptions = (options, ...args) => {
wrapped.withOptions = function (options, ...args) {
return schedule(options, fn, ...args);
};

Expand Down
1 change: 0 additions & 1 deletion lib/Group.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ Group = function () {
class Group {
constructor(limiterOptions = {}) {
this.deleteKey = this.deleteKey.bind(this);
this.updateSettings = this.updateSettings.bind(this);
this.limiterOptions = limiterOptions;
parser.load(this.limiterOptions, this.defaults, this);
this.Events = new Events(this);
Expand Down
104 changes: 49 additions & 55 deletions lib/Sync.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,14 @@
"use strict";

function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest(); }
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }

function _iterableToArrayLimit(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }

function _toArray(arr) { return _arrayWithHoles(arr) || _iterableToArray(arr) || _nonIterableRest(); }

function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance"); }

function _iterableToArray(iter) { if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === "[object Arguments]") return Array.from(iter); }

function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }

var DLList,
Sync,
splice = [].splice;
var DLList, Sync;
DLList = require("./DLList");
Sync = class Sync {
constructor(name, Promise) {
this.submit = this.submit.bind(this);
this.schedule = this.schedule.bind(this);
this.name = name;
this.Promise = Promise;
this._running = 0;
Expand All @@ -30,56 +20,60 @@ Sync = class Sync {
}

_tryToRun() {
var next;

if (this._running < 1 && this._queue.length > 0) {
this._running++;
next = this._queue.shift();
return next.task(...next.args, (...args) => {
this._running--;

this._tryToRun();

return typeof next.cb === "function" ? next.cb(...args) : void 0;
});
}
var _this = this;

return _asyncToGenerator(function* () {
var args, cb, error, reject, resolve, returned, task;

if (_this._running < 1 && _this._queue.length > 0) {
_this._running++;

var _this$_queue$shift = _this._queue.shift();

task = _this$_queue$shift.task;
args = _this$_queue$shift.args;
resolve = _this$_queue$shift.resolve;
reject = _this$_queue$shift.reject;
cb = yield _asyncToGenerator(function* () {
try {
returned = yield task(...args);
return function () {
return resolve(returned);
};
} catch (error1) {
error = error1;
return function () {
return reject(error);
};
}
})();
_this._running--;

_this._tryToRun();

return cb();
}
})();
}

submit(task, ...args) {
var _ref, _ref2, _splice$call, _splice$call2;

var cb, ref;
ref = args, (_ref = ref, _ref2 = _toArray(_ref), args = _ref2.slice(0), _ref), (_splice$call = splice.call(args, -1), _splice$call2 = _slicedToArray(_splice$call, 1), cb = _splice$call2[0], _splice$call);
schedule(task, ...args) {
var promise, reject, resolve;
resolve = reject = null;
promise = new this.Promise(function (_resolve, _reject) {
resolve = _resolve;
return reject = _reject;
});

this._queue.push({
task,
args,
cb
resolve,
reject
});

return this._tryToRun();
}
this._tryToRun();

schedule(task, ...args) {
var wrapped;

wrapped = function wrapped(...args) {
var _ref3, _ref4, _splice$call3, _splice$call4;

var cb, ref;
ref = args, (_ref3 = ref, _ref4 = _toArray(_ref3), args = _ref4.slice(0), _ref3), (_splice$call3 = splice.call(args, -1), _splice$call4 = _slicedToArray(_splice$call3, 1), cb = _splice$call4[0], _splice$call3);
return task(...args).then(function (...args) {
return cb(null, ...args);
}).catch(function (...args) {
return cb(...args);
});
};

return new this.Promise((resolve, reject) => {
return this.submit(wrapped, ...args, function (...args) {
return (args[0] != null ? reject : (args.shift(), resolve))(...args);
});
});
return promise;
}

};
Expand Down
Loading

0 comments on commit 89db784

Please sign in to comment.