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

ngrx-correlation-id

v1.1.4

Published

NGRX Store feature to track tasks via correlation id

Downloads

29

Readme

ngrx-correlation-id

npm version CircleCI Coverage Status

A ngrx store library to track an asynchronous activity such as a http request, a ngrx effect or anything else.

Supports

  • Angular 13 and ngrx/store@13
  • Angular 12 and ngrx/store@12
  • Angular 11 and ngrx/store@11
  • Angular 10 and ngrx/store@10
  • Angular 9 and ngrx/store@9
  • Angular 8 and ngrx/store@8
  • Angular 7 and ngrx/store@7
  • Angular 6 and ngrx/store@6

API short list

Please check source code

  • NgrxCorrelationIdModule
  • CidTask
  • cidTask(correlationId, Observable)
  • cidWait()
  • store.select(selectCid, correlationId)
  • cidStart(correlationId: string)
  • cidPayload(correlationId: string, payload: any)
  • cidEnd(correlationId: string)
  • cidRemove(correlationId: string)

How to use

Import NgrxCorrelationIdModule in the same module where you import StoreModule.forRoot.

import {NgrxCorrelationIdModule} from 'ngrx-correlation-id';

@NgModule({
    imports: [
        StoreModule.forRoot(/* ... */),
        NgrxCorrelationIdModule, // <- import it here
    ],
})
export class AppModule {}

Add cid: string to props of an action you want to track.

export const loadUsers = createAction(
  '[User] Load Users',
  props<{cid: string}>(), // <- correlation id
);

Wrap an effect pipe with cidTask. The first argument is a cid of the current task. The second argument is a stream that should be tracked.

Optionally you can dispatch cidPayload action with a custom payload. In this case we want our payload to be an array of ids of users.

import {cidPayload, cidTask} from 'ngrx-correlation-id';

@Injectable()
export class UsersEffects {
    @Effect()
    public readonly loadUsers$ = this.actions$.pipe(
        ofType(loadUsers),
        switchMap(({cid}) => cidTask(cid, this.http.get<Array<User>>('v2/api/users').pipe(
            switchMap(users => of(
                upsertUsers({items: users}),
                cidPayload({cid, payload: users.map(user => user.id)}),
            )),
        ))),
    );

    constructor(
        protected readonly actions$: Actions,
        private readonly http: HttpClient,
    ) {}
}

Update a component to use cid and all the features of the lib.

import {cidRemove, CidTask, cidWait, selectCid} from 'ngrx-correlation-id';

export class EntityComponent implements OnInit, OnDestroy {
    public readonly users$: Observable<Array<User>>;

    // set here an unique value to distinguish this task from others.
    private readonly cid: string = `randomString`;

    constructor(private readonly store: Store) {
        // selecting the related task that belongs to cid.
        this.users$ = this.store.select<CidTask<Array<string>>>(selectCid, this.cid).pipe(

            // doesn't emit until the task isn't completed
            cidWait(), // waits until the task starts, then waits until the task ends and the task.

            map(task => task.payload),
            withLatestFrom(this.store.select(selectUsers)),
            map(([ids, users]) => users && users.filter(user => ids.indexOf(user.id) !== -1) || []),
        );
    }

    public ngOnInit(): void {
        // dispatching an action to load users.
        this.store.dispatch(loadUsers({cid: this.cid}));
    }

    public ngOnDestroy(): void {
        // clearing payload and selector
        this.store.dispatch(cidRemove({cid: this.cid}));
        selectCid.release();
    }
}