-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Effects): Ensure effects are only subscribed to once
- Loading branch information
1 parent
c40432d
commit 089abdc
Showing
8 changed files
with
115 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { of } from 'rxjs/observable/of'; | ||
import { Effect } from '../src/effects'; | ||
import { SingletonEffectsService } from '../src/singleton-effects.service'; | ||
|
||
describe('SingletonEffectsService', () => { | ||
it('should filter out duplicate effect instances and register new ones', () => { | ||
class Source1 { | ||
@Effect() a$ = of('a'); | ||
@Effect() b$ = of('b'); | ||
@Effect() c$ = of('c'); | ||
} | ||
class Source2 { | ||
@Effect() d$ = of('d'); | ||
@Effect() e$ = of('e'); | ||
@Effect() f$ = of('f'); | ||
} | ||
const instance1 = new Source1(); | ||
const instance2 = new Source2(); | ||
let singletonEffectsService = new SingletonEffectsService(); | ||
|
||
let result = singletonEffectsService.removeExistingAndRegisterNew([ instance1 ]); | ||
expect(result).toContain(instance1); | ||
|
||
result = singletonEffectsService.removeExistingAndRegisterNew([ instance1, instance2 ]); | ||
expect(result).not.toContain(instance1); | ||
expect(result).toContain(instance2); | ||
|
||
result = singletonEffectsService.removeExistingAndRegisterNew([ instance1, instance2 ]); | ||
expect(result).not.toContain(instance1); | ||
expect(result).not.toContain(instance2); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { Injectable } from '@angular/core'; | ||
|
||
@Injectable() | ||
export class SingletonEffectsService { | ||
private registeredEffects: string[] = []; | ||
|
||
removeExistingAndRegisterNew (effectInstances: any[]): any[] { | ||
return effectInstances.filter(instance => { | ||
const instanceAsString = instance.constructor.toString(); | ||
if (this.registeredEffects.indexOf(instanceAsString) === -1) { | ||
this.registeredEffects.push(instanceAsString); | ||
return true; | ||
} | ||
return false; | ||
}); | ||
} | ||
} |