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

action_switcher

v2.1.10

Published

Action switcher for redux

Downloads

9

Readme

Action switcher for redux

Usage

So the "switcher" is basically typescript-friendly reduce "function", which also has "combineReducers" functionality.

Pretty small library, you can use when:

  • you want to have "unnamed" actions (action types will be just "1", "2" etc), you can give type name though
  • you want to have action factory and the code that will change state in one place
  • you want to reuse code when you have the same state signature in several places

Imagine you have next types:

interface IState1 {
	a: string;
	b: number;
	c: string;
}

interface IState2 {
	d: string;
}

type IState3 = IState1 & IState2;

interface IState4 extends IState1 {
	e: IState2;
}

You can create action with next signature:

interface IActionWithStringValue extends IAction {
	value: string;
}

and create next action factories:

const switcher = new ActionSwitcher<IState1>({});

const setA = createActionFactory(switcher, {
    apply(state: IState1, action: IActionWithStringValue): IState1 {
        return { ...state, a: action.value };
    },
});

const setC = createActionFactory(switcher, {
    apply(state: IState1, action: IActionWithStringValue): IState1 {
        return { ...state, c: action.value };
    },
});

let state1 = {} as IState1;
state1 = switcher.apply(state1, setA({value: "hello"}));
state1 = switcher.apply(state1, setC({value: "world"}));

we created here 2 action factories which are using the same action signature, but have different types (they will be here "1", "2", as we didn't specify type explicitly). switcher.apply is the reducer function. So in normal application it will be used as dispatch(setA({value: "hello"}))

To create an action factory for actions with known type, use next:

const setC = createActionFactory(switcher, {
    apply(state: IState1, action: IActionWithStringValue): IState1 {
        return { ...state, c: action.value };
    },
    TYPE: "set_c",
});

We could also create a switcher for intersection types:

const switcher = new ActionSwitcher<IState3>({});

const setA = createActionFactory(switcher, {
    apply(state: IState3, action: IActionWithStringValue): IState3 {
        return { ...state, a: action.value };
    },
});

const switcher2 = new ActionSwitcher<IState2>({});

const setD = createActionFactory(switcher2, {
    apply(state: IState2, action: IActionWithStringValue): IState2 {
        return { ...state, d: action.value };
    },
});

switcher.attachBrother(switcher2);

let state3 = {} as IState3;

state3 = switcher.apply(state3, setA({value: "hello"}));
state3 = switcher.apply(state3, setD({value: "hello3"}));
expect(state3).toEqual({a: "hello", d: "hello3"});

We can also create a switcher for "child" state (basically combineReducers):

const switcher = new ActionSwitcher<IState4>({});

const setA = createActionFactory(switcher, {
    apply(state: IState4, action: IActionWithStringValue): IState4 {
        return { ...state, a: action.value };
    },
});

const switcher2 = new ActionSwitcher<IState2>({});

const setD = createActionFactory(switcher2, {
    apply(state: IState2, action: IActionWithStringValue): IState2 {
        return { ...state, d: action.value };
    },
});

switcher.attachChild("e", switcher2);

let state4 = {} as IState4;

state4 = switcher.apply(state4, setA({value: "hello"}));
state4 = switcher.apply(state4, setD({value: "hello3"}));
expect(state4).toEqual({a: "hello", e: {d: "hello3"}});

As a bonus, when you attach switchers to parent/brother switcher, it moves rules from the brother/child switcher to itself, so you will have only one map "action type" -> "action applier". Additionally attachChild takes 3-rd boolean argument (false by default) to "enhance" action type with field name. For example if switcher.attachChild("e", switcher2); would create an action with type action_type then switcher.attachChild("e", switcher2, true); would create e/action_type.

Switcher can be attached to a child array

switcher.attachArrayChild("st2", switcher2, 3, true);

which means - attach to child array state at field st2, initial state should have 3 values, use st2/ as type prefix

It does not set initial state for you, you can get it as rootSwitcher.getInitialState(); and use it as

const rootReducer = (state: IState, action: any) => {
	return rootSwitcher.apply(state, action);
};

const initialState = rootSwitcher.getInitialState();

createStore(rootReducer, initialState);

Example of usage can be seen at: electron-react-typescript-boilerplate fork.

GENERAL SWITCHERS/STATES

Error/Success

IErrorSuccessState is a simple state (import from general/states), defined as

export interface IErrorSuccessState {
	lastError: string | null;
	lastSuccess: string | null;
}

The corresonding switcher (import from general/switchers) can be created as createErrorSuccessSwitcher and defines next actions (action factories): RESET_ERROR_AND_SUCCESS to reset both error and success, GOT_ERROR to set error only, GOT_SUCCESS_RESULT to set success result only. createErrorSuccessSwitcher creates action switcher and to get an access to corresponding action factories you can cast the switcher.factories to IErrorSuccessFactories defined as

export interface IErrorSuccessFactories {
	resetErrorAndSuccess: ICreateAction<IAction>;
	gotError: ICreateAction<IActionWithMessage>;
	gotSuccessResult: ICreateAction<IActionWithMessage>;
}