Skip to content

Instantly share code, notes, and snippets.

View nathan-lapinski's full-sized avatar
💭
♦️s and 🦀

Nate Lapinski nathan-lapinski

💭
♦️s and 🦀
View GitHub Profile
const INITIAL_VALUE = Symbol('INITIAL_VALUE');
declare type INTERIM_VALUES = typeof INITIAL_VALUE | boolean | UrlTree;
export function prioritizedGuardValue():
OperatorFunction<Observable<boolean|UrlTree>[], boolean|UrlTree> {
return switchMap(obs => {
return combineLatest(
...obs.map(o => o.pipe(take(1), startWith(INITIAL_VALUE as INTERIM_VALUES))))
.pipe(
scan(
@nathan-lapinski
nathan-lapinski / router.ts
Created December 2, 2018 11:30
excerpt of router.ts demonstrating how switchMap is used to cancel and cleanup pending navigations
private setupNavigations(transitions: Observable<NavigationTransition>): Observable<NavigationTransition> {
return transitions.pipe(
filter(t => t.id !== 0),
// Extract URL
map(t => ({...t, extractedUrl: this.urlHandlingStrategy.extract(t.rawUrl)}) as NavigationTransition),
// Using switchMap so we cancel executing navigations when a new one comes in
switchMap(t => {
@nathan-lapinski
nathan-lapinski / guard_priority.ts
Created November 27, 2018 09:45
An example showing guard priority for canActivate
{ path: 'redir',
canActivate: [CanActivateRouteGuard, CanActivateRouteGuard2],
children: [{
path: 'dir',
component: NeverGetHereComponent,
canActivate: [ChildCanActivateRouteGuard]
}]
}
@nathan-lapinski
nathan-lapinski / app.module.ts
Created November 24, 2018 04:59
minimal routes
const ROUTES: Route[] = [
{ path: 'target', component: TargetComponent },
{ path: 'redir', component: NeverGetHereComponent, canActivate: [CanActivateRouteGuard] }
];
@nathan-lapinski
nathan-lapinski / new_guard.ts
Created November 23, 2018 15:42
example guard which returns a UrlTree
import { Injectable } from '@angular/core';
import { CanActivate,
ActivatedRouteSnapshot,
RouterStateSnapshot,
Router,
UrlTree } from '@angular/router';
@Injectable()
export class CanActivateRouteGuard implements CanActivate {
@nathan-lapinski
nathan-lapinski / pipe.ts
Last active January 3, 2019 04:35
pipe example
const pipe = (...fns) => initialVal => fns.reduce((g,f) => f(g), initialVal);
const add1 = x => x + 1;
const mul2 = x => x * 2;
const res = pipe(add1,mul2)(0); // mul2(add1(0)) === 2
subscribe(observerOrNext?: PartialObserver<T> | ((value: T) => void),
error?: (error: any) => void,
complete?: () => void): Subscription {
const { operator } = this;
const sink = toSubscriber(observerOrNext, error, complete);
if (operator) {
operator.call(sink, this.source);
} else {
@nathan-lapinski
nathan-lapinski / router_preloader.ts
Created November 10, 2018 14:07
preloading on nav
setUpPreloading(): void {
this.subscription =
this.router.events
.pipe(filter((e: Event) => e instanceof NavigationEnd), concatMap(() => this.preload()))
.subscribe(() => {});
}
@nathan-lapinski
nathan-lapinski / preloading.ts
Created November 10, 2018 13:59
specifying a preloading strategy
RouterModule.forRoot(ROUTES, {
preloadingStrategy: CustomPreloadingStrategy | PreloadAllModules | NoPreloading
})
@nathan-lapinski
nathan-lapinski / apply_redirects.ts
Last active November 10, 2018 13:48
merging a loaded router configuration
return this.configLoader.load(ngModule.injector, route)
.pipe(map((cfg: LoadedRouterConfig) => {
route._loadedConfig = cfg;
return cfg;
}));