Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5c78bbf

Browse files
committed
feat(cdk): added provideRxRenderStrategies provider function
Exposed `provideRxRenderStrategies` provider function in order to improve the DX of configuring render strategies
1 parent d12d2db commit 5c78bbf

File tree

2 files changed

+49
-4
lines changed

2 files changed

+49
-4
lines changed

libs/cdk/render-strategies/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ export {
33
RxConcurrentStrategies,
44
} from './lib/concurrent-strategies';
55
export {
6+
provideRxRenderStrategies,
67
RX_RENDER_STRATEGIES_CONFIG,
78
RxRenderStrategiesConfig,
89
} from './lib/config';

libs/cdk/render-strategies/src/lib/config.ts

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { InjectionToken } from '@angular/core';
1+
import { InjectionToken, Provider } from '@angular/core';
22
import { RX_CONCURRENT_STRATEGIES } from './concurrent-strategies';
33
import {
44
RxCustomStrategyCredentials,
@@ -38,9 +38,7 @@ export function mergeDefaultConfig<T extends string>(
3838
): Required<RxRenderStrategiesConfig<T | RxDefaultStrategyNames>> {
3939
const custom: RxRenderStrategiesConfig<T> = cfg
4040
? cfg
41-
: ({
42-
customStrategies: {},
43-
} as any);
41+
: ({ customStrategies: {} } as any);
4442
return {
4543
...RX_RENDER_STRATEGIES_DEFAULTS,
4644
...custom,
@@ -50,3 +48,49 @@ export function mergeDefaultConfig<T extends string>(
5048
},
5149
};
5250
}
51+
52+
/**
53+
* @description
54+
* Can be used to set the default render strategy or create custom render strategies.
55+
*
56+
* With this function you can customize the behavior of:
57+
* - `rxLet` directive
58+
* - `rxFor` directive
59+
* - `rxIf` directive
60+
* - `rxVirtualFor` directive
61+
* - `rxVirtualView` directive
62+
* - `RxStrategyProvider` service.
63+
*
64+
* @example
65+
* import { provideRxRenderStrategies } from '@rx-angular/cdk/render-strategies';
66+
*
67+
* const appConfig: ApplicationConfig = {
68+
* providers: [
69+
* provideRxRenderStrategies({
70+
* primaryStrategy: 'sync',
71+
* customStrategies: {
72+
* sync: {
73+
* name: 'sync',
74+
* work: (cdRef) => { cdRef.detectChanges(); },
75+
* behavior: ({ work }) => (o$) => o$.pipe(tap(() => work()))
76+
* },
77+
* asap: {
78+
* name: 'asap',
79+
* work: (cdRef) => { cdRef.detectChanges(); },
80+
* behavior: ({ work }) => (o$) => o$.pipe(delay(0, asapScheduler), tap(() => work()))
81+
* },
82+
* }),
83+
* ],
84+
* };
85+
*
86+
* @param config - The configuration object.
87+
* @returns A provider that can be used to set the default render strategy or create custom render strategies.
88+
*/
89+
export function provideRxRenderStrategies(
90+
config: RxRenderStrategiesConfig<string>,
91+
): Provider {
92+
return {
93+
provide: RX_RENDER_STRATEGIES_CONFIG,
94+
useValue: mergeDefaultConfig(config),
95+
} satisfies Provider;
96+
}

0 commit comments

Comments
 (0)