@ledgerhq/live-promise
Advanced tools
Comparing version 0.0.1 to 0.0.2-next.0
module.exports = { | ||
parser: "@typescript-eslint/parser", | ||
env: { | ||
browser: true, | ||
es6: true, | ||
node: true, | ||
jest: true, | ||
}, | ||
extends: [ | ||
"eslint:recommended", | ||
"plugin:@typescript-eslint/eslint-recommended", | ||
"plugin:@typescript-eslint/recommended", | ||
"prettier", | ||
overrides: [ | ||
{ | ||
files: ["src/**/*.test.{ts,tsx}"], | ||
env: { | ||
"jest/globals": true, | ||
}, | ||
plugins: ["jest"], | ||
}, | ||
], | ||
globals: { | ||
Atomics: "readonly", | ||
SharedArrayBuffer: "readonly", | ||
}, | ||
plugins: ["@typescript-eslint", "prettier"], | ||
rules: { | ||
"no-console": ["error", { allow: ["warn", "error"] }], | ||
"linebreak-style": ["error", "unix"], | ||
semi: ["error", "always"], | ||
"no-unused-vars": "off", | ||
"import/prefer-default-export": 0, | ||
"no-plusplus": 0, | ||
"no-underscore-dangle": 0, | ||
"prefer-template": 0, | ||
"no-await-in-loop": 0, | ||
"no-restricted-syntax": 0, | ||
"consistent-return": 0, | ||
"no-lonely-if": 0, | ||
"no-use-before-define": 0, | ||
"no-nested-ternary": 0, | ||
"import/no-cycle": 0, | ||
"no-multi-assign": 0, | ||
"guard-for-in": 0, | ||
"no-continue": 0, | ||
"lines-between-class-members": 0, | ||
"prefer-destructuring": 0, | ||
"prettier/prettier": "error", | ||
"@typescript-eslint/no-use-before-define": "off", | ||
"@typescript-eslint/no-unused-vars": ["error", { argsIgnorePattern: "^_" }], | ||
"@typescript-eslint/no-empty-function": "off", | ||
"@typescript-eslint/no-namespace": ["error", { allowDeclarations: true }], | ||
"@typescript-eslint/no-explicit-any": 0, | ||
"@typescript-eslint/ban-types": [ | ||
"error", | ||
{ | ||
extendDefaults: true, | ||
types: { | ||
"{}": false, | ||
}, | ||
}, | ||
], | ||
"@typescript-eslint/no-explicit-any": "warn", | ||
}, | ||
}; |
@@ -1,12 +0,1 @@ | ||
var __assign = (this && this.__assign) || function () { | ||
__assign = Object.assign || function(t) { | ||
for (var s, i = 1, n = arguments.length; i < n; i++) { | ||
s = arguments[i]; | ||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) | ||
t[p] = s[p]; | ||
} | ||
return t; | ||
}; | ||
return __assign.apply(this, arguments); | ||
}; | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
@@ -21,68 +10,14 @@ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
}; | ||
var __generator = (this && this.__generator) || function (thisArg, body) { | ||
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; | ||
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; | ||
function verb(n) { return function (v) { return step([n, v]); }; } | ||
function step(op) { | ||
if (f) throw new TypeError("Generator is already executing."); | ||
while (g && (g = 0, op[0] && (_ = 0)), _) try { | ||
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; | ||
if (y = 0, t) op = [op[0] & 2, t.value]; | ||
switch (op[0]) { | ||
case 0: case 1: t = op; break; | ||
case 4: _.label++; return { value: op[1], done: false }; | ||
case 5: _.label++; y = op[1]; op = [0]; continue; | ||
case 7: op = _.ops.pop(); _.trys.pop(); continue; | ||
default: | ||
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } | ||
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } | ||
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } | ||
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } | ||
if (t[2]) _.ops.pop(); | ||
_.trys.pop(); continue; | ||
} | ||
op = body.call(thisArg, _); | ||
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } | ||
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; | ||
} | ||
}; | ||
var __read = (this && this.__read) || function (o, n) { | ||
var m = typeof Symbol === "function" && o[Symbol.iterator]; | ||
if (!m) return o; | ||
var i = m.call(o), r, ar = [], e; | ||
try { | ||
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value); | ||
} | ||
catch (error) { e = { error: error }; } | ||
finally { | ||
try { | ||
if (r && !r.done && (m = i["return"])) m.call(i); | ||
} | ||
finally { if (e) throw e.error; } | ||
} | ||
return ar; | ||
}; | ||
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) { | ||
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) { | ||
if (ar || !(i in from)) { | ||
if (!ar) ar = Array.prototype.slice.call(from, 0, i); | ||
ar[i] = from[i]; | ||
} | ||
} | ||
return to.concat(ar || Array.prototype.slice.call(from)); | ||
}; | ||
import { log } from "@ledgerhq/logs"; | ||
export var delay = function (ms) { | ||
return new Promise(function (f) { return setTimeout(f, ms); }); | ||
}; | ||
var defaults = { | ||
export const delay = (ms) => new Promise(f => setTimeout(f, ms)); | ||
const defaults = { | ||
maxRetry: 4, | ||
interval: 300, | ||
intervalMultiplicator: 1.5, | ||
context: "" | ||
context: "", | ||
}; | ||
export function retry(f, options) { | ||
var _a = __assign(__assign({}, defaults), options), maxRetry = _a.maxRetry, interval = _a.interval, intervalMultiplicator = _a.intervalMultiplicator, context = _a.context; | ||
const { maxRetry, interval, intervalMultiplicator, context } = Object.assign(Object.assign({}, defaults), options); | ||
function rec(remainingTry, i) { | ||
var result = f(); | ||
const result = f(); | ||
if (remainingTry <= 0) { | ||
@@ -92,7 +27,5 @@ return result; | ||
// In case of failure, wait the interval, retry the action | ||
return result["catch"](function (e) { | ||
return result.catch(e => { | ||
log("promise-retry", context + " failed. " + remainingTry + " retry remain. " + String(e)); | ||
return delay(i).then(function () { | ||
return rec(remainingTry - 1, i * intervalMultiplicator); | ||
}); | ||
return delay(i).then(() => rec(remainingTry - 1, i * intervalMultiplicator)); | ||
}); | ||
@@ -102,14 +35,9 @@ } | ||
} | ||
export var atomicQueue = function (job, queueIdentifier) { | ||
if (queueIdentifier === void 0) { queueIdentifier = function () { return ""; }; } | ||
var queues = {}; | ||
return function () { | ||
var args = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
args[_i] = arguments[_i]; | ||
} | ||
var id = queueIdentifier.apply(void 0, __spreadArray([], __read(args), false)); | ||
var queue = queues[id] || Promise.resolve(); | ||
var p = queue.then(function () { return job.apply(void 0, __spreadArray([], __read(args), false)); }); | ||
queues[id] = p["catch"](function () { }); | ||
export const atomicQueue = (job, queueIdentifier = () => "") => { | ||
const queues = {}; | ||
return (...args) => { | ||
const id = queueIdentifier(...args); | ||
const queue = queues[id] || Promise.resolve(); | ||
const p = queue.then(() => job(...args)); | ||
queues[id] = p.catch(() => { }); | ||
return p; | ||
@@ -119,8 +47,8 @@ }; | ||
export function execAndWaitAtLeast(ms, cb) { | ||
var startTime = Date.now(); | ||
return cb().then(function (r) { | ||
var remaining = ms - (Date.now() - startTime); | ||
const startTime = Date.now(); | ||
return cb().then(r => { | ||
const remaining = ms - (Date.now() - startTime); | ||
if (remaining <= 0) | ||
return r; | ||
return delay(remaining).then(function () { return r; }); | ||
return delay(remaining).then(() => r); | ||
}); | ||
@@ -136,49 +64,27 @@ } | ||
export function promiseAllBatched(batch, items, fn) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const data = Array(items.length); | ||
const queue = items.map((item, index) => ({ | ||
item, | ||
index, | ||
})); | ||
function step() { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var first, item, index, _a, _b; | ||
return __generator(this, function (_c) { | ||
switch (_c.label) { | ||
case 0: | ||
if (queue.length === 0) | ||
return [2 /*return*/]; | ||
first = queue.shift(); | ||
if (!first) return [3 /*break*/, 2]; | ||
item = first.item, index = first.index; | ||
_a = data; | ||
_b = index; | ||
return [4 /*yield*/, fn(item, index)]; | ||
case 1: | ||
_a[_b] = _c.sent(); | ||
_c.label = 2; | ||
case 2: return [4 /*yield*/, step()]; | ||
case 3: | ||
_c.sent(); // each time an item redeem, we schedule another one | ||
return [2 /*return*/]; | ||
} | ||
}); | ||
return __awaiter(this, void 0, void 0, function* () { | ||
if (queue.length === 0) | ||
return; | ||
const first = queue.shift(); | ||
if (first) { | ||
const { item, index } = first; | ||
data[index] = yield fn(item, index); | ||
} | ||
yield step(); // each time an item redeem, we schedule another one | ||
}); | ||
} | ||
var data, queue; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
data = Array(items.length); | ||
queue = items.map(function (item, index) { return ({ | ||
item: item, | ||
index: index | ||
}); }); | ||
// initially, we schedule <batch> items in parallel | ||
return [4 /*yield*/, Promise.all(Array(Math.min(batch, items.length)) | ||
.fill(function () { return undefined; }) | ||
.map(step))]; | ||
case 1: | ||
// initially, we schedule <batch> items in parallel | ||
_a.sent(); | ||
return [2 /*return*/, data]; | ||
} | ||
}); | ||
// initially, we schedule <batch> items in parallel | ||
yield Promise.all(Array(Math.min(batch, items.length)) | ||
.fill(() => undefined) | ||
.map(step)); | ||
return data; | ||
}); | ||
} | ||
//# sourceMappingURL=promise.js.map |
@@ -10,62 +10,21 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
}; | ||
var __generator = (this && this.__generator) || function (thisArg, body) { | ||
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; | ||
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; | ||
function verb(n) { return function (v) { return step([n, v]); }; } | ||
function step(op) { | ||
if (f) throw new TypeError("Generator is already executing."); | ||
while (g && (g = 0, op[0] && (_ = 0)), _) try { | ||
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; | ||
if (y = 0, t) op = [op[0] & 2, t.value]; | ||
switch (op[0]) { | ||
case 0: case 1: t = op; break; | ||
case 4: _.label++; return { value: op[1], done: false }; | ||
case 5: _.label++; y = op[1]; op = [0]; continue; | ||
case 7: op = _.ops.pop(); _.trys.pop(); continue; | ||
default: | ||
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } | ||
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } | ||
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } | ||
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } | ||
if (t[2]) _.ops.pop(); | ||
_.trys.pop(); continue; | ||
} | ||
op = body.call(thisArg, _); | ||
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } | ||
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; | ||
} | ||
}; | ||
import { promiseAllBatched } from "./promise"; | ||
test("promiseAllBatched", function () { return __awaiter(void 0, void 0, void 0, function () { | ||
var promisifyIdPlusOne, p, _a, _b, _c, _d, _e; | ||
return __generator(this, function (_f) { | ||
switch (_f.label) { | ||
case 0: | ||
promisifyIdPlusOne = function (a) { return Promise.resolve(a + 1); }; | ||
p = promiseAllBatched(5, [], promisifyIdPlusOne); | ||
expect(typeof p.then).toBe("function"); | ||
_a = expect; | ||
return [4 /*yield*/, p]; | ||
case 1: | ||
_a.apply(void 0, [_f.sent()]).toEqual([]); | ||
_b = expect; | ||
return [4 /*yield*/, promiseAllBatched(5, [1, 2, 3, 4, 5, 6, 7, 8], promisifyIdPlusOne)]; | ||
case 2: | ||
_b.apply(void 0, [_f.sent()]).toEqual([2, 3, 4, 5, 6, 7, 8, 9]); | ||
_c = expect; | ||
return [4 /*yield*/, promiseAllBatched(1, [1, 2, 3, 4, 5, 6, 7, 8], promisifyIdPlusOne)]; | ||
case 3: | ||
_c.apply(void 0, [_f.sent()]).toEqual([2, 3, 4, 5, 6, 7, 8, 9]); | ||
_d = expect; | ||
return [4 /*yield*/, promiseAllBatched(10, [1, 2, 3, 4, 5, 6, 7, 8], promisifyIdPlusOne)]; | ||
case 4: | ||
_d.apply(void 0, [_f.sent()]).toEqual([2, 3, 4, 5, 6, 7, 8, 9]); | ||
_e = expect; | ||
return [4 /*yield*/, promiseAllBatched(2, Array(6).fill(0), function (_, i) { return Promise.resolve(i); })]; | ||
case 5: | ||
_e.apply(void 0, [_f.sent()]).toEqual([0, 1, 2, 3, 4, 5]); | ||
return [2 /*return*/]; | ||
} | ||
}); | ||
}); }); | ||
test("promiseAllBatched", () => __awaiter(void 0, void 0, void 0, function* () { | ||
const promisifyIdPlusOne = (a) => Promise.resolve(a + 1); | ||
const p = promiseAllBatched(5, [], promisifyIdPlusOne); | ||
expect(typeof p.then).toBe("function"); | ||
expect(yield p).toEqual([]); | ||
expect(yield promiseAllBatched(5, [1, 2, 3, 4, 5, 6, 7, 8], promisifyIdPlusOne)).toEqual([ | ||
2, 3, 4, 5, 6, 7, 8, 9, | ||
]); | ||
expect(yield promiseAllBatched(1, [1, 2, 3, 4, 5, 6, 7, 8], promisifyIdPlusOne)).toEqual([ | ||
2, 3, 4, 5, 6, 7, 8, 9, | ||
]); | ||
expect(yield promiseAllBatched(10, [1, 2, 3, 4, 5, 6, 7, 8], promisifyIdPlusOne)).toEqual([ | ||
2, 3, 4, 5, 6, 7, 8, 9, | ||
]); | ||
expect(yield promiseAllBatched(2, Array(6).fill(0), (_, i) => Promise.resolve(i))).toEqual([ | ||
0, 1, 2, 3, 4, 5, | ||
]); | ||
})); | ||
//# sourceMappingURL=promise.test.js.map |
@@ -16,4 +16,4 @@ "use strict"; | ||
}; | ||
exports.__esModule = true; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
__exportStar(require("./promise"), exports); | ||
//# sourceMappingURL=index.js.map |
"use strict"; | ||
var __assign = (this && this.__assign) || function () { | ||
__assign = Object.assign || function(t) { | ||
for (var s, i = 1, n = arguments.length; i < n; i++) { | ||
s = arguments[i]; | ||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) | ||
t[p] = s[p]; | ||
} | ||
return t; | ||
}; | ||
return __assign.apply(this, arguments); | ||
}; | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
@@ -22,71 +11,17 @@ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
}; | ||
var __generator = (this && this.__generator) || function (thisArg, body) { | ||
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; | ||
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; | ||
function verb(n) { return function (v) { return step([n, v]); }; } | ||
function step(op) { | ||
if (f) throw new TypeError("Generator is already executing."); | ||
while (g && (g = 0, op[0] && (_ = 0)), _) try { | ||
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; | ||
if (y = 0, t) op = [op[0] & 2, t.value]; | ||
switch (op[0]) { | ||
case 0: case 1: t = op; break; | ||
case 4: _.label++; return { value: op[1], done: false }; | ||
case 5: _.label++; y = op[1]; op = [0]; continue; | ||
case 7: op = _.ops.pop(); _.trys.pop(); continue; | ||
default: | ||
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } | ||
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } | ||
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } | ||
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } | ||
if (t[2]) _.ops.pop(); | ||
_.trys.pop(); continue; | ||
} | ||
op = body.call(thisArg, _); | ||
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } | ||
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; | ||
} | ||
}; | ||
var __read = (this && this.__read) || function (o, n) { | ||
var m = typeof Symbol === "function" && o[Symbol.iterator]; | ||
if (!m) return o; | ||
var i = m.call(o), r, ar = [], e; | ||
try { | ||
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value); | ||
} | ||
catch (error) { e = { error: error }; } | ||
finally { | ||
try { | ||
if (r && !r.done && (m = i["return"])) m.call(i); | ||
} | ||
finally { if (e) throw e.error; } | ||
} | ||
return ar; | ||
}; | ||
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) { | ||
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) { | ||
if (ar || !(i in from)) { | ||
if (!ar) ar = Array.prototype.slice.call(from, 0, i); | ||
ar[i] = from[i]; | ||
} | ||
} | ||
return to.concat(ar || Array.prototype.slice.call(from)); | ||
}; | ||
exports.__esModule = true; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.promiseAllBatched = exports.execAndWaitAtLeast = exports.atomicQueue = exports.retry = exports.delay = void 0; | ||
var logs_1 = require("@ledgerhq/logs"); | ||
var delay = function (ms) { | ||
return new Promise(function (f) { return setTimeout(f, ms); }); | ||
}; | ||
const logs_1 = require("@ledgerhq/logs"); | ||
const delay = (ms) => new Promise(f => setTimeout(f, ms)); | ||
exports.delay = delay; | ||
var defaults = { | ||
const defaults = { | ||
maxRetry: 4, | ||
interval: 300, | ||
intervalMultiplicator: 1.5, | ||
context: "" | ||
context: "", | ||
}; | ||
function retry(f, options) { | ||
var _a = __assign(__assign({}, defaults), options), maxRetry = _a.maxRetry, interval = _a.interval, intervalMultiplicator = _a.intervalMultiplicator, context = _a.context; | ||
const { maxRetry, interval, intervalMultiplicator, context } = Object.assign(Object.assign({}, defaults), options); | ||
function rec(remainingTry, i) { | ||
var result = f(); | ||
const result = f(); | ||
if (remainingTry <= 0) { | ||
@@ -96,7 +31,5 @@ return result; | ||
// In case of failure, wait the interval, retry the action | ||
return result["catch"](function (e) { | ||
return result.catch(e => { | ||
(0, logs_1.log)("promise-retry", context + " failed. " + remainingTry + " retry remain. " + String(e)); | ||
return (0, exports.delay)(i).then(function () { | ||
return rec(remainingTry - 1, i * intervalMultiplicator); | ||
}); | ||
return (0, exports.delay)(i).then(() => rec(remainingTry - 1, i * intervalMultiplicator)); | ||
}); | ||
@@ -107,14 +40,9 @@ } | ||
exports.retry = retry; | ||
var atomicQueue = function (job, queueIdentifier) { | ||
if (queueIdentifier === void 0) { queueIdentifier = function () { return ""; }; } | ||
var queues = {}; | ||
return function () { | ||
var args = []; | ||
for (var _i = 0; _i < arguments.length; _i++) { | ||
args[_i] = arguments[_i]; | ||
} | ||
var id = queueIdentifier.apply(void 0, __spreadArray([], __read(args), false)); | ||
var queue = queues[id] || Promise.resolve(); | ||
var p = queue.then(function () { return job.apply(void 0, __spreadArray([], __read(args), false)); }); | ||
queues[id] = p["catch"](function () { }); | ||
const atomicQueue = (job, queueIdentifier = () => "") => { | ||
const queues = {}; | ||
return (...args) => { | ||
const id = queueIdentifier(...args); | ||
const queue = queues[id] || Promise.resolve(); | ||
const p = queue.then(() => job(...args)); | ||
queues[id] = p.catch(() => { }); | ||
return p; | ||
@@ -125,8 +53,8 @@ }; | ||
function execAndWaitAtLeast(ms, cb) { | ||
var startTime = Date.now(); | ||
return cb().then(function (r) { | ||
var remaining = ms - (Date.now() - startTime); | ||
const startTime = Date.now(); | ||
return cb().then(r => { | ||
const remaining = ms - (Date.now() - startTime); | ||
if (remaining <= 0) | ||
return r; | ||
return (0, exports.delay)(remaining).then(function () { return r; }); | ||
return (0, exports.delay)(remaining).then(() => r); | ||
}); | ||
@@ -143,47 +71,25 @@ } | ||
function promiseAllBatched(batch, items, fn) { | ||
return __awaiter(this, void 0, void 0, function () { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const data = Array(items.length); | ||
const queue = items.map((item, index) => ({ | ||
item, | ||
index, | ||
})); | ||
function step() { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var first, item, index, _a, _b; | ||
return __generator(this, function (_c) { | ||
switch (_c.label) { | ||
case 0: | ||
if (queue.length === 0) | ||
return [2 /*return*/]; | ||
first = queue.shift(); | ||
if (!first) return [3 /*break*/, 2]; | ||
item = first.item, index = first.index; | ||
_a = data; | ||
_b = index; | ||
return [4 /*yield*/, fn(item, index)]; | ||
case 1: | ||
_a[_b] = _c.sent(); | ||
_c.label = 2; | ||
case 2: return [4 /*yield*/, step()]; | ||
case 3: | ||
_c.sent(); // each time an item redeem, we schedule another one | ||
return [2 /*return*/]; | ||
} | ||
}); | ||
return __awaiter(this, void 0, void 0, function* () { | ||
if (queue.length === 0) | ||
return; | ||
const first = queue.shift(); | ||
if (first) { | ||
const { item, index } = first; | ||
data[index] = yield fn(item, index); | ||
} | ||
yield step(); // each time an item redeem, we schedule another one | ||
}); | ||
} | ||
var data, queue; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
data = Array(items.length); | ||
queue = items.map(function (item, index) { return ({ | ||
item: item, | ||
index: index | ||
}); }); | ||
// initially, we schedule <batch> items in parallel | ||
return [4 /*yield*/, Promise.all(Array(Math.min(batch, items.length)) | ||
.fill(function () { return undefined; }) | ||
.map(step))]; | ||
case 1: | ||
// initially, we schedule <batch> items in parallel | ||
_a.sent(); | ||
return [2 /*return*/, data]; | ||
} | ||
}); | ||
// initially, we schedule <batch> items in parallel | ||
yield Promise.all(Array(Math.min(batch, items.length)) | ||
.fill(() => undefined) | ||
.map(step)); | ||
return data; | ||
}); | ||
@@ -190,0 +96,0 @@ } |
@@ -11,63 +11,22 @@ "use strict"; | ||
}; | ||
var __generator = (this && this.__generator) || function (thisArg, body) { | ||
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; | ||
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; | ||
function verb(n) { return function (v) { return step([n, v]); }; } | ||
function step(op) { | ||
if (f) throw new TypeError("Generator is already executing."); | ||
while (g && (g = 0, op[0] && (_ = 0)), _) try { | ||
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; | ||
if (y = 0, t) op = [op[0] & 2, t.value]; | ||
switch (op[0]) { | ||
case 0: case 1: t = op; break; | ||
case 4: _.label++; return { value: op[1], done: false }; | ||
case 5: _.label++; y = op[1]; op = [0]; continue; | ||
case 7: op = _.ops.pop(); _.trys.pop(); continue; | ||
default: | ||
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } | ||
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } | ||
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } | ||
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } | ||
if (t[2]) _.ops.pop(); | ||
_.trys.pop(); continue; | ||
} | ||
op = body.call(thisArg, _); | ||
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } | ||
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; | ||
} | ||
}; | ||
exports.__esModule = true; | ||
var promise_1 = require("./promise"); | ||
test("promiseAllBatched", function () { return __awaiter(void 0, void 0, void 0, function () { | ||
var promisifyIdPlusOne, p, _a, _b, _c, _d, _e; | ||
return __generator(this, function (_f) { | ||
switch (_f.label) { | ||
case 0: | ||
promisifyIdPlusOne = function (a) { return Promise.resolve(a + 1); }; | ||
p = (0, promise_1.promiseAllBatched)(5, [], promisifyIdPlusOne); | ||
expect(typeof p.then).toBe("function"); | ||
_a = expect; | ||
return [4 /*yield*/, p]; | ||
case 1: | ||
_a.apply(void 0, [_f.sent()]).toEqual([]); | ||
_b = expect; | ||
return [4 /*yield*/, (0, promise_1.promiseAllBatched)(5, [1, 2, 3, 4, 5, 6, 7, 8], promisifyIdPlusOne)]; | ||
case 2: | ||
_b.apply(void 0, [_f.sent()]).toEqual([2, 3, 4, 5, 6, 7, 8, 9]); | ||
_c = expect; | ||
return [4 /*yield*/, (0, promise_1.promiseAllBatched)(1, [1, 2, 3, 4, 5, 6, 7, 8], promisifyIdPlusOne)]; | ||
case 3: | ||
_c.apply(void 0, [_f.sent()]).toEqual([2, 3, 4, 5, 6, 7, 8, 9]); | ||
_d = expect; | ||
return [4 /*yield*/, (0, promise_1.promiseAllBatched)(10, [1, 2, 3, 4, 5, 6, 7, 8], promisifyIdPlusOne)]; | ||
case 4: | ||
_d.apply(void 0, [_f.sent()]).toEqual([2, 3, 4, 5, 6, 7, 8, 9]); | ||
_e = expect; | ||
return [4 /*yield*/, (0, promise_1.promiseAllBatched)(2, Array(6).fill(0), function (_, i) { return Promise.resolve(i); })]; | ||
case 5: | ||
_e.apply(void 0, [_f.sent()]).toEqual([0, 1, 2, 3, 4, 5]); | ||
return [2 /*return*/]; | ||
} | ||
}); | ||
}); }); | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const promise_1 = require("./promise"); | ||
test("promiseAllBatched", () => __awaiter(void 0, void 0, void 0, function* () { | ||
const promisifyIdPlusOne = (a) => Promise.resolve(a + 1); | ||
const p = (0, promise_1.promiseAllBatched)(5, [], promisifyIdPlusOne); | ||
expect(typeof p.then).toBe("function"); | ||
expect(yield p).toEqual([]); | ||
expect(yield (0, promise_1.promiseAllBatched)(5, [1, 2, 3, 4, 5, 6, 7, 8], promisifyIdPlusOne)).toEqual([ | ||
2, 3, 4, 5, 6, 7, 8, 9, | ||
]); | ||
expect(yield (0, promise_1.promiseAllBatched)(1, [1, 2, 3, 4, 5, 6, 7, 8], promisifyIdPlusOne)).toEqual([ | ||
2, 3, 4, 5, 6, 7, 8, 9, | ||
]); | ||
expect(yield (0, promise_1.promiseAllBatched)(10, [1, 2, 3, 4, 5, 6, 7, 8], promisifyIdPlusOne)).toEqual([ | ||
2, 3, 4, 5, 6, 7, 8, 9, | ||
]); | ||
expect(yield (0, promise_1.promiseAllBatched)(2, Array(6).fill(0), (_, i) => Promise.resolve(i))).toEqual([ | ||
0, 1, 2, 3, 4, 5, | ||
]); | ||
})); | ||
//# sourceMappingURL=promise.test.js.map |
{ | ||
"name": "@ledgerhq/live-promise", | ||
"version": "0.0.1", | ||
"version": "0.0.2-next.0", | ||
"description": "Ledger Live Promise utilities", | ||
@@ -24,18 +24,8 @@ "keywords": [ | ||
"dependencies": { | ||
"@ledgerhq/logs": "^6.10.1" | ||
"@ledgerhq/logs": "^6.11.0-next.0" | ||
}, | ||
"devDependencies": { | ||
"@types/jest": "^29.2.4", | ||
"@typescript-eslint/eslint-plugin": "^5.46.1", | ||
"@typescript-eslint/parser": "^5.46.1", | ||
"eslint": "^7.32.0", | ||
"eslint-config-prettier": "^8.3.0", | ||
"eslint-config-typescript": "^3.0.0", | ||
"eslint-formatter-pretty": "^3.0.1", | ||
"eslint-plugin-prettier": "^3.4.0", | ||
"eslint-plugin-typescript": "^0.14.0", | ||
"jest": "^28.1.1", | ||
"prettier": "^2.8.1", | ||
"ts-jest": "^28.0.5", | ||
"typescript": "^4.9.4" | ||
"ts-jest": "^28.0.5" | ||
}, | ||
@@ -47,6 +37,7 @@ "scripts": { | ||
"watch": "tsc --watch", | ||
"lint": "eslint ./src --no-error-on-unmatched-pattern --ext .ts,.tsx", | ||
"lint": "eslint ./src --no-error-on-unmatched-pattern --ext .ts,.tsx --cache", | ||
"lint:fix": "pnpm lint --fix", | ||
"test": "jest" | ||
"test": "jest", | ||
"unimported": "unimported" | ||
} | ||
} |
@@ -8,14 +8,14 @@ import { promiseAllBatched } from "./promise"; | ||
expect(await p).toEqual([]); | ||
expect( | ||
await promiseAllBatched(5, [1, 2, 3, 4, 5, 6, 7, 8], promisifyIdPlusOne) | ||
).toEqual([2, 3, 4, 5, 6, 7, 8, 9]); | ||
expect( | ||
await promiseAllBatched(1, [1, 2, 3, 4, 5, 6, 7, 8], promisifyIdPlusOne) | ||
).toEqual([2, 3, 4, 5, 6, 7, 8, 9]); | ||
expect( | ||
await promiseAllBatched(10, [1, 2, 3, 4, 5, 6, 7, 8], promisifyIdPlusOne) | ||
).toEqual([2, 3, 4, 5, 6, 7, 8, 9]); | ||
expect( | ||
await promiseAllBatched(2, Array(6).fill(0), (_, i) => Promise.resolve(i)) | ||
).toEqual([0, 1, 2, 3, 4, 5]); | ||
expect(await promiseAllBatched(5, [1, 2, 3, 4, 5, 6, 7, 8], promisifyIdPlusOne)).toEqual([ | ||
2, 3, 4, 5, 6, 7, 8, 9, | ||
]); | ||
expect(await promiseAllBatched(1, [1, 2, 3, 4, 5, 6, 7, 8], promisifyIdPlusOne)).toEqual([ | ||
2, 3, 4, 5, 6, 7, 8, 9, | ||
]); | ||
expect(await promiseAllBatched(10, [1, 2, 3, 4, 5, 6, 7, 8], promisifyIdPlusOne)).toEqual([ | ||
2, 3, 4, 5, 6, 7, 8, 9, | ||
]); | ||
expect(await promiseAllBatched(2, Array(6).fill(0), (_, i) => Promise.resolve(i))).toEqual([ | ||
0, 1, 2, 3, 4, 5, | ||
]); | ||
}); |
import { log } from "@ledgerhq/logs"; | ||
export const delay = (ms: number): Promise<void> => | ||
new Promise((f) => setTimeout(f, ms)); | ||
export const delay = (ms: number): Promise<void> => new Promise(f => setTimeout(f, ms)); | ||
const defaults = { | ||
@@ -10,6 +9,3 @@ maxRetry: 4, | ||
}; | ||
export function retry<A>( | ||
f: () => Promise<A>, | ||
options?: Partial<typeof defaults> | ||
): Promise<A> { | ||
export function retry<A>(f: () => Promise<A>, options?: Partial<typeof defaults>): Promise<A> { | ||
const { maxRetry, interval, intervalMultiplicator, context } = { | ||
@@ -28,10 +24,5 @@ ...defaults, | ||
// In case of failure, wait the interval, retry the action | ||
return result.catch((e) => { | ||
log( | ||
"promise-retry", | ||
context + " failed. " + remainingTry + " retry remain. " + String(e) | ||
); | ||
return delay(i).then(() => | ||
rec(remainingTry - 1, i * intervalMultiplicator) | ||
); | ||
return result.catch(e => { | ||
log("promise-retry", context + " failed. " + remainingTry + " retry remain. " + String(e)); | ||
return delay(i).then(() => rec(remainingTry - 1, i * intervalMultiplicator)); | ||
}); | ||
@@ -45,3 +36,3 @@ } | ||
job: Job<R, A>, | ||
queueIdentifier: (...args: A) => string = () => "" | ||
queueIdentifier: (...args: A) => string = () => "", | ||
): Job<R, A> => { | ||
@@ -57,8 +48,5 @@ const queues: Record<string, any> = {}; | ||
}; | ||
export function execAndWaitAtLeast<A>( | ||
ms: number, | ||
cb: () => Promise<A> | ||
): Promise<A> { | ||
export function execAndWaitAtLeast<A>(ms: number, cb: () => Promise<A>): Promise<A> { | ||
const startTime = Date.now(); | ||
return cb().then((r) => { | ||
return cb().then(r => { | ||
const remaining = ms - (Date.now() - startTime); | ||
@@ -80,3 +68,3 @@ if (remaining <= 0) return r; | ||
items: Array<A>, | ||
fn: (arg0: A, arg1: number) => Promise<B> | ||
fn: (arg0: A, arg1: number) => Promise<B>, | ||
): Promise<B[]> { | ||
@@ -103,5 +91,5 @@ const data = Array(items.length); | ||
.fill(() => undefined) | ||
.map(step) | ||
.map(step), | ||
); | ||
return data; | ||
} |
{ | ||
"extends": "../../tsconfig.base", | ||
"compilerOptions": { | ||
"declaration": true, | ||
"declarationMap": true, | ||
"sourceMap": true, | ||
"strict": true, | ||
"noImplicitAny": false, | ||
"noImplicitThis": false, | ||
"allowSyntheticDefaultImports": true, | ||
"esModuleInterop": true, | ||
"skipLibCheck": true, | ||
"forceConsistentCasingInFileNames": true, | ||
"moduleResolution": "node", | ||
"downlevelIteration": true, | ||
"resolveJsonModule": true, | ||
"module": "commonjs", | ||
"lib": ["es2020", "dom"], | ||
@@ -17,0 +11,0 @@ "outDir": "lib" |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
3
35
30351
446
1