Giter Club home page Giter Club logo

ngrx-utils's Introduction

@ngrx-utils CircleCI Maintainability codecov Known Vulnerabilities

@ngrx-utils

Reusable logic library for Angular application

Quick start

npm i -S @ngrx-utils/store
# or
yarn add @ngrx-utils/store

What in the box?

routerLinkMatch directive

This directive will give you ability to add a class to the element when router url match a regular expression. The syntax is same with ngClass but replace the true/false expression with your string based regexp (like the string you pass to new RegExp('')).

Example: active-class will be added to a tag when router URL contains this segment: products/12345

<a routerLink="/products"
   [routerLinkMatch]="{
     "active-class": "products/\\d+"
   }"></a>

push pipe

This is a modified version of async pipe in @angular/common package. All the code implement are almost the same but this push pipe will call detectChanges() instead of markForCheck() so your pipe continue to work even in {ngZone: 'noop'} environment.

// main.ts
platformBrowserDynamic()
  .bootstrapModule(AppModule, { ngZone: 'noop' })
  .catch((err) => console.log(err));

// app.module.ts
import { PushPipeModule } from '@ngrx-utils/store';
@NgModule({
  imports: [PushPipeModule],
})
export class AppModule {}
// heavy-compute.component.ts
import { Component, OnInit, NgZone } from '@angular/core';
@Component({
  template: `<h2>Test: {{ test$ | push }}</h2>`,
})
export class HeavyComputeComponent implements OnInit {
  compute() {
    //...heavy computing
  }

  ngOnInit() {
    this.compute();
  }
}

ngLet directive

We often use *ngIf="stream$ | async as stream" to subscribe to an observable property and rename it to a template variable. But with nested template, *ngIf might remove your template which may not be expected.

NgLet to rescue:

import { NgLetModule } from '@ngrx-utils/store';

@NgModule({
  imports: [NgLetModule],
})
export class FeatureModule {}

Replace *ngIf with *ngLet:

<ng-container *ngLet="(filterDate$ | async) as filterDate">
  <pick-date
    [registeredAt]="(device$ | async)?.registeredAt"
    [firstDate]="filterDate?.from"
    [secondDate]="filterDate?.to"
  ></pick-date>
</ng-container>

*ngLet just hold a reference to the result of async pipe in a template variable and don't have any special logic like structure directives such as *ngIf or *ngFor so it run faster and very handy.

You can also subscribe to multiple observable separately with *ngLet like this:

<ng-container
  *ngLet="{
        device: device$ | async,
        date: filterDate$ | async
      } as options"
>
  <pick-date
    [registeredAt]="options.device?.registeredAt"
    [firstDate]="options.date?.from"
    [secondDate]="options.date?.to"
  ></pick-date>
</ng-container>

Actually this is an feature request in angular for quite long time as described in here but not yet been accepted.

untilDestroy pipeable operator

Have you ever feel odd when have to repeat calling unsubscribe with subscriptions in ngOnDestroy, or creating a Subject property, add takeUntil() to subscription, call next() in ngOnDestroy?

With untilDestroy pipeable operator:

import { untilDestroy } from '@ngrx-utils/store';

export class MyComponent implements OnDestroy {
  user: User;

  constructor(userService: UserService) {
    userService
      .getUsers()
      /** Automatically unsubscribe on destroy */
      .pipe(untilDestroy(this))
      .subscribe((user) => (this.user = user));
  }

  /** Must have */
  ngOnDestroy() {}
}

NOTE: You still have to declare ngOnDestroy in Component because Angular does not support dynamically add component method in AOT mode

Credit to @SanderElias, this operator is inspired from his idea but he's currently not publishing it as an npm package.

How to contribute

  • Fork this repo
  • Add your awesome feature and include it in the top level export
  • Send a PR here and describe some use cases.

ngrx-utils's People

Contributors

dependabot[bot] avatar nigrosimone avatar sandangel avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

ngrx-utils's Issues

angular 8

Is there any Angular 8 upgrade path?

Here is the error I am getting when running ng update @angular/cli @angular/core @ngrx/[email protected].

Package "@ngrx-utils/store" has an incompatible peer dependency to "@ngrx/store" (requires "^6.0.0-beta.1", would install "8.0.0-rc.0").
Package "@ngrx-utils/store" has an incompatible peer dependency to "@angular/common" (requires "^6.0.0 || ^6.0.0-rc.0" (extended), would install "8.0.0").
Package "@ngrx-utils/store" has an incompatible peer dependency to "@angular/core" (requires "^6.0.0 || ^6.0.0-rc.0" (extended), would install "8.0.0").

Feature Request: update to angular 10

./node_modules/@ngrx-utils/store/__ivy_ngcc__/fesm2015/ngrx-utils-store.js
Module Warning (from ./node_modules/source-map-loader/dist/cjs.js):
Failed to parse source map: "ng:/@ngrx-utils/store/lib/directives/routerLinkMatch.ts" URL is not supported

Updating to ng 10 can remove this warning.

split ngrx-utils, so that there will be no dependency on @ngrx/store

I am using ngxs but like to use ngLet, untilDestroy etc
today we ngrx-utils lib has hard dependency on @ngrx/store . would be nice to be able to use ngrx-utils without installing @ngrx/store

 「wdm」: Failed to compile.
