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

vuex-extension-sync

v0.6.0

Published

A Vuex plugin to share store through webextensions components

Downloads

33

Readme

Original great project MitsuhaKitsune/vuex-webextensions, unfortunately it's not working with Manifest v3 and Vue3+Vuex4, so this is completly rewritten version.

Vuex extension sync

A Vuex plugin to sync shared store between background worker script and other extensions contexts: content script, popup, options. Any commit of mutation synced with others automatically and can be persisted in storage.

Current limitations:

  • Only mutations synced, not actions (I don't plan to do it because conceptually the action doesn't change the state)
  • Only chrome extensions
  • Vue3 + Vuex4

Installation

npm i vuex-extension-sync

Usage

Import the plugin into your store file:

import {createStore} from 'vuex';
import VuexExtensionSync from 'vuex-extension-sync';

export default createStore({
  ...
  plugins: [VuexExtensionSync({
    persist: ['somePersistedKey'],
    ignore: ['SOME_MUTATION'],
    debug: false,
  })],
});

Persistent states

⚠ Persistent states using chrome.storage.local to save the state keys in your browser's storage, be sure to grant storage permision.

It's usefull for save settings between browser reload. Use persist: String[] plugin option.

VuexExtensionSync({
  persist: ['somePersistedKey'],
})

Ignored mutations

For skip syncing some mutations use ignore: String[] plugin option.

VuexExtensionSync({
  ignore: ['SOME_MUTATION_1', 'SOME_MUTATION_N'],
})

Debug

For debugging use debug: Boolean|String plugin option. Default info.

Available levels: trace, debug, info, warn, error.

Boolean values mapping: true -> debug, false -> info.

VuexExtensionSync({
  debug: 'warn',
})

Keepalive background worker

Content page keeps reconnecting to terminated worker. When it's reconnects mutation @@SYNC_RECONNECT_MUTATION is fired. If you need to do something on reconnect (resend some state for example), create @@SYNC_RECONNECT_MUTATION mutation with some logic or create it empty and use store.subscribe to catch it.

Strategy

In a real extension, I ran into a problem with state synchronization between all pages.

I have music player control panel extension, when popup play button pressed mutation PLAY sent to content-script and activate play on site.

When site's player starts playing content-script sent back to background mutation PLAY with actual playing state, background updates it's store.

Now if I opened two tabs with same player site, popup's button click sent PLAY to both pages, they started playing and sent back PLAY, background got it from first and replicated to second, second stopped playing and sent it back. So we have a recursion.

To solve this problem I added new broadcast strategy to sync mutations only with master content-script page and ignore other content-script pages, but keep syncing with popup, options pages. Master page is elected by function. When master page is closed there is new election called.

Plugin options: strategy: "broadcast" | "master" - default broadcast. When broadcast chosen it's sync between all pages; When master chosen it's sync only with master content-script which elected by default function "first is master"; electionFunc: function - default null means function "first is master":

function electionFuncFirstIsMaster(ports: Map<Port, Meta>): Port | undefined {
  const [[port] = []] = [...ports.entries()]
    .filter(([, meta]) => meta.usedInMasterStrategy)
    .sort(([, aMeta], [, bMeta]) => aMeta.created - bMeta.created);
  return port;
}

type Port = {
  //@see https://developer.chrome.com/docs/extensions/reference/runtime/#type-Port
}

type Meta = {
    usedInMasterStrategy: boolean, //is used for master port election, cs pages only 
    master: boolean, //is master
    created: number //timestamp
}