UNPKG

oidc-client-rx

Version:

ReactiveX enhanced OIDC and OAuth2 protocol support for browser-based JavaScript applications

44 lines (43 loc) 1.65 kB
import { InjectionToken, inject } from "injection-js"; import { fromEventPattern, takeUntil } from "rxjs"; import { DESTORY_REF } from "../../resources/index.js"; import { AbstractRouter, ROUTER_ABS_PATH_PATTERN } from "../../router/index.js"; const TANSTACK_ROUTER = new InjectionToken("TANSTACK_ROUTER"); class TanStackRouterAdapter { navigateByUrl(url) { this.router.navigate({ href: ROUTER_ABS_PATH_PATTERN.test(url) ? url : `/${url}` }); } getCurrentNavigation() { return { extractedUrl: this.beforeLoadToLocation?.href || this.router.state.location.href }; } constructor(){ this.router = inject(TANSTACK_ROUTER); this.beforeLoadToLocation = null; this.destoryRef$ = inject(DESTORY_REF); fromEventPattern((handler)=>this.router.subscribe("onBeforeLoad", handler), (unsubscribe)=>unsubscribe()).pipe(takeUntil(this.destoryRef$)).subscribe((event)=>{ this.beforeLoadToLocation = event.toLocation; }); fromEventPattern((handler)=>this.router.subscribe("onResolved", handler), (unsubscribe)=>unsubscribe()).pipe(takeUntil(this.destoryRef$)).subscribe(()=>{ this.beforeLoadToLocation = null; }); } } function withTanstackRouter(router) { return { ɵproviders: [ { provide: TANSTACK_ROUTER, useValue: router }, { provide: AbstractRouter, useClass: TanStackRouterAdapter } ] }; } export { TANSTACK_ROUTER, TanStackRouterAdapter, withTanstackRouter };