|
| 1 | +import { provide, Injectable, Provider } from 'angular2/core'; |
| 2 | +import { Connection, ConnectionBackend, Http, XHRBackend, RequestOptions, Request, RequestMethods, Response, ResponseOptions, ReadyStates } from 'angular2/http'; |
| 3 | + |
| 4 | +@Injectable() |
| 5 | +export class CachePrimedConnectionBackend extends ConnectionBackend { |
| 6 | + private _preCachedResponses: PreCachedResponses; |
| 7 | + |
| 8 | + constructor(private _underlyingBackend: ConnectionBackend, private _baseResponseOptions: ResponseOptions) { |
| 9 | + super(); |
| 10 | + this._preCachedResponses = (<any>window).__preCachedResponses || {}; |
| 11 | + } |
| 12 | + |
| 13 | + public createConnection(request: Request): Connection { |
| 14 | + let cacheKey = request.url; |
| 15 | + if (request.method === RequestMethods.Get && this._preCachedResponses.hasOwnProperty(cacheKey)) { |
| 16 | + return new CacheHitConnection(request, this._preCachedResponses[cacheKey], this._baseResponseOptions); |
| 17 | + } else { |
| 18 | + return this._underlyingBackend.createConnection(request); |
| 19 | + } |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +class CacheHitConnection implements Connection { |
| 24 | + readyState: ReadyStates; |
| 25 | + request: Request; |
| 26 | + response: any; |
| 27 | + |
| 28 | + constructor (req: Request, cachedResponse: PreCachedResponse, baseResponseOptions: ResponseOptions) { |
| 29 | + this.request = req; |
| 30 | + this.readyState = ReadyStates.Done; |
| 31 | + |
| 32 | + // Workaround for difficulty consuming CommonJS default exports in TypeScript. Note that it has to be a dynamic |
| 33 | + // 'require', and not an 'import' statement, because the module isn't available on the server. |
| 34 | + // All this badness goes away with the next update of Angular 2, as it exposes Observable directly from angular2/core. |
| 35 | + // -- |
| 36 | + // The current version of Angular exposes the following SystemJS module directly (it is *not* coming from the |
| 37 | + // @reactivex/rxjs NPM package - it's coming from angular2). |
| 38 | + let obsCtor: any = require('@reactivex/rxjs/dist/cjs/Observable'); |
| 39 | + this.response = new obsCtor(responseObserver => { |
| 40 | + let response = new Response(new ResponseOptions({ body: cachedResponse.body, status: cachedResponse.statusCode })); |
| 41 | + responseObserver.next(response); |
| 42 | + responseObserver.complete(); |
| 43 | + }); |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +declare var require: any; // Part of the workaround mentioned below. Can remove this after updating Angular. |
| 48 | + |
| 49 | +interface PreCachedResponses { |
| 50 | + [url: string]: PreCachedResponse; |
| 51 | +} |
| 52 | + |
| 53 | +interface PreCachedResponse { |
| 54 | + statusCode: number; |
| 55 | + body: string; |
| 56 | +} |
| 57 | + |
| 58 | +export const CACHE_PRIMED_HTTP_PROVIDERS = [ |
| 59 | + provide(Http, { |
| 60 | + useFactory: (xhrBackend, requestOptions, responseOptions) => new Http(new CachePrimedConnectionBackend(xhrBackend, responseOptions), requestOptions), |
| 61 | + deps: [XHRBackend, RequestOptions, ResponseOptions] |
| 62 | + }), |
| 63 | +]; |
0 commit comments