@uppy/utils
Advanced tools
Comparing version 2.3.0 to 2.4.0
@@ -0,1 +1,16 @@ | ||
/** | ||
* Array.prototype.findIndex ponyfill for old browsers. | ||
*/ | ||
function findIndex(array, predicate) { | ||
for (var i = 0; i < array.length; i++) { | ||
if (predicate(array[i])) return i; | ||
} | ||
return -1; | ||
} | ||
function createCancelError() { | ||
return new Error('Cancelled'); | ||
} | ||
module.exports = | ||
@@ -81,7 +96,12 @@ /*#__PURE__*/ | ||
_proto._queue = function _queue(fn) { | ||
_proto._queue = function _queue(fn, options) { | ||
var _this3 = this; | ||
if (options === void 0) { | ||
options = {}; | ||
} | ||
var handler = { | ||
fn: fn, | ||
priority: options.priority || 0, | ||
abort: function abort() { | ||
@@ -94,3 +114,12 @@ _this3._dequeue(handler); | ||
}; | ||
this.queuedHandlers.push(handler); | ||
var index = findIndex(this.queuedHandlers, function (other) { | ||
return handler.priority > other.priority; | ||
}); | ||
if (index === -1) { | ||
this.queuedHandlers.push(handler); | ||
} else { | ||
this.queuedHandlers.splice(index, 0, handler); | ||
} | ||
return handler; | ||
@@ -107,3 +136,3 @@ }; | ||
_proto.run = function run(fn) { | ||
_proto.run = function run(fn, queueOptions) { | ||
if (this.activeRequests < this.limit) { | ||
@@ -113,6 +142,6 @@ return this._call(fn); | ||
return this._queue(fn); | ||
return this._queue(fn, queueOptions); | ||
}; | ||
_proto.wrapPromiseFunction = function wrapPromiseFunction(fn) { | ||
_proto.wrapPromiseFunction = function wrapPromiseFunction(fn, queueOptions) { | ||
var _this4 = this; | ||
@@ -125,14 +154,15 @@ | ||
return new Promise(function (resolve, reject) { | ||
var queuedRequest = _this4.run(function () { | ||
var queuedRequest; | ||
var outerPromise = new Promise(function (resolve, reject) { | ||
queuedRequest = _this4.run(function () { | ||
var cancelError; | ||
var promise; | ||
var innerPromise; | ||
try { | ||
promise = Promise.resolve(fn.apply(void 0, args)); | ||
innerPromise = Promise.resolve(fn.apply(void 0, args)); | ||
} catch (err) { | ||
promise = Promise.reject(err); | ||
innerPromise = Promise.reject(err); | ||
} | ||
promise.then(function (result) { | ||
innerPromise.then(function (result) { | ||
if (cancelError) { | ||
@@ -153,6 +183,12 @@ reject(cancelError); | ||
return function () { | ||
cancelError = new Error('Cancelled'); | ||
cancelError = createCancelError(); | ||
}; | ||
}); | ||
}, queueOptions); | ||
}); | ||
outerPromise.abort = function () { | ||
queuedRequest.abort(); | ||
}; | ||
return outerPromise; | ||
}; | ||
@@ -159,0 +195,0 @@ }; |
{ | ||
"name": "@uppy/utils", | ||
"description": "Shared utility functions for Uppy Core and plugins maintained by the Uppy team.", | ||
"version": "2.3.0", | ||
"version": "2.4.0", | ||
"license": "MIT", | ||
@@ -23,3 +23,3 @@ "main": "lib/index.js", | ||
}, | ||
"gitHead": "fa32da7cf3b1a518a86304ae169c657f2622e5cd" | ||
"gitHead": "0481589f3bd589eabd78fd91af9e2688e356f4f9" | ||
} |
@@ -0,1 +1,15 @@ | ||
/** | ||
* Array.prototype.findIndex ponyfill for old browsers. | ||
*/ | ||
function findIndex (array, predicate) { | ||
for (let i = 0; i < array.length; i++) { | ||
if (predicate(array[i])) return i | ||
} | ||
return -1 | ||
} | ||
function createCancelError () { | ||
return new Error('Cancelled') | ||
} | ||
module.exports = class RateLimitedQueue { | ||
@@ -70,5 +84,6 @@ constructor (limit) { | ||
_queue (fn) { | ||
_queue (fn, options = {}) { | ||
const handler = { | ||
fn, | ||
priority: options.priority || 0, | ||
abort: () => { | ||
@@ -81,3 +96,11 @@ this._dequeue(handler) | ||
} | ||
this.queuedHandlers.push(handler) | ||
const index = findIndex(this.queuedHandlers, (other) => { | ||
return handler.priority > other.priority | ||
}) | ||
if (index === -1) { | ||
this.queuedHandlers.push(handler) | ||
} else { | ||
this.queuedHandlers.splice(index, 0, handler) | ||
} | ||
return handler | ||
@@ -93,42 +116,51 @@ } | ||
run (fn) { | ||
run (fn, queueOptions) { | ||
if (this.activeRequests < this.limit) { | ||
return this._call(fn) | ||
} | ||
return this._queue(fn) | ||
return this._queue(fn, queueOptions) | ||
} | ||
wrapPromiseFunction (fn) { | ||
return (...args) => new Promise((resolve, reject) => { | ||
const queuedRequest = this.run(() => { | ||
let cancelError | ||
let promise | ||
try { | ||
promise = Promise.resolve(fn(...args)) | ||
} catch (err) { | ||
promise = Promise.reject(err) | ||
} | ||
wrapPromiseFunction (fn, queueOptions) { | ||
return (...args) => { | ||
let queuedRequest | ||
const outerPromise = new Promise((resolve, reject) => { | ||
queuedRequest = this.run(() => { | ||
let cancelError | ||
let innerPromise | ||
try { | ||
innerPromise = Promise.resolve(fn(...args)) | ||
} catch (err) { | ||
innerPromise = Promise.reject(err) | ||
} | ||
promise.then((result) => { | ||
if (cancelError) { | ||
reject(cancelError) | ||
} else { | ||
queuedRequest.done() | ||
resolve(result) | ||
innerPromise.then((result) => { | ||
if (cancelError) { | ||
reject(cancelError) | ||
} else { | ||
queuedRequest.done() | ||
resolve(result) | ||
} | ||
}, (err) => { | ||
if (cancelError) { | ||
reject(cancelError) | ||
} else { | ||
queuedRequest.done() | ||
reject(err) | ||
} | ||
}) | ||
return () => { | ||
cancelError = createCancelError() | ||
} | ||
}, (err) => { | ||
if (cancelError) { | ||
reject(cancelError) | ||
} else { | ||
queuedRequest.done() | ||
reject(err) | ||
} | ||
}) | ||
}, queueOptions) | ||
}) | ||
return () => { | ||
cancelError = new Error('Cancelled') | ||
} | ||
}) | ||
}) | ||
outerPromise.abort = () => { | ||
queuedRequest.abort() | ||
} | ||
return outerPromise | ||
} | ||
} | ||
} |
@@ -52,12 +52,19 @@ declare module '@uppy/utils/lib/Translator' { | ||
export type QueueEntry = { | ||
abort: () => void | ||
done: () => void | ||
abort: () => void, | ||
done: () => void, | ||
} | ||
export type QueueOptions = { | ||
priority?: number | ||
} | ||
} | ||
class RateLimitedQueue { | ||
constructor (limit: number) | ||
run (fn: () => RateLimitedQueue.AbortFunction): RateLimitedQueue.QueueEntry | ||
constructor(limit: number) | ||
run( | ||
fn: () => RateLimitedQueue.AbortFunction, | ||
queueOptions?: RateLimitedQueue.QueueOptions | ||
): RateLimitedQueue.QueueEntry | ||
wrapPromiseFunction( | ||
fn: () => RateLimitedQueue.PromiseFunction | ||
fn: () => RateLimitedQueue.PromiseFunction, | ||
queueOptions?: RateLimitedQueue.QueueOptions | ||
): RateLimitedQueue.PromiseFunction | ||
@@ -144,3 +151,3 @@ } | ||
filename: string | ||
): { name: string, extension: string | undefined }; | ||
): { name: string, extension: string | undefined } | ||
export = getFileNameAndExtension | ||
@@ -147,0 +154,0 @@ } |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
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
125662
2602
0