Skip to content
This repository has been archived by the owner on Jul 12, 2019. It is now read-only.

Commit

Permalink
feat(cache): reset cache for browser request
Browse files Browse the repository at this point in the history
  • Loading branch information
Gorniv committed May 21, 2018
1 parent 2b48652 commit c6e4176
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 8 deletions.
21 changes: 17 additions & 4 deletions lib/ngx-transfer-http/src/transfer-http.service.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
import { Injectable } from '@angular/core';
import { Injectable, Inject, PLATFORM_ID } from '@angular/core';
import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http';
import { TransferState } from '@angular/platform-browser';
import { Observable } from 'rxjs';
import { tap } from 'rxjs/operators';
import { fromPromise } from 'rxjs/observable/fromPromise';
import { isPlatformBrowser, isPlatformServer } from '@angular/common';

@Injectable()
export class TransferHttpService {
constructor(protected transferState: TransferState,
private httpClient: HttpClient) {
constructor(
protected transferState: TransferState,
private httpClient: HttpClient,
@Inject(PLATFORM_ID) private platformId: Object
) {

}

public request(method: string, uri: string | Request, options?: {
Expand Down Expand Up @@ -226,13 +231,21 @@ export class TransferHttpService {
}
}

private resolveData(key: string): any {
private resolveData(key: any): any {
const data = this.getFromCache(key);

if (!data) {
throw new Error();
}

if (isPlatformBrowser(this.platformId)) {
// Client only code.
this.transferState.remove(key);
}
if (isPlatformServer(this.platformId)) {
// Server only code.
}

return fromPromise(Promise.resolve(data));
}

Expand Down
2 changes: 1 addition & 1 deletion src/app/transfer-back/transfer-back.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ <h1>{{ "back-http.title" | translate }}</h1>
<p>{{ "back-http.text" | translate }}</p>

<hr>

<button (click)="loadPost()">load post</button>
<strong>{{ "back-http.delay" | translate }}</strong>
<pre *ngIf="result; else noResult">
{{ result | json }}
Expand Down
12 changes: 9 additions & 3 deletions src/app/transfer-back/transfer-back.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,26 @@ export class TransferBackComponent implements OnInit {
public resultPost: any;

constructor(private http: TransferHttpService,
@Inject(AppStorage) private appStorage: Storage,
@Inject('ORIGIN_URL') public baseUrl: string) {
@Inject(AppStorage) private appStorage: Storage,
@Inject('ORIGIN_URL') public baseUrl: string) {
console.log(`ORIGIN_URL=${baseUrl}`);
}

ngOnInit(): void {
this.http.get('https://reqres.in/api/users?delay=3').subscribe(result => {
this.result = result;
});
this.http.post('https://reqres.in/api/users', JSON.stringify({
this.loadPost();
}

loadPost() {
console.log('see network');
const resultSub = this.http.post('https://reqres.in/api/users', JSON.stringify({
'name': 'morpheus',
'job': 'leader'
})).subscribe(result => {
this.resultPost = result;
resultSub.unsubscribe();
});
}
}

0 comments on commit c6e4176

Please sign in to comment.