UNPKG

oidc-client-rx

Version:

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

45 lines (44 loc) 1.77 kB
import { InjectionToken, inject } from "@outposts/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() { var _this_beforeLoadToLocation; return { extractedUrl: (null == (_this_beforeLoadToLocation = this.beforeLoadToLocation) ? void 0 : _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 };