From: Eduardo San Martin Morote Date: Wed, 12 May 2021 14:55:49 +0000 (+0200) Subject: docs: note about subscribe in plugins X-Git-Tag: v2.0.0-alpha.17~21 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=26a1fe3f9c16d1b26e6972b9626ba309c98d483c;p=thirdparty%2Fvuejs%2Fpinia.git docs: note about subscribe in plugins --- diff --git a/docs/core-concepts/plugins.md b/docs/core-concepts/plugins.md index 3d66453f..a9623375 100644 --- a/docs/core-concepts/plugins.md +++ b/docs/core-concepts/plugins.md @@ -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: