Skip to content

Commit

Permalink
feat(createSubmit): 引入 id 唯一标识
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed Jan 18, 2024
1 parent ebb9df2 commit 93a37c0
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 21 deletions.
32 changes: 26 additions & 6 deletions src/utils/createSubmit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,27 @@ describe('createSubmit', () => {
await _.success('成功', 20)
})

expect(start).toBeCalled().toBeCalledTimes(1).toBeCalledWith('开始')
expect(fail).toBeCalled().toBeCalledTimes(1).toBeCalledWith('失败', 10)
expect(success).toBeCalled().toBeCalledTimes(1).toBeCalledWith('成功', 20)
expect(complete).toBeCalled().toBeCalledTimes(1)
expect(start).toBeCalled().toBeCalledTimes(1).toBeCalledWith('开始', 1)
expect(fail).toBeCalled().toBeCalledTimes(1).toBeCalledWith('失败', 10, 1)
expect(success)
.toBeCalled()
.toBeCalledTimes(1)
.toBeCalledWith('成功', 20, 1)
expect(complete).toBeCalled().toBeCalledWith(1).toBeCalledTimes(1)

await submit(async _ => {
await _.start('开始')
await _.fail('失败', 10)
await _.success('成功', 20)
})

expect(start).toBeCalled().toBeCalledTimes(2).toBeCalledWith('开始', 2)
expect(fail).toBeCalled().toBeCalledTimes(2).toBeCalledWith('失败', 10, 2)
expect(success)
.toBeCalled()
.toBeCalledTimes(2)
.toBeCalledWith('成功', 20, 2)
expect(complete).toBeCalled().toBeCalledWith(2).toBeCalledTimes(2)
})

test('异常回调', async () => {
Expand Down Expand Up @@ -59,7 +76,10 @@ describe('createSubmit', () => {
await submit.fail('失败', 10)
await submit.success('成功', 20)

expect(fail).toBeCalled().toBeCalledTimes(1).toBeCalledWith('失败', 10)
expect(success).toBeCalled().toBeCalledTimes(1).toBeCalledWith('成功', 20)
expect(fail).toBeCalled().toBeCalledTimes(1).toBeCalledWith('失败', 10, 0)
expect(success)
.toBeCalled()
.toBeCalledTimes(1)
.toBeCalledWith('成功', 20, 0)
})
})
34 changes: 19 additions & 15 deletions src/utils/createSubmit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,33 @@ export interface CreateSubmitOptions<T = string> {
*
* @param message 提示信息
*/
start(message?: T): AsyncOrSync<any>
start(message: T | undefined, id: number): AsyncOrSync<any>

/**
* 失败回调。
*
* @param message 提示信息
* @param duration 持续时间(毫秒)
*/
fail(message: T, duration: number): AsyncOrSync<any>
fail(message: T, duration: number, id: number): AsyncOrSync<any>

/**
* 成功回调。
*
* @param message 提示信息
* @param duration 持续时间(毫秒)
*/
success(message: T, duration: number): AsyncOrSync<any>
success(message: T, duration: number, id: number): AsyncOrSync<any>

/**
* 完成回调。
*/
complete(): AsyncOrSync<any>
complete(id: number): AsyncOrSync<any>

/**
* 异常回调。
*/
throw?(error: unknown): AsyncOrSync<any>
throw?(error: unknown, id: number): AsyncOrSync<any>
}

export interface SubmitActionPayload<T = string> {
Expand Down Expand Up @@ -67,6 +67,8 @@ export type CreateSubmitResult<T = string> = (<TResult>(
) => Promise<TResult>) &
Pick<SubmitActionPayload<T>, 'fail' | 'success'>

let idCounter = 1

/**
* 创建提交类行为。
*
Expand All @@ -91,34 +93,36 @@ export function createSubmit<T>(
export function createSubmit<T>(
options: CreateSubmitOptions<T>,
): CreateSubmitResult<T> {
const payload: SubmitActionPayload<T> = {
const getPayload = (id: number): SubmitActionPayload<T> => ({
start(message) {
return run(() => options.start(message))
return run(() => options.start(message, id))
},
fail(message, duration = 1500) {
return run(() => options.fail(message, duration)).then(() =>
return run(() => options.fail(message, duration, id)).then(() =>
wait(duration),
)
},
success(message, duration = 1500) {
return run(() => options.success(message, duration)).then(() =>
return run(() => options.success(message, duration, id)).then(() =>
wait(duration),
)
},
}
})
const res: CreateSubmitResult<T> = action => {
return action(payload)
const id = idCounter++
return action(getPayload(id))
.then(res => {
return run(() => options.complete()).then(() => res)
return run(() => options.complete(id)).then(() => res)
})
.catch((error: unknown) => {
if (options.throw) {
options.throw(error)
options.throw(error, id)
}
return Promise.reject(error)
})
}
res.success = payload.success
res.fail = payload.fail
const globalAction = getPayload(0)
res.success = globalAction.success
res.fail = globalAction.fail
return res
}

0 comments on commit 93a37c0

Please sign in to comment.