Skip to content

Commit

Permalink
make PromiseData partial
Browse files Browse the repository at this point in the history
  • Loading branch information
ajmnz committed Oct 4, 2023
1 parent b80b637 commit b9cb2d6
Showing 1 changed file with 38 additions and 17 deletions.
55 changes: 38 additions & 17 deletions src/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,28 +127,49 @@ class Observer {
promise: PromiseT<ToastData>,
data?: PromiseData<ToastData>,
) => {
const id = this.create({
...data,
promise,
type: 'loading',
message: data.loading,
});
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) => {
const message =
typeof data.success === 'function'
? data.success(promiseData)
: data.success;
this.create({ id, type: 'success', message });
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) => {
const message =
typeof data.error === 'function'
? data.error(error)
: data.error;
this.create({ id, type: 'error', message });
if (data.error !== undefined) {
shouldDismiss = false;
const message = typeof data.error === 'function' ? data.error(error) : data.error;
this.create({ id, type: 'error', message });
}
})
.finally(data.finally);
.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

0 comments on commit b9cb2d6

Please sign in to comment.