Created
November 13, 2017 02:13
-
-
Save DingWeizhe/e03213523ffda99107b98316e1d0cc74 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
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