stack-promises
Advanced tools
Comparing version 0.0.4 to 0.0.5
@@ -1,1 +0,86 @@ | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});const e=()=>!0;const s=new Error("Stack is empty"),r=new Error("Promise is not actual"),t=new Error("stackPromises only works with functions that returns a Promise");exports.default=()=>{const o=[],n=[],i=({task:e,index:s})=>()=>{let r=(({task:e,index:s})=>{const r=n.find(({task:r,index:t})=>e===r&&s===t);if(r)return r.promise})({task:e,index:s});return r||(r=e(),(({task:e,promise:s,index:r})=>{n.push({task:e,promise:s,index:r})})({promise:r,task:e,index:s})),r},c=()=>((s,r=e)=>s.reduce((e,s)=>e.then(({success:e,errors:t,results:o})=>{let n;return n=r(s)?s():Promise.reject((e=>{const s=new Error("Promise was not running");return s.basePromise=e,s.id="ERROR_NOT_RUNNING",s.name="Not running",s})(s)),n.then(s=>({errors:t,success:[...e,s],results:[...o,s],isSuccessful:!0,isError:!1})).catch(s=>({success:e,errors:[...t,s],results:[...o,s],isSuccessful:!1,isError:!0}))}),Promise.resolve({success:[],errors:[],results:[]})))(o),u=()=>0===o.length?Promise.reject(s):new Promise((e,s)=>{const t=(({resolve:e,reject:s})=>({results:t,isSuccessful:n})=>{if(t.length===o.length){const r=t[t.length-1];n?e(r):s(r)}else s(r)})({resolve:e,reject:s});c().then(t).catch(t)});return u.add=e=>{if("function"!=typeof e)throw t;const s=o.length;return o.push(i({task:e,index:s})),u},u},exports.isEmptyStackError=e=>e===s,exports.isPromiseIsNotActualError=e=>e===r; | ||
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.isPromiseIsNotActualError = 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; | ||
}; | ||
exports.isEmptyStackError = isEmptyStackError; | ||
const promiseIsNotActualError = new Error('Promise is not actual'); | ||
const isPromiseIsNotActualError = (error) => { | ||
return error === promiseIsNotActualError; | ||
}; | ||
exports.isPromiseIsNotActualError = isPromiseIsNotActualError; | ||
const notFunctionError = new Error('stackPromises only works with functions that returns a Promise'); | ||
const creteStackPromises = () => { | ||
const runnersStack = []; | ||
const tasksStack = []; | ||
const addToTasksStack = ({ task, promise, index }) => { | ||
tasksStack.push({ task, promise, index }); | ||
}; | ||
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 resolveRunner = ({ task, index }) => { | ||
return () => { | ||
let promise = getPromiseFromTasksStackByTask({ task, index }); | ||
if (!promise) { | ||
promise = task(); | ||
addToTasksStack({ promise, task, index }); | ||
} | ||
return promise; | ||
}; | ||
}; | ||
const resolveFinishResultPromise = ({ resolve, reject, }) => { | ||
return ({ results, isSuccessful }) => { | ||
const sizePromises = results.length; | ||
const sizeStackPromises = runnersStack.length; | ||
if (sizePromises === sizeStackPromises) { | ||
const lastResult = results[results.length - 1]; | ||
if (isSuccessful) { | ||
resolve(lastResult); | ||
} | ||
else { | ||
reject(lastResult); | ||
} | ||
} | ||
else { | ||
reject(promiseIsNotActualError); | ||
} | ||
}; | ||
}; | ||
const runStackPromises = () => { | ||
return sequent_promises_1.default(runnersStack); | ||
}; | ||
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; | ||
}; | ||
result.add = addTaskToStack; | ||
return result; | ||
}; | ||
exports.default = creteStackPromises; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "stack-promises", | ||
"version": "0.0.4", | ||
"version": "0.0.5", | ||
"description": "sequentPromises resolves Promises sequentially", | ||
@@ -21,10 +21,5 @@ "keywords": [ | ||
}, | ||
"exports": { | ||
".": { | ||
"import": "./dist/index.mjs", | ||
"require": "./dist/index.js" | ||
} | ||
}, | ||
"main": "dist/index.js", | ||
"module": "dist/index.mjs", | ||
"main": "dist/index.umd.js", | ||
"module": "dist/index.es5.js", | ||
"typings": "dist/index.d.ts", | ||
"files": [ | ||
@@ -35,5 +30,8 @@ "dist/**/*", | ||
"scripts": { | ||
"build": "NODE_ENV=production rollup --config", | ||
"format": "prettier --write \"src/**/*.js\"", | ||
"lint": "eslint src/. --ext .js", | ||
"prebuild": "rimraf dist", | ||
"build": "yarn build:ts && yarn build:js", | ||
"build:js": "NODE_ENV=production rollup --config", | ||
"build:ts": "tsc --build tsconfig.build.json", | ||
"format": "prettier --write \"src/**/*.ts\"", | ||
"lint": "eslint src/. --ext .ts", | ||
"prepare": "npm run build", | ||
@@ -45,31 +43,31 @@ "prepublishOnly": "npm test && npm run lint", | ||
"dependencies": { | ||
"promise-delay": "^2.1.0", | ||
"sequent-promises": "^0.1.1" | ||
"sequent-promises": "^0.1.2" | ||
}, | ||
"devDependencies": { | ||
"@babel/core": "^7.10.2", | ||
"@babel/preset-env": "^7.10.2", | ||
"@commitlint/cli": "^8.3.5", | ||
"@commitlint/config-conventional": "^8.3.4", | ||
"@rollup/plugin-node-resolve": "^8.0.1", | ||
"babel-eslint": "10.1.0", | ||
"babel-jest": "^26.0.1", | ||
"eslint": "^7.2.0", | ||
"eslint-config-airbnb": "^18.1.0", | ||
"eslint-config-prettier": "^6.11.0", | ||
"eslint-plugin-import": "^2.21.2", | ||
"eslint-plugin-jest": "^23.13.2", | ||
"eslint-plugin-jsx-a11y": "^6.2.3", | ||
"eslint-plugin-prettier": "^3.1.3", | ||
"eslint-plugin-react": "^7.20.0", | ||
"husky": "^4.2.5", | ||
"jest": "26.0.1", | ||
"lint-staged": "^10.2.9", | ||
"prettier": "^2.0.5", | ||
"rollup": "^2.15.0", | ||
"rollup-plugin-babel": "^4.4.0", | ||
"rollup-plugin-terser": "^6.1.0", | ||
"standard-version": "^8.0.0" | ||
"@commitlint/cli": "^13.1.0", | ||
"@commitlint/config-conventional": "^13.1.0", | ||
"@rollup/plugin-commonjs": "^19.0.2", | ||
"@rollup/plugin-node-resolve": "^13.0.4", | ||
"@types/jest": "^26.0.24", | ||
"@types/node": "^16.4.5", | ||
"@typescript-eslint/eslint-plugin": "^4.28.5", | ||
"@typescript-eslint/parser": "^4.28.5", | ||
"eslint": "^7.31.0", | ||
"eslint-config-prettier": "^8.3.0", | ||
"eslint-plugin-import": "^2.23.4", | ||
"eslint-plugin-jest": "^24.4.0", | ||
"eslint-plugin-prettier": "^3.4.0", | ||
"husky": "^7.0.1", | ||
"jest": "27.0.6", | ||
"lint-staged": "^11.1.1", | ||
"prettier": "^2.3.2", | ||
"promise-delay": "^2.1.0", | ||
"rollup": "^2.55.0", | ||
"rollup-plugin-terser": "^7.0.2", | ||
"rollup-plugin-typescript2": "0.30.0", | ||
"standard-version": "^9.3.1", | ||
"ts-jest": "^27.0.4", | ||
"typescript": "^4.3.5" | ||
}, | ||
"main:src": "src/index.js", | ||
"main:src": "src/index.ts", | ||
"standard-version": { | ||
@@ -76,0 +74,0 @@ "scripts": { |
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
28201
1
11
107
24
- Removedpromise-delay@^2.1.0
- Removedpromise-delay@2.1.0(transitive)
Updatedsequent-promises@^0.1.2