move player timestamp to separate store

- for performance related issue
This commit is contained in:
jeffvli
2025-11-16 21:49:31 -08:00
parent 243d29f7a7
commit 4451389b6a
3 changed files with 89 additions and 62 deletions
+44
View File
@@ -0,0 +1,44 @@
import { subscribeWithSelector } from 'zustand/middleware';
import { createWithEqualityFn } from 'zustand/traditional';
interface TimestampState {
setTimestamp: (timestamp: number) => void;
timestamp: number;
}
export const useTimestampStoreBase = createWithEqualityFn<TimestampState>()(
subscribeWithSelector((set) => ({
setTimestamp: (timestamp: number) => {
set({ timestamp });
},
timestamp: 0,
})),
);
export const subscribePlayerProgress = (
onChange: (properties: { timestamp: number }, prev: { timestamp: number }) => void,
) => {
return useTimestampStoreBase.subscribe(
(state) => state.timestamp,
(timestamp, prevTimestamp) => {
onChange({ timestamp }, { timestamp: prevTimestamp });
},
{
equalityFn: (a, b) => {
return a === b;
},
},
);
};
export const usePlayerProgress = () => {
return useTimestampStoreBase((state) => state.timestamp);
};
export const usePlayerTimestamp = () => {
return useTimestampStoreBase((state) => state.timestamp);
};
export const setTimestamp = (timestamp: number) => {
useTimestampStoreBase.getState().setTimestamp(timestamp);
};