Skip to content

Instantly share code, notes, and snippets.

@Devcon4
Created June 6, 2018 14:17
Show Gist options
  • Select an option

  • Save Devcon4/7e465580809d241adbba78159bca75c3 to your computer and use it in GitHub Desktop.

Select an option

Save Devcon4/7e465580809d241adbba78159bca75c3 to your computer and use it in GitHub Desktop.
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;
}
}
<div style="text-align:center">
<h1>
{{ dataState.subject | async}}
</h1>
<h1>
{{ otherState.subject | async}}
</h1>
</div>
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
constructor(public dataState: GenericStateService<Data>, public otherState: OtherStateService) { }
}
@NgModule({
declarations: [
AppComponent,
],
imports: [
BrowserModule,
HttpModule,
],
providers: [
dataService,
storeService,
dataStateService,
OtherStateService,
NetworkDataStateServcie,
{ provide: 'dataState', useValue: new GenericStateService<Data>() },
],
bootstrap: [ AppComponent ]
})
export class AppModule { }
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.
}
@Injectable({
providedIn: 'root'
})
export class GenericStateService<T> extends Action<T> { }
interface IActionable<T> {
state: T;
subject: BehaviorSubject<T>;
}
@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).
}
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