Last active
May 30, 2018 16:30
-
-
Save Devcon4/4fac8aff83f1cba3cfbe26510ff0f73a to your computer and use it in GitHub Desktop.
Example of state
This file contains hidden or 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
| export class Action<T> { | |
| _subject = new BehaviorSubject<T>(undefined); | |
| public get state() : T { | |
| return this._subject.getValue(); | |
| } | |
| public set state(v : T) { | |
| this._subject.next(v); | |
| } | |
| public get subject(): BehaviorSubject<T> { | |
| return this._subject; | |
| } | |
| } |
This file contains hidden or 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
| <div style="text-align:center"> | |
| <h1> | |
| {{ dataState.subject | async}} | |
| </h1> | |
| <h1> | |
| {{ otherState.subject | async}} | |
| </h1> | |
| </div> |
This file contains hidden or 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
| @Component({ | |
| selector: 'app-root', | |
| templateUrl: './app.component.html', | |
| styleUrls: ['./app.component.css'] | |
| }) | |
| export class AppComponent { | |
| constructor(public dataState: GenericStateService<Data>, public otherState: OtherStateService) { } | |
| } |
This file contains hidden or 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
| @NgModule({ | |
| declarations: [ | |
| AppComponent, | |
| ], | |
| imports: [ | |
| BrowserModule, | |
| HttpModule, | |
| ], | |
| providers: [ | |
| dataService, | |
| storeService, | |
| dataStateService, | |
| OtherStateService | |
| { provide: 'dataState', useValue: new GenericStateService<Data>() }, | |
| ], | |
| bootstrap: [ AppComponent ] | |
| }) | |
| export class AppModule { } |
This file contains hidden or 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
| @Injectable({ | |
| providedIn: 'root' | |
| }) | |
| export class GenericStateService<T> extends Action<T> { } |
This file contains hidden or 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
| Class Other { | |
| Id: number; | |
| Name: string; | |
| } | |
| @Injectable({ | |
| providedIn: 'root' | |
| }) | |
| export class DataStateService extends Action<Other> { | |
| SpecialGet(Id: number) { | |
| // DataState specific logic. | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment