|
| 1 | +import { Provider, NgModule, Inject } from '@angular/core'; |
| 2 | +import { Headers, Http, ResponseOptions, RequestOptionsArgs, Response } from '@angular/http'; |
| 3 | +import { Observable } from 'rxjs/Observable'; |
| 4 | +import 'rxjs/add/observable/of'; |
| 5 | +import 'rxjs/add/operator/map'; |
| 6 | +const globalSerializedStateKey = 'HTTP_STATE_TRANSFER'; |
| 7 | +const backingStoreDIToken = 'HTTP_STATE_BACKING_STORE'; |
| 8 | + |
| 9 | +export interface CacheOptions { |
| 10 | + permanent: boolean; |
| 11 | +} |
| 12 | + |
| 13 | +export interface CachedHttpResponse { |
| 14 | + headers: { [name: string]: any } | null; |
| 15 | + status: number; |
| 16 | + statusText: string | null; |
| 17 | + text: string; |
| 18 | + url: string; |
| 19 | +} |
| 20 | + |
| 21 | +export type BackingStore = { [key: string]: CachedHttpResponse }; |
| 22 | + |
| 23 | +export class HttpWithStateTransfer { |
| 24 | + private backingStore: BackingStore; |
| 25 | + private http: Http; |
| 26 | + |
| 27 | + constructor(@Inject(Http) http: Http, @Inject(backingStoreDIToken) backingStore: BackingStore) { |
| 28 | + this.http = http; |
| 29 | + this.backingStore = backingStore; |
| 30 | + } |
| 31 | + |
| 32 | + public stateForTransfer(): any { |
| 33 | + return { [globalSerializedStateKey]: this.backingStore }; |
| 34 | + } |
| 35 | + |
| 36 | + public get(url: string, options?: CacheOptions, requestOptions?: RequestOptionsArgs): Observable<Response> { |
| 37 | + return this.getCachedResponse(/* cacheKey */ url, () => this.http.get(url, requestOptions), options); |
| 38 | + } |
| 39 | + |
| 40 | + private getCachedResponse(cacheKey: string, provider: () => Observable<Response>, options?: CacheOptions): Observable<Response> { |
| 41 | + // By default, the cache is only used for the *first* client-side read. So, we're only performing |
| 42 | + // a one-time transfer of server-side response to the client. If you want to keep and reuse cached |
| 43 | + // responses continually during server-side and client-side execution, set 'permanent' to 'true. |
| 44 | + const isClient = typeof window !== 'undefined'; |
| 45 | + const isPermanent = options && options.permanent; |
| 46 | + |
| 47 | + const allowReadFromCache = isClient || isPermanent; |
| 48 | + if (allowReadFromCache && this.backingStore.hasOwnProperty(cacheKey)) { |
| 49 | + const cachedValue = this.backingStore[cacheKey]; |
| 50 | + if (!isPermanent) { |
| 51 | + delete this.backingStore[cacheKey]; |
| 52 | + } |
| 53 | + return Observable.of(new Response(new ResponseOptions({ |
| 54 | + body: cachedValue.text, |
| 55 | + headers: new Headers(cachedValue.headers), |
| 56 | + status: cachedValue.status, |
| 57 | + url: cachedValue.url |
| 58 | + }))); |
| 59 | + } |
| 60 | + |
| 61 | + return provider() |
| 62 | + .map(response => { |
| 63 | + const allowWriteToCache = !isClient || isPermanent; |
| 64 | + if (allowWriteToCache) { |
| 65 | + this.backingStore[cacheKey] = { |
| 66 | + headers: response.headers ? response.headers.toJSON() : null, |
| 67 | + status: response.status, |
| 68 | + statusText: response.statusText, |
| 69 | + text: response.text(), |
| 70 | + url: response.url |
| 71 | + }; |
| 72 | + } |
| 73 | + |
| 74 | + return response; |
| 75 | + }); |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +export function defaultBackingStoreFactory() { |
| 80 | + const transferredData = typeof window !== 'undefined' ? (window as any)[globalSerializedStateKey] : null; |
| 81 | + return transferredData || {}; |
| 82 | +} |
| 83 | + |
| 84 | +@NgModule({ |
| 85 | + providers: [ |
| 86 | + // The backing store is a separate DI service so you could override exactly how it gets |
| 87 | + // transferred from server to client |
| 88 | + { provide: backingStoreDIToken, useFactory: defaultBackingStoreFactory }, |
| 89 | + |
| 90 | + { provide: HttpWithStateTransfer, useClass: HttpWithStateTransfer }, |
| 91 | + ] |
| 92 | +}) |
| 93 | +export class HttpWithStateTransferModule { |
| 94 | +} |
0 commit comments