Iterate over all stores - is _s
an API guarantee?
#1859
-
Hi, I'm looking to {get a listing of, iterate over} all stores in my application. This is in support of running hooks against any stores implementing particular interfaces at certain times in the application lifecycle (onLogout, etc.). In #911 there seems to be a common solution of the form:
Which is a fine solution, assuming Is the |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
const stores = new Set()
pinia.use(({ store }) => {
stores.add(store)
}) |
Beta Was this translation helpful? Give feedback.
-
Here is typed solution: import { Store } from "pinia";
const AllPiniaStores = new Set<Store>();
pinia.use(({ store }) => {
AllPiniaStores.add(store);
store.$all = AllPiniaStores;
store.$reset_all = () => store.$all.forEach((s) => s.$reset());
});
declare module "pinia" {
export interface PiniaCustomProperties {
$all: typeof AllPiniaStores;
/** Reset all the stores */
$reset_all: () => void;
}
} |
Beta Was this translation helpful? Give feedback.
_s
is internal so it could change anytime and it wouldn't be a major version. It's unlikely to disappear though as it's crucial internally. You can create a similar solution with a plugin: