-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BREAKING CHANGE: First Release
- Loading branch information
Showing
21 changed files
with
6,540 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"extends": ["standard"], | ||
"rules": { | ||
"no-console": "off", | ||
"semi": ["error", "never"] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
name: Release | ||
on: | ||
push: | ||
branches: | ||
- master | ||
|
||
jobs: | ||
release: | ||
name: Release | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v2 | ||
with: | ||
fetch-depth: 0 | ||
- name: Setup Node.js | ||
uses: actions/setup-node@v2 | ||
with: | ||
node-version: 'lts/*' | ||
- name: Install dependencies | ||
run: yarn | ||
- name: Build | ||
run: yarn build | ||
- name: Release | ||
env: | ||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
NPM_TOKEN: ${{ secrets.NPM_TOKEN }} | ||
run: npx semantic-release |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
logs | ||
*.log | ||
npm-debug.log* | ||
.DS_Store | ||
|
||
coverage | ||
node_modules | ||
build | ||
.env.local | ||
.env.development.local | ||
.env.test.local | ||
.env.production.local | ||
|
||
*.cache | ||
.env |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"hooks": { | ||
"pre-commit": "lint-staged" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"*.{ts,js}": ["prettier --write", "eslint --fix", "git add"], | ||
"*.svg": ["svgo", "git add"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
lts/fermium |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"bracketSpacing": true, | ||
"jsxBracketSameLine": false, | ||
"printWidth": 100, | ||
"semi": false, | ||
"singleQuote": true, | ||
"tabWidth": 2, | ||
"trailingComma": "all", | ||
"useTabs": true, | ||
"arrowParens": "avoid" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
save-prefix "" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# Promise until tired | ||
|
||
[![semantic-release](https://img.shields.io/badge/semantic-release-e10079.svg?logo=semantic-release)](https://github.com/semantic-release/semantic-release) | ||
[![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com) | ||
![ts](https://badgen.net/badge/Built%20With/TypeScript/blue) [![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square)](https://github.com/prettier/prettier) | ||
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT) | ||
|
||
Too long unresolved promise is tired and I gonna give up. | ||
|
||
This is the promise with timeout. | ||
|
||
## Installation | ||
|
||
``` | ||
yarn add promise-until-tired | ||
``` | ||
|
||
### Usage | ||
|
||
```ts | ||
import newPromiseUntilTired, { TiredFromWaitingPromiseResolveTooLongError } from 'promise-until-tired' | ||
|
||
try { | ||
const value = await newPromiseUntilTired((resolve, reject) => { | ||
// logic | ||
}, timeout) | ||
} catch (error) { | ||
if (error instanceof TiredFromWaitingPromiseResolveTooLongError) { | ||
// handle timeout logic | ||
} else { | ||
// handle other reject logic | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
module.exports = { | ||
moduleFileExtensions: ['json', 'ts', 'js'], | ||
transform: { | ||
'^.+\\.ts$': 'ts-jest', | ||
}, | ||
testRegex: '((\\.|/)(test|spec))\\.ts$', | ||
transformIgnorePatterns: ['/node_modules/', '/dist/'], | ||
testEnvironment: 'jsdom', | ||
moduleDirectories: ['node_modules', 'src'], | ||
coverageReporters: ['cobertura', 'lcov', 'json', 'text'], | ||
coverageThreshold: { | ||
global: { | ||
statements: 80, | ||
branches: 80, | ||
functions: 80, | ||
lines: 80, | ||
}, | ||
}, | ||
collectCoverageFrom: [ | ||
'src/**/*.ts', | ||
'!src/**/*.d.ts', | ||
'!src/**/types.ts', | ||
'!src/**/constants.ts', | ||
'!src/types/index.ts', | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
{ | ||
"name": "promise-until-tired", | ||
"version": "0.0.0", | ||
"types": "build", | ||
"deploy": "build", | ||
"entry": "src/index.ts", | ||
"main": "build/index.js", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/wongnai/promise-until-tired.git" | ||
}, | ||
"homepage": "https://github.com/wongnai/promise-until-tired#readme", | ||
"license": "MIT", | ||
"scripts": { | ||
"build": "rollup -c", | ||
"lint": "eslint --ext .ts,.js --quiet", | ||
"test": "jest --coverage" | ||
}, | ||
"devDependencies": { | ||
"@rollup/plugin-commonjs": "19.0.0", | ||
"@rollup/plugin-json": "4.1.0", | ||
"@rollup/plugin-node-resolve": "13.0.0", | ||
"@rollup/plugin-typescript": "8.2.1", | ||
"@types/jest": "27.0.2", | ||
"@types/node": "15.12.1", | ||
"@types/qs": "6.9.7", | ||
"eslint": "7.28.0", | ||
"eslint-config-standard": "16.0.3", | ||
"husky": "6.0.0", | ||
"jest": "27.3.1", | ||
"lint-staged": "11.0.0", | ||
"prettier": "2.3.1", | ||
"rollup": "2.51.0", | ||
"rollup-plugin-cleaner": "1.0.0", | ||
"rollup-plugin-includepaths": "0.2.4", | ||
"rollup-plugin-visualizer": "5.5.0", | ||
"semantic-release": "18.0.0", | ||
"ts-jest": "27.0.7", | ||
"typescript": "4.4.4" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import path from 'path' | ||
|
||
import commonjs from '@rollup/plugin-commonjs' | ||
import json from '@rollup/plugin-json' | ||
import resolve from '@rollup/plugin-node-resolve' | ||
import typescript from '@rollup/plugin-typescript' | ||
import visualizer from 'rollup-plugin-visualizer' | ||
|
||
import config from './package.json' | ||
|
||
export default [{ | ||
input: config.entry, | ||
output: { | ||
format: 'cjs', | ||
sourcemap: true, | ||
dir: config.deploy, | ||
}, | ||
plugins: [ | ||
resolve({ | ||
browser: true, | ||
}), | ||
commonjs(), | ||
typescript({ | ||
declaration: true, | ||
declarationDir: config.deploy, | ||
exclude: 'src/**/*.test.ts', | ||
}), | ||
json(), | ||
visualizer({ | ||
filename: path.resolve(config.deploy, 'stat.html'), | ||
}), | ||
], | ||
}] |
9 changes: 9 additions & 0 deletions
9
src/errors/TiredFromWaitingPromiseResolveTooLongError/index.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import TiredFromWaitingPromiseResolveTooLongError from '.' | ||
|
||
describe('TiredFromWaitingPromiseResolveTooLongError', () => { | ||
describe('message', () => { | ||
it('should return message to inform that we give up already about the response from promise which including the limit of time to wait', () => { | ||
expect(new TiredFromWaitingPromiseResolveTooLongError(10000).message).toBe('I have waited this promise resolving for 10 seconds, so I am tired and forgive to wait for this.') | ||
}) | ||
}) | ||
}) |
9 changes: 9 additions & 0 deletions
9
src/errors/TiredFromWaitingPromiseResolveTooLongError/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { MS_IN_SEC } from "variables" | ||
|
||
class TiredFromWaitingPromiseResolveTooLongError extends Error { | ||
constructor(timeout: number) { | ||
super(`I have waited this promise resolving for ${timeout / MS_IN_SEC} seconds, so I am tired and forgive to wait for this.`) | ||
} | ||
} | ||
|
||
export default TiredFromWaitingPromiseResolveTooLongError |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import TiredFromWaitingPromiseResolveTooLongError from "errors/TiredFromWaitingPromiseResolveTooLongError" | ||
import newPromiseUntilTired from "." | ||
|
||
describe('newPromiseUntilTired', () => { | ||
describe('never resolve and timeout', () => { | ||
it('should reject TiredFromWaitingPromiseResolveTooLongError', async () => { | ||
await expect(newPromiseUntilTired<never>(() => {}, 500)).rejects.toBeInstanceOf(TiredFromWaitingPromiseResolveTooLongError) | ||
}) | ||
}) | ||
|
||
describe('resolve after timeout', () => { | ||
it('should reject TiredFromWaitingPromiseResolveTooLongError', async () => { | ||
await expect(newPromiseUntilTired<void>((resolve) => { | ||
setTimeout(resolve, 1000) | ||
}, 500)).rejects.toBeInstanceOf(TiredFromWaitingPromiseResolveTooLongError) | ||
}) | ||
}) | ||
|
||
describe('reject after timeout', () => { | ||
it('should reject TiredFromWaitingPromiseResolveTooLongError', async () => { | ||
await expect(newPromiseUntilTired<void>((_, reject) => { | ||
setTimeout(reject, 1000) | ||
}, 500)).rejects.toBeInstanceOf(TiredFromWaitingPromiseResolveTooLongError) | ||
}) | ||
}) | ||
|
||
describe('resolve before timeout', () => { | ||
it('should resolve the value correctly', async () => { | ||
const expectedResolvedValue = 'resolved-value' | ||
|
||
await expect(newPromiseUntilTired<string>((resolve) => { | ||
setTimeout(() => resolve(expectedResolvedValue), 500) | ||
}, 1000)).resolves.toBe(expectedResolvedValue) | ||
}) | ||
}) | ||
|
||
describe('reject before timeout', () => { | ||
it('should reject the value correctly', async () => { | ||
const expectedRejectedValue = 'rejected-value' | ||
|
||
await expect(newPromiseUntilTired<string>((_, reject) => { | ||
setTimeout(() => reject(expectedRejectedValue), 500) | ||
}, 1000)).rejects.toBe(expectedRejectedValue) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import TimeoutError from "errors/TiredFromWaitingPromiseResolveTooLongError" | ||
import { PromiseExecutor } from "types" | ||
|
||
function newPromiseUntilTired<T>(executor: PromiseExecutor<T>, timeout: number) { | ||
let isResolved = false | ||
|
||
return new Promise<T>((resolve, reject) => { | ||
setTimeout(() => { | ||
if (!isResolved) { | ||
reject(new TimeoutError(timeout)) | ||
} | ||
}, timeout) | ||
|
||
const resolveWithMarked = (resolvedValue: T) => { | ||
isResolved = true | ||
|
||
resolve(resolvedValue) | ||
} | ||
|
||
const rejectWithMarked = (rejectedValue: any) => { | ||
isResolved = true | ||
|
||
reject(rejectedValue) | ||
} | ||
|
||
executor(resolveWithMarked, rejectWithMarked) | ||
}) | ||
} | ||
|
||
export const TiredFromWaitingPromiseResolveTooLongError = TimeoutError | ||
|
||
export default newPromiseUntilTired |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export type ResolverFunction<T> = (resolvedValue: T) => void | ||
|
||
export type PromiseExecutor<T> = (resolve: ResolverFunction<T>, reject: ResolverFunction<any>) => void |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const MS_IN_SEC = 1000 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
{ | ||
"compilerOptions": { | ||
"target": "es2019", | ||
"module": "esnext", | ||
"strict": true, | ||
"baseUrl": "./src", | ||
"importHelpers": true, | ||
"moduleResolution": "node", | ||
"esModuleInterop": true, | ||
"allowSyntheticDefaultImports": true, | ||
"sourceMap": true, | ||
"types": [ | ||
"jest", | ||
"node" | ||
], | ||
"noEmit": true, | ||
"forceConsistentCasingInFileNames": true, | ||
"resolveJsonModule": true, | ||
"isolatedModules": false, | ||
"strictNullChecks": true, | ||
"suppressImplicitAnyIndexErrors": true | ||
}, | ||
"include": [ | ||
"src/**/*.ts", | ||
], | ||
"exclude": [ | ||
"node_modules" | ||
] | ||
} |
Oops, something went wrong.