Skip to content

Commit

Permalink
refactor: rename to OnIdentifyEffects
Browse files Browse the repository at this point in the history
  • Loading branch information
timdeschryver committed Dec 3, 2018
1 parent 252c063 commit df26b3b
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 14 deletions.
14 changes: 11 additions & 3 deletions modules/effects/spec/effect_sources.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { cold, getTestScheduler } from 'jasmine-marbles';
import { concat, NEVER, Observable, of, throwError, timer } from 'rxjs';
import { map } from 'rxjs/operators';

import { Effect, EffectSources, EffectIdentifier } from '../';
import { Effect, EffectSources, OnIdentifyEffects } from '../';

describe('EffectSources', () => {
let mockErrorReporter: ErrorHandler;
Expand Down Expand Up @@ -84,19 +84,27 @@ describe('EffectSources', () => {
never = timer(50, getTestScheduler() as any).pipe(map(() => 'update'));
}

class SourceWithIdentifier implements EffectIdentifier {
class SourceWithIdentifier implements OnIdentifyEffects {
effectIdentifier: string;
@Effect() i$ = alwaysOf(i);

ngrxOnIdentifyEffects() {
return this.effectIdentifier;
}

constructor(identifier: string) {
this.effectIdentifier = identifier;
}
}

class SourceWithIdentifier2 implements EffectIdentifier {
class SourceWithIdentifier2 implements OnIdentifyEffects {
effectIdentifier: string;
@Effect() i2$ = alwaysOf(i2);

ngrxOnIdentifyEffects() {
return this.effectIdentifier;
}

constructor(identifier: string) {
this.effectIdentifier = identifier;
}
Expand Down
13 changes: 10 additions & 3 deletions modules/effects/src/effect_sources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
import { verifyOutput } from './effect_notification';
import { resolveEffectSource } from './effects_resolver';
import { getSourceForInstance } from './effects_metadata';
import { onIdentifyEffectsKey } from './lifecycle_hooks';

@Injectable()
export class EffectSources extends Subject<any> {
Expand All @@ -30,9 +31,7 @@ export class EffectSources extends Subject<any> {
toActions(): Observable<Action> {
return this.pipe(
groupBy(getSourceForInstance),
mergeMap(source$ =>
source$.pipe(groupBy(instance => instance.effectIdentifier || ''))
),
mergeMap(source$ => source$.pipe(groupBy(effectsInstance))),
mergeMap(source$ =>
source$.pipe(
exhaustMap(resolveEffectSource),
Expand All @@ -51,3 +50,11 @@ export class EffectSources extends Subject<any> {
);
}
}

function effectsInstance(source: any) {
if (onIdentifyEffectsKey in source) {
return source[onIdentifyEffectsKey]();
}

return '';
}
2 changes: 1 addition & 1 deletion modules/effects/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ export { OnRunEffects } from './on_run_effects';
export { EffectNotification } from './effect_notification';
export { ROOT_EFFECTS_INIT } from './effects_root_module';
export { UPDATE_EFFECTS, UpdateEffects } from './effects_feature_module';
export { EffectIdentifier } from './lifecycle_hooks';
export { OnIdentifyEffects, onIdentifyEffectsKey } from './lifecycle_hooks';
15 changes: 11 additions & 4 deletions modules/effects/src/lifecycle_hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,25 @@
* ### Set an identifier for an Effects class
*
* ```ts
* class EffectWithIdentifier implements EffectIdentifier {
* effectIdentifier: string;
* class EffectWithIdentifier implements OnIdentifyEffects {
* private effectIdentifier: string;
*
* ngrxOnIdentifyEffects () {
* return this.effectIdentifier;
* }
*
* constructor(identifier: string) {
* this.effectIdentifier = identifier;
* }
* ```
*/
export interface EffectIdentifier {
export interface OnIdentifyEffects {
/**
* @description
* String identifier to differentiate effect instances.
*/
effectIdentifier: string;
ngrxOnIdentifyEffects: () => string;
}

export const onIdentifyEffectsKey: keyof OnIdentifyEffects =
'ngrxOnIdentifyEffects';
6 changes: 3 additions & 3 deletions projects/ngrx.io/content/guide/migration/v7.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,11 @@ export class MyEffects {
}
```

### reserved property `effectIdentifier`
### reserved property `ngrxOnIdentifyEffects`

The property `effectIdentifier` is a reserved property name.
The property `ngrxOnIdentifyEffects` is a reserved property name.

This property is used by the `EffectIdentifier` interface, by implementing this interface you define a unique identifier to register an Effects class instance multiple times.
This property is used by the `OnIdentifyEffects` interface, by implementing this interface you define a unique identifier to register an Effects class instance multiple times.

## @ngrx/router-store

Expand Down

0 comments on commit df26b3b

Please sign in to comment.