Skip to content

Commit

Permalink
feat(customer): create injection tokens with factory (#3241)
Browse files Browse the repository at this point in the history
  • Loading branch information
griest024 authored Oct 14, 2024
1 parent 615ccbc commit bd0421a
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 66 deletions.
17 changes: 11 additions & 6 deletions libs/customer/driver/src/interfaces/address-service.interface.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
import { InjectionToken } from '@angular/core';
import { Observable } from 'rxjs';

import { DaffIdentifiable } from '@daffodil/core';
import {
DaffIdentifiable,
createSingleInjectionToken,
} from '@daffodil/core';
import { DaffCustomerAddress } from '@daffodil/customer';

/**
* An injection token for the customer driver.
*/
export const DaffCustomerAddressDriver = new InjectionToken<DaffCustomerAddressDriverInterface>('DaffCustomerAddressDriver');
export const {
/**
* An injection token for the customer driver.
*/
token: DaffCustomerAddressDriver,
provider: daffProvideCustomerAddressDriver,
} = createSingleInjectionToken<DaffCustomerAddressDriverInterface>('DaffCustomerAddressDriver');

/**
* The customer driver is responsible for loading customers.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { InjectionToken } from '@angular/core';
import { Observable } from 'rxjs';

import { createSingleInjectionToken } from '@daffodil/core';
import { DaffCustomer } from '@daffodil/customer';

/**
* An injection token for the customer driver.
*/
export const DaffCustomerDriver = new InjectionToken<DaffCustomerDriverInterface>('DaffCustomerDriver');
export const {
/**
* An injection token for the customer driver.
*/
token: DaffCustomerDriver,
provider: daffProvideCustomerDriver,
} = createSingleInjectionToken<DaffCustomerDriverInterface>('DaffCustomerDriver');

/**
* The customer driver is responsible for loading customers.
Expand Down
16 changes: 9 additions & 7 deletions libs/customer/state/src/injection-tokens/error-matcher.token.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { InjectionToken } from '@angular/core';

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

/**
* Transforms `DaffError`s into `DaffStateError`s before they are serialized into state.
* Can be used to further refine Daffodil errors into more specific app errors.
*/
export const DAFF_CUSTOMER_ERROR_MATCHER = new InjectionToken<typeof daffTransformErrorToStateError>(
export const {
/**
* Transforms `DaffError`s into `DaffStateError`s before they are serialized into state.
* Can be used to further refine Daffodil errors into more specific app errors.
*/
token: DAFF_CUSTOMER_ERROR_MATCHER,
provider: daffProvideCustomerErrorMatcher,
} = createSingleInjectionToken<typeof daffTransformErrorToStateError>(
'DAFF_CUSTOMER_ERROR_MATCHER',
{ factory: () => daffTransformErrorToStateError },
);
2 changes: 1 addition & 1 deletion libs/customer/state/src/injection-tokens/public_api.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export { DAFF_CUSTOMER_ERROR_MATCHER } from './error-matcher.token';
export * from './error-matcher.token';
61 changes: 25 additions & 36 deletions libs/customer/state/src/reducers/token/extra.token.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,31 @@
import {
InjectionToken,
Provider,
} from '@angular/core';
import { ActionReducer } from '@ngrx/store';

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

import { DaffCustomerReducersState } from '../reducers.interface';

/**
* A token to hold the injectable extra reducers.
*
* Prefer using {@link daffCustomerProvideExtraReducers}.
*/
export const DAFF_CUSTOMER_EXTRA_REDUCERS = new InjectionToken<ActionReducer<DaffCustomerReducersState>[]>(
export const {
/**
* A token to hold the injectable extra reducers.
*
* Prefer using {@link daffCustomerProvideExtraReducers}.
*/
token: DAFF_CUSTOMER_EXTRA_REDUCERS,

/**
* Provides additional reducers that run after the standard Daffodil customer reducers.
*
* ```ts
* providers: [
* ...daffCustomerProvideExtraReducers(
* myReducer1,
* myReducer2
* )
* ]
* ```
*/
provider: daffCustomerProvideExtraReducers,
} = createMultiInjectionToken<ActionReducer<DaffCustomerReducersState>>(
'DAFF_CUSTOMER_EXTRA_REDUCERS',
{
factory: () => [],
providedIn: 'any',
},
{ providedIn: 'any' },
);

/**
* Provides additional reducers that run after the standard Daffodil customer reducers.
*
* ```ts
* providers: [
* ...daffCustomerProvideExtraReducers(
* myReducer1,
* myReducer2
* )
* ]
* ```
*/
export function daffCustomerProvideExtraReducers(
...reducers: ActionReducer<DaffCustomerReducersState>[]
): Provider[] {
return reducers.map(reducer => ({
provide: DAFF_CUSTOMER_EXTRA_REDUCERS,
useValue: reducer,
multi: true,
}));
}
23 changes: 12 additions & 11 deletions libs/customer/state/src/reducers/token/reducers.token.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import {
inject,
InjectionToken,
} from '@angular/core';
import { inject } from '@angular/core';
import {
ActionReducer,
combineReducers,
} from '@ngrx/store';

import { createSingleInjectionToken } from '@daffodil/core';
import { daffComposeReducers } from '@daffodil/core/state';
// these unused imports are a workaround
import { DaffCustomer } from '@daffodil/customer';
Expand All @@ -17,13 +15,16 @@ import { daffCustomerAddressEntitiesReducer } from '../address-entities/public_a
import { daffCustomerReducer } from '../customer/reducer';
import { DaffCustomerReducersState } from '../reducers.interface';

/**
* An internal token to hold the Daffodil customer reducers.
* Includes the extra and standard reducers.
*
* @docs-private
*/
export const DAFF_CUSTOMER_REDUCERS = new InjectionToken<ActionReducer<DaffCustomerReducersState>>(
export const {
/**
* An internal token to hold the Daffodil customer reducers.
* Includes the extra and standard reducers.
*
* @docs-private
*/
token: DAFF_CUSTOMER_REDUCERS,
provider: daffProvideCustomerReducers,
} = createSingleInjectionToken<ActionReducer<DaffCustomerReducersState>>(
'DAFF_CUSTOMER_REDUCERS',
{
providedIn: 'any',
Expand Down

0 comments on commit bd0421a

Please sign in to comment.