stack-promises
Advanced tools
Comparing version 0.0.13 to 1.0.0
export declare const isEmptyStackError: (error: Error) => boolean; | ||
export declare const promiseIsNotActualError: Error; | ||
export declare const isPromiseIsNotActualError: (error: Error) => boolean; | ||
declare const creteStackPromises: <T = any>({ noRejectIsNotActual, noRunIsNotActual, }?: { | ||
declare const creteStackPromises: <T>({ noRejectIsNotActual, noRunIsNotActual, }?: { | ||
noRejectIsNotActual?: boolean | undefined; | ||
@@ -18,2 +18,1 @@ noRunIsNotActual?: boolean | undefined; | ||
export default creteStackPromises; | ||
//# sourceMappingURL=index.d.ts.map |
@@ -1,135 +0,104 @@ | ||
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
const b = "ERROR_NOT_RUNNING", I = (e) => { | ||
const o = new Error("Promise was not running"); | ||
return o.basePromise = e, o.id = b, o.name = "Not running", o; | ||
}, L = async (e, o) => e.reduce( | ||
async (a, c) => a.then(async ({ success: k, errors: m, results: f }) => (!o || o(c) ? c() : Promise.reject(I(c))).then((i) => ({ | ||
errors: m, | ||
success: [...k, i], | ||
results: [...f, i], | ||
isSuccessful: !0, | ||
isError: !1 | ||
})).catch((i) => ({ | ||
success: k, | ||
errors: [...m, i], | ||
results: [...f, i], | ||
isSuccessful: !1, | ||
isError: !0 | ||
}))), | ||
Promise.resolve({ | ||
success: [], | ||
errors: [], | ||
results: [], | ||
isSuccessful: !1, | ||
isError: !1 | ||
}) | ||
), S = new Error("Stack is empty"), B = (e) => e === S, E = new Error("Promise is not actual"), O = (e) => e === E, x = new Error( | ||
"stackPromises only works with functions that returns a Promise" | ||
), _ = ({ | ||
noRejectIsNotActual: e = !1, | ||
noRunIsNotActual: o = !1 | ||
} = {}) => { | ||
const a = [], c = [], k = ({ task: s, index: r }) => { | ||
c.push({ task: s, index: r }); | ||
}, m = (s, { task: r, index: t }) => { | ||
const n = c.find(({ task: l, index: P }) => r === l && t === P); | ||
if (!n) | ||
throw new Error("Task not found"); | ||
if (n.promise) | ||
throw new Error("Task is already running"); | ||
n.promise = s; | ||
}, f = ({ | ||
task: s, | ||
index: r | ||
}) => { | ||
const t = c.find(({ task: n, index: l }) => s === n && r === l); | ||
if (t) | ||
return t.promise; | ||
}, i = ({ task: s }) => { | ||
const r = c.at(-1); | ||
return (r == null ? void 0 : r.task) === s; | ||
}, y = ({ task: s, index: r }) => (k({ task: s, index: r }), async () => { | ||
let t = f({ task: s, index: r }); | ||
const n = i({ task: s }); | ||
return !t && o && !n ? Promise.resolve() : (t || (t = s({ isActual: n }), m(t, { task: s, index: r })), t); | ||
}), p = ({ | ||
resolve: s, | ||
reject: r | ||
}) => ({ results: t, isSuccessful: n }) => { | ||
const l = t.length, P = a.length; | ||
if (l === P) { | ||
const h = t.at(-1); | ||
if (n) { | ||
s(h); | ||
return; | ||
} | ||
r(h); | ||
return; | ||
} | ||
if (e) { | ||
const h = t.at(-1); | ||
s(h); | ||
return; | ||
} | ||
r(E); | ||
}; | ||
let d = !1; | ||
const w = () => { | ||
d = !0; | ||
}, R = () => { | ||
d = !1; | ||
}, g = () => d, N = async () => (w(), L(a, g)), u = async () => { | ||
if (a.length === 0) | ||
throw S; | ||
return new Promise((s, r) => { | ||
const t = p({ resolve: s, reject: r }); | ||
N().then(t).catch(t); | ||
}); | ||
}, T = (s) => { | ||
if (typeof s != "function") | ||
throw x; | ||
const r = a.length; | ||
return a.push(y({ task: s, index: r })), u; | ||
}, A = async (s) => (T(s), u()), v = () => { | ||
a.length = 0, c.length = 0; | ||
}, F = () => { | ||
R(), v(); | ||
}; | ||
return u.add = T, u.run = A, u.stop = F, u; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.isPromiseIsNotActualError = exports.promiseIsNotActualError = exports.isEmptyStackError = void 0; | ||
const sequent_promises_1 = __importDefault(require("sequent-promises")); | ||
const emptyStackError = new Error('Stack is empty'); | ||
const isEmptyStackError = (error) => { | ||
return error === emptyStackError; | ||
export { | ||
_ as default, | ||
B as isEmptyStackError, | ||
O as isPromiseIsNotActualError, | ||
E as promiseIsNotActualError | ||
}; | ||
exports.isEmptyStackError = isEmptyStackError; | ||
exports.promiseIsNotActualError = new Error('Promise is not actual'); | ||
const isPromiseIsNotActualError = (error) => { | ||
return error === exports.promiseIsNotActualError; | ||
}; | ||
exports.isPromiseIsNotActualError = isPromiseIsNotActualError; | ||
const notFunctionError = new Error('stackPromises only works with functions that returns a Promise'); | ||
const creteStackPromises = ({ noRejectIsNotActual = false, noRunIsNotActual = false, } = {}) => { | ||
const runnersStack = []; | ||
const tasksStack = []; | ||
const addToTasksStack = ({ task, index }) => { | ||
tasksStack.push({ task, index }); | ||
}; | ||
const addPromiseToTasksStack = (promise, { task: desiredTask, index: desiredIndex }) => { | ||
const taskRunner = tasksStack.find(({ task, index }) => { | ||
return desiredTask === task && desiredIndex === index; | ||
}); | ||
if (!taskRunner) { | ||
throw new Error('Task not found'); | ||
} | ||
if (taskRunner.promise) { | ||
throw new Error('Task is already running'); | ||
} | ||
taskRunner.promise = promise; | ||
}; | ||
const getPromiseFromTasksStackByTask = ({ task: desiredTask, index: desiredIndex, }) => { | ||
const taskRunner = tasksStack.find(({ task, index }) => { | ||
return desiredTask === task && desiredIndex === index; | ||
}); | ||
if (taskRunner) { | ||
return taskRunner.promise; | ||
} | ||
return undefined; | ||
}; | ||
const hasLastFromTasksStackByTask = ({ task: desiredTask }) => { | ||
const lastTaskRunner = tasksStack[tasksStack.length - 1]; | ||
const isLastTask = (lastTaskRunner === null || lastTaskRunner === void 0 ? void 0 : lastTaskRunner.task) === desiredTask; | ||
return isLastTask; | ||
}; | ||
const resolveRunner = ({ task, index }) => { | ||
addToTasksStack({ task, index }); | ||
return () => { | ||
let promise = getPromiseFromTasksStackByTask({ task, index }); | ||
const isActual = hasLastFromTasksStackByTask({ task }); | ||
if (!promise && noRunIsNotActual && !isActual) { | ||
return Promise.resolve(); | ||
} | ||
if (!promise) { | ||
promise = task({ isActual }); | ||
addPromiseToTasksStack(promise, { task, index }); | ||
} | ||
return promise; | ||
}; | ||
}; | ||
const resolveFinishResultPromise = ({ resolve, reject, }) => { | ||
return ({ results, isSuccessful }) => { | ||
const sizePromises = results.length; | ||
const sizeStackPromises = runnersStack.length; | ||
const isActual = sizePromises === sizeStackPromises; | ||
if (isActual) { | ||
const lastResult = results[results.length - 1]; | ||
if (isSuccessful) { | ||
return resolve(lastResult); | ||
} | ||
return reject(lastResult); | ||
} | ||
if (noRejectIsNotActual) { | ||
const lastResult = results[results.length - 1]; | ||
return resolve(lastResult); | ||
} | ||
reject(exports.promiseIsNotActualError); | ||
return undefined; | ||
}; | ||
}; | ||
let isCanRunTasks = false; | ||
const enableRunTasks = () => { | ||
isCanRunTasks = true; | ||
}; | ||
const disableRunTasks = () => { | ||
isCanRunTasks = false; | ||
}; | ||
const canRunTask = () => { | ||
return isCanRunTasks; | ||
}; | ||
const runStackPromises = () => { | ||
enableRunTasks(); | ||
return (0, sequent_promises_1.default)(runnersStack, canRunTask); | ||
}; | ||
const result = () => { | ||
if (runnersStack.length === 0) { | ||
return Promise.reject(emptyStackError); | ||
} | ||
return new Promise((resolve, reject) => { | ||
const finishResultPromise = resolveFinishResultPromise({ resolve, reject }); | ||
runStackPromises().then(finishResultPromise).catch(finishResultPromise); | ||
}); | ||
}; | ||
const addTaskToStack = (task) => { | ||
if (typeof task !== 'function') { | ||
throw notFunctionError; | ||
} | ||
const index = runnersStack.length; | ||
runnersStack.push(resolveRunner({ task, index })); | ||
return result; | ||
}; | ||
const run = (task) => { | ||
addTaskToStack(task); | ||
return result(); | ||
}; | ||
const clearStacks = () => { | ||
runnersStack.length = 0; | ||
tasksStack.length = 0; | ||
}; | ||
const stop = () => { | ||
disableRunTasks(); | ||
clearStacks(); | ||
}; | ||
result.add = addTaskToStack; | ||
result.run = run; | ||
result.stop = stop; | ||
return result; | ||
}; | ||
exports.default = creteStackPromises; | ||
//# sourceMappingURL=index.js.map |
107
package.json
{ | ||
"name": "stack-promises", | ||
"version": "0.0.13", | ||
"version": "1.0.0", | ||
"description": "sequentPromises resolves Promises sequentially", | ||
@@ -21,59 +21,70 @@ "keywords": [ | ||
}, | ||
"main": "dist/index.js", | ||
"module": "dist/index.es5.js", | ||
"typings": "dist/index.d.ts", | ||
"type": "module", | ||
"exports": { | ||
".": { | ||
"require": "./dist/index.cjs", | ||
"import": "./dist/index.js" | ||
} | ||
}, | ||
"main": "./dist/index.js", | ||
"types": "dist/index.d.ts", | ||
"files": [ | ||
"dist/**/*", | ||
"README.md" | ||
"dist" | ||
], | ||
"scripts": { | ||
"prebuild": "rimraf dist", | ||
"build": "yarn build:ts && yarn build:js", | ||
"build:js": "NODE_ENV=production rollup --config --bundleConfigAsCjs", | ||
"build:ts": "tsc --build tsconfig.build.json", | ||
"format": "prettier --write \"src/**/*.ts\"", | ||
"lint": "eslint src/. --ext .ts", | ||
"prepare": "yarn build", | ||
"prepublishOnly": "yarn test && yarn lint", | ||
"release": "standard-version", | ||
"test": "jest" | ||
"build": "vite build", | ||
"lint": "yarn lint:js && yarn lint:ts", | ||
"lint:js": "eslint src --ext ts,tsx --ignore-path .gitignore --report-unused-disable-directives --max-warnings 0", | ||
"lint:ts": "tsc", | ||
"prepare": "husky install", | ||
"prepublishOnly": "yarn build", | ||
"preview": "vite preview", | ||
"release": "standard-version && yarn release:publish", | ||
"release:major": "standard-version --release-as major && yarn release:publish", | ||
"release:pre": "standard-version --prerelease && yarn release:publish", | ||
"release:publish": "git push --follow-tags origin master && npm publish", | ||
"test": "jest --watch", | ||
"test:ci": "cross-env CI=true jest --passWithNoTests", | ||
"test:coverage": "yarn test:ci --coverage --reporters=default --reporters=jest-junit" | ||
}, | ||
"dependencies": { | ||
"sequent-promises": "^0.1.4" | ||
"sequent-promises": "^1.0.0" | ||
}, | ||
"devDependencies": { | ||
"@commitlint/cli": "^17.5.0", | ||
"@commitlint/config-conventional": "^17.4.4", | ||
"@rollup/plugin-commonjs": "^24.0.1", | ||
"@rollup/plugin-node-resolve": "^15.0.1", | ||
"@types/jest": "^29.5.0", | ||
"@types/node": "^18.15.5", | ||
"@typescript-eslint/eslint-plugin": "^5.56.0", | ||
"@typescript-eslint/parser": "^5.56.0", | ||
"eslint": "^8.36.0", | ||
"eslint-config-airbnb": "^19.0.4", | ||
"eslint-config-airbnb-typescript": "17.0.0", | ||
"eslint-config-prettier": "^8.8.0", | ||
"eslint-plugin-import": "^2.27.5", | ||
"eslint-plugin-jest": "^27.2.1", | ||
"eslint-plugin-prettier": "^4.2.1", | ||
"@babel/preset-typescript": "^7.23.2", | ||
"@commitlint/cli": "^18.0.0", | ||
"@commitlint/config-conventional": "^18.0.0", | ||
"@nabla/vite-plugin-eslint": "^1.5.0", | ||
"@types/jest": "^29.5.6", | ||
"@typescript-eslint/eslint-plugin": "^6.9.0", | ||
"@typescript-eslint/parser": "^6.9.0", | ||
"cross-env": "^7.0.3", | ||
"eslint": "^8.52.0", | ||
"eslint-config-airbnb-base": "^15.0.0", | ||
"eslint-config-airbnb-typescript": "^17.1.0", | ||
"eslint-config-prettier": "^9.0.0", | ||
"eslint-import-resolver-typescript": "^3.6.1", | ||
"eslint-plugin-flowtype": "^8.0.3", | ||
"eslint-plugin-import": "^2.29.0", | ||
"eslint-plugin-jest": "^27.4.3", | ||
"eslint-plugin-prettier": "^5.0.1", | ||
"eslint-plugin-unicorn": "^48.0.1", | ||
"husky": "^8.0.3", | ||
"jest": "29.5.0", | ||
"lint-staged": "^13.2.0", | ||
"prettier": "^2.8.6", | ||
"promise-delay": "^2.1.0", | ||
"rollup": "^3.20.0", | ||
"rollup-plugin-terser": "^7.0.2", | ||
"rollup-plugin-typescript2": "0.34.1", | ||
"jest": "^29.7.0", | ||
"jest-environment-jsdom": "^29.7.0", | ||
"jest-extended": "^4.0.2", | ||
"jest-junit": "^16.0.0", | ||
"lint-staged": "^15.0.2", | ||
"prettier": "^3.0.3", | ||
"standard-version": "^9.5.0", | ||
"ts-jest": "^29.0.5", | ||
"typescript": "^5.0.2" | ||
"ts-jest": "^29.1.1", | ||
"ts-node": "^10.9.1", | ||
"tsc-files": "^1.1.4", | ||
"typescript": "^5.2.2", | ||
"vite": "^4.5.0", | ||
"vite-plugin-dts": "^3.6.1", | ||
"vite-tsconfig-paths": "^4.2.1" | ||
}, | ||
"main:src": "src/index.ts", | ||
"standard-version": { | ||
"scripts": { | ||
"prerelease": "npm run lint && CI=true npm test", | ||
"postchangelog": "prettier --write \"CHANGELOG.md\"" | ||
} | ||
} | ||
"peerDependencies": {}, | ||
"main:src": "src/index.ts" | ||
} |
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
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
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
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
1
Yes
12124
33
6
130
1
+ Addedsequent-promises@1.0.3(transitive)
- Removedsequent-promises@0.1.4(transitive)
Updatedsequent-promises@^1.0.0