useLibrary.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import { onBeforeUnmount, onMounted, ref } from 'vue';
  2. import LibraryApi from 'src/api/LibraryApi';
  3. import { SystemMessage } from 'src/utils/Message';
  4. import { until } from '@vueuse/core';
  5. export const useLibrary = () => {
  6. const movies = ref([]);
  7. const tvs = ref([]);
  8. const refreshCacheLoading = ref(false);
  9. const libraryRefreshStatus = ref(null);
  10. let getRefreshStatusTimer = null;
  11. const getLibraryRefreshStatus = async () => {
  12. const [res] = await LibraryApi.getRefreshStatus();
  13. libraryRefreshStatus.value = res.status;
  14. };
  15. const getLibraryList = async () => {
  16. const [res, err] = await LibraryApi.getList();
  17. if (err !== null) {
  18. SystemMessage.error(err.message);
  19. } else {
  20. movies.value = res.movie_infos;
  21. tvs.value = res.season_infos;
  22. }
  23. };
  24. const refreshLibrary = async () => {
  25. refreshCacheLoading.value = true;
  26. const [, err] = await LibraryApi.refreshLibrary();
  27. if (err !== null) {
  28. SystemMessage.error(err.message);
  29. } else {
  30. libraryRefreshStatus.value = null;
  31. getRefreshStatusTimer = setInterval(() => {
  32. getLibraryRefreshStatus();
  33. }, 1000);
  34. await until(libraryRefreshStatus).toBe('stopped');
  35. clearInterval(getRefreshStatusTimer);
  36. getRefreshStatusTimer = null;
  37. getLibraryList();
  38. SystemMessage.success('更新成功');
  39. }
  40. refreshCacheLoading.value = false;
  41. };
  42. onMounted(() => {
  43. getLibraryList();
  44. getLibraryRefreshStatus();
  45. });
  46. onBeforeUnmount(() => {
  47. clearInterval(getRefreshStatusTimer);
  48. });
  49. return {
  50. movies,
  51. tvs,
  52. refreshLibrary,
  53. refreshCacheLoading,
  54. };
  55. };