-
Notifications
You must be signed in to change notification settings - Fork 672
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(abc:reuse-tab): add provide (#1707)
- Loading branch information
Showing
14 changed files
with
179 additions
and
122 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import { | ||
ENVIRONMENT_INITIALIZER, | ||
EnvironmentProviders, | ||
Provider, | ||
inject, | ||
makeEnvironmentProviders | ||
} from '@angular/core'; | ||
import { RouteReuseStrategy } from '@angular/router'; | ||
|
||
import { REUSE_TAB_CACHED_MANAGER, ReuseTabCachedManagerFactory } from './reuse-tab.cache'; | ||
import { ReuseTabMatchMode, ReuseTabRouteParamMatchMode } from './reuse-tab.interfaces'; | ||
import { ReuseTabService } from './reuse-tab.service'; | ||
import { REUSE_TAB_STORAGE_KEY, REUSE_TAB_STORAGE_STATE, ReuseTabLocalStorageState } from './reuse-tab.state'; | ||
import { ReuseTabStrategy } from './reuse-tab.strategy'; | ||
|
||
export enum ReuseTabFeatureKind { | ||
CacheManager, | ||
Store | ||
} | ||
|
||
export interface ReuseTabFeature<KindT extends ReuseTabFeatureKind> { | ||
ɵkind: KindT; | ||
ɵproviders: Provider[]; | ||
} | ||
|
||
function makeFeature<KindT extends ReuseTabFeatureKind>(kind: KindT, providers: Provider[]): ReuseTabFeature<KindT> { | ||
return { | ||
ɵkind: kind, | ||
ɵproviders: providers | ||
}; | ||
} | ||
|
||
/** | ||
* Configures reuse-tab to be available for injection. | ||
* | ||
* @see {@link withLocalStorage} | ||
* @see {@link withCacheManager} | ||
*/ | ||
export function provideReuseTabConfig(options?: { | ||
debug?: boolean; | ||
mode?: ReuseTabMatchMode; | ||
routeParamMatchMode?: ReuseTabRouteParamMatchMode; | ||
max?: number; | ||
excludes?: RegExp[]; | ||
storeKey?: string; | ||
cacheManager?: ReuseTabFeature<ReuseTabFeatureKind.CacheManager>; | ||
store?: ReuseTabFeature<ReuseTabFeatureKind.Store>; | ||
}): EnvironmentProviders { | ||
const providers: Provider[] = [ | ||
{ | ||
provide: REUSE_TAB_STORAGE_KEY, | ||
useValue: options?.storeKey ?? '_reuse-tab-state' | ||
}, | ||
(options?.cacheManager ?? withCacheManager()).ɵproviders, | ||
(options?.store ?? withLocalStorage()).ɵproviders, | ||
{ | ||
provide: RouteReuseStrategy, | ||
useClass: ReuseTabStrategy, | ||
deps: [ReuseTabService] | ||
}, | ||
{ | ||
provide: ENVIRONMENT_INITIALIZER, | ||
multi: true, | ||
useValue: () => { | ||
const srv = inject(ReuseTabService); | ||
if (options?.debug) srv.debug = options.debug; | ||
if (options?.mode) srv.mode = options.mode; | ||
if (options?.routeParamMatchMode) srv.routeParamMatchMode = options.routeParamMatchMode; | ||
if (options?.max) srv.max = options.max; | ||
if (options?.excludes) srv.excludes = options.excludes; | ||
} | ||
} | ||
]; | ||
|
||
return makeEnvironmentProviders(providers); | ||
} | ||
|
||
export function withCacheManager(): ReuseTabFeature<ReuseTabFeatureKind.CacheManager> { | ||
return makeFeature(ReuseTabFeatureKind.CacheManager, [ | ||
{ | ||
provide: REUSE_TAB_CACHED_MANAGER, | ||
useFactory: () => new ReuseTabCachedManagerFactory() | ||
} | ||
]); | ||
} | ||
|
||
export function withLocalStorage(): ReuseTabFeature<ReuseTabFeatureKind.Store> { | ||
return makeFeature(ReuseTabFeatureKind.Store, [ | ||
{ | ||
provide: REUSE_TAB_STORAGE_STATE, | ||
useFactory: () => new ReuseTabLocalStorageState() | ||
} | ||
]); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.