Skip to content

About

Legacy @select, @select$, @SelectSignal, @dispatch and @Substore decorators for the @angular-redux2/store

Resources

Contributing

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

angular-redux2/decorators

Documentation npm version downloads License: MPL 2.0 CI Discord Ask DeepWiki

@angular-redux2/decorators brings the 5.x @Select, @Select$, @SelectSignal, @Dispatch, and @Substore decorators back on top of the @angular-redux2/store 6 signal store. Decorated components keep working after the upgrade, and you move them to the signal APIs one at a time.

Key Features

  • Change one import: swap @angular-redux2/store for @angular-redux2/decorators in the import, and a 5.x component compiles and runs on version 6.
  • Side by side with signals: a decorated member and an injectSelect selection share one store, so both styles can live in one application, or in one component.
  • Free after the first read: a decorated property builds its selection once and then becomes a plain property, so a template that reads it on every change detection pass pays for the decorator once.
  • No global store: provideNgReduxDecorators() hands the store of the application to the decorators and takes it back when the application is destroyed, so one test never reads the store of another.
  • Sub-stores that follow the path: @Substore creates one sub-store per instance whose path follows basePath(), and releases it in ngOnDestroy.
  • Testing mocks: add the provider after provideMockNgRedux, and the decorators read the mock.

Installation

npm install @angular-redux2/decorators

Requires @angular-redux2/store 6, Angular 20 or newer, RxJS 7.5 or newer, and Redux 5.

The decorators define getters on the class prototype, so they need this in tsconfig.json:

{
    "compilerOptions": {
        "experimentalDecorators": true,
        "useDefineForClassFields": false
    }
}

Without "useDefineForClassFields": false, the class field hides the getter, and every decorated property silently reads undefined.

Usage

Providing the store

import { provideNgRedux } from '@angular-redux2/store';
import { provideNgReduxDecorators } from '@angular-redux2/decorators';
import { type ApplicationConfig, isDevMode } from '@angular/core';

export const appConfig: ApplicationConfig = {
    providers: [
        provideNgRedux({ reducer: rootReducer, devTools: isDevMode() }),
        provideNgReduxDecorators()
    ]
};

Reading and writing state

import type { Observable } from 'rxjs';
import type { Signal } from '@angular/core';

import { Component } from '@angular/core';
import { Dispatch, Select, SelectSignal } from '@angular-redux2/decorators';

@Component({
    selector: 'app-counter',
    template: '<button (click)="increment()">{{ count$ | async }} / {{ name() }}</button>'
})
export class CounterComponent {
    @Select('counter') readonly count$: Observable<number>;
    @SelectSignal([ 'user', 'name' ]) readonly name: Signal<string>;

    @Dispatch
    increment() {
        return { type: 'INCREMENT' };
    }
}

Working on one branch

import { Dispatch, Select, Substore } from '@angular-redux2/decorators';

@Component({ template: '{{ loc$ | async }}' })
@Substore(userReducer)
export class UserComponent {
    @Input() userId: string;
    @Select('loc') readonly loc$: Observable<number>;

    basePath = () => this.userId ? [ 'users', this.userId ] : null;

    @Dispatch
    addCode(lines: number) {
        return { type: 'ADD_LOC', payload: lines };
    }
}
Decorator Does
@Select An observable of a key, a path, or a function of the state
@Select$ A selection piped through an RxJS transformer
@SelectSignal A signal of a key, a path, or a function of the state
@Dispatch Dispatches the return value of a method, unless it returns false
@Substore Scopes the members of a class to a sub-store at basePath()

Migrating off the decorators

This package is a bridge. Write new components with injectSelect, injectSubstore, and inject(NgRedux), and once the last decorated component is migrated, remove the package, the provider, and "useDefineForClassFields": false.

See Migrating from 5.x for why the decorators left the core and a side-by-side guide for each one.

Documentation

Full guides and the complete API reference live at angular-redux2.github.io/decorators.

Contributing

Contributions are welcome!
Please see our Contributing Guide for details.

Links

Documentation, GitHub Repository, Issue Tracker, npm Package

License

This project is licensed under the Mozilla Public License 2.0 - see the LICENSE file for details.

About

Legacy @select, @select$, @SelectSignal, @dispatch and @Substore decorators for the @angular-redux2/store

Resources

Contributing

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages