CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-pinia

Intuitive, type safe and flexible Store for Vue

Pending

Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

Overview
Eval results
Files

pinia-instance.mddocs/

Pinia Instance Management

Core functions for creating and managing the global Pinia instance that coordinates all stores in your application.

Capabilities

Create Pinia Instance

Creates a new Pinia instance to be used by the application. This is typically done once at the application root level.

/**
 * Creates a Pinia instance to be used by the application
 * @returns A new Pinia instance with plugin system and state management
 */
function createPinia(): Pinia;

interface Pinia {
  /** Vue plugin install method */
  install(app: App): void;
  /** Add a plugin to extend Pinia's functionality */
  use(plugin: PiniaPlugin): Pinia;
  /** Reactive state container for all stores */
  state: Ref<Record<string, StateTree>>;
  /** Array of installed plugins */
  _p: PiniaPlugin[];
  /** Reference to the Vue app instance */
  _a: App | null;
  /** Effect scope for reactivity cleanup */
  _e: EffectScope;
  /** Map of all store instances */
  _s: Map<string, StoreGeneric>;
}

Usage Examples:

import { createApp } from "vue";
import { createPinia } from "pinia";

const app = createApp({});
const pinia = createPinia();

// Install Pinia as a Vue plugin
app.use(pinia);

// Add plugins before or after installation
pinia.use(myCustomPlugin);

Dispose Pinia Instance

Disposes of a Pinia instance by stopping its effect scope and removing the state, plugins, and stores. Useful for testing and applications with multiple Pinia instances.

/**
 * Dispose a Pinia instance by stopping its effectScope and removing the state, plugins and stores
 * @param pinia - Pinia instance to dispose
 */
function disposePinia(pinia: Pinia): void;

Usage Examples:

import { createPinia, disposePinia } from "pinia";

const pinia = createPinia();

// Later, clean up the instance
disposePinia(pinia);

// The pinia instance cannot be used anymore after disposal

Set Active Pinia

Sets the active Pinia instance. This allows calling useStore() outside of a component setup after installing Pinia's plugin.

/**
 * Sets the active pinia instance
 * @param pinia - Pinia instance to set as active
 */
function setActivePinia(pinia: Pinia): void;

Usage Examples:

import { createPinia, setActivePinia, defineStore } from "pinia";

const pinia = createPinia();
setActivePinia(pinia);

// Now you can use stores outside of Vue components
const useStore = defineStore("main", () => ({ count: ref(0) }));
const store = useStore(); // Works outside of component setup

Get Active Pinia

Gets the currently active Pinia instance, if any.

/**
 * Gets the currently active pinia instance
 * @returns The active Pinia instance or undefined if none is set
 */
function getActivePinia(): Pinia | undefined;

Usage Examples:

import { getActivePinia } from "pinia";

const currentPinia = getActivePinia();

if (currentPinia) {
  // Use the active pinia instance
  console.log("Active Pinia found");
} else {
  console.log("No active Pinia instance");
}

Plugin System

Pinia Plugin

Plugins extend Pinia's functionality and receive a context object with access to the Pinia instance, Vue app, current store, and store options.

type PiniaPlugin = (context: PiniaPluginContext) => Partial<PiniaCustomProperties & PiniaCustomStateProperties> | void;

interface PiniaPluginContext<Id extends string = string, S extends StateTree = {}, G = {}, A = {}> {
  /** The Pinia instance */
  pinia: Pinia;
  /** The Vue app instance */
  app: App;
  /** The current store being processed */
  store: Store<Id, S, G, A>;
  /** The store options passed to defineStore */
  options: DefineStoreOptionsInPlugin<Id, S, G, A>;
}

interface PiniaCustomProperties {}
interface PiniaCustomStateProperties {}

Usage Examples:

import { PiniaPluginContext } from "pinia";

// Plugin that adds a custom method to all stores
const customPlugin = ({ store }: PiniaPluginContext) => {
  return {
    customMethod() {
      console.log(`Called from store: ${store.$id}`);
    },
  };
};

// Plugin that adds state properties
const statePlugin = ({ store }: PiniaPluginContext) => {
  store.$state.createdAt = new Date();
};

// Use the plugins
pinia.use(customPlugin);
pinia.use(statePlugin);

Types

interface DefineStoreOptionsInPlugin<Id extends string, S extends StateTree, G, A> {
  id: Id;
  state?: () => S;
  getters?: G;
  actions?: A;
  hydrate?(storeState: UnwrapRef<S>, initialState: UnwrapRef<S>): void;
}

type StoreGeneric = Store<string, StateTree, Record<string, any>, Record<string, any>>;

Install with Tessl CLI

npx tessl i tessl/npm-pinia

docs

index.md

options-api.md

pinia-instance.md

store-definition.md

store-usage.md

tile.json