Skip to content

Commit

Permalink
feat(product-composite): create injection tokens with factory (#3256)
Browse files Browse the repository at this point in the history
  • Loading branch information
griest024 authored Oct 14, 2024
1 parent aba565c commit b5e8729
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 55 deletions.
2 changes: 1 addition & 1 deletion libs/product-composite/routing/src/config/public_api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ export { daffProductCompositeRoutingConfigDefaultFactory } from './default';
export { DaffProductCompositeRoutingConfig } from './interface';
export {
DAFF_PRODUCT_COMPOSITE_ROUTING_CONFIG,
daffProductCompositeRoutingProvideConfig,
daffProvideProductCompositeRoutingConfig,
} from './token';
26 changes: 17 additions & 9 deletions libs/product-composite/routing/src/config/token.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,33 @@
import {
FactoryProvider,
inject,
InjectionToken,
} from '@angular/core';

import { DaffBase64ServiceToken } from '@daffodil/core';
import {
createSingleInjectionToken,
DaffBase64ServiceToken,
} from '@daffodil/core';

import { daffProductCompositeRoutingConfigDefaultFactory } from './default';
import { DaffProductCompositeRoutingConfig } from './interface';

/**
* The token used to provide @daffodil/product-composite/routing config data.
*/
export const DAFF_PRODUCT_COMPOSITE_ROUTING_CONFIG = new InjectionToken<DaffProductCompositeRoutingConfig>('DAFF_PRODUCT_COMPOSITE_ROUTING_CONFIG', {
factory: () => daffProductCompositeRoutingConfigDefaultFactory(inject(DaffBase64ServiceToken)),
});
export const {
/**
* The token used to provide @daffodil/product-composite/routing config data.
*/
token: DAFF_PRODUCT_COMPOSITE_ROUTING_CONFIG,
} = createSingleInjectionToken<DaffProductCompositeRoutingConfig>(
'DAFF_PRODUCT_COMPOSITE_ROUTING_CONFIG',
{
factory: () =>
daffProductCompositeRoutingConfigDefaultFactory(inject(DaffBase64ServiceToken)),
},
);

/**
* Provides the {@link DAFF_PRODUCT_COMPOSITE_ROUTING_CONFIG} token.
*/
export function daffProductCompositeRoutingProvideConfig(config: Partial<DaffProductCompositeRoutingConfig>): FactoryProvider {
export function daffProvideProductCompositeRoutingConfig(config: Partial<DaffProductCompositeRoutingConfig>): FactoryProvider {
return {
provide: DAFF_PRODUCT_COMPOSITE_ROUTING_CONFIG,
useFactory: () => ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { DaffCompositeProductApplyOption } from '@daffodil/product-composite/sta
import { DaffCompositeProductFactory } from '@daffodil/product-composite/testing';

import { DaffProductCompositePageEffects } from './product-page.effects';
import { daffProductCompositeRoutingProvideConfig } from '../config/public_api';
import { daffProvideProductCompositeRoutingConfig } from '../config/public_api';
import { DaffProductCompositeQueryParamService } from '../services/query-param.service';

@Component({ template: '' })
Expand All @@ -39,7 +39,7 @@ describe('@daffodil/product-composite/routing | DaffProductCompositePageEffects'
providers: [
DaffProductCompositePageEffects,
provideMockActions(() => actions$),
daffProductCompositeRoutingProvideConfig({
daffProvideProductCompositeRoutingConfig({
compositeSelectionQueryParam: queryParam,
}),
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
DaffCompositeProduct,
DaffCompositeProductItemOption,
} from '@daffodil/product-composite';
import { daffProductCompositeRoutingProvideConfig } from '@daffodil/product-composite/routing';
import { daffProvideProductCompositeRoutingConfig } from '@daffodil/product-composite/routing';
import {
DaffCompositeProductStateTestingModule,
MockDaffCompositeProductFacade,
Expand All @@ -39,7 +39,7 @@ describe('@daffodil/product-composite/routing | DaffProductCompositeRoutingShare
DaffCompositeProductStateTestingModule,
],
providers: [
daffProductCompositeRoutingProvideConfig({
daffProvideProductCompositeRoutingConfig({
compositeSelectionQueryParam: 'queryParam',
}),
],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,39 +1,28 @@
import {
InjectionToken,
Provider,
} from '@angular/core';
import { ActionReducer } from '@ngrx/store';

import { createMultiInjectionToken } from '@daffodil/core';

import { DaffCompositeProductReducersState } from '../composite-product-reducers-state.interface';

/**
* A token to hold the injectable extra reducers.
*
* Prefer using {@link daffProductCompositeProvideExtraReducers}.
*/
export const DAFF_PRODUCT_COMPOSITE_EXTRA_REDUCERS = new InjectionToken<ActionReducer<DaffCompositeProductReducersState>[]>(
'DAFF_PRODUCT_COMPOSITE_EXTRA_REDUCERS',
{ factory: () => []},
);
export const {
/**
* A token to hold the injectable extra reducers.
*
* Prefer using {@link daffProductCompositeProvideExtraReducers}.
*/
token: DAFF_PRODUCT_COMPOSITE_EXTRA_REDUCERS,

/**
* Provides additional reducers that run after the standard Daffodil cart reducers.
*
* ```ts
* providers: [
* ...daffProductCompositeProvideExtraReducers(
* myReducer1,
* myReducer2
* )
* ]
* ```
*/
export function daffProductCompositeProvideExtraReducers(
...reducers: ActionReducer<Partial<DaffCompositeProductReducersState>>[]
): Provider[] {
return reducers.map(reducer => ({
provide: DAFF_PRODUCT_COMPOSITE_EXTRA_REDUCERS,
useValue: reducer,
multi: true,
}));
}
/**
* Provides additional reducers that run after the standard Daffodil cart reducers.
*
* ```ts
* providers: [
* ...daffProductCompositeProvideExtraReducers(
* myReducer1,
* myReducer2
* )
* ]
* ```
*/
provider: daffProductCompositeProvideExtraReducers,
} = createMultiInjectionToken<ActionReducer<DaffCompositeProductReducersState>>('DAFF_PRODUCT_COMPOSITE_EXTRA_REDUCERS');
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,23 @@ import {
combineReducers,
} from '@ngrx/store';

import { createSingleInjectionToken } from '@daffodil/core';
import { daffComposeReducers } from '@daffodil/core/state';

import { DAFF_PRODUCT_COMPOSITE_EXTRA_REDUCERS } from './extra.token';
import { daffCompositeProductReducers } from '../composite-product-reducers';
import { DaffCompositeProductReducersState } from '../composite-product-reducers-state.interface';

/**
* An internal token to hold the Daffodil composite product reducers.
* Includes the extra and standard reducers.
*
* @docs-private
*/
export const DAFF_PRODUCT_COMPOSITE_REDUCERS = new InjectionToken<ActionReducer<DaffCompositeProductReducersState>>(
export const {
/**
* An internal token to hold the Daffodil composite product reducers.
* Includes the extra and standard reducers.
*
* @docs-private
*/
token: DAFF_PRODUCT_COMPOSITE_REDUCERS,
provider: daffProvideProductCompositeReducers,
} = createSingleInjectionToken<ActionReducer<DaffCompositeProductReducersState>>(
'DAFF_PRODUCT_COMPOSITE_REDUCERS',
{
factory: () => daffComposeReducers([
Expand Down

0 comments on commit b5e8729

Please sign in to comment.