Skip to content

Instantly share code, notes, and snippets.

@sod
Created May 28, 2020 15:01
Show Gist options
  • Save sod/7c59bf435e43c192f589815997bce76c to your computer and use it in GitHub Desktop.
Save sod/7c59bf435e43c192f589815997bce76c to your computer and use it in GitHub Desktop.
import {Action, ActionCreator} from '@ngrx/store';
import {AbstractType, InjectionToken, Type} from '@angular/core';
import {Observable} from 'rxjs';
declare type DispatchType<T> = T extends {
dispatch: infer U;
}
? U
: true;
declare type ObservableType<T, OriginalType> = T extends false ? OriginalType : Action;
declare type InjectionType<T> = Type<T> | InjectionToken<T> | AbstractType<T>;
export interface LazyEffectConfig {
/**
* Determines if the action emitted by the effect is dispatched to the store.
* If false, effect does not need to return type `Observable<Action>`.
*/
dispatch?: boolean;
/**
* Determines if the effect will be resubscribed to if an error occurs in the main actions stream.
*/
useEffectsErrorHandler?: boolean;
}
export function createLazyEffect<
C extends LazyEffectConfig,
DT extends DispatchType<C>,
OT extends ObservableType<DT, OT>,
R extends Observable<OT>,
AC extends ActionCreator[],
DEP1,
DEP2,
DEP3,
DEP4,
DEP5,
DEP6,
DEP7,
DEP8,
DEP9,
V = ReturnType<AC[number]>
>(
config: {
actions: AC;
effect: (action$: Observable<V>, ...dependencies: [DEP1, DEP2, DEP3, DEP4, DEP5, DEP6, DEP7, DEP8, DEP9]) => R;
dependencies: Partial<
[
InjectionType<DEP1>,
InjectionType<DEP2>,
InjectionType<DEP3>,
InjectionType<DEP4>,
InjectionType<DEP5>,
InjectionType<DEP6>,
InjectionType<DEP7>,
InjectionType<DEP8>,
InjectionType<DEP9>,
]
>;
} & Partial<C>,
): any {
return config;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment