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

command-pack

v1.1.4

Published

cqs pattern for redux

Downloads

8

Readme

Command-Pack for redux

CQS implementation for redux https://martinfowler.com/bliki/CommandQuerySeparation.html

Installation

npm i -S command-pack

Counter Sample

Sample for Counter

First I need to explain what is command and handler. Basically a command is just a data dictionary that carries required parameters for the handler. To create a command it is needed to make a new brand class that extends command-pack Command.

Commands:

For our counter sample, we need two commands: First, IncreaseCounter to increase total and DecreaseCounter.

- IncreaseCounter.js

import {Command} from "command-pack";

export default class IncreaseCounter extends Command {

    amount;

    constructor(amount) {
        super();
        this.amount = amount;
    }

    handle(state) {
        return {
            ...state,
            total: state.total + this.amount
        };
    }
}

- DecreaseCounter.js

import {Command} from "command-pack";

export default class IncreaseCounter extends Command {

    amount;

    constructor(amount) {
        super();
        this.amount = amount;
    }

    handle(state) {
        return {
            ...state,
            total: state.total - this.amount
        };
    }
}

Now we have commands... and they know how to handle this data parameters with handle method by given state.

Command Registration

import { CommandContainer } from "command-pack";

const container = new CommandContainer("counterStore") // Store name
    .setDefaultState({total: 0}) // Store default state
    .register(IncreaseCounter) // our commands
    .register(DecreaseCounter);

CommandExecutor

import { CommandContainer, CommandExecutor} from "command-pack";

CommandExecutor.add(new CommandContainer("counterStore")
    .setDefaultState({total: 0})
    .register(IncreaseCounter)
    .register(DecreaseCounter)
);

CommandExecutor.createStore() for Redux-Provider

import React from 'react';
import ReactDOM from 'react-dom';
import Provider from "react-redux/src/components/Provider";

import { CommandContainer, CommandExecutor} from "command-pack";

import IncreaseCounter from "./components/counter/IncreaseCounter";
import DecreaseCounter from "./components/counter/DecreaseCounter";
import Counter from './components/counter/Counter';

CommandExecutor.add(new CommandContainer("counterStore")
    .setDefaultState({total: 0})
    .register(IncreaseCounter)
    .register(DecreaseCounter)
);

ReactDOM
    .render(
        <Provider store={CommandExecutor.createStore()}>
            <div>
                <h2>COUNTER Sample</h2>
                <Counter/>
            </div>
        </Provider>
        , document.getElementById('root'));

registerServiceWorker();

Counter React Component

import React from 'react'
import {connect} from "react-redux";
import {CommandExecutor} from "command-pack";
import DecreaseCounter from "./DecreaseCounter";
import IncreaseCounter from "./IncreaseCounter";

class Counter extends React.Component {

    inc() {
        CommandExecutor.execute(new IncreaseCounter(1));
    }

    dec() {
        CommandExecutor.execute(new DecreaseCounter(1));
    }

    render() {
        const total = this.props.counterStore.total;

        return (<div>Counter : {total}
            <button onClick={this.dec.bind(this)}>decrease</button>
            <button onClick={this.inc.bind(this)}>increase</button>
        </div>)
    }
}

export default connect(x => {
    return {counterStore: x.counterStore}
})(Counter);

ASYNC example

To solve that problem, I tried with middlewares thunk and etc... But it just increases complexity. With command-pack it is really easy to implement an async flow.

Basically you need two commands:

  • StartDownload to start flow
  • DownloadCompleted to get the results

- StartDownload.js

import {Command, CommandExecutor} from "command-pack";

export default class StartDownload extends Command {

    url;

    constructor(url) {
        super();
        this.url = url;
    }

    handle(state) {
        fetch (this.url).then (content=>{
            CommandExecutor.execute (new DownloadCompleted (content));
        });
        return {
            ...state,
            downloading : true
        };
    }
}

- DownloadCompleted.js

import {Command} from "command-pack";

export default class DownloadCompleted extends Command {

    content;

    constructor(content) {
        super();
        this.content = content;
    }

    handle(state) {
        return {
            ...state,
            downloading : false,
            content : this.content
        };
    }
}