Skip to content

Commit

Permalink
Make ES5-compatible again as requested in #21
Browse files Browse the repository at this point in the history
  • Loading branch information
Rogier Schouten committed May 11, 2020
1 parent 80d2871 commit 9aba05c
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .jshintrc
@@ -1,7 +1,7 @@
{
"node": true, // Enable globals available when code is running inside of the NodeJS runtime environment.
"browser": true, // Standard browser globals e.g. `window`, `document`.
"esversion": 6, // Allow ES.next specific features such as `const` and `let`.
"esversion": 5,
"bitwise": false, // Prohibit bitwise operators (&, |, ^, etc.).
"camelcase": false, // Permit only camelcase for `var` and `object indexes`.
"curly": false, // Require {} for every new block or scope.
Expand Down
4 changes: 2 additions & 2 deletions gruntfile.js
Expand Up @@ -4,7 +4,7 @@ module.exports = function(grunt) {
// Unified Watch Object
var watchFiles = {
libJS: ['gruntfile.js', 'index.js', 'lib/**/*.js'],
testJS: ['test/**/*.js'],
testJS: ['test/**/*.js']
};

// Project Configuration
Expand Down Expand Up @@ -40,7 +40,7 @@ module.exports = function(grunt) {
timeout: 5000,
noFail: false
}
},
}
}
});

Expand Down
56 changes: 53 additions & 3 deletions lib/index.js
@@ -1,16 +1,66 @@
'use strict';

// Small Map polyfill to remain ES5-compatible as requested in issue #21 while also solving PR #30

var MapPolyfill = function () {
this.elements = [];
this.size = 0;
};

MapPolyfill.prototype.has = function (key) {
var i;
for (i = 0; i < this.elements.length; ++i) {
if (this.elements[i].key === key) {
return true;
}
}
return false;
};

MapPolyfill.prototype.get = function (key) {
var i;
for (i = 0; i < this.elements.length; ++i) {
if (this.elements[i].key === key) {
return this.elements[i].value;
}
}
return false;
};

MapPolyfill.prototype.set = function (key, value) {
var i;
for (i = 0; i < this.elements.length; ++i) {
if (this.elements[i].key === key) {
this.elements[i].value = value;
return;
}
}
this.elements.push({ key: key, value: value });
this.size++;
};

MapPolyfill.prototype.delete = function (key) {
var i;
for (i = 0; i < this.elements.length; ++i) {
if (this.elements[i].key === key) {
this.elements.splice(i, 1);
this.size--;
return;
}
}
};

var AsyncLock = function (opts) {
opts = opts || {};

this.Promise = opts.Promise || Promise;

// format: {key : [fn, fn]}
// queues.has(key) indicates job running for key
this.queues = new Map();
this.queues = new MapPolyfill();

// domain of current running func {key : fn}
this.domains = new Map();
this.domains = new MapPolyfill();

// lock is reentrant for same domain
this.domainReentrant = opts.domainReentrant || false;
Expand Down Expand Up @@ -252,4 +302,4 @@ AsyncLock.prototype._promiseTry = function(fn) {
}
};

module.exports = AsyncLock;
module.exports = AsyncLock;

0 comments on commit 9aba05c

Please sign in to comment.