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

subscriptions are lost during HMR #991

Open
duoyue opened this issue Jan 25, 2022 · 9 comments
Open

subscriptions are lost during HMR #991

duoyue opened this issue Jan 25, 2022 · 9 comments
Labels
✨ enhancement New feature or request HMR 🔥 Related to Hot Module Replacement

Comments

@duoyue
Copy link

duoyue commented Jan 25, 2022

I have some code like this , it could output correctly when app first loaded or refresh ,but don‘t work after HMR.

const pinia = createPinia();
pinia.use(({ store }) => {
  store.$subscribe((mutations, state) => {
    console.log('trigger subscritions ', mutations, state);
  });
});

I have add some breakpoints in Pinia.js ,and found that subscriptions is emty [] after HMR.

// the  "subscriptions" is empty [] after HMR
function triggerSubscriptions(subscriptions, ...args) {
    subscriptions.forEach((callback) => {
        callback(...args);
    });
}
@duoyue duoyue changed the title $subscribe are lost during HMR subscriptions are lost during HMR Jan 26, 2022
@posva
Copy link
Member

posva commented Jan 27, 2022

Maybe this can be fixed but it does require some extra logic because any subscription that happens inside a plugin should not be transferred. There could be other cases of subscriptions that should not be transferred to avoid duplication.

Or maybe there is even a better solution within acceptHMRUpdate

@posva posva added ✨ enhancement New feature or request HMR 🔥 Related to Hot Module Replacement labels Jan 27, 2022
@davidmyersdev
Copy link

@posva what do you mean by "any subscription that happens inside a plugin should not be transferred" here? I'm also running into this problem when trying to develop a plugin locally. The plugin only receives the $subscribe callback once before it is removed. Hot-reloading for plugins seems difficult since the initial plugin registration can have side effects on the current store state (e.g. hydrating the state from localStorage).

@davidmyersdev
Copy link

Additionally, it looks like this problem goes away when using detached: true by preventing the removeSubscription callback from firing on unmount. Is that configuration expected for plugins that need to subscribe to mutations/actions?

@Alanscut
Copy link
Contributor

Passing { detached: true } as the second parameter seems to fix this.

@tansan-dev
Copy link

thanks

@posva

This comment was marked as outdated.

@F0rsaken

This comment was marked as outdated.

@posva

This comment was marked as outdated.

@F0rsaken
Copy link

F0rsaken commented Nov 7, 2022

@posva sure, created PR with docs update #1781 👍

@github-project-automation github-project-automation bot moved this to 🆕 Triaging in Pinia Roadmap Sep 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
✨ enhancement New feature or request HMR 🔥 Related to Hot Module Replacement
Projects
Status: 📋 Backlog
Development

No branches or pull requests

6 participants