Skip to content

Instantly share code, notes, and snippets.

@arturovt
Last active February 28, 2021 03:34
Show Gist options
  • Save arturovt/8acd858c4a92d119c6d01c6a82ffb547 to your computer and use it in GitHub Desktop.
Save arturovt/8acd858c4a92d119c6d01c6a82ffb547 to your computer and use it in GitHub Desktop.
// This will be used on the client-side
@Injectable({ providedIn: 'root' })
export class ArticleService {
constructor(private transferState: TransferState) {}
getArticle(id: string): Observable<Article> {
const key = makeStateKey<Article>(`article:${id}`);
const article = this.transferState.get(key, null!);
return of(article);
}
}
// This will be used on the server-side
@Injectable()
export class ServerArticleService {
constructor(private http: HttpClient, private transferState: TransferState) {}
getArticle(id: string): Observable<Article> {
return this.http.get<Article>(`${environment.apiUrl}/api/article/${params.id}`).pipe(
tap(article => {
const key = makeStateKey<Article>(`article:${id}`);
this.transferState.set(key, article);
}),
);
}
}
@NgModule({
providers: [
{
provide: ArticleService,
useClass: ServerArticleService,
},
],
})
export class AppServerModule {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment