Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrating from pinia-plugin-persistedstate-2 #274

Closed
4 tasks done
Stanzilla opened this issue Jan 21, 2024 · 6 comments
Closed
4 tasks done

Migrating from pinia-plugin-persistedstate-2 #274

Stanzilla opened this issue Jan 21, 2024 · 6 comments
Labels
🔍️ pending triage This issue needs to be looked into

Comments

@Stanzilla
Copy link

Stanzilla commented Jan 21, 2024

Describe the bug

I'm currently migrating my app from pinia-plugin-persistedstate-2 to this version since it is no longer maintained. I use it inside of an Electron app and have ipc functions mapped to store methods:

async function getStore(key) {
  return await ipcRenderer.invoke("getStore", key);
}

async function setStore(key, value) {
  return await ipcRenderer.invoke("setStore", key, value);
}

async function deleteStore(key) {
  return await ipcRenderer.invoke("deleteStore", key);
}

(async () => {
  const app = createApp(App);
  const pinia = createPinia();

  pinia.use(
    createPersistedState({
      storage: {
        getItem: async (key) => {
          return getStore(key);
        },
        setItem: async (key, value) => {
          return setStore(key, value);
        },
        removeItem: async (key) => {
          return deleteStore(key);
        },
      },
    }),
  );

and it complains that

  Type 'Promise<any>' is not assignable to type 'string'.ts(2322)

Is that what is mentioned on https://prazdevs.github.io/pinia-plugin-persistedstate/guide/limitations.html?

Reproduction

import { ipcRenderer } from "electron"; import FloatingVue from "floating-vue"; import { createPinia } from "pinia"; import { createPersistedState } from "pinia-plugin-persistedstate"; import { createApp } from "vue"; import App from "./App.vue"; import { i18n } from "./libs/i18n"; import "virtual:uno.css";  async function getStore(key) {   return await ipcRenderer.invoke("getStore", key); }  async function setStore(key, value) {   return await ipcRenderer.invoke("setStore", key, value); }  async function deleteStore(key) {   return await ipcRenderer.invoke("deleteStore", key); }  (async () => {   const app = createApp(App);   const pinia = createPinia();    pinia.use(     createPersistedState({       storage: {         getItem: async (key) => {           return getStore(key);         },         setItem: async (key, value) => {           return setStore(key, value);         },         removeItem: async (key) => {           return deleteStore(key);         },       },     }),   );    app.use(pinia);   app.use(i18n);    app.use(FloatingVue, {     themes: {       "info-tooltip": {         $extend: "tooltip",       },     },   });    app.mount("#app").$nextTick(() => {     postMessage({ payload: "removeLoading" }, "*");   }); })();

System Info

"pinia-plugin-persistedstate": "^3.2.1",#    "pinia": "^2.1.7",

Used Package Manager

pnpm

Validations

@Stanzilla Stanzilla added the 🔍️ pending triage This issue needs to be looked into label Jan 21, 2024
@prazdevs
Copy link
Owner

The plugin only supports synchronous storages to match pinia's mutation system. This is indeed one of the limitation.

@Stanzilla
Copy link
Author

The plugin only supports synchronous storages to match pinia's mutation system. This is indeed one of the limitation.

Can you suggest a migration path for users coming from the abandoned -2 version then?

@prazdevs
Copy link
Owner

Well I didnt make the -2.

This plugin has only ever officially supported synchronous storages as mentioned in several issues (#17 #111 #214).
You can use pinia actions for this, or the hooks, or still use non-awaited functions in the storage, but may experience side effects.

@Stanzilla
Copy link
Author

Well I didnt make the -2.

This plugin has only ever officially supported synchronous storages as mentioned in several issues (#17 #111 #214). You can use pinia actions for this, or the hooks, or still use non-awaited functions in the storage, but may experience side effects.

Oh yeah, I'm aware, I was just wondering if you have a suggestion for a replacement path, I'll look at actions but that looked like I have to have those defined for every store instead of globally, thanks!

@Stanzilla
Copy link
Author

Well I didnt make the -2.
This plugin has only ever officially supported synchronous storages as mentioned in several issues (#17 #111 #214). You can use pinia actions for this, or the hooks, or still use non-awaited functions in the storage, but may experience side effects.

Oh yeah, I'm aware, I was just wondering if you have a suggestion for a replacement path, I'll look at actions but that looked like I have to have those defined for every store instead of globally, thanks!

Would be cool if you could do something like this:

pinia.use(
    createPersistedState({
      actions: {
        getItem: async (key) => {
          return getStore(key);
        },
        setItem: async (key, value) => {
          return setStore(key, value);
        },
        removeItem: async (key) => {
          return deleteStore(key);
        },
      },
    }),
  );

@prazdevs
Copy link
Owner

Due to the way the plugin behaves and how pinia handles it, synchronozing data from async sources is out of the scope of the plugin. My advice would be to have calls done on app startup then calling stores to update the data. That would be the safest way without risking race conditions having async calls in sync functions.

Closing this 😌

@prazdevs prazdevs closed this as not planned Won't fix, can't repro, duplicate, stale Jan 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🔍️ pending triage This issue needs to be looked into
Projects
None yet
Development

No branches or pull requests

2 participants