ERROR in node_modules/@ngrx-utils/store/src/decorators/dispatch.d.ts(1,31): error TS2307: Cannot find module '@ngrx/store'.
node_modules/@ngrx-utils/store/src/decorators/ngrx-select.d.ts(1,23): error TS2307: Cannot find module '@ngrx/store'.
node_modules/@ngrx-utils/store/src/decorators/select.d.ts(1,26): error TS2307: Cannot find module '@ngrx/store'.

RxJS 6 support?

Hi, .thanks for a great library.

Now my question: the current version breaks on RxJS6, because of exported symbols being moved in RxJS.

Yes we can use rxjs-compat but it would be nice if ngrx-utils supported rxjs6 out of the box.

Any ideas?

Cheers

ERROR in ./node_modules/rxjs/operators/takeUntil.js
Module not found: Error: Can't resolve 'rxjs-compat/operators/takeUntil' in '/Users/spock/project/dba/test/frontend/node_modules/rxjs/operators'
 @ ./node_modules/rxjs/operators/takeUntil.js 6:9-51
 @ ./node_modules/@ngrx-utils/store/release/@ngrx-utils/store.es5.js

@Dispatch with array of action doesn't trigger.

Versions :
"@ngrx-utils/store": "^0.10.0",
"@ngrx/effects": "^5.2.0",
"@ngrx/entity": "^5.2.0",
"@ngrx/router-store": "^5.2.0",
"@ngrx/store": "^5.2.0",
"@ngrx/store-devtools": "^5.2.0",

How to reproduce :
@dispatch()
navToAll() {
return new Navigate(['/all']);
}
^ this is working well (or any other valid action)

@dispatch()
navToAll() {
return [new Navigate(['/all'])];
}
^ this produces an error :

ERROR TypeError: Unexpected action in method return type, expected object of type 'Action'

I will try to dig deeper in the code soon.

Redux Dev Tools

Better type inference with ofAction pipeable operator and use instanceof to filter action instead of call new Action().type. You won't have to use type cast.

The reason why I did this is because it won't work w/ Redux Dev Tools for replay.

grid-layout

implement a flex-layout like package but using grid mechanism instead.

  • Investigate angular/cdk/layout
  • Design API
  • Implement

Select doesnt work with HMR

@select() doesnt work with HMR after hot reload. Observable is NULL (works when manual assignment)

DOESNT work after HMR hot reload:

 @Select(data)
  data$: Observable<any>;

Works

 data$: Observable<any>;
 this.data$ = store.select(data);

i rly love your work, however if selecr doesnt work in HMR, it is for too big price

"Export select was not found in ngrx/store"

Getting the following errors when trying to install ngrx-utils:

(Angular 6.0.0-beta.1, Typescript 2.6.2):

`WARNING in ./node_modules/@ngrx-utils/store/release/@ngrx-utils/store.es5.js
128:40-46 "export 'select' was not found in '@ngrx/store'
@ ./node_modules/@ngrx-utils/store/release/@ngrx-utils/store.es5.js
@ ./src/app/main/app.module.ts
@ ./src/app/index.ts
@ multi (webpack)-dev-server/client?http://0.0.0.0:8080 ./src/app/index.ts

WARNING in ./node_modules/@ngrx-utils/store/release/@ngrx-utils/store.es5.js
78:56-62 "export 'select' was not found in '@ngrx/store'
@ ./node_modules/@ngrx-utils/store/release/@ngrx-utils/store.es5.js
@ ./src/app/main/app.module.ts
@ ./src/app/index.ts
@ multi (webpack)-dev-server/client?http://0.0.0.0:8080 ./src/app/index.ts`

Support for Angular 9

Is this project still alive? Its peer dependencies include Angular up to version 8, so trying to use it with Angular 9 gives an error

bug routerLinkMatch: does not apply.

list of link changed after router.events has emitted will lead to routerLinkMatch won't apply active class.

should change to routerLinkActive implementation to ensure apply active class at the last of the event queue.

*ngLet for multiple observables not working with *ngIf

When I access multiple async pipes in ngLet, and then check them in ngIf, the component is drawn and value subscrbed is undefined.

if I write it without ngLet, everything works fine.

Is it bug, or am I doing something wrong?

Both cases:

  <ng-container *ngLet="{ all: (rolesList$ | async), user: (userRoles$ | async) } as roles">
    <pa-user-details-roles-form
      #roles
      *ngIf="roles.all && roles.user"
      [userRoles]="roles.user"
      [allRoles]="roles.all"
      [editable$]="editable$"
    ></pa-user-details-roles-form>
  </ng-container>
</div>   

--> allRoles: undefined




<div class="row ">
  <pa-user-details-roles-form
    #roles
    *ngIf="(rolesList$ | async) && (userRoles$ | async)"
    [userRoles]="userRoles$ | async"
    [allRoles]="rolesList$ | async"
    [editable$]="editable$"
  ></pa-user-details-roles-form>
</div>

--> all good

abstract gulp tools to an standalone package

abstract gulp tool to a standalone package which will have:

  • Predefined tasks to run in development: CI, testing, deploy, publish...
  • Config file to declare peer angular/ngrx deps.

updates

What's different with ngrx-actions
No need reflect-metadata as a dependency

Not true anymore ;P

Also, it would be appreciated to give some credit ;)

ofAction: deprecated

due to violation of redux principle (serialize / deserialize), ofAction operator will be deprecated and soon be removed when v1.0 release

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.