Skip to content

Instantly share code, notes, and snippets.

@Devcon4
Last active May 30, 2018 16:30
Show Gist options
  • Select an option

  • Save Devcon4/4fac8aff83f1cba3cfbe26510ff0f73a to your computer and use it in GitHub Desktop.

Select an option

Save Devcon4/4fac8aff83f1cba3cfbe26510ff0f73a to your computer and use it in GitHub Desktop.
Example of state
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
{ provide: 'dataState', useValue: new GenericStateService<Data>() },
],
bootstrap: [ AppComponent ]
})
export class AppModule { }
@Injectable({
providedIn: 'root'
})
export class GenericStateService<T> extends Action<T> { }
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