Created
June 6, 2018 14:17
-
-
Save Devcon4/7e465580809d241adbba78159bca75c3 to your computer and use it in GitHub Desktop.
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 interface IActionable<T> { | |
| state: T; | |
| subject: BehaviorSubject<T>; | |
| } | |
| 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, | |
| NetworkDataStateServcie, | |
| { 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
| interface ICollectionable<T> { | |
| collection: IActionable<T>; | |
| } | |
| interface IDocumentable<T> { | |
| doc: IActionable<T>; | |
| } | |
| export abstract class FirebaseNetwork<T> implements ICollectionable<T[]>, IDocumentable<T> { | |
| doc: Action<T> = new Action(); | |
| collection: Action<T[]> = new Action(); | |
| constructor(private typeName: string, private afs: AngularFirestore) { } | |
| // Rest Methods to set state objs. | |
| } |
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
| interface IActionable<T> { | |
| state: T; | |
| subject: BehaviorSubject<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
| @Injectable({ | |
| providedIn: 'root' | |
| }) | |
| export class NetworkDataStateService extends FirebaseNetwork<Other> { | |
| constructor(@Inject(AngularFirestore) afs: AngularFirestore) { | |
| super('Other', afs); | |
| } | |
| // special rest method overrides or implementations (should not ever really make any). | |
| } |
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 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