Skip to content

Commit

Permalink
feat(customer-store-credit): create injection tokens with factory (#3245
Browse files Browse the repository at this point in the history
)
  • Loading branch information
griest024 authored Oct 14, 2024
1 parent c457f77 commit b54761b
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 60 deletions.
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 { DaffCustomerStoreCredit } from '@daffodil/customer-store-credit';

/**
* An injection token for the customer store credit driver.
*/
export const DaffCustomerStoreCreditDriver = new InjectionToken<DaffCustomerStoreCreditDriverInterface>('DaffCustomerStoreCreditDriver');
export const {
/**
* An injection token for the customer store credit driver.
*/
token: DaffCustomerStoreCreditDriver,
provider: daffProvideCustomerStoreCreditDriver,
} = createSingleInjectionToken<DaffCustomerStoreCreditDriverInterface>('DaffCustomerStoreCreditDriver');

/**
* The customer store credit driver is responsible for loading customers.
Expand Down
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_STORE_CREDIT_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_STORE_CREDIT_ERROR_MATCHER,
provider: daffProvideCustomerStoreCreditErrorMatcher,
} = createSingleInjectionToken<typeof daffTransformErrorToStateError>(
'DAFF_CUSTOMER_STORE_CREDIT_ERROR_MATCHER',
{ factory: () => daffTransformErrorToStateError },
);
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export { DAFF_CUSTOMER_STORE_CREDIT_ERROR_MATCHER } from './error-matcher.token';
export * from './error-matcher.token';
61 changes: 25 additions & 36 deletions libs/customer-store-credit/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 { DaffCustomerStoreCreditReducersState } from '../reducers.interface';

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

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

/**
* Provides additional reducers that run after the standard Daffodil customer reducers.
*
* ```ts
* providers: [
* ...daffCustomerStoreCreditProvideExtraReducers(
* myReducer1,
* myReducer2
* )
* ]
* ```
*/
export function daffCustomerStoreCreditProvideExtraReducers(
...reducers: ActionReducer<DaffCustomerStoreCreditReducersState>[]
): Provider[] {
return reducers.map(reducer => ({
provide: DAFF_CUSTOMER_STORE_CREDIT_EXTRA_REDUCERS,
useValue: reducer,
multi: true,
}));
}
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 { DaffCustomerStoreCredit } from '@daffodil/customer-store-credit';
Expand All @@ -15,13 +13,16 @@ import { DAFF_CUSTOMER_STORE_CREDIT_EXTRA_REDUCERS } from './extra.token';
import { DaffCustomerStoreCreditReducersState } from '../reducers.interface';
import { daffCustomerStoreCreditReducer } from '../store-credit/public_api';

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

0 comments on commit b54761b

Please sign in to comment.