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 🙏

© 2024 – Pkg Stats / Ryan Hefner

vue-shared-store

v2.0.0

Published

The most easy way to define a shared store composition api for vue.

Downloads

6

Readme

vue-shared-store

The most easy way to define a shared store composition api for vue3.

Install

npm i vue-shared-store

Usage

// a.ts

import { defineStore } from 'vue-shared-store';
import { computed } from 'vue';

export const useState = defineStore(
    {
        a: 1,
    },
    (state) => {
        const b = computed(() => state.value.a + 1);

        const inc = () => state.value.a ++;

        return { b, inc };
    },
);

In the previous code, we define a shared state which is an object contains a a property. The defineStore function return a composition api function which will be used in different components which use the shared state, and when the shared state is modified in one component, the other components which use this shared state will react the mutation.

// b.vue
<script setup>
import { useState } from './a';
const { b } = useState();
</script>

<template>
    <span>{{b}}</span>
</template>

In this component, we use the exposed composition api function to use value b.

// c.vue
<script setup>
import { useState } from './a';
const { inc } = useState();
</script>

<template>
    <button @click="inc">inc</button>
</template>

In this component, we invoke the the inc function to modify the shared state, however the state is shared thus the preivous component which use value b will change too.

API

defineStore(initial, define?, options?)
  • initial: shared initial state
  • define: define the composition api function, recieve shared state wrapped by a ref, if not pass, the composition api function return the shared state directly
  • options
    • name: to indentify current observer's name
    • plugins: array

Plugin

A plugin is a function which using composition api.

Plugin: (state, options, initial) => void
createSharedStoreMutationObserver(options): Plugin

Create a shared store mutation observer plugin. Can only be used in debug mode to trace code source line. options:

  • debug: boolean, true to console the debugger message
  • onChange: ({ time, name, oldValue, newValue, keyPath?, state, initial, trace, typeof }) => void

Example:

const useState = defineStore(
    { a: { b: [1, 2, 3] } },
    (state) => {
        const b = computed(() => state.value.a.b[2] + 1);

        const inc = () => state.value.a.b[2]++;

        return { b, inc };
    },
    {
        plugins: [
            createSharedStoreMutationObserver({
                debug: process.env.NODE_ENV === 'development',
                onChange({ time, newValue, keyPath }) {
                    record(time, keyPath, newValue);
                },
            }),
        ],
    },
);