Skip to content

Commit 666b14b

Browse files
committed
fix(loading): rework util to make it composable
1 parent c552b6f commit 666b14b

File tree

5 files changed

+16
-11
lines changed

5 files changed

+16
-11
lines changed

src/stores/data/history.store.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ export const useHistoryStore = defineStore(HistoryStoreConstants.Store, () => {
110110

111111
Logger.debug('Fetching History', { page, limit, start, end });
112112
loading.value = true;
113-
const timeout = debounceLoading(history, loadingPlaceholder, !page);
113+
const { clearLoading } = debounceLoading(history, loadingPlaceholder, !page);
114114
const query: TraktHistoryGetQuery = {
115115
pagination: { page, limit },
116116
start_at: start?.toISOString(),
@@ -129,7 +129,7 @@ export const useHistoryStore = defineStore(HistoryStoreConstants.Store, () => {
129129
history.value = history.value.filter(h => h.id >= 0);
130130
throw e;
131131
} finally {
132-
clearTimeout(timeout);
132+
clearLoading();
133133
loading.value = false;
134134
}
135135
};

src/stores/data/list.store.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ export const useListStore = defineStore(ListStoreConstants.Store, () => {
346346
if (!parallel) loading.value = true;
347347
typeLoading[list.type] = true;
348348
listDictionaryLoading[list.id.toString()] = true;
349-
const timeout = debounceLoading(listItems, loadingPlaceholder, !page);
349+
const { clearLoading } = debounceLoading(listItems, loadingPlaceholder, !page, 1000);
350350

351351
try {
352352
const response = await fetchItems(list, { pagination: { page, limit } });
@@ -364,7 +364,7 @@ export const useListStore = defineStore(ListStoreConstants.Store, () => {
364364
listItems.value = listItems.value.filter(l => l.type !== ListScrollItemType.loading);
365365
throw e;
366366
} finally {
367-
clearTimeout(timeout);
367+
clearLoading();
368368
if (!parallel) loading.value = false;
369369
typeLoading[list.type] = false;
370370
listDictionaryLoading[list.id.toString()] = false;

src/stores/data/progress.store.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ export const useProgressStore = defineStore('data.progress', () => {
114114

115115
Logger.debug('Fetching progress');
116116
loading.value = true;
117-
const timeout = debounceLoading(progress, loadingPlaceholder, true);
117+
const { clearLoading } = debounceLoading(progress, loadingPlaceholder, true);
118118
try {
119119
progress.value = await fetchProgressData();
120120
evicted.progress = false;
@@ -132,7 +132,7 @@ export const useProgressStore = defineStore('data.progress', () => {
132132
NotificationService.error('Failed to fetch progress', error);
133133
throw error;
134134
} finally {
135-
clearTimeout(timeout);
135+
clearLoading();
136136
loading.value = false;
137137
}
138138
};

src/stores/data/search.store.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ export const useSearchStore = defineStore(SearchStoreConstants.Store, () => {
134134
Logger.debug('Fetching search results', { types: types.value, query: query.value, search: search.value });
135135

136136
loading.value = true;
137-
const timeout = debounceLoading(searchResults, loadingPlaceholder, !page);
137+
const { clearLoading } = debounceLoading(searchResults, loadingPlaceholder, !page);
138138
const request: TraktSearch = {
139139
type: types.value,
140140
escape: !query.value,
@@ -159,7 +159,7 @@ export const useSearchStore = defineStore(SearchStoreConstants.Store, () => {
159159
searchErrors[JSON.stringify(request)] = ErrorCount.fromDictionary(searchErrors, JSON.stringify(request), e);
160160
throw e;
161161
} finally {
162-
clearTimeout(timeout);
162+
clearLoading();
163163
loading.value = false;
164164
}
165165
};

src/utils/store.utils.ts

+8-3
Original file line numberDiff line numberDiff line change
@@ -127,10 +127,15 @@ export const useDebouncedSearch = (search: Ref<string>, delay = 350, disabled?:
127127
return debouncedSearch;
128128
};
129129

130-
export const debounceLoading = <T>(data: Ref<T[]>, placeholder: Ref<T[]>, clear?: boolean) =>
131-
setTimeout(() => {
130+
export const debounceLoading = <T>(data: Ref<T[]>, placeholder: Ref<T[]>, clear?: boolean, time = 100) => {
131+
const timeout = setTimeout(() => {
132132
data.value = clear ? placeholder.value : [...data.value, ...placeholder.value];
133-
}, 100);
133+
}, time);
134+
return {
135+
timeout,
136+
clearLoading: () => clearTimeout(timeout),
137+
};
138+
};
134139

135140
export const useWaitReady = () => {
136141
let resolve: (value: boolean) => void;

0 commit comments

Comments
 (0)