0.1.5 • Published 12 months ago

pinia-misc v0.1.5

Weekly downloads
-
License
MIT
Repository
github
Last release
12 months ago

pinia-misc

NPM version

Get started

pnpm i pinia-misc

Usage

  1. Setup plugin with your createPinia fn
import { createPinia } from 'pinia'
import { persistPlugin } from 'pinia-misc'

const pinia = createPinia()
pinia.use(persistPlugin)
  1. Usage in pinia store
import { defineStore } from 'pinia'

const useTestStore = defineStore('counter', {
  persist: {
    enabled: true,
    strategies: [
      {
        key: 'foobar',
        flush: 'lazy',
      },
    ],
  },
  state: () => ({
    counter: 0,
  }),
  actions: {
    increment() {
      this.counter++
    },
    randomizeCounter() {
      this.counter = Math.round(100 * Math.random())
    },
  },
})
  1. Typescript support (Optional)
{
  "types": [
    "pinia-misc"
  ]
}

Then you can use persist option with type emits.

  1. Persist options
type Flush = 'sync' | 'async' | 'lazy'

interface PersistStrategy {
  /**
   * Persist key, if not set will use store id instead
   */
  key?: string
  /**
   * Storage to use, if not set will use sessionStorage
   */
  storage?: Storage
  /**
   * Paths to persist, if not set will persist all, can be string or array
   */
  paths?: string[] | string
  /**
   * Flush strategy, if not set will use 'sync', the others two are 'async' and 'lazy', which can be used for better performance
   *
   * sync: persist immediately
   * async: persist after promise resolved
   * lazy: persist on window beforeunload
   */
  flush?: Flush
}

interface PersistOptions {
  /**
   * Whether to persist the state on the client.
   */
  enabled: true
  /**
   * Custom strategies to persisting the state.
   */
  strategies?: PersistStrategy[]
}

License

MIT License © 2022 guygubaby

0.1.5

12 months ago

0.1.3

2 years ago

0.1.2

2 years ago

0.1.1

2 years ago

0.1.0

2 years ago

0.0.1

2 years ago