Giter Site home page Giter Site logo

vuex-broadcast's Introduction

Vuex Broadcast

Broadcast and share Vuex modules mutations between mupltiple instances of the same Vue application (like brower tabs) to keep them in sync.

Install

npm install --save vuex-broadcast

Usage

  • add a broadcast key to the namespaced modules you want to broadcast the mutations
// myModule.js

{
  broadcast: true,
  namespaced: true,

  state: {},
  mutations: {},
  actions: {}
}
  • register the Vuex plugin
import VuexBroadcast from "vuex-broadcast"

new Vuex.Store({
  modules: {}
  plugins: [new VuexBroadcast()]
})

From now on, each mutatons of the modules with the broadcast key set to true will be boradcasted to other app instances and applied in each of their Vuex stores.

Options

You can pass an options argument to the contructor with the following properties:

  • moduleName [String] defaults to "vuexBroadcast": the vuex module name that will be registered, defaults to "vuexBroadcast"
  • enableLeaderElection [Boolean], defaults to false: enable leader election
  • mainChannelName [String] defaults to "vuexBroadcast": the broadcast channel name, mainly used to elect a leader between all the app instances
  • you can pass any option of the broadcast-channel library: https://github.com/pubkey/broadcast-channel/#set-options-when-creating-a-channel-optional

Leader Election

As stated in https://github.com/pubkey/broadcast-channel/#using-the-leaderelection You can also elect a leader between all of the instances of you app.

This plugin registers a vuex module, with its name set by the moduleName option. This module exposes a state named isLeader which is set to true if the current instance is elected as leader.

You can access it like any regular vuex module:

// in components via
this.$store.state[moduleName].isLeader
// or
mapState([moduleName], ['isLeader'])

// in your vuex store in:
// actions
myAction ({state, commit, rootState}, myActionArgument) {
  const isLeader = rootState[moduleName].isLeader
}

// getters
myGetter: (state, getters, rootState) => {
  const isLeader = rootState[moduleName].isLeader
}

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.