New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

rxsource

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

rxsource

Vuex Plugin for providing a reactive single data source to the state tree via branch mappings

latest
Source
npmnpm
Version
1.0.2
Version published
Maintainers
1
Created
Source

rxsource

Vuex Plugin for providing a reactive X single data source to the state tree via branch mappings.

The power of this library is that you can tie an Observer to your ajax responses or a socket connection, then do a little mapping, and push into your application state from a single source.

Components in the app can just "announce" what data they need and they don't have to manage dispatching to the store, it will be done for them. Similarly, a socket based api can push to a socket on the client that is updating that same Observable Subject.

When a change comes in at any time, the Rxsource library that was passed the Observable will update the state tree and propagate changes down to any child component listening for updates. Ez-peezy reactive updates from a single, simple to reason about, source!

Usage

Install the project into your vue project

npm i -S rxsource

Import it into your main store file

import { Rxsource, rxsourceModule } from 'rxsource'

Add the mutation, actions, and plugin to your Vuex store

  const testMap = [
    {
      branch: 'hello',
      key: 'helloWorld',
      updateByMerging: false // defaults to replace. Note: Arrays are concatenated
    }
  ]

  const debug = process.env.NODE_ENV !== 'production'

  const store = new Vuex.Store({
    actions: {...actions, ...rxsourceModule.actions},
    mutations: {...rxsourceModule.mutations},
    plugins: [
      Rxsource(somethingObservable$, testMap)
    ],
    getters,
    modules: {
      hello,
      world
    },
    strict: debug
  })

Observable argument

Rxsource([Observable], [Mapper])

You'll see you need to pass the Rxsource plugin an Observable and a map.

The Observable should expose a .subscribe method that gets updated via a "next" callback as the first callback. Rxsource relies on this standard observable structure. Behind the scenes we just subscribe to the observable then route the incoming data to a particular branch of your store based on the map you provide.

This structure works very well with GraphQL responses that return the key used by the API schema.

Mapper argument

Rxsource([Observable], [Mapper])

The mapper is a collection specifying the mapping and the merge strategy to be used internally.

  const testMap = [
    {
      branch: 'hello',
      key: 'helloWorld',
      updateByMerging: false // defaults to replace. Note: Arrays are concatenated
    }
  ]

branch specifies what branch on your vuex state tree you want to apply the data to. key is the key to look for on the incoming data. FOr instance, given the incoming data has a structure of

  {"getUserById":{id:1234,name:"Eli"}}

and the mapper has a configuration of

  const testMap = [
    {
      branch: 'user',
      key: 'getUserById'
    }
  ]

then the user branch of the state tree will be updated with the data contained in getUserById.

updateByMerging sets the merge strategy. If set to false or if it's not present then the default merge strategy is to replace the data in that particular branch of he state tree with the incoming data. If set to tru, then the incoming data will be merged (if it's an object), or it will be concatenated (if it's an array).

Keywords

npm-package

FAQs

Package last updated on 26 Sep 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts