Skip to content

Commit

Permalink
update dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
kaisukez committed Oct 13, 2023
1 parent 2c8d9a8 commit 327e853
Show file tree
Hide file tree
Showing 11 changed files with 3,161 additions and 7,013 deletions.
6 changes: 2 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
node_modules
.idea

node_modules
dist
coverage

experiment
*.ignore.ts
9,886 changes: 3,007 additions & 6,879 deletions package-lock.json

Large diffs are not rendered by default.

18 changes: 9 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@
"test": "jest -c jest.config.js --coverage"
},
"devDependencies": {
"@types/jest": "^27.4.0",
"@types/node": "^17.0.18",
"@types/node-fetch": "^2.5.12",
"jest": "^27.5.1",
"node-abort-controller": "^3.0.1",
"node-fetch": "^2.5.12",
"ts-jest": "^27.1.3",
"tsup": "^5.11.13",
"typescript": "^4.5.5"
"@types/jest": "^29.5.5",
"@types/node": "^20.8.5",
"@types/node-fetch": "^2.6.6",
"jest": "^29.7.0",
"node-abort-controller": "^3.1.1",
"node-fetch": "^3.3.2",
"ts-jest": "^29.1.1",
"tsup": "^7.2.0",
"typescript": "^5.2.2"
}
}
30 changes: 19 additions & 11 deletions src/CancellationError.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,35 +9,39 @@ async function sleep(ms: number) {
describe('async', () => {
it('should throw an Error', async () => {
const error = new Error('error')

async function task() {
await sleep(1)
throw error
}

await expect(task()).rejects.toBe(error)
})

it('should throw an Error even if you use CancellationError.ignoreAsync', async () => {
const error = new Error('error')

async function task() {
await sleep(1)
throw error
}

await expect(CancellationError.ignoreAsync(() => task())).rejects.toBe(error)
})

it('should throw a CancellationError', async () => {
const token = new CancellationToken(cancel => {
setTimeout(async () => {
await cancel()
}, 5)
})

async function task(token: CancellationToken) {
await sleep(10)
expect(token.isCancellationRequested).toBe(true)
token.throwIfCancellationRequested()
}

await expect(task(token)).rejects.toBeInstanceOf(CancellationError)
})

Expand All @@ -47,57 +51,61 @@ describe('async', () => {
await cancel()
}, 5)
})

async function task(token: CancellationToken) {
await sleep(10)
expect(token.isCancellationRequested).toBe(true)
token.throwIfCancellationRequested()
}

await expect(CancellationError.ignoreAsync(() => task(token))).resolves.toBe(undefined)
})
})

describe('sync', () => {
it('should throw an Error', () => {
const error = new Error('error')

function task() {
throw error
}

expect(() => task()).toThrow(error)
})

it('should throw an Error even if you use CancellationError.ignoreSync', async () => {
const error = new Error('error')

function task() {
throw error
}

expect(() => CancellationError.ignoreSync(() => task())).toThrow(error)
})

it('should throw a CancellationError', async () => {
const token = new CancellationToken(async cancel => {
await cancel()
})

async function task(token: CancellationToken) {
expect(token.isCancellationRequested).toBe(true)
token.throwIfCancellationRequested()
}

await expect(task(token)).rejects.toBeInstanceOf(CancellationError)
})

it('should not throw an error', async () => {
const token = new CancellationToken(async cancel => {
await cancel()
})

function task(token: CancellationToken) {
expect(token.isCancellationRequested).toBe(true)
token.throwIfCancellationRequested()
}

expect(() => CancellationError.ignoreSync(() => task(token))).not.toThrow()
})
})
})
13 changes: 7 additions & 6 deletions src/CancellationError.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,30 @@
export default class CancellationError {
constructor(
public readonly reason?: any,
) {}
) {
}

public static async ignoreAsync<T>(promise: () => Promise<T>): Promise<T|undefined> {
public static async ignoreAsync<T>(promise: () => Promise<T>): Promise<T | undefined> {
try {
return await promise()
} catch (error) {
if (error instanceof CancellationError) {
return
}

throw error
}
}

public static ignoreSync<T>(func: () => T): T|undefined {
public static ignoreSync<T>(func: () => T): T | undefined {
try {
return func()
} catch (error) {
if (error instanceof CancellationError) {
return
}

throw error
}
}
}
}
Loading

0 comments on commit 327e853

Please sign in to comment.