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

ngx-store-decorators

v1.5.0

Published

A set of useful decorators for Angular application. It helps to quickly get values from services, get Observables and subscribe to them. Also provides decorators to quickly create a facade between the application and the store.

Downloads

15

Readme

NgxStoreDecorators for Angular Build Status Coverage Status

NPM

NgxStoreDecorators is a set of useful decorators and classes for quickly create NgRx store facades, assigning values, observables or observables values to class properties.

This package introduce:

@StoreSelect(), @StoreSubscribe(), @StoreDispatch() decorators and StoreFacade abstract class for NgRx store maintaining,

@Select(), @Subscribe() decorators and WithSubscriptions abstract class to assign observables or observables values and handle subscriptions,

also @Get() to assign values from injected classes method or properties.

Example

Sample store facade service:

@Injectable()
export class CounterFacadeService extends StoreFacade {
  @StoreSelect(selectors.getCount) 
  public count$: Observable<number>;

  @StoreSelect(selectors.getCountMutliplyBy) 
  public countMultiplyBy$: (multiplyBy: number) => Observable<number>;

  @StoreSubscribe(selectors.getCount) 
  public count: number;

  public constructor(protected store: Store<State>) {
    super();
  }

  @StoreDispatch(actions.CounterSet)
  public counterSet(payload: number) {}
}

without Store decorators and StoreFacade abstract class:

@Injectable()
export class CounterFacadeService {

  public count$: Observable<number>;
  
  public countMultiplyBy$: (multiplyBy: number) => Observable<number>;
  
  public count: number;

  public readonly subscriptions = new Subscription();
  
  public constructor(protected store: Store<State>) {
    this.count$ = this.store.pipe(select(selectors.getCount));
    
    this.countMultiplyBy$ = (multiplyBy: number) => this.store.pipe(select(selectors.getCountMutliplyBy(multiplyBy)));
    
    this.subscriptions.add(
      this.store.pipe(select(selectors.getCount))
      .subscribe(value => this.count = value));
  }

  public counterSet(payload: number): void {
    this.store.dispatch(new actions.CounterSet(payload))
  }
  
  public unsubscribeAll(): void {
    this.subscriptions.unsubscribe();
  }
}

Sample usage in component or service:

@Component({
  selector: 'app-basic-usage',
  templateUrl: './basic-usage.component.html',
  styleUrls: ['./basic-usage.component.scss']
})
export class BasicUsageComponent extends WithSubscriptions implements OnDestroy {
  @Select(CounterFacadeService, 'count$')
  public count$: Observable<number>;

  @Subscribe(CounterFacadeService, 'count$')
  public count: number;
  
  @Get(CounterFacadeService, 'count')
  public getCount: number;
  
  @Get(CounterFacadeService, 'countFunction')
  public getCountFunction: number;

  public constructor(public counterFacadeService: CounterFacadeService) {
    super();
  }

  public ngOnDestroy(): void {
    this.unsubscribeAll();
  }
}

without decorators and WithSubscriptions abstract class:

@Component({
  selector: 'app-basic-usage',
  templateUrl: './basic-usage.component.html',
  styleUrls: ['./basic-usage.component.scss']
})
export class BasicUsageComponent implements OnInit, OnDestroy {
  public count$: Observable<number>;

  public count: number;
  
  public getCount: number;
  
  public getCountFunction: number;
  
  public readonly subscriptions = new Subscription();

  public constructor(public counterFacadeService: CounterFacadeService) {}

  public ngOnInit(): void {
    this.count$ = this.counterFacadeService.count$
    
    this.subscriptions.add(
      this.counterFacadeService.count$
      .subscribe(value => this.count = value)
    )
    
    this.getCount = this.counterFacadeService.count;
    
    this.getCountFunction = this.counterFacadeService.countFunction();
  }
  
  public ngOnDestroy(): void {
    this.unsubscribeAll();
  }
  
  public unsubscribeAll(): void {
    this.subscriptions.unsubscribe();
  }
}

Decorators configuration (optional)

log - [?boolean]

Default is set to false. Set to true if you want to console.log all new values from an observable. Optional for @StoreSelect(), @StoreSubscribe(), @Select(), @Subscribe()

pipe - [?OperatorFunction<any, any>[]]

Here you can pass some RxJs operators. Optional for @StoreSelect(), @StoreSubscribe(), @Select(), @Subscribe()

subscriptionsCollector - [?string]

Default is set to subscriptions witch should be an RxJs Subscription instance. You can extend component or service with StoreFacade or WithSubscriptions abstract classes to automatically add this property to class. Optional for @StoreSubscribe(), @Subscribe()

takeUntil - [?string]

You can handle subscriptions by takeUntil operator. Here you just pass a name of the class property witch should be Subject instance. Optional for @StoreSubscribe(), @Subscribe()

args - [?any[]]

You can resolve injection method with given arguments. Optional for @Select(), @Subscribe(), @get()

Demo

To see decorators and classes usage in real app check DEMO or download git repository then type

 npm install // OR
 yarn
 
 ng serve

Then navigate to http://localhost:4200/.