@@ -5,10 +5,43 @@ import { EffectMetadata, EffectConfig } from './models';
5
5
const CREATE_EFFECT_METADATA_KEY = '__@ngrx/effects_create__' ;
6
6
7
7
type DispatchType < T > = T extends { dispatch : infer U } ? U : unknown ;
8
+ type ReturnType < T > = T extends false ? Observable < unknown > : Observable < Action > ;
9
+ /**
10
+ * @description
11
+ * Creates an effect from an `Observable` and an `EffectConfig`.
12
+ *
13
+ * @param source A function which returns an `Observable`.
14
+ * @param config A partial `EffectConfig` to configure the effect. By default, `dispatch` is true and `resubscribeOnError` is true.
15
+ * @returns If `EffectConfig`.`dispatch` is true, returns `Observable<Action>`. Else, returns `Observable<unknown>`.
16
+ *
17
+ * @usageNotes
18
+ *
19
+ * ** Mapping to a different action **
20
+ * ```ts
21
+ * effectName$ = createEffect(
22
+ * () => this.actions$.pipe(
23
+ * ofType(FeatureActions.actionOne),
24
+ * map(() => FeatureActions.actionTwo())
25
+ * )
26
+ * );
27
+ * ```
28
+ *
29
+ * ** Non-dispatching effects **
30
+ * ```ts
31
+ * effectName$ = createEffect(
32
+ * () => this.actions$.pipe(
33
+ * ofType(FeatureActions.actionOne),
34
+ * tap(() => console.log('Action One Dispatched'))
35
+ * ),
36
+ * { dispatch: false }
37
+ * // FeatureActions.actionOne is not dispatched
38
+ * );
39
+ * ```
40
+ */
8
41
export function createEffect <
9
42
C extends EffectConfig ,
10
43
T extends DispatchType < C > ,
11
- O extends T extends false ? Observable < unknown > : Observable < Action > ,
44
+ O extends ReturnType < T > ,
12
45
R extends O | ( ( ...args : any [ ] ) => O )
13
46
> ( source : ( ) => R , config ?: Partial < C > ) : R {
14
47
const effect = source ( ) ;
0 commit comments