@scoir/async-throttler
Advanced tools
Comparing version 3.0.0-beta.ed162590 to 3.0.0
@@ -1,3 +0,17 @@ | ||
class Throttler { | ||
constructor(cb) { | ||
"use strict"; | ||
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.default = void 0; | ||
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck")); | ||
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass")); | ||
var Throttler = /*#__PURE__*/function () { | ||
function Throttler(cb) { | ||
(0, _classCallCheck2.default)(this, Throttler); | ||
this.cb = cb; | ||
@@ -12,38 +26,52 @@ this.cb({ | ||
execute(promise, delay = 700) { | ||
this.cb({ | ||
isProcessing: true, | ||
isProcessed: false, | ||
isComplete: false, | ||
isError: false | ||
}); // NOTE: this is exposed only for testing and should not be used outside of that context | ||
(0, _createClass2.default)(Throttler, [{ | ||
key: "execute", | ||
value: function execute(promise) { | ||
var _this = this; | ||
this._executedPromise = Promise.all([promise, new Promise(resolve => setTimeout(resolve, delay))]).then(() => this.cb({ | ||
isProcessing: false, | ||
isProcessed: true, | ||
isComplete: true, | ||
isError: false | ||
})).catch(() => this.cb({ | ||
isProcessing: false, | ||
isProcessed: false, | ||
isComplete: true, | ||
isError: true | ||
})); | ||
} | ||
var delay = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 700; | ||
this.cb({ | ||
isProcessing: true, | ||
isProcessed: false, | ||
isComplete: false, | ||
isError: false | ||
}); // NOTE: this is exposed only for testing and should not be used outside of that context | ||
reset() { | ||
this.cb({ | ||
isProcessing: false, | ||
isProcessed: false, | ||
isComplete: false, | ||
isError: false | ||
}); | ||
} | ||
this._executedPromise = Promise.all([promise, new Promise(function (resolve) { | ||
return setTimeout(resolve, delay); | ||
})]).then(function () { | ||
return _this.cb({ | ||
isProcessing: false, | ||
isProcessed: true, | ||
isComplete: true, | ||
isError: false | ||
}); | ||
}).catch(function () { | ||
return _this.cb({ | ||
isProcessing: false, | ||
isProcessed: false, | ||
isComplete: true, | ||
isError: true | ||
}); | ||
}); | ||
} | ||
}, { | ||
key: "reset", | ||
value: function reset() { | ||
this.cb({ | ||
isProcessing: false, | ||
isProcessed: false, | ||
isComplete: false, | ||
isError: false | ||
}); | ||
} | ||
}]); | ||
return Throttler; | ||
}(); | ||
} | ||
export default { | ||
create: cb => { | ||
var _default = { | ||
create: function create(cb) { | ||
return new Throttler(cb); | ||
} | ||
}; | ||
}; | ||
exports.default = _default; |
@@ -1,5 +0,16 @@ | ||
import throttler from './index'; | ||
it('should create a throttler', () => { | ||
const cb = jest.fn(); | ||
throttler.create(cb); | ||
"use strict"; | ||
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); | ||
var _regenerator = _interopRequireDefault(require("@babel/runtime/regenerator")); | ||
var _asyncToGenerator2 = _interopRequireDefault(require("@babel/runtime/helpers/asyncToGenerator")); | ||
var _index = _interopRequireDefault(require("./index")); | ||
it('should create a throttler', function () { | ||
var cb = jest.fn(); | ||
_index.default.create(cb); | ||
expect(cb).toHaveBeenCalledWith({ | ||
@@ -12,52 +23,94 @@ isProcessing: false, | ||
}); | ||
describe('# execute', () => { | ||
let cb; | ||
let inst; | ||
beforeEach(() => { | ||
describe('# execute', function () { | ||
var cb; | ||
var inst; | ||
beforeEach(function () { | ||
cb = jest.fn(); | ||
inst = throttler.create(cb); | ||
inst = _index.default.create(cb); | ||
cb.mockReset(); | ||
jest.useFakeTimers(); | ||
}); | ||
it('should allow promise execution', async () => { | ||
const promise = Promise.resolve(); | ||
inst.execute(promise); | ||
expect(cb).toHaveBeenCalledWith({ | ||
isProcessing: true, | ||
isProcessed: false, | ||
isComplete: false, | ||
isError: false | ||
}); | ||
}); | ||
it('should wait for both promises to finish', async () => { | ||
inst.execute(Promise.resolve('data')); | ||
cb.mockReset(); | ||
expect(cb).not.toHaveBeenCalled(); | ||
jest.advanceTimersByTime(700); | ||
await inst._executedPromise; | ||
expect(cb).toHaveBeenCalledWith({ | ||
isProcessing: false, | ||
isProcessed: true, | ||
isComplete: true, | ||
isError: false | ||
}); | ||
}); | ||
it('should notify on failed promises', async () => { | ||
inst.execute(Promise.reject('data')); | ||
cb.mockReset(); | ||
expect(cb).not.toHaveBeenCalled(); | ||
jest.advanceTimersByTime(700); | ||
await inst._executedPromise; | ||
expect(cb).toHaveBeenCalledWith({ | ||
isProcessing: false, | ||
isProcessed: false, | ||
isComplete: true, | ||
isError: true | ||
}); | ||
}); | ||
it('should allow promise execution', /*#__PURE__*/(0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee() { | ||
var promise; | ||
return _regenerator.default.wrap(function _callee$(_context) { | ||
while (1) { | ||
switch (_context.prev = _context.next) { | ||
case 0: | ||
promise = Promise.resolve(); | ||
inst.execute(promise); | ||
expect(cb).toHaveBeenCalledWith({ | ||
isProcessing: true, | ||
isProcessed: false, | ||
isComplete: false, | ||
isError: false | ||
}); | ||
case 3: | ||
case "end": | ||
return _context.stop(); | ||
} | ||
} | ||
}, _callee); | ||
}))); | ||
it('should wait for both promises to finish', /*#__PURE__*/(0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee2() { | ||
return _regenerator.default.wrap(function _callee2$(_context2) { | ||
while (1) { | ||
switch (_context2.prev = _context2.next) { | ||
case 0: | ||
inst.execute(Promise.resolve('data')); | ||
cb.mockReset(); | ||
expect(cb).not.toHaveBeenCalled(); | ||
jest.advanceTimersByTime(700); | ||
_context2.next = 6; | ||
return inst._executedPromise; | ||
case 6: | ||
expect(cb).toHaveBeenCalledWith({ | ||
isProcessing: false, | ||
isProcessed: true, | ||
isComplete: true, | ||
isError: false | ||
}); | ||
case 7: | ||
case "end": | ||
return _context2.stop(); | ||
} | ||
} | ||
}, _callee2); | ||
}))); | ||
it('should notify on failed promises', /*#__PURE__*/(0, _asyncToGenerator2.default)( /*#__PURE__*/_regenerator.default.mark(function _callee3() { | ||
return _regenerator.default.wrap(function _callee3$(_context3) { | ||
while (1) { | ||
switch (_context3.prev = _context3.next) { | ||
case 0: | ||
inst.execute(Promise.reject('data')); | ||
cb.mockReset(); | ||
expect(cb).not.toHaveBeenCalled(); | ||
jest.advanceTimersByTime(700); | ||
_context3.next = 6; | ||
return inst._executedPromise; | ||
case 6: | ||
expect(cb).toHaveBeenCalledWith({ | ||
isProcessing: false, | ||
isProcessed: false, | ||
isComplete: true, | ||
isError: true | ||
}); | ||
case 7: | ||
case "end": | ||
return _context3.stop(); | ||
} | ||
} | ||
}, _callee3); | ||
}))); | ||
}); | ||
describe('# reset', () => { | ||
it('should reset the flags', () => { | ||
const cb = jest.fn(); | ||
const inst = throttler.create(cb); | ||
describe('# reset', function () { | ||
it('should reset the flags', function () { | ||
var cb = jest.fn(); | ||
var inst = _index.default.create(cb); | ||
cb.mockReset(); | ||
@@ -64,0 +117,0 @@ inst.reset(); |
{ | ||
"name": "@scoir/async-throttler", | ||
"version": "3.0.0-beta.ed162590", | ||
"version": "3.0.0", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "author": "@scoir", |
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
10453
7
297
1
0