Skip to content

Commit 0518d60

Browse files
authored
feat(cart-store-credit): create injection tokens with factory (#3237)
1 parent b07ab07 commit 0518d60

File tree

5 files changed

+52
-57
lines changed

5 files changed

+52
-57
lines changed

libs/cart-store-credit/driver/src/interfaces/store-credit-service.interface.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1-
import { InjectionToken } from '@angular/core';
21
import { Observable } from 'rxjs';
32

43
import { DaffCartWithStoreCredit } from '@daffodil/cart-store-credit';
4+
import { createSingleInjectionToken } from '@daffodil/core';
55

66
/**
77
* An injection token for the cart store credit driver.
88
*/
9-
export const DaffCartStoreCreditDriver = new InjectionToken<DaffCartStoreCreditDriverInterface>('DaffCartStoreCreditDriver');
9+
export const {
10+
token: DaffCartStoreCreditDriver,
11+
provider: daffProvideCartStoreCreditDriver,
12+
} = createSingleInjectionToken<DaffCartStoreCreditDriverInterface>('DaffCartStoreCreditDriver');
1013

1114
/**
1215
* The cart store credit driver is responsible for loading carts.
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
import { InjectionToken } from '@angular/core';
2-
1+
import { createSingleInjectionToken } from '@daffodil/core';
32
import { daffTransformErrorToStateError } from '@daffodil/core/state';
43

5-
/**
6-
* Transforms `DaffError`s into `DaffStateError`s before they are serialized into state.
7-
* Can be used to further refine Daffodil errors into more specific app errors.
8-
*/
9-
export const DAFF_CART_STORE_CREDIT_ERROR_MATCHER = new InjectionToken<typeof daffTransformErrorToStateError>(
4+
export const {
5+
/**
6+
* Transforms `DaffError`s into `DaffStateError`s before they are serialized into state.
7+
* Can be used to further refine Daffodil errors into more specific app errors.
8+
*/
9+
token: DAFF_CART_STORE_CREDIT_ERROR_MATCHER,
10+
provider: daffProvideCartStoreCreditErrorMatcher,
11+
} = createSingleInjectionToken<typeof daffTransformErrorToStateError>(
1012
'DAFF_CART_STORE_CREDIT_ERROR_MATCHER',
1113
{ factory: () => daffTransformErrorToStateError },
1214
);
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export { DAFF_CART_STORE_CREDIT_ERROR_MATCHER } from './error-matcher.token';
1+
export * from './error-matcher.token';
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,31 @@
1-
import {
2-
InjectionToken,
3-
Provider,
4-
} from '@angular/core';
51
import { ActionReducer } from '@ngrx/store';
62

3+
import { createMultiInjectionToken } from '@daffodil/core';
4+
75
import { DaffCartStoreCreditReducersState } from '../reducers.interface';
86

9-
/**
10-
* A token to hold the injectable extra reducers.
11-
*
12-
* Prefer using {@link daffCartStoreCreditProvideExtraReducers}.
13-
*/
14-
export const DAFF_CART_STORE_CREDIT_EXTRA_REDUCERS = new InjectionToken<ActionReducer<DaffCartStoreCreditReducersState>[]>(
7+
export const {
8+
/**
9+
* A token to hold the injectable extra reducers.
10+
*
11+
* Prefer using {@link daffCartStoreCreditProvideExtraReducers}.
12+
*/
13+
token: DAFF_CART_STORE_CREDIT_EXTRA_REDUCERS,
14+
15+
/**
16+
* Provides additional reducers that run after the standard Daffodil cart reducers.
17+
*
18+
* ```ts
19+
* providers: [
20+
* ...daffCartStoreCreditProvideExtraReducers(
21+
* myReducer1,
22+
* myReducer2
23+
* )
24+
* ]
25+
* ```
26+
*/
27+
provider: daffCartStoreCreditProvideExtraReducers,
28+
} = createMultiInjectionToken<ActionReducer<DaffCartStoreCreditReducersState>>(
1529
'DAFF_CART_STORE_CREDIT_EXTRA_REDUCERS',
16-
{
17-
factory: () => [],
18-
providedIn: 'any',
19-
},
30+
{ providedIn: 'any' },
2031
);
21-
22-
/**
23-
* Provides additional reducers that run after the standard Daffodil cart reducers.
24-
*
25-
* ```ts
26-
* providers: [
27-
* ...daffCartStoreCreditProvideExtraReducers(
28-
* myReducer1,
29-
* myReducer2
30-
* )
31-
* ]
32-
* ```
33-
*/
34-
export function daffCartStoreCreditProvideExtraReducers(
35-
...reducers: ActionReducer<DaffCartStoreCreditReducersState>[]
36-
): Provider[] {
37-
return reducers.map(reducer => ({
38-
provide: DAFF_CART_STORE_CREDIT_EXTRA_REDUCERS,
39-
useValue: reducer,
40-
multi: true,
41-
}));
42-
}

libs/cart-store-credit/state/src/reducers/token/reducers.token.ts

+12-11
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,28 @@
1-
import {
2-
inject,
3-
InjectionToken,
4-
} from '@angular/core';
1+
import { inject } from '@angular/core';
52
import {
63
ActionReducer,
74
combineReducers,
85
} from '@ngrx/store';
96

107
import { DaffCartWithStoreCredit } from '@daffodil/cart-store-credit';
8+
import { createSingleInjectionToken } from '@daffodil/core';
119
import { daffComposeReducers } from '@daffodil/core/state';
1210
// these unused imports are a workaround
1311

1412
import { DAFF_CART_STORE_CREDIT_EXTRA_REDUCERS } from './extra.token';
1513
import { DaffCartStoreCreditReducersState } from '../reducers.interface';
1614
import { daffCartStoreCreditReducer } from '../store-credit/public_api';
1715

18-
/**
19-
* An internal token to hold the Daffodil cart reducers.
20-
* Includes the extra and standard reducers.
21-
*
22-
* @docs-private
23-
*/
24-
export const DAFF_CART_STORE_CREDIT_REDUCERS = new InjectionToken<ActionReducer<DaffCartStoreCreditReducersState>>(
16+
export const {
17+
/**
18+
* An internal token to hold the Daffodil cart reducers.
19+
* Includes the extra and standard reducers.
20+
*
21+
* @docs-private
22+
*/
23+
token: DAFF_CART_STORE_CREDIT_REDUCERS,
24+
provider: daffProvideCartStoreCreditReducers,
25+
} = createSingleInjectionToken<ActionReducer<DaffCartStoreCreditReducersState>>(
2526
'DAFF_CART_STORE_CREDIT_REDUCERS',
2627
{
2728
providedIn: 'any',

0 commit comments

Comments
 (0)