Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(effects): run user provided effects defined as injection token #3851

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion modules/effects/spec/integration.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { NgModule, Injectable } from '@angular/core';
import { NgModule, Injectable, InjectionToken } from '@angular/core';
import { TestBed } from '@angular/core/testing';
import { RouterTestingModule } from '@angular/router/testing';
import { Router } from '@angular/router';
Expand Down Expand Up @@ -113,6 +113,34 @@ describe('NgRx Effects Integration spec', () => {
expect(functionalEffectRun).toHaveBeenCalledTimes(2);
});

it('runs user provided effects defined as injection token', () => {
const userProvidedEffectRun = jest.fn<void, []>();

const TOKEN_EFFECTS = new InjectionToken('Token Effects', {
providedIn: 'root',
factory: () => ({
userProvidedEffect$: createEffect(
() => concat(of('ngrx'), NEVER).pipe(tap(userProvidedEffectRun)),
{ dispatch: false }
),
}),
});

TestBed.configureTestingModule({
imports: [StoreModule.forRoot({}), EffectsModule.forRoot([])],
providers: [
{
provide: USER_PROVIDED_EFFECTS,
useValue: [TOKEN_EFFECTS],
multi: true,
},
],
});
TestBed.inject(EffectSources);

expect(userProvidedEffectRun).toHaveBeenCalledTimes(1);
});

describe('actions', () => {
const createDispatchedReducer =
(dispatchedActions: string[] = []) =>
Expand Down
16 changes: 16 additions & 0 deletions modules/effects/spec/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import {
getSourceForInstance,
isClass,
isClassInstance,
isToken,
} from '../src/utils';
import { InjectionToken } from '@angular/core';

describe('getSourceForInstance', () => {
it('gets the prototype for an instance of a source', () => {
Expand Down Expand Up @@ -55,3 +57,17 @@ describe('getClasses', () => {
expect(classes).toEqual([C1, C2, C3]);
});
});

describe('isToken', () => {
it('returns true for a class', () => {
expect(isToken(class C {})).toBe(true);
});

it('returns true for an injection token', () => {
expect(isToken(new InjectionToken('foo'))).toBe(true);
});

it('returns false for a record', () => {
expect(isToken({ foo: 'bar' })).toBe(false);
});
});
24 changes: 16 additions & 8 deletions modules/effects/src/effects_module.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { inject, ModuleWithProviders, NgModule, Type } from '@angular/core';
import {
inject,
InjectionToken,
ModuleWithProviders,
NgModule,
Type,
} from '@angular/core';
import { EffectsFeatureModule } from './effects_feature_module';
import { EffectsRootModule } from './effects_root_module';
import { EffectsRunner } from './effects_runner';
Expand All @@ -11,7 +17,7 @@ import {
USER_PROVIDED_EFFECTS,
} from './tokens';
import { FunctionalEffect } from './models';
import { getClasses, isClass } from './utils';
import { getClasses, isToken } from './utils';

@NgModule({})
export class EffectsModule {
Expand Down Expand Up @@ -94,9 +100,11 @@ export class EffectsModule {

function createEffectsInstances(
effectsGroups: Array<Type<unknown> | Record<string, FunctionalEffect>>[],
userProvidedEffectsGroups: Type<unknown>[][]
userProvidedEffectsGroups: Array<Type<unknown> | InjectionToken<unknown>>[]
): unknown[] {
const effects: Array<Type<unknown> | Record<string, FunctionalEffect>> = [];
const effects: Array<
Type<unknown> | Record<string, FunctionalEffect> | InjectionToken<unknown>
> = [];

for (const effectsGroup of effectsGroups) {
effects.push(...effectsGroup);
Expand All @@ -106,10 +114,10 @@ function createEffectsInstances(
effects.push(...userProvidedEffectsGroup);
}

return effects.map((effectsClassOrRecord) =>
isClass(effectsClassOrRecord)
? inject(effectsClassOrRecord)
: effectsClassOrRecord
return effects.map((effectsTokenOrRecord) =>
isToken(effectsTokenOrRecord)
? inject(effectsTokenOrRecord)
: effectsTokenOrRecord
);
}

Expand Down
6 changes: 3 additions & 3 deletions modules/effects/src/tokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import { FunctionalEffect } from './models';
export const _ROOT_EFFECTS_GUARD = new InjectionToken<void>(
'@ngrx/effects Internal Root Guard'
);
export const USER_PROVIDED_EFFECTS = new InjectionToken<Type<unknown>[][]>(
'@ngrx/effects User Provided Effects'
);
export const USER_PROVIDED_EFFECTS = new InjectionToken<
Array<Type<unknown> | InjectionToken<unknown>>[]
>('@ngrx/effects User Provided Effects');
export const _ROOT_EFFECTS = new InjectionToken<
[Array<Type<unknown> | Record<string, FunctionalEffect>>]
>('@ngrx/effects Internal Root Effects');
Expand Down
11 changes: 10 additions & 1 deletion modules/effects/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Type } from '@angular/core';
import { InjectionToken, Type } from '@angular/core';

export function getSourceForInstance<T>(instance: T): T {
return Object.getPrototypeOf(instance);
Expand All @@ -22,6 +22,15 @@ export function getClasses(
return classesAndRecords.filter(isClass);
}

export function isToken(
tokenOrRecord:
| Type<unknown>
| InjectionToken<unknown>
| Record<string, unknown>
): tokenOrRecord is Type<unknown> | InjectionToken<unknown> {
return tokenOrRecord instanceof InjectionToken || isClass(tokenOrRecord);
}

// TODO: replace with RxJS interfaces when possible
// needs dependency on RxJS >=7
export interface NextNotification<T> {
Expand Down