mirror of
https://github.com/p-stream/p-stream.git
synced 2026-05-12 19:20:57 +00:00
update time to apply bookmark, watching, and history changes
This commit is contained in:
parent
9af6e36d94
commit
93b5971f1f
3 changed files with 134 additions and 16 deletions
|
|
@ -59,6 +59,7 @@ export function BookmarkSyncer() {
|
||||||
clearUpdateQueue();
|
clearUpdateQueue();
|
||||||
}, [clearUpdateQueue]);
|
}, [clearUpdateQueue]);
|
||||||
|
|
||||||
|
// Regular interval sync
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const interval = setInterval(() => {
|
const interval = setInterval(() => {
|
||||||
(async () => {
|
(async () => {
|
||||||
|
|
@ -79,5 +80,58 @@ export function BookmarkSyncer() {
|
||||||
};
|
};
|
||||||
}, [removeUpdateItem, url]);
|
}, [removeUpdateItem, url]);
|
||||||
|
|
||||||
|
// Immediate sync when items are added or removed
|
||||||
|
useEffect(() => {
|
||||||
|
let syncTimeout: NodeJS.Timeout | null = null;
|
||||||
|
|
||||||
|
const syncImmediately = async () => {
|
||||||
|
if (!url) return;
|
||||||
|
const state = useBookmarkStore.getState();
|
||||||
|
const user = useAuthStore.getState();
|
||||||
|
// Only sync if there are items in the queue
|
||||||
|
if (state.updateQueue.length > 0) {
|
||||||
|
await syncBookmarks(
|
||||||
|
state.updateQueue,
|
||||||
|
removeUpdateItem,
|
||||||
|
url,
|
||||||
|
user.account,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const debouncedSync = () => {
|
||||||
|
if (syncTimeout) {
|
||||||
|
clearTimeout(syncTimeout);
|
||||||
|
}
|
||||||
|
syncTimeout = setTimeout(syncImmediately, 100);
|
||||||
|
};
|
||||||
|
|
||||||
|
// Override the addBookmark function to trigger immediate sync
|
||||||
|
const originalAddBookmark = useBookmarkStore.getState().addBookmark;
|
||||||
|
useBookmarkStore.setState({
|
||||||
|
addBookmark: (...args) => {
|
||||||
|
originalAddBookmark(...args);
|
||||||
|
// Trigger debounced sync after adding bookmark
|
||||||
|
debouncedSync();
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
// Override removeBookmark to trigger immediate sync
|
||||||
|
const originalRemoveBookmark = useBookmarkStore.getState().removeBookmark;
|
||||||
|
useBookmarkStore.setState({
|
||||||
|
removeBookmark: (...args) => {
|
||||||
|
originalRemoveBookmark(...args);
|
||||||
|
// Trigger debounced sync after removing bookmark
|
||||||
|
debouncedSync();
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
if (syncTimeout) {
|
||||||
|
clearTimeout(syncTimeout);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}, [removeUpdateItem, url]);
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -59,6 +59,7 @@ export function ProgressSyncer() {
|
||||||
clearUpdateQueue();
|
clearUpdateQueue();
|
||||||
}, [clearUpdateQueue]);
|
}, [clearUpdateQueue]);
|
||||||
|
|
||||||
|
// Regular interval sync
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const interval = setInterval(() => {
|
const interval = setInterval(() => {
|
||||||
(async () => {
|
(async () => {
|
||||||
|
|
@ -79,5 +80,58 @@ export function ProgressSyncer() {
|
||||||
};
|
};
|
||||||
}, [removeUpdateItem, url]);
|
}, [removeUpdateItem, url]);
|
||||||
|
|
||||||
|
// Immediate sync when items are added or removed
|
||||||
|
useEffect(() => {
|
||||||
|
let syncTimeout: NodeJS.Timeout | null = null;
|
||||||
|
|
||||||
|
const syncImmediately = async () => {
|
||||||
|
if (!url) return;
|
||||||
|
const state = useProgressStore.getState();
|
||||||
|
const user = useAuthStore.getState();
|
||||||
|
// Only sync if there are items in the queue
|
||||||
|
if (state.updateQueue.length > 0) {
|
||||||
|
await syncProgress(
|
||||||
|
state.updateQueue,
|
||||||
|
removeUpdateItem,
|
||||||
|
url,
|
||||||
|
user.account,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const debouncedSync = () => {
|
||||||
|
if (syncTimeout) {
|
||||||
|
clearTimeout(syncTimeout);
|
||||||
|
}
|
||||||
|
syncTimeout = setTimeout(syncImmediately, 100);
|
||||||
|
};
|
||||||
|
|
||||||
|
// Override the updateItem function to trigger immediate sync
|
||||||
|
const originalUpdateItem = useProgressStore.getState().updateItem;
|
||||||
|
useProgressStore.setState({
|
||||||
|
updateItem: (...args) => {
|
||||||
|
originalUpdateItem(...args);
|
||||||
|
// Trigger debounced sync after updating item
|
||||||
|
debouncedSync();
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
// Override removeItem to trigger immediate sync
|
||||||
|
const originalRemoveItem = useProgressStore.getState().removeItem;
|
||||||
|
useProgressStore.setState({
|
||||||
|
removeItem: (...args) => {
|
||||||
|
originalRemoveItem(...args);
|
||||||
|
// Trigger debounced sync after removing item
|
||||||
|
debouncedSync();
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
if (syncTimeout) {
|
||||||
|
clearTimeout(syncTimeout);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}, [removeUpdateItem, url]);
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -66,18 +66,16 @@ export function WatchHistorySyncer() {
|
||||||
clearUpdateQueue();
|
clearUpdateQueue();
|
||||||
}, [clearUpdateQueue]);
|
}, [clearUpdateQueue]);
|
||||||
|
|
||||||
// Immediate sync when items are added to queue
|
// Immediate sync when items are added or removed
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
let lastQueueLength = 0;
|
let syncTimeout: NodeJS.Timeout | null = null;
|
||||||
|
|
||||||
const checkAndSync = async () => {
|
const syncImmediately = async () => {
|
||||||
const currentQueueLength =
|
if (!url) return;
|
||||||
useWatchHistoryStore.getState().updateQueue.length;
|
const state = useWatchHistoryStore.getState();
|
||||||
// Only sync immediately if queue grew (items were added)
|
const user = useAuthStore.getState();
|
||||||
if (currentQueueLength > lastQueueLength && currentQueueLength > 0) {
|
// Only sync if there are items in the queue
|
||||||
if (!url) return;
|
if (state.updateQueue.length > 0) {
|
||||||
const state = useWatchHistoryStore.getState();
|
|
||||||
const user = useAuthStore.getState();
|
|
||||||
await syncWatchHistory(
|
await syncWatchHistory(
|
||||||
state.updateQueue,
|
state.updateQueue,
|
||||||
removeUpdateItem,
|
removeUpdateItem,
|
||||||
|
|
@ -85,7 +83,13 @@ export function WatchHistorySyncer() {
|
||||||
user.account,
|
user.account,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
lastQueueLength = currentQueueLength;
|
};
|
||||||
|
|
||||||
|
const debouncedSync = () => {
|
||||||
|
if (syncTimeout) {
|
||||||
|
clearTimeout(syncTimeout);
|
||||||
|
}
|
||||||
|
syncTimeout = setTimeout(syncImmediately, 100);
|
||||||
};
|
};
|
||||||
|
|
||||||
// Override the addItem function to trigger immediate sync
|
// Override the addItem function to trigger immediate sync
|
||||||
|
|
@ -93,20 +97,26 @@ export function WatchHistorySyncer() {
|
||||||
useWatchHistoryStore.setState({
|
useWatchHistoryStore.setState({
|
||||||
addItem: (...args) => {
|
addItem: (...args) => {
|
||||||
originalAddItem(...args);
|
originalAddItem(...args);
|
||||||
// Trigger sync after adding item
|
// Trigger debounced sync after adding item
|
||||||
setTimeout(checkAndSync, 100);
|
debouncedSync();
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
// Also override removeItem
|
// Override removeItem to trigger immediate sync
|
||||||
const originalRemoveItem = useWatchHistoryStore.getState().removeItem;
|
const originalRemoveItem = useWatchHistoryStore.getState().removeItem;
|
||||||
useWatchHistoryStore.setState({
|
useWatchHistoryStore.setState({
|
||||||
removeItem: (...args) => {
|
removeItem: (...args) => {
|
||||||
originalRemoveItem(...args);
|
originalRemoveItem(...args);
|
||||||
// Trigger sync after removing item
|
// Trigger debounced sync after removing item
|
||||||
setTimeout(checkAndSync, 100);
|
debouncedSync();
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
if (syncTimeout) {
|
||||||
|
clearTimeout(syncTimeout);
|
||||||
|
}
|
||||||
|
};
|
||||||
}, [removeUpdateItem, url]);
|
}, [removeUpdateItem, url]);
|
||||||
|
|
||||||
// Regular interval sync
|
// Regular interval sync
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue