I'm trying to think functional reactive and set up a list of elements on a page as a stream. This is using Angular2 but the problem should be similar to any stream-based architecture. So, I have two streams currently, the initial stream (http call to get a list of users from github) and a remove user stream (occurs when the remove user button is clicked). I believe the marble diagram would look like the following:
|[user1,user2,user3]| <--- http initial stream
|---------------------x----------x-----... <--- x denotes user removed
How do I combine these streams to get this to work? I'm also thinking later of having more streams for sorting and ordering. Am I going about this the right way? Here's the code (note this code is incomplete, currently the removeUser$ is not interactive with the user$ which it should):
export class UserGridComponent implements OnInit {
public users$: Observable<any>;
public removeUser$: Subject;
constructor(private _githubUserService: GithubUserService) { }
ngOnInit() {
this.removeUser$ = new Subject()
.subscribe((user) => { console.log('next ' + JSON.stringify(user)});
this.users$ = this._githubUserService.getUsers()
.map((res) => res.json());
}
}
Here is the Plunker
Currently I am only logging to console that the remove button is clicked and passes the user.
Here is the html template which shows that I subscribe to the user$ by using the async pipe (Angular2):
<md-list>
<h1 md-header>GitHub Users</h1>
<md-list-item *ngFor="let user of users$ | async">
<a href="https://github.com/{{user.login}}" target="_new">
<img md-list-avatar [src]="user.avatar_url">
</a>
<h4>{{user.login}}</h4>
<button md-icon-button (click)="removeUser$.next(user)">
<md-icon>cancel</md-icon>
</button>
</md-list-item>
</md-list>
users$observable so it is likely still cold.