Skip to content

Commit

Permalink
fix: fixed the promise bug
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaoluoboding committed Nov 7, 2023
1 parent 9608865 commit c297881
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 47 deletions.
41 changes: 0 additions & 41 deletions packages/Toast.vue
Original file line number Diff line number Diff line change
Expand Up @@ -290,47 +290,6 @@ watchEffect(() => {
offset.value = heightIndex.value * GAP + toastsHeightBefore.value
})
watchEffect(() => {
if (props.toast && isPromise(props.toast)) {
const toastInstance = props.toast
promiseStatus.value = 'loading'
const promiseHandler = (promise: Promise<any>) => {
promise
.then((data) => {
if (
toastInstance.success &&
typeof toastInstance.success === 'function'
) {
promiseResult.value = toastInstance.success(data)
}
promiseStatus.value = 'success'
})
.catch((error) => {
if (
toastInstance.error &&
typeof toastInstance.error === 'function'
) {
promiseResult.value = toastInstance.error(error)
}
promiseStatus.value = 'error'
})
}
// console.group('Toast begin')
// console.log(isPromise(props.toast))
// console.log(props.toast)
// console.log(props.toast.promise instanceof Promise)
// console.log(typeof props.toast.promise === 'function')
// console.groupEnd()
if (props.toast.promise instanceof Promise) {
promiseHandler(props.toast.promise as any)
} else if (typeof props.toast.promise === 'function') {
promiseHandler(props.toast?.promise?.())
}
}
})
function handleCloseToast() {
if (!disabled.value || dismissible.value) {
deleteToast()
Expand Down
14 changes: 12 additions & 2 deletions packages/Toaster.vue
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ import {
watch,
watchEffect,
useAttrs,
CSSProperties
CSSProperties,
nextTick
} from 'vue'
import {
HeightT,
Expand Down Expand Up @@ -183,7 +184,16 @@ onMounted(() => {
return
}
toasts.value = [toast, ...toasts.value]
nextTick(() => {
const indexOfExistingToast = toasts.value.findIndex((t) => t.id === toast.id);
// Update the toast if it already exists
if (indexOfExistingToast !== -1) {
toasts.value.splice(indexOfExistingToast, 1, toast)
} else {
toasts.value = [toast, ...toasts.value]
}
})
})
onUnmounted(() => {
Expand Down
5 changes: 2 additions & 3 deletions packages/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ class Observer {

publish = (data: ToastT) => {
this.subscribers.forEach((subscriber) => subscriber(data))
this.toasts = [...this.toasts, data]
}

addToast = (data: ToastT) => {
Expand All @@ -51,12 +50,12 @@ class Observer {
typeof data?.id === 'number' || data.id?.length! > 0
? data.id!
: toastsCounter++
const alreadyExists = this.toasts.find((toast) => {
const indexOfExistingToast = this.toasts.findIndex((toast) => {
return toast.id === id
})
const dismissible = data.dismissible === undefined ? true : data.dismissible

if (alreadyExists) {
if (indexOfExistingToast !== -1) {
this.toasts = this.toasts.map((toast) => {
if (toast.id === id) {
this.publish({ ...toast, ...data, id, title: message })
Expand Down
2 changes: 1 addition & 1 deletion packages/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -516,12 +516,12 @@ html[dir='rtl'],
--size: 16px;
height: var(--size);
width: var(--size);
position: absolute;
inset: 0;
z-index: 10;
}

.sonner-loading-wrapper[data-visible='false'] {
display: none;
transform-origin: center;
animation: sonner-fade-out 0.2s ease forwards;
}
Expand Down

0 comments on commit c297881

Please sign in to comment.