Skip to content

Commit bd0421a

Browse files
authored
feat(customer): create injection tokens with factory (#3241)
1 parent 615ccbc commit bd0421a

File tree

6 files changed

+66
-66
lines changed

6 files changed

+66
-66
lines changed

libs/customer/driver/src/interfaces/address-service.interface.ts

+11-6
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
1-
import { InjectionToken } from '@angular/core';
21
import { Observable } from 'rxjs';
32

4-
import { DaffIdentifiable } from '@daffodil/core';
3+
import {
4+
DaffIdentifiable,
5+
createSingleInjectionToken,
6+
} from '@daffodil/core';
57
import { DaffCustomerAddress } from '@daffodil/customer';
68

7-
/**
8-
* An injection token for the customer driver.
9-
*/
10-
export const DaffCustomerAddressDriver = new InjectionToken<DaffCustomerAddressDriverInterface>('DaffCustomerAddressDriver');
9+
export const {
10+
/**
11+
* An injection token for the customer driver.
12+
*/
13+
token: DaffCustomerAddressDriver,
14+
provider: daffProvideCustomerAddressDriver,
15+
} = createSingleInjectionToken<DaffCustomerAddressDriverInterface>('DaffCustomerAddressDriver');
1116

1217
/**
1318
* The customer driver is responsible for loading customers.

libs/customer/driver/src/interfaces/customer-service.interface.ts

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

3+
import { createSingleInjectionToken } from '@daffodil/core';
44
import { DaffCustomer } from '@daffodil/customer';
55

6-
/**
7-
* An injection token for the customer driver.
8-
*/
9-
export const DaffCustomerDriver = new InjectionToken<DaffCustomerDriverInterface>('DaffCustomerDriver');
6+
export const {
7+
/**
8+
* An injection token for the customer driver.
9+
*/
10+
token: DaffCustomerDriver,
11+
provider: daffProvideCustomerDriver,
12+
} = createSingleInjectionToken<DaffCustomerDriverInterface>('DaffCustomerDriver');
1013

1114
/**
1215
* The customer driver is responsible for loading customers.
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_CUSTOMER_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_CUSTOMER_ERROR_MATCHER,
10+
provider: daffProvideCustomerErrorMatcher,
11+
} = createSingleInjectionToken<typeof daffTransformErrorToStateError>(
1012
'DAFF_CUSTOMER_ERROR_MATCHER',
1113
{ factory: () => daffTransformErrorToStateError },
1214
);
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export { DAFF_CUSTOMER_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 { DaffCustomerReducersState } from '../reducers.interface';
86

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

libs/customer/state/src/reducers/token/reducers.token.ts

+12-11
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
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

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

20-
/**
21-
* An internal token to hold the Daffodil customer reducers.
22-
* Includes the extra and standard reducers.
23-
*
24-
* @docs-private
25-
*/
26-
export const DAFF_CUSTOMER_REDUCERS = new InjectionToken<ActionReducer<DaffCustomerReducersState>>(
18+
export const {
19+
/**
20+
* An internal token to hold the Daffodil customer reducers.
21+
* Includes the extra and standard reducers.
22+
*
23+
* @docs-private
24+
*/
25+
token: DAFF_CUSTOMER_REDUCERS,
26+
provider: daffProvideCustomerReducers,
27+
} = createSingleInjectionToken<ActionReducer<DaffCustomerReducersState>>(
2728
'DAFF_CUSTOMER_REDUCERS',
2829
{
2930
providedIn: 'any',

0 commit comments

Comments
 (0)