npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

pinia-plugin-subscription

v0.0.2

Published

Pinia plugin for Vue.js that helps building Pinia plugins by centralizing subscriber registration and providing a `Store` base class for store helpers.

Readme

pinia-plugin-subscription

Pinia plugin for Vue.js that helps building Pinia plugins by centralizing subscriber registration and providing a Store base class for store helpers.

This project provides:

  • a lightweight mechanism to declare "subscribers" that are invoked when stores are registered or updated by Pinia;
  • a Store base class (helper wrapper) to ease interacting with Pinia stores from subscribers or other plugin code;
  • an API to create a Pinia plugin from a list of subscribers;
  • the $reset method to all stores modified by the plugin.

The main goal is to offer a clear API for writing reusable Pinia plugins and to make it easy to extend stores from plugin code.

Installation

Ensure Pinia is installed, then register the plugin in your main.ts:

import { createApp } from 'vue'
import { createPinia } from 'pinia'
import { createPlugin } from 'pinia-plugin-subscription'
import { myStoreSubscriber } from './src/core/my-store'
import App from './App.vue'

const app = createApp(App)
const pinia = createPinia()

// Register plugin (subscribers array, debug mode)
pinia.use(createPlugin([myStoreSubscriber], true))

app.use(pinia)
app.mount('#app')

Usage — Examples

1) Subscriber using a Store subclass:

import PluginSubscriber from 'pinia-plugin-subscription'
import { Store } from 'pinia-plugin-subscription'

class MyPlugin extends Store {
  protected static override _requiredKeys?: string[] | undefined = ['my-plugin-option']

  constructor(store, options, debug = false) {
    super(store, options, debug)
    this.doSomething()
  }
}

class MyPluginSubscriber extends PluginSubscriber<MyPlugin> {
  constructor() {
    super('my-plugin', MyPlugin.customizeStore.bind(MyPlugin))
  }
}

export const myStoreSubscriber = new MyPluginSubscriber()

2) Simple subscriber implementing PluginSubscriberInterface:

import type { PluginSubscriberInterface } from 'pinia-plugin-subscription'

export const myStoreSubscriber: PluginSubscriberInterface = {
  name: 'my-plugin',

  invoke: (context, debug) => {
    // context contains `store`, `options`, `pinia`
    console.log('store registered', context.store.$id)

    return true
  },
  
  resetStoreCallback: (store) => {
    console.log('store reset:', store.$id)
  }
}

Advanced Features

  • Debug mode: Pass true as the second argument to createPlugin to enable detailed logging and plugin filtering.
  • Reset callbacks: Define resetStoreCallback to run custom logic when a store is reset.

API Reference

createPlugin(subscribers: PluginSubscriber[], debug?: boolean): PiniaPlugin

Creates and returns a Pinia plugin from the provided subscribers. Each subscriber is invoked when a store is registered.

PluginSubscriberInterface

An object with at least an invoke(context: PiniaPluginContext, debug?: boolean) method, plus optional properties:

  • resetStoreCallback?: (store?: any) => void
  • storeOnActionSubscription?: { store, callback } (getter)
  • storeMutationSubscription?: { store, callback } (getter)
  • subscriptions?: Record<string, Function> (plugin-specific subscription functions)

The PluginSubscriber Abstract Class

The project provides an abstract PluginSubscriber implementation (see src/plugins/pluginSubscriber.ts) to simplify creating reusable subscribers.

Typical usage:

  • The subscriber instantiates a Store (or subclass) via a factory (MyStore.customizeStore).
  • The instance exposes:
    • subscriptions (from getSubscriptions())
    • storeMutationSubscription (from storeSubscribe)
    • storeOnActionSubscription (from onAction)
    • optional pluginCreated(store) hook called after initialization

Example:

import PluginSubscriber from 'pinia-plugin-subscription'
import StoreExtension from './src/extending-pinia-store/core/StoreExtension'
import { addStore } from './src/extending-pinia-store/plugins/stores'

class ExtendingStoreSubscriber extends PluginSubscriber<StoreExtension> {
  constructor() {
    super('extendsPiniaStore', StoreExtension.customizeStore.bind(StoreExtension))
    this.pluginCreated = addStore
  }
}

export const extendingStoreSubscriber = new ExtendingStoreSubscriber()

The Store Class — Summary

The Store class (see src/core/Store.ts) is a wrapper around a PiniaStore providing:

  • Properties: debug, options, state, store.
  • Useful methods:
    • addToState(name, value?) — adds a property to store state and exposes it as a Ref when appropriate.
    • addSubscription(pluginName, subscription) — registers a plugin-specific subscription function.
    • getSubscriptions() — returns registered subscriptions.
    • storeSubscribe (getter/setter) — factory for store.$subscribe ({ store, callback }).
    • onAction (getter/setter) — factory for onAction ({ store, callback }).
    • static customizeStore(store, options, debug?) — recommended factory for class instantiation.
    • debugLog(message, args) — conditional logging.
    • Helpers: stateHas(), storeHas(), getValue().

Testing

This plugin is tested with Vitest. Coverage reports are available in the coverage/ directory.

License

MIT