Adding a reconnect feature in case first Pusher request fails
Now, if the first pusher request fails, a waiting message will be displayed and the application will reconnect when the pusher comes back alive or the network connection is established again.
This commit is contained in:
parent
9eb4206fe0
commit
fcf0888864
17 changed files with 981 additions and 32 deletions
|
@ -1,15 +1,24 @@
|
|||
import { writable } from "svelte/store";
|
||||
import {derived, writable} from "svelte/store";
|
||||
|
||||
interface ErrorMessage {
|
||||
id: string|undefined;
|
||||
closable: boolean; // Whether it can be closed by a user action or not
|
||||
message: string | number | boolean | undefined;
|
||||
}
|
||||
|
||||
/**
|
||||
* A store that contains a list of error messages to be displayed.
|
||||
*/
|
||||
function createErrorStore() {
|
||||
const { subscribe, set, update } = writable<string[]>([]);
|
||||
const { subscribe, set, update } = writable<ErrorMessage[]>([]);
|
||||
|
||||
return {
|
||||
subscribe,
|
||||
addErrorMessage: (e: string | Error): void => {
|
||||
update((messages: string[]) => {
|
||||
addErrorMessage: (e: string | Error, options?: {
|
||||
closable?: boolean,
|
||||
id?: string
|
||||
}): void => {
|
||||
update((messages: ErrorMessage[]) => {
|
||||
let message: string;
|
||||
if (e instanceof Error) {
|
||||
message = e.message;
|
||||
|
@ -17,17 +26,36 @@ function createErrorStore() {
|
|||
message = e;
|
||||
}
|
||||
|
||||
if (!messages.includes(message)) {
|
||||
messages.push(message);
|
||||
if (!messages.find(errorMessage => errorMessage.message === message)) {
|
||||
messages.push({
|
||||
message,
|
||||
closable: options?.closable ?? true,
|
||||
id: options?.id
|
||||
});
|
||||
}
|
||||
|
||||
return messages;
|
||||
});
|
||||
},
|
||||
clearMessages: (): void => {
|
||||
set([]);
|
||||
clearMessageById: (id: string): void => {
|
||||
update((messages: ErrorMessage[]) => {
|
||||
messages = messages.filter(message => message.id !== id);
|
||||
return messages;
|
||||
});
|
||||
},
|
||||
clearClosableMessages: (): void => {
|
||||
update((messages: ErrorMessage[]) => {
|
||||
messages = messages.filter(message => message.closable);
|
||||
return messages;
|
||||
});
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
export const errorStore = createErrorStore();
|
||||
|
||||
|
||||
export const hasClosableMessagesInErrorStore = derived(errorStore, ($errorStore) => {
|
||||
const closableMessage = $errorStore.find(errorMessage => errorMessage.closable);
|
||||
return !!closableMessage;
|
||||
});
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue