|
| 1 | +import {ConnectionBackend, Connection} from '../interfaces'; |
| 2 | +import {ReadyStates, RequestMethods, RequestMethodsMap} from '../enums'; |
| 3 | +import {Request} from '../static_request'; |
| 4 | +import {Response} from '../static_response'; |
| 5 | +import {ResponseOptions, BaseResponseOptions} from '../base_response_options'; |
| 6 | +import {Injectable} from 'angular2/di'; |
| 7 | +import {BrowserJsonp} from './browser_jsonp'; |
| 8 | +import {EventEmitter, ObservableWrapper} from 'angular2/src/facade/async'; |
| 9 | +import {StringWrapper, isPresent, ENUM_INDEX, makeTypeError} from 'angular2/src/facade/lang'; |
| 10 | + |
| 11 | +export class JSONPConnection implements Connection { |
| 12 | + readyState: ReadyStates; |
| 13 | + request: Request; |
| 14 | + response: EventEmitter; |
| 15 | + private _id: string; |
| 16 | + private _script: Element; |
| 17 | + private _responseData: any; |
| 18 | + private _finished: boolean = false; |
| 19 | + |
| 20 | + constructor(req: Request, private _dom: BrowserJsonp, |
| 21 | + private baseResponseOptions?: ResponseOptions) { |
| 22 | + if (req.method !== RequestMethods.GET) { |
| 23 | + throw makeTypeError("JSONP requests must use GET request method."); |
| 24 | + } |
| 25 | + this.request = req; |
| 26 | + this.response = new EventEmitter(); |
| 27 | + this.readyState = ReadyStates.LOADING; |
| 28 | + this._id = _dom.nextRequestID(); |
| 29 | + |
| 30 | + _dom.exposeConnection(this._id, this); |
| 31 | + |
| 32 | + // Workaround Dart |
| 33 | + // url = url.replace(/=JSONP_CALLBACK(&|$)/, `generated method`); |
| 34 | + let callback = _dom.requestCallback(this._id); |
| 35 | + let url: string = req.url; |
| 36 | + if (url.indexOf('=JSONP_CALLBACK&') > -1) { |
| 37 | + url = StringWrapper.replace(url, '=JSONP_CALLBACK&', `=${callback}&`); |
| 38 | + } else if (url.lastIndexOf('=JSONP_CALLBACK') === url.length - '=JSONP_CALLBACK'.length) { |
| 39 | + url = StringWrapper.substring(url, 0, url.length - '=JSONP_CALLBACK'.length) + `=${callback}`; |
| 40 | + } |
| 41 | + |
| 42 | + let script = this._script = _dom.build(url); |
| 43 | + |
| 44 | + script.addEventListener('load', (event) => { |
| 45 | + if (this.readyState === ReadyStates.CANCELLED) return; |
| 46 | + this.readyState = ReadyStates.DONE; |
| 47 | + _dom.cleanup(script); |
| 48 | + if (!this._finished) { |
| 49 | + ObservableWrapper.callThrow( |
| 50 | + this.response, makeTypeError('JSONP injected script did not invoke callback.')); |
| 51 | + return; |
| 52 | + } |
| 53 | + |
| 54 | + let responseOptions = new ResponseOptions({body: this._responseData}); |
| 55 | + if (isPresent(this.baseResponseOptions)) { |
| 56 | + responseOptions = this.baseResponseOptions.merge(responseOptions); |
| 57 | + } |
| 58 | + |
| 59 | + ObservableWrapper.callNext(this.response, new Response(responseOptions)); |
| 60 | + }); |
| 61 | + |
| 62 | + script.addEventListener('error', (error) => { |
| 63 | + if (this.readyState === ReadyStates.CANCELLED) return; |
| 64 | + this.readyState = ReadyStates.DONE; |
| 65 | + _dom.cleanup(script); |
| 66 | + ObservableWrapper.callThrow(this.response, error); |
| 67 | + }); |
| 68 | + |
| 69 | + _dom.send(script); |
| 70 | + } |
| 71 | + |
| 72 | + finished(data?: any) { |
| 73 | + // Don't leak connections |
| 74 | + this._finished = true; |
| 75 | + this._dom.removeConnection(this._id); |
| 76 | + if (this.readyState === ReadyStates.CANCELLED) return; |
| 77 | + this._responseData = data; |
| 78 | + } |
| 79 | + |
| 80 | + dispose(): void { |
| 81 | + this.readyState = ReadyStates.CANCELLED; |
| 82 | + let script = this._script; |
| 83 | + this._script = null; |
| 84 | + if (isPresent(script)) { |
| 85 | + this._dom.cleanup(script); |
| 86 | + } |
| 87 | + ObservableWrapper.callReturn(this.response); |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +@Injectable() |
| 92 | +export class JSONPBackend implements ConnectionBackend { |
| 93 | + constructor(private _browserJSONP: BrowserJsonp, private _baseResponseOptions: ResponseOptions) {} |
| 94 | + createConnection(request: Request): JSONPConnection { |
| 95 | + return new JSONPConnection(request, this._browserJSONP, this._baseResponseOptions); |
| 96 | + } |
| 97 | +} |
0 commit comments