mirror of
https://github.com/basketikun/infinite-canvas.git
synced 2026-08-01 06:01:13 +08:00
472bf8b732
Co-Authored-By: Codex <noreply@openai.com>
35 lines
868 B
TypeScript
35 lines
868 B
TypeScript
import localforage from "localforage";
|
|
import type { StateStorage } from "zustand/middleware";
|
|
|
|
localforage.config({
|
|
name: "infinite-canvas",
|
|
storeName: "app_state",
|
|
});
|
|
|
|
export const localForageStorage: StateStorage = {
|
|
getItem: async (name) => {
|
|
if (typeof window === "undefined") return null;
|
|
try {
|
|
return (await localforage.getItem<string>(name)) || null;
|
|
} catch {
|
|
return window.localStorage.getItem(name);
|
|
}
|
|
},
|
|
setItem: async (name, value) => {
|
|
if (typeof window === "undefined") return;
|
|
try {
|
|
await localforage.setItem(name, value);
|
|
} catch {
|
|
window.localStorage.setItem(name, value);
|
|
}
|
|
},
|
|
removeItem: async (name) => {
|
|
if (typeof window === "undefined") return;
|
|
try {
|
|
await localforage.removeItem(name);
|
|
} catch {
|
|
window.localStorage.removeItem(name);
|
|
}
|
|
},
|
|
};
|