Skip to content

Instantly share code, notes, and snippets.

@dcagnetta
Last active April 13, 2021 00:35
Show Gist options
  • Save dcagnetta/8bcccaaa9cee1bd0f19603ea5373dc3e to your computer and use it in GitHub Desktop.
Save dcagnetta/8bcccaaa9cee1bd0f19603ea5373dc3e to your computer and use it in GitHub Desktop.
ANGULAR STREAMLINE PROVIDERS
// token to access a stream with the organisation information we need
// this is what we inject e.g. @Inject(ORG_INFO) readonly orgInfo$: Observable<string>
export const ORG_INFO = new InjectionToken<Observable<string>>(
'Organisation name information'
);
// This is what we add to `providers` array in module or component
export const ORGANISATION_PROVIDERS: Provider[] = [
{
provide: ORG_INFO,
deps: [ActivatedRoute, OrgService],
useFactory: orgFactory,
},
];
/**
* @summary extracts org Id from URL and then returns the organisation name
* */
export function orgFactory(
{ params }: ActivatedRoute,
orgService: OrgService): Observable<string> {
return params.pipe(
switchMap(({orgId}) => {
return orgService.getOrganisation(orgId)
.pipe(
map((organisation: Organisation) => organisation.name)
);
})
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment