]> git.ipfire.org Git - thirdparty/vuejs/pinia.git/commitdiff
docs: note about subscribe in plugins
authorEduardo San Martin Morote <posva13@gmail.com>
Wed, 12 May 2021 14:55:49 +0000 (16:55 +0200)
committerEduardo San Martin Morote <posva13@gmail.com>
Wed, 12 May 2021 14:55:49 +0000 (16:55 +0200)
docs/core-concepts/plugins.md

index 3d66453fbe53200b1b77be7b205fe4f131d9eb07..a96233759b393a27a047585de2e3f70baa2a8604 100644 (file)
@@ -73,6 +73,24 @@ pinia.use(({ store }) => {
 
 This is why you can access all computed properties without `.value`.
 
+## `$subscribe` inside plugins
+
+Because of the limitation mentioned above about plugins being invoked **every time `useStore()` is called**, it's important to avoid _subscribing_ multiple times by keeping track of the registered subscriptions:
+
+```ts
+let isRegistered
+pinia.use({ store }) => {
+  if (!isRegistered) {
+  store.$subscribe(() => {
+    // react to store changes
+  })
+  isRegistered = true
+  }
+})
+```
+
+The same is true for `store.$onAction()`.
+
 ## Adding new options
 
 It is possible to create new options when defining stores to later on consume the options on plugins. For example, you could create a `debounce` option that allows you to debounce any action: