Template Upload
This commit is contained in:
100
node_modules/rxjs/observable/dom/AjaxObservable.d.ts
generated
vendored
Normal file
100
node_modules/rxjs/observable/dom/AjaxObservable.d.ts
generated
vendored
Normal file
@ -0,0 +1,100 @@
|
||||
import { Observable } from '../../Observable';
|
||||
import { Subscriber } from '../../Subscriber';
|
||||
import { Subscription } from '../../Subscription';
|
||||
export interface AjaxRequest {
|
||||
url?: string;
|
||||
body?: any;
|
||||
user?: string;
|
||||
async?: boolean;
|
||||
method: string;
|
||||
headers?: Object;
|
||||
timeout?: number;
|
||||
password?: string;
|
||||
hasContent?: boolean;
|
||||
crossDomain?: boolean;
|
||||
createXHR?: () => XMLHttpRequest;
|
||||
progressSubscriber?: Subscriber<any>;
|
||||
resultSelector?: <T>(response: AjaxResponse) => T;
|
||||
responseType?: string;
|
||||
}
|
||||
export interface AjaxCreationMethod {
|
||||
(): <T>(urlOrRequest: string | AjaxRequest) => Observable<T>;
|
||||
get: <T>(url: string, resultSelector?: (response: AjaxResponse) => T, headers?: Object) => Observable<T>;
|
||||
post: <T>(url: string, body?: any, headers?: Object) => Observable<T>;
|
||||
put: <T>(url: string, body?: any, headers?: Object) => Observable<T>;
|
||||
delete: <T>(url: string, headers?: Object) => Observable<T>;
|
||||
getJSON: <T, R>(url: string, resultSelector?: (data: T) => R, headers?: Object) => Observable<R>;
|
||||
}
|
||||
export declare function ajaxGet<T>(url: string, resultSelector?: (response: AjaxResponse) => T, headers?: Object): AjaxObservable<T>;
|
||||
export declare function ajaxPost<T>(url: string, body?: any, headers?: Object): Observable<T>;
|
||||
export declare function ajaxDelete<T>(url: string, headers?: Object): Observable<T>;
|
||||
export declare function ajaxPut<T>(url: string, body?: any, headers?: Object): Observable<T>;
|
||||
export declare function ajaxGetJSON<T, R>(url: string, resultSelector?: (data: T) => R, headers?: Object): Observable<R>;
|
||||
/**
|
||||
* Creates an observable for an Ajax request with either a request object with url, headers, etc or a string for a URL.
|
||||
*
|
||||
* @example
|
||||
* source = Rx.Observable.ajax('/products');
|
||||
* source = Rx.Observable.ajax( url: 'products', method: 'GET' });
|
||||
*
|
||||
* @param {Object} request Can be one of the following:
|
||||
*
|
||||
* A string of the URL to make the Ajax call.
|
||||
* An object with the following properties
|
||||
* - url: URL of the request
|
||||
* - body: The body of the request
|
||||
* - method: Method of the request, such as GET, POST, PUT, PATCH, DELETE
|
||||
* - async: Whether the request is async
|
||||
* - headers: Optional headers
|
||||
* - crossDomain: true if a cross domain request, else false
|
||||
* - createXHR: a function to override if you need to use an alternate XMLHttpRequest implementation.
|
||||
* - resultSelector: a function to use to alter the output value type of the Observable. Gets {AjaxResponse} as an argument
|
||||
* @returns {Observable} An observable sequence containing the XMLHttpRequest.
|
||||
*/
|
||||
export declare class AjaxObservable<T> extends Observable<T> {
|
||||
static create: AjaxCreationMethod;
|
||||
private request;
|
||||
constructor(urlOrRequest: string | AjaxRequest);
|
||||
protected _subscribe(subscriber: Subscriber<T>): Subscription | Function | void;
|
||||
}
|
||||
export declare class AjaxSubscriber<T> extends Subscriber<Event> {
|
||||
request: AjaxRequest;
|
||||
private xhr;
|
||||
private resultSelector;
|
||||
private done;
|
||||
constructor(destination: Subscriber<T>, request: AjaxRequest);
|
||||
next(e: Event): void;
|
||||
private send();
|
||||
private serializeBody(body, contentType);
|
||||
private setHeaders(xhr, headers);
|
||||
private setupEvents(xhr, request);
|
||||
unsubscribe(): void;
|
||||
}
|
||||
/** A normalized AJAX response */
|
||||
export declare class AjaxResponse {
|
||||
originalEvent: Event;
|
||||
xhr: XMLHttpRequest;
|
||||
request: AjaxRequest;
|
||||
/** {number} the HTTP status code */
|
||||
status: number;
|
||||
/** {string|ArrayBuffer|Document|object|any} the response data */
|
||||
response: any;
|
||||
/** {string} the raw responseText */
|
||||
responseText: string;
|
||||
/** {string} the responsType (e.g. 'json', 'arraybuffer', or 'xml') */
|
||||
responseType: string;
|
||||
constructor(originalEvent: Event, xhr: XMLHttpRequest, request: AjaxRequest);
|
||||
}
|
||||
/** A normalized AJAX error */
|
||||
export declare class AjaxError extends Error {
|
||||
/** {XMLHttpRequest} the XHR instance associated with the error */
|
||||
xhr: XMLHttpRequest;
|
||||
/** {AjaxRequest} the AjaxRequest associated with the error */
|
||||
request: AjaxRequest;
|
||||
/** {number} the HTTP status code */
|
||||
status: number;
|
||||
constructor(message: string, xhr: XMLHttpRequest, request: AjaxRequest);
|
||||
}
|
||||
export declare class AjaxTimeoutError extends AjaxError {
|
||||
constructor(xhr: XMLHttpRequest, request: AjaxRequest);
|
||||
}
|
343
node_modules/rxjs/observable/dom/AjaxObservable.js
generated
vendored
Normal file
343
node_modules/rxjs/observable/dom/AjaxObservable.js
generated
vendored
Normal file
@ -0,0 +1,343 @@
|
||||
"use strict";
|
||||
var __extends = (this && this.__extends) || function (d, b) {
|
||||
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
|
||||
function __() { this.constructor = d; }
|
||||
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
||||
};
|
||||
var _this = this;
|
||||
var root_1 = require('../../util/root');
|
||||
var tryCatch_1 = require('../../util/tryCatch');
|
||||
var errorObject_1 = require('../../util/errorObject');
|
||||
var Observable_1 = require('../../Observable');
|
||||
var Subscriber_1 = require('../../Subscriber');
|
||||
var createXHRDefault = function () {
|
||||
var xhr = new root_1.root.XMLHttpRequest();
|
||||
if (_this.crossDomain) {
|
||||
if ('withCredentials' in xhr) {
|
||||
xhr.withCredentials = true;
|
||||
return xhr;
|
||||
}
|
||||
else if (!!root_1.root.XDomainRequest) {
|
||||
return new root_1.root.XDomainRequest();
|
||||
}
|
||||
else {
|
||||
throw new Error('CORS is not supported by your browser');
|
||||
}
|
||||
}
|
||||
else {
|
||||
return xhr;
|
||||
}
|
||||
};
|
||||
function defaultGetResultSelector(response) {
|
||||
return response.response;
|
||||
}
|
||||
function ajaxGet(url, resultSelector, headers) {
|
||||
if (resultSelector === void 0) { resultSelector = defaultGetResultSelector; }
|
||||
if (headers === void 0) { headers = null; }
|
||||
return new AjaxObservable({ method: 'GET', url: url, resultSelector: resultSelector, headers: headers });
|
||||
}
|
||||
exports.ajaxGet = ajaxGet;
|
||||
;
|
||||
function ajaxPost(url, body, headers) {
|
||||
return new AjaxObservable({ method: 'POST', url: url, body: body, headers: headers });
|
||||
}
|
||||
exports.ajaxPost = ajaxPost;
|
||||
;
|
||||
function ajaxDelete(url, headers) {
|
||||
return new AjaxObservable({ method: 'DELETE', url: url, headers: headers });
|
||||
}
|
||||
exports.ajaxDelete = ajaxDelete;
|
||||
;
|
||||
function ajaxPut(url, body, headers) {
|
||||
return new AjaxObservable({ method: 'PUT', url: url, body: body, headers: headers });
|
||||
}
|
||||
exports.ajaxPut = ajaxPut;
|
||||
;
|
||||
function ajaxGetJSON(url, resultSelector, headers) {
|
||||
var finalResultSelector = resultSelector ? function (res) { return resultSelector(res.response); } : function (res) { return res.response; };
|
||||
return new AjaxObservable({ method: 'GET', url: url, responseType: 'json', resultSelector: finalResultSelector, headers: headers });
|
||||
}
|
||||
exports.ajaxGetJSON = ajaxGetJSON;
|
||||
;
|
||||
/**
|
||||
* Creates an observable for an Ajax request with either a request object with url, headers, etc or a string for a URL.
|
||||
*
|
||||
* @example
|
||||
* source = Rx.Observable.ajax('/products');
|
||||
* source = Rx.Observable.ajax( url: 'products', method: 'GET' });
|
||||
*
|
||||
* @param {Object} request Can be one of the following:
|
||||
*
|
||||
* A string of the URL to make the Ajax call.
|
||||
* An object with the following properties
|
||||
* - url: URL of the request
|
||||
* - body: The body of the request
|
||||
* - method: Method of the request, such as GET, POST, PUT, PATCH, DELETE
|
||||
* - async: Whether the request is async
|
||||
* - headers: Optional headers
|
||||
* - crossDomain: true if a cross domain request, else false
|
||||
* - createXHR: a function to override if you need to use an alternate XMLHttpRequest implementation.
|
||||
* - resultSelector: a function to use to alter the output value type of the Observable. Gets {AjaxResponse} as an argument
|
||||
* @returns {Observable} An observable sequence containing the XMLHttpRequest.
|
||||
*/
|
||||
var AjaxObservable = (function (_super) {
|
||||
__extends(AjaxObservable, _super);
|
||||
function AjaxObservable(urlOrRequest) {
|
||||
_super.call(this);
|
||||
var request = {
|
||||
async: true,
|
||||
createXHR: createXHRDefault,
|
||||
crossDomain: false,
|
||||
headers: {},
|
||||
method: 'GET',
|
||||
responseType: 'json',
|
||||
timeout: 0
|
||||
};
|
||||
if (typeof urlOrRequest === 'string') {
|
||||
request.url = urlOrRequest;
|
||||
}
|
||||
else {
|
||||
for (var prop in urlOrRequest) {
|
||||
if (urlOrRequest.hasOwnProperty(prop)) {
|
||||
request[prop] = urlOrRequest[prop];
|
||||
}
|
||||
}
|
||||
}
|
||||
this.request = request;
|
||||
}
|
||||
AjaxObservable.prototype._subscribe = function (subscriber) {
|
||||
return new AjaxSubscriber(subscriber, this.request);
|
||||
};
|
||||
AjaxObservable.create = (function () {
|
||||
var create = function (urlOrRequest) {
|
||||
return new AjaxObservable(urlOrRequest);
|
||||
};
|
||||
create.get = ajaxGet;
|
||||
create.post = ajaxPost;
|
||||
create.delete = ajaxDelete;
|
||||
create.put = ajaxPut;
|
||||
create.getJSON = ajaxGetJSON;
|
||||
return create;
|
||||
})();
|
||||
return AjaxObservable;
|
||||
}(Observable_1.Observable));
|
||||
exports.AjaxObservable = AjaxObservable;
|
||||
var AjaxSubscriber = (function (_super) {
|
||||
__extends(AjaxSubscriber, _super);
|
||||
function AjaxSubscriber(destination, request) {
|
||||
_super.call(this, destination);
|
||||
this.request = request;
|
||||
this.done = false;
|
||||
var headers = request.headers = request.headers || {};
|
||||
// force CORS if requested
|
||||
if (!request.crossDomain && !headers['X-Requested-With']) {
|
||||
headers['X-Requested-With'] = 'XMLHttpRequest';
|
||||
}
|
||||
// ensure content type is set
|
||||
if (!('Content-Type' in headers)) {
|
||||
headers['Content-Type'] = 'application/x-www-form-urlencoded; charset=UTF-8';
|
||||
}
|
||||
// properly serialize body
|
||||
request.body = this.serializeBody(request.body, request.headers['Content-Type']);
|
||||
this.resultSelector = request.resultSelector;
|
||||
this.send();
|
||||
}
|
||||
AjaxSubscriber.prototype.next = function (e) {
|
||||
this.done = true;
|
||||
var _a = this, resultSelector = _a.resultSelector, xhr = _a.xhr, request = _a.request, destination = _a.destination;
|
||||
var response = new AjaxResponse(e, xhr, request);
|
||||
if (resultSelector) {
|
||||
var result = tryCatch_1.tryCatch(resultSelector)(response);
|
||||
if (result === errorObject_1.errorObject) {
|
||||
this.error(errorObject_1.errorObject.e);
|
||||
}
|
||||
else {
|
||||
destination.next(result);
|
||||
}
|
||||
}
|
||||
else {
|
||||
destination.next(response);
|
||||
}
|
||||
};
|
||||
AjaxSubscriber.prototype.send = function () {
|
||||
var _a = this, request = _a.request, _b = _a.request, user = _b.user, method = _b.method, url = _b.url, async = _b.async, password = _b.password, headers = _b.headers, body = _b.body;
|
||||
var createXHR = request.createXHR;
|
||||
var xhr = tryCatch_1.tryCatch(createXHR).call(request);
|
||||
if (xhr === errorObject_1.errorObject) {
|
||||
this.error(errorObject_1.errorObject.e);
|
||||
}
|
||||
else {
|
||||
this.xhr = xhr;
|
||||
// open XHR first
|
||||
var result = void 0;
|
||||
if (user) {
|
||||
result = tryCatch_1.tryCatch(xhr.open).call(xhr, method, url, async, user, password);
|
||||
}
|
||||
else {
|
||||
result = tryCatch_1.tryCatch(xhr.open).call(xhr, method, url, async);
|
||||
}
|
||||
if (result === errorObject_1.errorObject) {
|
||||
this.error(errorObject_1.errorObject.e);
|
||||
return;
|
||||
}
|
||||
// timeout and responseType can be set once the XHR is open
|
||||
xhr.timeout = request.timeout;
|
||||
xhr.responseType = request.responseType;
|
||||
// set headers
|
||||
this.setHeaders(xhr, headers);
|
||||
// now set up the events
|
||||
this.setupEvents(xhr, request);
|
||||
// finally send the request
|
||||
if (body) {
|
||||
xhr.send(body);
|
||||
}
|
||||
else {
|
||||
xhr.send();
|
||||
}
|
||||
}
|
||||
};
|
||||
AjaxSubscriber.prototype.serializeBody = function (body, contentType) {
|
||||
if (!body || typeof body === 'string') {
|
||||
return body;
|
||||
}
|
||||
var splitIndex = contentType.indexOf(';');
|
||||
if (splitIndex !== -1) {
|
||||
contentType = contentType.substring(0, splitIndex);
|
||||
}
|
||||
switch (contentType) {
|
||||
case 'application/x-www-form-urlencoded':
|
||||
return Object.keys(body).map(function (key) { return (key + "=" + encodeURI(body[key])); }).join('&');
|
||||
case 'application/json':
|
||||
return JSON.stringify(body);
|
||||
}
|
||||
};
|
||||
AjaxSubscriber.prototype.setHeaders = function (xhr, headers) {
|
||||
for (var key in headers) {
|
||||
if (headers.hasOwnProperty(key)) {
|
||||
xhr.setRequestHeader(key, headers[key]);
|
||||
}
|
||||
}
|
||||
};
|
||||
AjaxSubscriber.prototype.setupEvents = function (xhr, request) {
|
||||
var progressSubscriber = request.progressSubscriber;
|
||||
xhr.ontimeout = function xhrTimeout(e) {
|
||||
var _a = xhrTimeout, subscriber = _a.subscriber, progressSubscriber = _a.progressSubscriber, request = _a.request;
|
||||
if (progressSubscriber) {
|
||||
progressSubscriber.error(e);
|
||||
}
|
||||
subscriber.error(new AjaxTimeoutError(this, request)); //TODO: Make betterer.
|
||||
};
|
||||
xhr.ontimeout.request = request;
|
||||
xhr.ontimeout.subscriber = this;
|
||||
xhr.ontimeout.progressSubscriber = progressSubscriber;
|
||||
if (xhr.upload && 'withCredentials' in xhr && root_1.root.XDomainRequest) {
|
||||
if (progressSubscriber) {
|
||||
xhr.onprogress = function xhrProgress(e) {
|
||||
var progressSubscriber = xhrProgress.progressSubscriber;
|
||||
progressSubscriber.next(e);
|
||||
};
|
||||
xhr.onprogress.progressSubscriber = progressSubscriber;
|
||||
}
|
||||
xhr.onerror = function xhrError(e) {
|
||||
var _a = xhrError, progressSubscriber = _a.progressSubscriber, subscriber = _a.subscriber, request = _a.request;
|
||||
if (progressSubscriber) {
|
||||
progressSubscriber.error(e);
|
||||
}
|
||||
subscriber.error(new AjaxError('ajax error', this, request));
|
||||
};
|
||||
xhr.onerror.request = request;
|
||||
xhr.onerror.subscriber = this;
|
||||
xhr.onerror.progressSubscriber = progressSubscriber;
|
||||
}
|
||||
xhr.onreadystatechange = function xhrReadyStateChange(e) {
|
||||
var _a = xhrReadyStateChange, subscriber = _a.subscriber, progressSubscriber = _a.progressSubscriber, request = _a.request;
|
||||
if (this.readyState === 4) {
|
||||
// normalize IE9 bug (http://bugs.jquery.com/ticket/1450)
|
||||
var status_1 = this.status === 1223 ? 204 : this.status;
|
||||
var response = (this.responseType === 'text' ? (this.response || this.responseText) : this.response);
|
||||
// fix status code when it is 0 (0 status is undocumented).
|
||||
// Occurs when accessing file resources or on Android 4.1 stock browser
|
||||
// while retrieving files from application cache.
|
||||
if (status_1 === 0) {
|
||||
status_1 = response ? 200 : 0;
|
||||
}
|
||||
if (200 <= status_1 && status_1 < 300) {
|
||||
if (progressSubscriber) {
|
||||
progressSubscriber.complete();
|
||||
}
|
||||
subscriber.next(e);
|
||||
subscriber.complete();
|
||||
}
|
||||
else {
|
||||
if (progressSubscriber) {
|
||||
progressSubscriber.error(e);
|
||||
}
|
||||
subscriber.error(new AjaxError('ajax error ' + status_1, this, request));
|
||||
}
|
||||
}
|
||||
};
|
||||
xhr.onreadystatechange.subscriber = this;
|
||||
xhr.onreadystatechange.progressSubscriber = progressSubscriber;
|
||||
xhr.onreadystatechange.request = request;
|
||||
};
|
||||
AjaxSubscriber.prototype.unsubscribe = function () {
|
||||
var _a = this, done = _a.done, xhr = _a.xhr;
|
||||
if (!done && xhr && xhr.readyState !== 4) {
|
||||
xhr.abort();
|
||||
}
|
||||
_super.prototype.unsubscribe.call(this);
|
||||
};
|
||||
return AjaxSubscriber;
|
||||
}(Subscriber_1.Subscriber));
|
||||
exports.AjaxSubscriber = AjaxSubscriber;
|
||||
/** A normalized AJAX response */
|
||||
var AjaxResponse = (function () {
|
||||
function AjaxResponse(originalEvent, xhr, request) {
|
||||
this.originalEvent = originalEvent;
|
||||
this.xhr = xhr;
|
||||
this.request = request;
|
||||
this.status = xhr.status;
|
||||
this.responseType = xhr.responseType;
|
||||
switch (this.responseType) {
|
||||
case 'json':
|
||||
if ('response' in xhr) {
|
||||
this.response = xhr.response;
|
||||
}
|
||||
else {
|
||||
this.response = JSON.parse(xhr.responseText || '');
|
||||
}
|
||||
break;
|
||||
case 'xml':
|
||||
this.response = xhr.responseXML;
|
||||
break;
|
||||
case 'text':
|
||||
default:
|
||||
this.response = ('response' in xhr) ? xhr.response : xhr.responseText;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return AjaxResponse;
|
||||
}());
|
||||
exports.AjaxResponse = AjaxResponse;
|
||||
/** A normalized AJAX error */
|
||||
var AjaxError = (function (_super) {
|
||||
__extends(AjaxError, _super);
|
||||
function AjaxError(message, xhr, request) {
|
||||
_super.call(this, message);
|
||||
this.message = message;
|
||||
this.xhr = xhr;
|
||||
this.request = request;
|
||||
this.status = xhr.status;
|
||||
}
|
||||
return AjaxError;
|
||||
}(Error));
|
||||
exports.AjaxError = AjaxError;
|
||||
var AjaxTimeoutError = (function (_super) {
|
||||
__extends(AjaxTimeoutError, _super);
|
||||
function AjaxTimeoutError(xhr, request) {
|
||||
_super.call(this, 'ajax timeout', xhr, request);
|
||||
}
|
||||
return AjaxTimeoutError;
|
||||
}(AjaxError));
|
||||
exports.AjaxTimeoutError = AjaxTimeoutError;
|
||||
//# sourceMappingURL=AjaxObservable.js.map
|
1
node_modules/rxjs/observable/dom/AjaxObservable.js.map
generated
vendored
Normal file
1
node_modules/rxjs/observable/dom/AjaxObservable.js.map
generated
vendored
Normal file
File diff suppressed because one or more lines are too long
35
node_modules/rxjs/observable/dom/WebSocketSubject.d.ts
generated
vendored
Normal file
35
node_modules/rxjs/observable/dom/WebSocketSubject.d.ts
generated
vendored
Normal file
@ -0,0 +1,35 @@
|
||||
import { Subject } from '../../Subject';
|
||||
import { Subscriber } from '../../Subscriber';
|
||||
import { Observable } from '../../Observable';
|
||||
import { Operator } from '../../Operator';
|
||||
import { Subscription } from '../../Subscription';
|
||||
import { Observer } from '../../Observer';
|
||||
export interface WebSocketSubjectConfig {
|
||||
url: string;
|
||||
protocol?: string | Array<string>;
|
||||
resultSelector?: <T>(e: MessageEvent) => T;
|
||||
openObserver?: Observer<Event>;
|
||||
closeObserver?: Observer<CloseEvent>;
|
||||
closingObserver?: Observer<void>;
|
||||
WebSocketCtor?: {
|
||||
new (url: string, protocol?: string | Array<string>): WebSocket;
|
||||
};
|
||||
}
|
||||
export declare class WebSocketSubject<T> extends Subject<T> {
|
||||
url: string;
|
||||
protocol: string | Array<string>;
|
||||
socket: WebSocket;
|
||||
openObserver: Observer<Event>;
|
||||
closeObserver: Observer<CloseEvent>;
|
||||
closingObserver: Observer<void>;
|
||||
WebSocketCtor: {
|
||||
new (url: string, protocol?: string | Array<string>): WebSocket;
|
||||
};
|
||||
resultSelector(e: MessageEvent): any;
|
||||
static create<T>(urlConfigOrSource: string | WebSocketSubjectConfig): WebSocketSubject<T>;
|
||||
constructor(urlConfigOrSource: string | WebSocketSubjectConfig | Observable<T>, destination?: Observer<T>);
|
||||
lift<R>(operator: Operator<T, R>): WebSocketSubject<T>;
|
||||
multiplex(subMsg: () => any, unsubMsg: () => any, messageFilter: (value: T) => boolean): Observable<{}>;
|
||||
protected _unsubscribe(): void;
|
||||
protected _subscribe(subscriber: Subscriber<T>): Subscription;
|
||||
}
|
175
node_modules/rxjs/observable/dom/WebSocketSubject.js
generated
vendored
Normal file
175
node_modules/rxjs/observable/dom/WebSocketSubject.js
generated
vendored
Normal file
@ -0,0 +1,175 @@
|
||||
"use strict";
|
||||
var __extends = (this && this.__extends) || function (d, b) {
|
||||
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
|
||||
function __() { this.constructor = d; }
|
||||
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
||||
};
|
||||
var Subject_1 = require('../../Subject');
|
||||
var Subscriber_1 = require('../../Subscriber');
|
||||
var Observable_1 = require('../../Observable');
|
||||
var Subscription_1 = require('../../Subscription');
|
||||
var root_1 = require('../../util/root');
|
||||
var ReplaySubject_1 = require('../../subject/ReplaySubject');
|
||||
var tryCatch_1 = require('../../util/tryCatch');
|
||||
var errorObject_1 = require('../../util/errorObject');
|
||||
var assign_1 = require('../../util/assign');
|
||||
var WebSocketSubject = (function (_super) {
|
||||
__extends(WebSocketSubject, _super);
|
||||
function WebSocketSubject(urlConfigOrSource, destination) {
|
||||
if (urlConfigOrSource instanceof Observable_1.Observable) {
|
||||
_super.call(this, destination, urlConfigOrSource);
|
||||
}
|
||||
else {
|
||||
_super.call(this);
|
||||
this.WebSocketCtor = root_1.root.WebSocket;
|
||||
if (typeof urlConfigOrSource === 'string') {
|
||||
this.url = urlConfigOrSource;
|
||||
}
|
||||
else {
|
||||
// WARNING: config object could override important members here.
|
||||
assign_1.assign(this, urlConfigOrSource);
|
||||
}
|
||||
if (!this.WebSocketCtor) {
|
||||
throw new Error('no WebSocket constructor can be found');
|
||||
}
|
||||
this.destination = new ReplaySubject_1.ReplaySubject();
|
||||
}
|
||||
}
|
||||
WebSocketSubject.prototype.resultSelector = function (e) {
|
||||
return JSON.parse(e.data);
|
||||
};
|
||||
WebSocketSubject.create = function (urlConfigOrSource) {
|
||||
return new WebSocketSubject(urlConfigOrSource);
|
||||
};
|
||||
WebSocketSubject.prototype.lift = function (operator) {
|
||||
var sock = new WebSocketSubject(this, this.destination);
|
||||
sock.operator = operator;
|
||||
return sock;
|
||||
};
|
||||
// TODO: factor this out to be a proper Operator/Subscriber implementation and eliminate closures
|
||||
WebSocketSubject.prototype.multiplex = function (subMsg, unsubMsg, messageFilter) {
|
||||
var self = this;
|
||||
return new Observable_1.Observable(function (observer) {
|
||||
var result = tryCatch_1.tryCatch(subMsg)();
|
||||
if (result === errorObject_1.errorObject) {
|
||||
observer.error(errorObject_1.errorObject.e);
|
||||
}
|
||||
else {
|
||||
self.next(result);
|
||||
}
|
||||
var subscription = self.subscribe(function (x) {
|
||||
var result = tryCatch_1.tryCatch(messageFilter)(x);
|
||||
if (result === errorObject_1.errorObject) {
|
||||
observer.error(errorObject_1.errorObject.e);
|
||||
}
|
||||
else if (result) {
|
||||
observer.next(x);
|
||||
}
|
||||
}, function (err) { return observer.error(err); }, function () { return observer.complete(); });
|
||||
return function () {
|
||||
var result = tryCatch_1.tryCatch(unsubMsg)();
|
||||
if (result === errorObject_1.errorObject) {
|
||||
observer.error(errorObject_1.errorObject.e);
|
||||
}
|
||||
else {
|
||||
self.next(result);
|
||||
}
|
||||
subscription.unsubscribe();
|
||||
};
|
||||
});
|
||||
};
|
||||
WebSocketSubject.prototype._unsubscribe = function () {
|
||||
this.socket = null;
|
||||
this.source = null;
|
||||
this.destination = new ReplaySubject_1.ReplaySubject();
|
||||
this.isStopped = false;
|
||||
this.hasErrored = false;
|
||||
this.hasCompleted = false;
|
||||
this.observers = null;
|
||||
this.isUnsubscribed = false;
|
||||
};
|
||||
WebSocketSubject.prototype._subscribe = function (subscriber) {
|
||||
var _this = this;
|
||||
if (!this.observers) {
|
||||
this.observers = [];
|
||||
}
|
||||
var subscription = _super.prototype._subscribe.call(this, subscriber);
|
||||
// HACK: For some reason transpilation wasn't honoring this in arrow functions below
|
||||
// Doesn't seem right, need to reinvestigate.
|
||||
var self = this;
|
||||
var WebSocket = this.WebSocketCtor;
|
||||
if (self.source || !subscription || subscription.isUnsubscribed) {
|
||||
return subscription;
|
||||
}
|
||||
if (self.url && !self.socket) {
|
||||
var socket_1 = self.protocol ? new WebSocket(self.url, self.protocol) : new WebSocket(self.url);
|
||||
self.socket = socket_1;
|
||||
socket_1.onopen = function (e) {
|
||||
var openObserver = self.openObserver;
|
||||
if (openObserver) {
|
||||
openObserver.next(e);
|
||||
}
|
||||
var queue = self.destination;
|
||||
self.destination = Subscriber_1.Subscriber.create(function (x) { return socket_1.readyState === 1 && socket_1.send(x); }, function (e) {
|
||||
var closingObserver = self.closingObserver;
|
||||
if (closingObserver) {
|
||||
closingObserver.next(undefined);
|
||||
}
|
||||
if (e && e.code) {
|
||||
socket_1.close(e.code, e.reason);
|
||||
}
|
||||
else {
|
||||
self._finalError(new TypeError('WebSocketSubject.error must be called with an object with an error code, ' +
|
||||
'and an optional reason: { code: number, reason: string }'));
|
||||
}
|
||||
}, function () {
|
||||
var closingObserver = self.closingObserver;
|
||||
if (closingObserver) {
|
||||
closingObserver.next(undefined);
|
||||
}
|
||||
socket_1.close();
|
||||
});
|
||||
if (queue && queue instanceof ReplaySubject_1.ReplaySubject) {
|
||||
subscription.add(queue.subscribe(self.destination));
|
||||
}
|
||||
};
|
||||
socket_1.onerror = function (e) { return self.error(e); };
|
||||
socket_1.onclose = function (e) {
|
||||
var closeObserver = self.closeObserver;
|
||||
if (closeObserver) {
|
||||
closeObserver.next(e);
|
||||
}
|
||||
if (e.wasClean) {
|
||||
self._finalComplete();
|
||||
}
|
||||
else {
|
||||
self._finalError(e);
|
||||
}
|
||||
};
|
||||
socket_1.onmessage = function (e) {
|
||||
var result = tryCatch_1.tryCatch(self.resultSelector)(e);
|
||||
if (result === errorObject_1.errorObject) {
|
||||
self._finalError(errorObject_1.errorObject.e);
|
||||
}
|
||||
else {
|
||||
self._finalNext(result);
|
||||
}
|
||||
};
|
||||
}
|
||||
return new Subscription_1.Subscription(function () {
|
||||
subscription.unsubscribe();
|
||||
if (!_this.observers || _this.observers.length === 0) {
|
||||
var socket = _this.socket;
|
||||
if (socket && socket.readyState < 2) {
|
||||
socket.close();
|
||||
}
|
||||
_this.socket = undefined;
|
||||
_this.source = undefined;
|
||||
_this.destination = new ReplaySubject_1.ReplaySubject();
|
||||
}
|
||||
});
|
||||
};
|
||||
return WebSocketSubject;
|
||||
}(Subject_1.Subject));
|
||||
exports.WebSocketSubject = WebSocketSubject;
|
||||
//# sourceMappingURL=WebSocketSubject.js.map
|
1
node_modules/rxjs/observable/dom/WebSocketSubject.js.map
generated
vendored
Normal file
1
node_modules/rxjs/observable/dom/WebSocketSubject.js.map
generated
vendored
Normal file
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user