The frontend component of PuffPastry, meant to interface with the backend, which in turn interfaces with the smart contract.
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

useLoading.ts 1.4 KiB

il y a 8 mois
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import { ref, reactive } from "vue";
  2. interface LoadingState {
  3. [key: string]: boolean;
  4. }
  5. const loading = ref<LoadingState>({});
  6. export const useLoading = () => {
  7. const register = (identifier: string, state = false) => {
  8. loading.value[identifier] = state;
  9. };
  10. const unregister = (identifier: string) => {
  11. delete loading.value[identifier];
  12. };
  13. const isLoading = (identifier: string): boolean => {
  14. if (!(identifier in loading.value)) {
  15. console.warn(`Loading state for "${identifier}" is not registered.`);
  16. return false;
  17. }
  18. return loading.value[identifier];
  19. };
  20. const show = (identifier: string) => {
  21. if (identifier in loading.value) {
  22. loading.value[identifier] = true;
  23. } else {
  24. console.warn(`Cannot show loading for unregistered identifier: "${identifier}"`);
  25. }
  26. };
  27. const hide = (identifier: string) => {
  28. if (identifier in loading.value) {
  29. loading.value[identifier] = false;
  30. } else {
  31. console.warn(`Cannot hide loading for unregistered identifier: "${identifier}"`);
  32. }
  33. };
  34. const isAnyLoading = (): boolean => {
  35. return Object.values(loading.value).some(state => state);
  36. };
  37. return reactive({
  38. register,
  39. unregister,
  40. isLoading,
  41. show,
  42. hide,
  43. isAnyLoading,
  44. loadingState: loading
  45. });
  46. };