Skip to content

Commit

Permalink
fix(shared): fix applyMiddleware can not catch error (#1952)
Browse files Browse the repository at this point in the history
  • Loading branch information
janryWang authored Aug 5, 2021
1 parent 9799180 commit 22f0379
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 15 deletions.
10 changes: 6 additions & 4 deletions packages/antd/src/__builtins__/loading.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ export const loading = async (
let loading = setTimeout(() => {
hide = message.loading(title)
}, 100)
const results = await processor()
hide?.()
clearTimeout(loading)
return results
try {
return await processor()
} finally {
hide?.()
clearTimeout(loading)
}
}
10 changes: 6 additions & 4 deletions packages/element/src/__builtins__/shared/loading.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ export const loading = async (
background: 'transparent',
})
}, 100)
const results = await processor()
loadingInstance?.close()
clearTimeout(loading)
return results
try {
return await processor()
} finally {
loadingInstance?.close()
clearTimeout(loading)
}
}
10 changes: 6 additions & 4 deletions packages/next/src/__builtins__/loading.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ export const loading = async (
let loading = setTimeout(() => {
Message.loading(title as any)
}, 100)
const results = await processor()
Message.hide()
clearTimeout(loading)
return results
try {
return await processor()
} finally {
Message.hide()
clearTimeout(loading)
}
}
12 changes: 12 additions & 0 deletions packages/shared/src/__tests__/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -724,3 +724,15 @@ test('applyMiddleware', async () => {
await sleep(16)
expect(resolved).toBeCalledTimes(0)
})

test('applyMiddleware with error', async () => {
try {
await applyMiddleware(0, [
() => {
throw 'this is error'
},
])
} catch (e) {
expect(e).toEqual('this is error')
}
})
6 changes: 3 additions & 3 deletions packages/shared/src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@ export interface IMiddleware<Payload = any, Result = any> {
}

export const applyMiddleware = (payload: any, fns: IMiddleware[] = []) => {
const compose = (payload: any, fns: IMiddleware[]) => {
const compose = (payload: any, fns: IMiddleware[]): Promise<any> => {
const prevPayload = payload
return Promise.resolve(
fns[0](payload, (payload) =>
compose(payload ?? prevPayload, fns.slice(1))
)
)
}
return new Promise((resolve) => {
return new Promise((resolve, reject) => {
compose(
payload,
fns.concat((payload) => {
resolve(payload)
})
)
).catch(reject)
})
}

0 comments on commit 22f0379

Please sign in to comment.