feat: persist player timestamp (#2043)

* feat: persist player timestamp
This commit is contained in:
Tiago Simionato
2026-05-21 04:16:46 -03:00
committed by GitHub
parent 2befcb4e74
commit 8f40894926
3 changed files with 109 additions and 10 deletions
+30 -6
View File
@@ -1,4 +1,5 @@
import { subscribeWithSelector } from 'zustand/middleware';
import { del, get, set } from 'idb-keyval';
import { persist, subscribeWithSelector } from 'zustand/middleware';
import { createWithEqualityFn } from 'zustand/traditional';
interface TimestampState {
@@ -6,13 +7,36 @@ interface TimestampState {
timestamp: number;
}
const timestampStorage = {
getItem: async (name: string) => {
const value = await get(name);
if (value === undefined) {
return null;
}
return { state: { timestamp: value }, version: 1 } as const;
},
removeItem: async (name: string) => {
await del(name);
},
setItem: async (name: string, value: { state: { timestamp: number }; version?: number }) => {
await set(name, value.state.timestamp);
},
};
export const useTimestampStoreBase = createWithEqualityFn<TimestampState>()(
subscribeWithSelector((set) => ({
setTimestamp: (timestamp: number) => {
set({ timestamp });
persist(
subscribeWithSelector((set) => ({
setTimestamp: (timestamp: number) => {
set({ timestamp });
},
timestamp: 0,
})),
{
name: 'player-timestamp',
storage: timestampStorage,
version: 1,
},
timestamp: 0,
})),
),
);
export const subscribePlayerProgress = (