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

redstore

v0.1.0

Published

Syntactic sugar for `ngrx/store` reducer

Downloads

2

Readme

redstore (experimental)

Syntactic sugar for ngrx/store reducer.

redstore (REDucerSTORE) will helps you writing your action-creators and reducers compact in just a few lines, integrated in Angular2's dependency injection system.

Example (TodoList)

It only takes 4 little steps:

  1. create your state structure. We want a TodoList where we can add and remove todos and give a name for the list.

    export interface IState {
        name?: string;
        todos?: string[];
    }
  2. create your Redstore which is action-creator and reducer in one class.

    @Redstore()
    @Injectable()
    export class TodoListRedstore {
    
        @Action()
        addTodo(item: string) {
            return item;
        }
    
        @StateCopy()
        static addTodo(_state: IState, item: string) {
            const _todos = _state.todos = flatarraycopy(_state.todos);
            _todos.push(item);
        }
    
        @Action()
        removeTodo(index: number) {
            return index;
        }
    
        @StateCopy()
        static removeTodo(_state: IState, index: number) {
            const _todos = _state.todos = flatarraycopy(_state.todos);
            _todos.splice(index, 1);
        }
    
        // shortcut `set...` for a reducer copying state and setting `name`
        @Action()
        setName(name: string) {
            return name;
        }
    
        // can be skipped as this is the default reducer for `set...`-Actions
        // @StateCopy()
        // static setName(_state: IState, name: string) {
        //     _state.name = name
        // }
    }
  3. provide ngrx/store module to your application (also see here)

    const storeModule = StoreModule.provideStore(
        getReducer(TodoListRedstore),
        {}
    );
    
    @NgModule({
      imports: [
        // ...
        storeModule
      ]
    })
    export class AppModule {}
  4. inject your Redstore-class (e.g. TodoListRedstore) where you want and use its methods to dispatch actions.

    @Component({
        selector: 'myTodo',
        template: require('./todo.component.html')
    })
    export class TodoComponent {
    
        todos$: Observable<string[]>;
    
        constructor(
            private todoListRedstore: TodoListRedstore,
            private store: Store<IState>
        ) {
            this.todos$ = store.map((state: IState) => state.todos);
        }
    
        addTodo(item: string) {
            // automagically dispatches addTodo-action
            this.todoListRedstore.addTodo(item);
        }
    
        removeTodo(index: number) {
            // automagically dispatches removeTodo-action
            this.todoListRedstore.removeTodo(index);
        }
    
        setName(name: string) {
            // automagically dispatches setName-action
            this.todoListRedstore.setName(name);
        }
    }

Ideas

  1. no boilerplate code

    1. no action-types have to be defined by the developer.

      Type is per convention <className>.<methodName>.

    2. no action-creators, but only payloads.

      @Action() decorated methods only returns the payload.

    3. no dispatch calls.

      @Action() decorated methods magically dispatch the corresponding action.

    4. no state copy and return code, but @StateCopy decorator.

      @StateCopy decorated reducer getting a flatcopy of state as first parameter and shall not return it.

  2. straight reducers

    1. no switch blocks, but invocation of reducers per convention.
    2. directly testable, because just static methods.
    3. optional default reducers, with name setX for @Action decorated methods per default adds a reducer setting x (lower camel-case for X) to the payload.