Skip to content

Instantly share code, notes, and snippets.

@DingWeizhe
Created November 13, 2017 02:13
Show Gist options
  • Save DingWeizhe/e03213523ffda99107b98316e1d0cc74 to your computer and use it in GitHub Desktop.
Save DingWeizhe/e03213523ffda99107b98316e1d0cc74 to your computer and use it in GitHub Desktop.
import { Subject } from "rxjs";
class Article {
title?: string;
content?: string;
comments?: Comment[];
}
class Comment {
content?: string;
}
class ArticleService {
constructor(private http: HttpClient) {}
articleMap: Map<string, Subject<Article>> = new Map();
getArticle(id: number): Subject<Article> {
if (!this.articleMap[id])
this.articleMap[id] = new Subject()
.merge(this.http.get(`/api/v1/articles/${id}`))
.shareReplay(1);
return this.articleMap[id];
}
async comment(id: number, content: string) {
let comment = await this.http.post(`/api/v1/comments`, { content }).toPromise();
let article = this.assets.getArticle(this.id)
.take(1)
.subscribe(article => {
article.comments.push(comment);
this.getArticle(id).next(article);
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment