The frontend component of PuffPastry, meant to interface with the backend, which in turn interfaces with the smart contract.
Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 

56 linhas
1.4 KiB

  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. };