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-rxjs

v0.4.1

Published

A type-safe Vuex alternative utilizing RxJS; Compatible with Vue Devtools

Downloads

5

Readme

vuex-rxjs 💽🔄👁

State management for Vue:

Installation ⬇

Install RxJS as a peer dependency: npm install rxjs --save

npm install vuex-rxjs --save
yarn add vuex-rxjs

Usage (the simple way) 👶

First, make sure VuexRxJS is properly installed/registered:

main.ts:

import {VuexRxJS} from "vuex-rxjs";

VuexRxJS(Vue);  // Install VuexRxJS

new Vue({
    render: h => h(App),
}).$mount('#app')

Then, define your application state and your first store:

store.ts:

import {Store} from "vuex-rxjs/dist/store";

/**
 * Define the state:
 */
class State {
    counter: number = 0;    // initial value for counter
}

/**
 * Define your store:
 */
class SimpleStore extends Store<State> {

    /**
     * Mutation:
     */
    increment() {
        this.commit({
            type: 'INCREMENT_COUNTER',
            payload: state => ({
                counter: state.counter + 1
            })
        });
        
        // NOTE: the payload function never alters the state;
        // instead, it returns a new state object
    }
}

/**
 * Instantiate your store and export it:
 */
export const store = new SimpleStore(new State());

Now you can use your store in all of your components:

Component.vue:

<template>
    <div id="app">
        <p>Count: {{count}}</p>
        <button @click="addOne()">Add 1</button>
    </div>
</template>

<script lang="ts">
    import {Component, Vue} from 'vue-property-decorator';
    import {store} from "./store";

    @Component
    export default class App extends Vue {
        
        // Bind a state property to a component property:
        count: number = store.bind(state => state.counter);

        // Call a store method:
        addOne() {
            store.increment();
        }
    }
</script>

🍻🎊 Done! 🎉🥂

Modules (advanced usage) 💪

In complex applications, you will probably want to split your state into different parts, like so:

Rootstate
└ auth
└ profile
└ article
└ ... 

Modules help you do exactly this. First, define your application state (root state):

root.store.ts:

/**
 * Define the root state:
 */
class RootState {
    auth = new AuthState();
    profile = new ProfileState();
    article = new ArticleState();
}

export const rootStore = new Store(new RootState());

profile.store.ts:

/**
 * Define the profile state:
 */
class ProfileState {
    username: string = '';
}

/**
 * Define your module store:
 */
class ProfileStore extends Module<ProfileState, RootState> {

    setUsername(newName: string) {
        this.commit({
            type: 'SET_USERNAME',
            payload: profile => ({
                username: newName
            })
        });
    }
}

export const profileStore = new ProfileStore(
    rootStore,
    rootState => rootState.profile,                         // mapping from the root state to the profile state
    (profile, rootState) => rootState.profile = profile     // mapping from the profile state to the root state
);

Component.vue:

<template>
    <div id="app">
        <p>Your name: {{username}}</p>
        <button @click="changeName()">I'm Bob now!</button>
    </div>
</template>

<script lang="ts">
    import {profileStore} from "./profile.store";

    @Component
    export default class App extends Vue {
    
        username: string = profileStore.bind(profile => profile.username);

        changeName() {
            profileStore.setUsername('Bob');
        }
    }
</script>

API and types

Store:

class Store<RootState> {

    constructor(initialState: RootState);
    
    // in case you need to directly subscribe to the state:
    state$: Observable<RootState>;
    
    // bind a state property to a component property:
    bind<PropertyType>(mapping: (state: RootState) => PropertyType): PropertyType;
    
    // mutate the state:
    commit(mutation: Mutation<RootState>): void;
    
    // get the current state and invoke mutations (or do other stuff) based on it:
    dispatch(action: Action<RootState>): Promise<void>;
}

Module:

class Module<ModuleState, RootState> {
    
    constructor(
        parentStore: IStore<RootState>,
        stateMap: (state: RootState) => ModuleState,
        dispatchMap: (moduleState: ModuleState, rootState: RootState) => void
    );
    
    // in case you need to directly subscribe to the module state:
    state$: Observable<ModuleState>;
    
    // bind a module state property to a component property:
    bind<PropertyType>(mapping: (state: ModuleState) => PropertyType): PropertyType;
    
    // mutate the module state:
    commit(mutation: Mutation<ModuleState, RootState>): void;
    
    // get the current module state and invoke mutations (or do other stuff) based on it:
    dispatch(action: Action<ModuleState, RootState>): Promise<void>;
}

Mutations and Actions:

interface Mutation<State, RootState=State> {
    type: string;
    payload: Payload<State, RootState>;
}

type Payload<State, RootState=State> = (state: State, rootState: RootState) => State;

type Action<State, RootState=State> = (state: State, rootState: RootState) => Promise<void>;

Examples 👀

Todo 🗒

  • Add tests
  • Add module example