2023-03-21 19:42:45 +00:00
|
|
|
import ship from "./ship";
|
2023-04-15 21:11:17 +00:00
|
|
|
import { browser } from "$app/environment";
|
2023-03-21 19:42:45 +00:00
|
|
|
|
|
|
|
export type Api = ReturnType<typeof ship.createStore>;
|
|
|
|
|
|
|
|
export const createApi = () => {
|
2023-04-15 21:11:17 +00:00
|
|
|
const state = browser
|
|
|
|
? JSON.parse(localStorage.getItem("ship") || "null")
|
|
|
|
: undefined;
|
|
|
|
|
2023-04-22 14:37:49 +00:00
|
|
|
const api = ship.createStore({
|
|
|
|
preloadedState: state,
|
|
|
|
});
|
|
|
|
|
|
|
|
api.dispatch.restore(state);
|
2023-04-15 21:11:17 +00:00
|
|
|
|
|
|
|
if (browser) {
|
|
|
|
api.subscribe(() => {
|
2023-04-22 14:37:49 +00:00
|
|
|
console.log("saving...", api.getState());
|
2023-04-15 21:11:17 +00:00
|
|
|
localStorage.setItem("ship", JSON.stringify(api.getState()));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return api;
|
2023-03-21 19:42:45 +00:00
|
|
|
};
|