Skip to content

Commit

Permalink
chore(webext): ensure to use the latest cached videos data on channel…
Browse files Browse the repository at this point in the history
… videos check
  • Loading branch information
AXeL-dev committed Nov 5, 2022
1 parent 3cc0267 commit 689f12e
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ export * from './useDidMountEffect';
export * from './useTimeout';
export * from './useInterval';
export * from './useForwardedRef';
export * from './useStateRef';
11 changes: 11 additions & 0 deletions src/hooks/useStateRef.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { useRef, useEffect } from 'react';

export function useStateRef<T>(state: T) {
const ref = useRef(state);

useEffect(() => {
ref.current = state;
}, [state]);

return ref; // should be ref & not ref.current
}
5 changes: 3 additions & 2 deletions src/ui/components/webext/Background/ChannelChecker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { useGetChannelVideosQuery } from 'store/services/youtube';
import { getDateBefore } from 'helpers/utils';
import { useAppSelector } from 'store';
import { selectChannelVideos } from 'store/selectors/videos';
import { useInterval } from 'hooks';
import { useInterval, useStateRef } from 'hooks';
import { log } from 'helpers/logger';

export interface CheckEndData {
Expand All @@ -26,6 +26,7 @@ export default function ChannelChecker(props: ChannelCheckerProps) {
const { channel, onCheckEnd } = props;
const [ready, setReady] = useState(false);
const cachedVideos = useAppSelector(selectChannelVideos(channel));
const cachedVideosRef = useStateRef(cachedVideos);
const publishedAfter = getDateBefore(config.videosSeniority).toISOString();
const pollingInterval = config.checkInterval * 60000; // convert minutes to milliseconds
const { data, isFetching, refetch } = useGetChannelVideosQuery(
Expand Down Expand Up @@ -57,7 +58,7 @@ export default function ChannelChecker(props: ChannelCheckerProps) {
const total = data?.total || 0;
log('Fetch ended:', total, data);
if (total > 0) {
const checkedVideosIds = cachedVideos
const checkedVideosIds = cachedVideosRef.current
.filter(
({ flags }) =>
flags.seen ||
Expand Down

0 comments on commit 689f12e

Please sign in to comment.