Coder Social home page Coder Social logo

andreisoroka / pinia-shared-state Goto Github PK

View Code? Open in Web Editor NEW

This project forked from wobsoriano/pinia-shared-state

0.0 1.0 0.0 713 KB

๐Ÿ A lightweight plugin to sync your pinia state across browser tabs.

Home Page: https://wobsoriano.github.io/pinia-shared-state/

License: MIT License

HTML 4.98% Vue 23.41% TypeScript 71.60%

pinia-shared-state's Introduction

pinia-shared-state

npm version bundle size

A lightweight module to sync your pinia state across browser tabs. Supports Vue 2 and 3.

Requirements

  • vue ^2.6.14 || ^3.2.0

Install

pnpm add pinia pinia-shared-state

Usage

import { PiniaSharedState } from 'pinia-shared-state'

// Pass the plugin to your application's pinia plugin
pinia.use(
  PiniaSharedState({
    // Enables the plugin for all stores. Defaults to true.
    enable: true,
    // If set to true this tab tries to immediately recover the shared state from another tab. Defaults to true.
    initialize: false,
    // Enforce a type. One of native, idb, localstorage or node. Defaults to native.
    type: 'localstorage',
  }),
)
const useStore = defineStore({
  id: 'counter',
  state: () => ({
    count: 0,
    foo: 'bar',
  }),
  share: {
    // An array of fields that the plugin will ignore.
    omit: ['foo'],
    // Override global config for this store.
    enable: true,
    initialize: true,
  },
})

You can also share state directly without installing the plugin:

import { defineComponent } from 'vue'
import { share, unshare } from 'pinia-shared-state'
import useStore from './store'

export default defineComponent({
  setup() {
    const counterStore = useStore()

    // Call `unshare` method to close the channel
    unshare()
  },
})

Credits

  • pinia - ๐Ÿ Intuitive, type safe, light and flexible Store for Vue using the composition api with DevTools support.
  • vue-demi - Creates Universal Library for Vue 2 & 3.
  • broadcast-channel - BroadcastChannel to send data between different browser-tabs or nodejs-processes.

License

MIT License.

pinia-shared-state's People

Contributors

posva avatar wobsoriano avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.