Skip to content

Commit

Permalink
feat: make promise return promise
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaoluoboding committed Nov 3, 2023
1 parent 51af3ec commit 9608865
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 18 deletions.
66 changes: 54 additions & 12 deletions packages/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,21 +92,15 @@ class Observer {
}

message = (message: string | Component, data?: ExternalToast) => {
const id = data?.id || toastsCounter++
this.publish({ ...data, id, title: message })
return id
return this.create({ ...data, message })
}

error = (message: string | Component, data?: ExternalToast) => {
const id = data?.id || toastsCounter++
this.publish({ ...data, id, type: 'error', title: message })
return id
return this.create({ ...data, type: 'error', message })
}

success = (message: string | Component, data?: ExternalToast) => {
const id = data?.id || toastsCounter++
this.publish({ ...data, id, type: 'success', title: message })
return id
return this.create({ ...data, type: 'success', message })
}

info = (message: string | Component, data?: ExternalToast) => {
Expand All @@ -121,9 +115,57 @@ class Observer {
return this.create({ ...data, type: 'loading', message })
}

promise = (promise: PromiseT, data?: PromiseData) => {
const id = data?.id || toastsCounter++
this.publish({ ...data, promise, id })
promise = <ToastData>(
promise: PromiseT<ToastData>,
data?: PromiseData<ToastData>
) => {
if (!data) {
// Nothing to show
return
}

let id: string | number | undefined = undefined
if (data.loading !== undefined) {
id = this.create({
...data,
promise,
type: 'loading',
message: data.loading
})
}

const p = promise instanceof Promise ? promise : promise()

let shouldDismiss = id !== undefined

p.then((promiseData) => {
if (data.success !== undefined) {
shouldDismiss = false
const message =
typeof data.success === 'function'
? data.success(promiseData)
: data.success
this.create({ id, type: 'success', message })
}
})
.catch((error) => {
if (data.error !== undefined) {
shouldDismiss = false
const message =
typeof data.error === 'function' ? data.error(error) : data.error
this.create({ id, type: 'error', message })
}
})
.finally(() => {
if (shouldDismiss) {
// Toast is still in load state (and will be indefinitely — dismiss it)
this.dismiss(id)
id = undefined
}

data.finally?.()
})

return id
}

Expand Down
11 changes: 5 additions & 6 deletions packages/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,11 @@ export type ToastTypes =

export type PromiseT<Data = any> = Promise<Data> | (() => Promise<Data>)

export type PromiseData = ExternalToast & {
loading: string | Component
// success: string | Component | ((data: any) => Component | string)
// error: string | Component | ((error: any) => Component | string)
success: (data: any) => Component | string
error: (error: any) => Component | string
export type PromiseData<ToastData = any> = ExternalToast & {
loading?: string | Component
success?: (data: ToastData) => string | Component
error?: (data: ToastData) => string | Component
finally?: () => void | Promise<void>
}

export interface ToastT {
Expand Down

0 comments on commit 9608865

Please sign in to comment.