├── .changeset ├── README.md └── config.json ├── .github └── workflows │ └── release.yml ├── .gitignore ├── .npmrc ├── CHANGELOG.md ├── LICENSE.md ├── README.md ├── eslint.config.js ├── examples ├── nuxt3 │ ├── .gitignore │ ├── README.md │ ├── app.vue │ ├── nuxt.config.ts │ ├── package.json │ ├── plugins │ │ └── shared-state.ts │ ├── store │ │ └── counter.ts │ └── tsconfig.json └── vite │ ├── .gitignore │ ├── README.md │ ├── index.html │ ├── package.json │ ├── public │ └── favicon.ico │ ├── src │ ├── App.vue │ ├── assets │ │ └── logo.png │ ├── main.ts │ ├── shims-vue.d.ts │ ├── store.ts │ └── vite-env.d.ts │ ├── tsconfig.json │ └── vite.config.ts ├── package.json ├── pnpm-lock.yaml ├── pnpm-workspace.yaml ├── src ├── index.ts ├── utils.ts └── vanilla.ts ├── tsconfig.base.json ├── tsconfig.json └── tsup.config.ts /.changeset/README.md: -------------------------------------------------------------------------------- 1 | # Changesets 2 | 3 | Hello and welcome! This folder has been automatically generated by `@changesets/cli`, a build tool that works 4 | with multi-package repos, or single-package repos to help you version and publish your code. You can 5 | find the full documentation for it [in our repository](https://github.com/changesets/changesets) 6 | 7 | We have a quick list of common questions to get you started engaging with this project in 8 | [our documentation](https://github.com/changesets/changesets/blob/main/docs/common-questions.md) 9 | -------------------------------------------------------------------------------- /.changeset/config.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "https://unpkg.com/@changesets/config@3.0.5/schema.json", 3 | "changelog": "@changesets/cli/changelog", 4 | "commit": false, 5 | "fixed": [], 6 | "linked": [], 7 | "access": "public", 8 | "baseBranch": "main", 9 | "updateInternalDependencies": "patch", 10 | "ignore": ["vite-example", "nuxt-example"] 11 | } 12 | -------------------------------------------------------------------------------- /.github/workflows/release.yml: -------------------------------------------------------------------------------- 1 | name: Release 2 | 3 | on: 4 | push: 5 | branches: 6 | - main 7 | 8 | concurrency: ${{ github.workflow }}-${{ github.ref }} 9 | 10 | jobs: 11 | release: 12 | permissions: 13 | contents: write # to create release (changesets/action) 14 | pull-requests: write # to create pull request (changesets/action) 15 | name: Release 16 | runs-on: ubuntu-latest 17 | steps: 18 | - name: Checkout Repo 19 | uses: actions/checkout@v3 20 | with: 21 | # This makes Actions fetch all Git history so that Changesets can generate changelogs with the correct commits 22 | fetch-depth: 0 23 | - uses: pnpm/action-setup@v4 24 | with: 25 | version: 9.15.4 26 | - name: Setup Node.js 27 | uses: actions/setup-node@v3 28 | with: 29 | node-version: 20.x 30 | cache: pnpm 31 | 32 | - run: pnpm install 33 | 34 | - name: Create Release Pull Request or Publish to npm 35 | id: changesets 36 | uses: changesets/action@v1 37 | with: 38 | publish: pnpm release 39 | env: 40 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 41 | NPM_TOKEN: ${{ secrets.NPM_TOKEN }} 42 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | node_modules 3 | /dist 4 | 5 | 6 | # local env files 7 | .env.local 8 | .env.*.local 9 | 10 | # Log files 11 | npm-debug.log* 12 | yarn-debug.log* 13 | yarn-error.log* 14 | pnpm-debug.log* 15 | 16 | # Editor directories and files 17 | .idea 18 | .vscode 19 | *.suo 20 | *.ntvs* 21 | *.njsproj 22 | *.sln 23 | *.sw? 24 | 25 | # npm 26 | *.tgz 27 | -------------------------------------------------------------------------------- /.npmrc: -------------------------------------------------------------------------------- 1 | ignore-workspace-root-check=true 2 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # pinia-shared-state 2 | 3 | ## 1.0.1 4 | 5 | ### Patch Changes 6 | 7 | - 0db3569: Remove vue 2 mention in README 8 | 9 | ## 1.0.0 10 | 11 | ### Major Changes 12 | 13 | - fe3f0e1: Introduce Pinia v3 14 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2022 Robert Soriano 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # pinia-shared-state 2 | 3 | [](https://www.npmjs.com/package/pinia-shared-state)   4 | 5 | Sync your Pinia state across browser tabs. 6 | 7 | ## Requirements 8 | 9 | - vue ^3.5.11 10 | 11 | ## Install 12 | 13 | ```sh 14 | npm install pinia-shared-state 15 | ``` 16 | 17 | ## Usage 18 | 19 | ```js 20 | import { PiniaSharedState } from 'pinia-shared-state' 21 | 22 | // Pass the plugin to your application's pinia plugin 23 | pinia.use( 24 | PiniaSharedState({ 25 | // Enables the plugin for all stores. Defaults to true. 26 | enable: true, 27 | // If set to true this tab tries to immediately recover the shared state from another tab. Defaults to true. 28 | initialize: false, 29 | // Enforce a type. One of native, idb, localstorage or node. Defaults to native. 30 | type: 'localstorage', 31 | }), 32 | ) 33 | ``` 34 | 35 | ```js 36 | const useStore = defineStore({ 37 | id: 'counter', 38 | state: () => ({ 39 | count: 0, 40 | foo: 'bar', 41 | }), 42 | share: { 43 | // An array of fields that the plugin will ignore. 44 | omit: ['foo'], 45 | // Override global config for this store. 46 | enable: true, 47 | initialize: true, 48 | }, 49 | }) 50 | ``` 51 | 52 | Vanilla usage: 53 | 54 | ```ts 55 | import { share } from 'pinia-shared-state' 56 | import { onMounted, onUnmounted } from 'vue' 57 | import useStore from './store' 58 | 59 | const counterStore = useStore() 60 | 61 | onMounted(() => { 62 | const { unshare } = share('counter', counterStore, { initialize: true }) 63 | 64 | onUnmounted(() => { 65 | // Call `unshare` method to close the channel 66 | unshare() 67 | }) 68 | }) 69 | ``` 70 | 71 | ## License 72 | 73 | MIT 74 | -------------------------------------------------------------------------------- /eslint.config.js: -------------------------------------------------------------------------------- 1 | import antfu from '@antfu/eslint-config' 2 | 3 | export default antfu({ 4 | vue: true, 5 | }) 6 | -------------------------------------------------------------------------------- /examples/nuxt3/.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | *.log* 3 | .nuxt 4 | .nitro 5 | .cache 6 | .output 7 | .env 8 | dist 9 | -------------------------------------------------------------------------------- /examples/nuxt3/README.md: -------------------------------------------------------------------------------- 1 | # Nuxt 3 Minimal Starter 2 | 3 | Look at the [nuxt 3 documentation](https://v3.nuxtjs.org) to learn more. 4 | 5 | ## Setup 6 | 7 | Make sure to install the dependencies: 8 | 9 | ```bash 10 | # yarn 11 | yarn install 12 | 13 | # npm 14 | npm install 15 | 16 | # pnpm 17 | pnpm install --shamefully-hoist 18 | ``` 19 | 20 | ## Development Server 21 | 22 | Start the development server on http://localhost:3000 23 | 24 | ```bash 25 | npm run dev 26 | ``` 27 | 28 | ## Production 29 | 30 | Build the application for production: 31 | 32 | ```bash 33 | npm run build 34 | ``` 35 | 36 | Locally preview production build: 37 | 38 | ```bash 39 | npm run preview 40 | ``` 41 | 42 | Checkout the [deployment documentation](https://v3.nuxtjs.org/guide/deploy/presets) for more information. 43 | -------------------------------------------------------------------------------- /examples/nuxt3/app.vue: -------------------------------------------------------------------------------- 1 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | - 13 | 14 | 15 | + 16 | 17 | 18 | 19 | 20 | 21 | 89 | -------------------------------------------------------------------------------- /examples/nuxt3/nuxt.config.ts: -------------------------------------------------------------------------------- 1 | import { defineNuxtConfig } from 'nuxt/config' 2 | 3 | // https://v3.nuxtjs.org/api/configuration/nuxt.config 4 | export default defineNuxtConfig({ 5 | modules: ['@pinia/nuxt'], 6 | }) 7 | -------------------------------------------------------------------------------- /examples/nuxt3/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "nuxt-example", 3 | "private": true, 4 | "scripts": { 5 | "build": "nuxt build", 6 | "dev": "nuxt dev", 7 | "generate": "nuxt generate", 8 | "preview": "nuxt preview" 9 | }, 10 | "dependencies": { 11 | "@pinia/nuxt": "^0.10.1", 12 | "nuxt": "^3.15.1", 13 | "pinia": "^3.0.1", 14 | "pinia-shared-state": "workspace:*" 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /examples/nuxt3/plugins/shared-state.ts: -------------------------------------------------------------------------------- 1 | import { PiniaSharedState } from 'pinia-shared-state' 2 | 3 | export default defineNuxtPlugin((nuxtApp) => { 4 | // @ts-expect-error: Missing types? 5 | nuxtApp.$pinia.use(PiniaSharedState({ 6 | enable: true, 7 | })) 8 | }) 9 | -------------------------------------------------------------------------------- /examples/nuxt3/store/counter.ts: -------------------------------------------------------------------------------- 1 | import { acceptHMRUpdate, defineStore } from 'pinia' 2 | 3 | export const useCounterStore = defineStore('counter', { 4 | state() { 5 | return { count: 0 } 6 | }, 7 | 8 | actions: { 9 | increment() { 10 | this.count++ 11 | }, 12 | decrement() { 13 | if (!this.count) 14 | return 15 | this.count-- 16 | }, 17 | }, 18 | 19 | share: { 20 | enable: true, 21 | }, 22 | }) 23 | 24 | if (import.meta.hot) 25 | import.meta.hot.accept(acceptHMRUpdate(useCounterStore, import.meta.hot)) 26 | -------------------------------------------------------------------------------- /examples/nuxt3/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | // https://v3.nuxtjs.org/concepts/typescript 3 | "extends": "./.nuxt/tsconfig.json", 4 | "compilerOptions": { 5 | "moduleResolution": "bundler", 6 | "strict": true, 7 | "noEmit": true 8 | } 9 | } 10 | -------------------------------------------------------------------------------- /examples/vite/.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | .DS_Store 3 | dist 4 | dist-ssr 5 | *.local 6 | -------------------------------------------------------------------------------- /examples/vite/README.md: -------------------------------------------------------------------------------- 1 | # Vue 3 + Typescript + Vite 2 | 3 | This template should help get you started developing with Vue 3 and Typescript in Vite. 4 | 5 | ## Recommended IDE Setup 6 | 7 | [VSCode](https://code.visualstudio.com/) + [Vetur](https://marketplace.visualstudio.com/items?itemName=octref.vetur). Make sure to enable `vetur.experimental.templateInterpolationService` in settings! 8 | 9 | ### If Using ` 12 |