@dfinity/gix-components
Version:
A UI kit developed by the GIX team
37 lines (36 loc) • 1.34 kB
JavaScript
import { nonNullish } from "@dfinity/utils";
import { derived, writable } from "svelte/store";
/**
* Store that reflects the app busy state.
* Is used to show the busy-screen that locks the UI.
*/
const initBusyStore = () => {
const DEFAULT_STATE = [];
const { subscribe, update, set } = writable(DEFAULT_STATE);
return {
subscribe,
/**
* Show the busy-screen if not visible
*/
startBusy({ initiator: newInitiator, text }) {
update((state) => [
...state.filter(({ initiator }) => newInitiator !== initiator),
{ initiator: newInitiator, text },
]);
},
/**
* Hide the busy-screen if no other initiators are done
*/
stopBusy(initiatorToRemove) {
update((state) => state.filter(({ initiator }) => initiator !== initiatorToRemove));
},
resetForTesting() {
set(DEFAULT_STATE);
},
};
};
export const busyStore = initBusyStore();
export const { startBusy, stopBusy } = busyStore;
export const busy = derived(busyStore, ($busyStore) => $busyStore.length > 0);
// Returns the newest message that was added to the store
export const busyMessage = derived(busyStore, ($busyStore) => $busyStore.reverse().find(({ text }) => nonNullish(text))?.text);