Since mutation is gone, we have no proper way to sync and track state across browser/node.js process #2114
-
What problem is this solvingIn the past with Vuex, we could sync store state across tabs or even node.js process by overriding mutation and propagate them. reference: vuex-electron The synchronization can be done well because mutations are cheap and synchronize. Also, by propagating mutations execution we can ensure the exact delta change among all the tabs/processes are the same. However, as Pinia doesn't come with mutations, it even allow developer update state directly, we no longer be able to grab the exact delta change by mutation just like how we did before. Therefore, many pinia libraries that claimed to support Pinia state sync will cause unexpect side effect and watcher to be triggered under the hood, take pinia-shared-state as example:
The mechanism under the hood is to deep tracking every root level state entry by DEEP watching them, which mean once any of the deep/nested property has changed, the syncing will be triggered. By reading the snippet above, we can see that the librar will propagate the whole state to other tabs, which means that all watchers [by object reference] from other tabs will be triggered, since a whole new state object is constructed and applied into these tabs state. To demostrate it as example, I provide the snippets below
Since we cannot always ask the developers to watch only value type, it must cause many watchers to be triggered and unexpected side effect will be occurred in long term. Proposed solutionI keep thinking for few days and could not come out with any solution/workaround. I know the design is Pinia core basis and it could not be changed easily. Although I have some possible workaround in my head, it is not decent. Therefore, it would be nice if anyone can provide any workaround to achieve that. Thanks in advance Possible Workaround:
Describe alternatives you've consideredNo response |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
You could indeed enforce a team to use only You could find different ways of syncing state across processes, for example, you can send the whole state (which could be heavy). You can also create your own diffing algorithm to sync processes but this is outside of pinia's scope |
Beta Was this translation helpful? Give feedback.
In that case, you need to implement a diffing algorithm