Fix EsLint Brackets (error markup 'const').
This commit is contained in:
1 parent
75add7333d
commit
d20e8a9d10
6005 files changed
+337788
No files matched your search
+352
@@ -0,0 +1,352 @@
|
||||
import { root } from '../../util/root';
|
||||
import { tryCatch } from '../../util/tryCatch';
|
||||
import { errorObject } from '../../util/errorObject';
|
||||
import { Observable } from '../../Observable';
|
||||
import { Subscriber } from '../../Subscriber';
|
||||
import { map } from '../../operators/map';
|
||||
function getCORSRequest() {
|
||||
if (root.XMLHttpRequest) {
|
||||
return new root.XMLHttpRequest();
|
||||
}
|
||||
else if (!!root.XDomainRequest) {
|
||||
return new root.XDomainRequest();
|
||||
}
|
||||
else {
|
||||
throw new Error('CORS is not supported by your browser');
|
||||
}
|
||||
}
|
||||
function getXMLHttpRequest() {
|
||||
if (root.XMLHttpRequest) {
|
||||
return new root.XMLHttpRequest();
|
||||
}
|
||||
else {
|
||||
let progId;
|
||||
try {
|
||||
const progIds = ['Msxml2.XMLHTTP', 'Microsoft.XMLHTTP', 'Msxml2.XMLHTTP.4.0'];
|
||||
for (let i = 0; i < 3; i++) {
|
||||
try {
|
||||
progId = progIds[i];
|
||||
if (new root.ActiveXObject(progId)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
catch (e) {
|
||||
}
|
||||
}
|
||||
return new root.ActiveXObject(progId);
|
||||
}
|
||||
catch (e) {
|
||||
throw new Error('XMLHttpRequest is not supported by your browser');
|
||||
}
|
||||
}
|
||||
}
|
||||
export function ajaxGet(url, headers = null) {
|
||||
return new AjaxObservable({ method: 'GET', url, headers });
|
||||
}
|
||||
export function ajaxPost(url, body, headers) {
|
||||
return new AjaxObservable({ method: 'POST', url, body, headers });
|
||||
}
|
||||
export function ajaxDelete(url, headers) {
|
||||
return new AjaxObservable({ method: 'DELETE', url, headers });
|
||||
}
|
||||
export function ajaxPut(url, body, headers) {
|
||||
return new AjaxObservable({ method: 'PUT', url, body, headers });
|
||||
}
|
||||
export function ajaxPatch(url, body, headers) {
|
||||
return new AjaxObservable({ method: 'PATCH', url, body, headers });
|
||||
}
|
||||
const mapResponse = map((x, index) => x.response);
|
||||
export function ajaxGetJSON(url, headers) {
|
||||
return mapResponse(new AjaxObservable({
|
||||
method: 'GET',
|
||||
url,
|
||||
responseType: 'json',
|
||||
headers
|
||||
}));
|
||||
}
|
||||
export class AjaxObservable extends Observable {
|
||||
constructor(urlOrRequest) {
|
||||
super();
|
||||
const request = {
|
||||
async: true,
|
||||
createXHR: function () {
|
||||
return this.crossDomain ? getCORSRequest() : getXMLHttpRequest();
|
||||
},
|
||||
crossDomain: true,
|
||||
withCredentials: false,
|
||||
headers: {},
|
||||
method: 'GET',
|
||||
responseType: 'json',
|
||||
timeout: 0
|
||||
};
|
||||
if (typeof urlOrRequest === 'string') {
|
||||
request.url = urlOrRequest;
|
||||
}
|
||||
else {
|
||||
for (const prop in urlOrRequest) {
|
||||
if (urlOrRequest.hasOwnProperty(prop)) {
|
||||
request[prop] = urlOrRequest[prop];
|
||||
}
|
||||
}
|
||||
}
|
||||
this.request = request;
|
||||
}
|
||||
_subscribe(subscriber) {
|
||||
return new AjaxSubscriber(subscriber, this.request);
|
||||
}
|
||||
}
|
||||
AjaxObservable.create = (() => {
|
||||
const create = (urlOrRequest) => {
|
||||
return new AjaxObservable(urlOrRequest);
|
||||
};
|
||||
create.get = ajaxGet;
|
||||
create.post = ajaxPost;
|
||||
create.delete = ajaxDelete;
|
||||
create.put = ajaxPut;
|
||||
create.patch = ajaxPatch;
|
||||
create.getJSON = ajaxGetJSON;
|
||||
return create;
|
||||
})();
|
||||
export class AjaxSubscriber extends Subscriber {
|
||||
constructor(destination, request) {
|
||||
super(destination);
|
||||
this.request = request;
|
||||
this.done = false;
|
||||
const headers = request.headers = request.headers || {};
|
||||
if (!request.crossDomain && !headers['X-Requested-With']) {
|
||||
headers['X-Requested-With'] = 'XMLHttpRequest';
|
||||
}
|
||||
if (!('Content-Type' in headers) && !(root.FormData && request.body instanceof root.FormData) && typeof request.body !== 'undefined') {
|
||||
headers['Content-Type'] = 'application/x-www-form-urlencoded; charset=UTF-8';
|
||||
}
|
||||
request.body = this.serializeBody(request.body, request.headers['Content-Type']);
|
||||
this.send();
|
||||
}
|
||||
next(e) {
|
||||
this.done = true;
|
||||
const { xhr, request, destination } = this;
|
||||
const response = new AjaxResponse(e, xhr, request);
|
||||
if (response.response === errorObject) {
|
||||
destination.error(errorObject.e);
|
||||
}
|
||||
else {
|
||||
destination.next(response);
|
||||
}
|
||||
}
|
||||
send() {
|
||||
const { request, request: { user, method, url, async, password, headers, body } } = this;
|
||||
const createXHR = request.createXHR;
|
||||
const xhr = tryCatch(createXHR).call(request);
|
||||
if (xhr === errorObject) {
|
||||
this.error(errorObject.e);
|
||||
}
|
||||
else {
|
||||
this.xhr = xhr;
|
||||
this.setupEvents(xhr, request);
|
||||
let result;
|
||||
if (user) {
|
||||
result = tryCatch(xhr.open).call(xhr, method, url, async, user, password);
|
||||
}
|
||||
else {
|
||||
result = tryCatch(xhr.open).call(xhr, method, url, async);
|
||||
}
|
||||
if (result === errorObject) {
|
||||
this.error(errorObject.e);
|
||||
return null;
|
||||
}
|
||||
if (async) {
|
||||
xhr.timeout = request.timeout;
|
||||
xhr.responseType = request.responseType;
|
||||
}
|
||||
if ('withCredentials' in xhr) {
|
||||
xhr.withCredentials = !!request.withCredentials;
|
||||
}
|
||||
this.setHeaders(xhr, headers);
|
||||
result = body ? tryCatch(xhr.send).call(xhr, body) : tryCatch(xhr.send).call(xhr);
|
||||
if (result === errorObject) {
|
||||
this.error(errorObject.e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
return xhr;
|
||||
}
|
||||
serializeBody(body, contentType) {
|
||||
if (!body || typeof body === 'string') {
|
||||
return body;
|
||||
}
|
||||
else if (root.FormData && body instanceof root.FormData) {
|
||||
return body;
|
||||
}
|
||||
if (contentType) {
|
||||
const splitIndex = contentType.indexOf(';');
|
||||
if (splitIndex !== -1) {
|
||||
contentType = contentType.substring(0, splitIndex);
|
||||
}
|
||||
}
|
||||
switch (contentType) {
|
||||
case 'application/x-www-form-urlencoded':
|
||||
return Object.keys(body).map(key => `${encodeURIComponent(key)}=${encodeURIComponent(body[key])}`).join('&');
|
||||
case 'application/json':
|
||||
return JSON.stringify(body);
|
||||
default:
|
||||
return body;
|
||||
}
|
||||
}
|
||||
setHeaders(xhr, headers) {
|
||||
for (let key in headers) {
|
||||
if (headers.hasOwnProperty(key)) {
|
||||
xhr.setRequestHeader(key, headers[key]);
|
||||
}
|
||||
}
|
||||
}
|
||||
setupEvents(xhr, request) {
|
||||
const progressSubscriber = request.progressSubscriber;
|
||||
function xhrTimeout(e) {
|
||||
const { subscriber, progressSubscriber, request } = xhrTimeout;
|
||||
if (progressSubscriber) {
|
||||
progressSubscriber.error(e);
|
||||
}
|
||||
const ajaxTimeoutError = new AjaxTimeoutError(this, request);
|
||||
if (ajaxTimeoutError.response === errorObject) {
|
||||
subscriber.error(errorObject.e);
|
||||
}
|
||||
else {
|
||||
subscriber.error(ajaxTimeoutError);
|
||||
}
|
||||
}
|
||||
xhr.ontimeout = xhrTimeout;
|
||||
xhrTimeout.request = request;
|
||||
xhrTimeout.subscriber = this;
|
||||
xhrTimeout.progressSubscriber = progressSubscriber;
|
||||
if (xhr.upload && 'withCredentials' in xhr) {
|
||||
if (progressSubscriber) {
|
||||
let xhrProgress;
|
||||
xhrProgress = function (e) {
|
||||
const { progressSubscriber } = xhrProgress;
|
||||
progressSubscriber.next(e);
|
||||
};
|
||||
if (root.XDomainRequest) {
|
||||
xhr.onprogress = xhrProgress;
|
||||
}
|
||||
else {
|
||||
xhr.upload.onprogress = xhrProgress;
|
||||
}
|
||||
xhrProgress.progressSubscriber = progressSubscriber;
|
||||
}
|
||||
let xhrError;
|
||||
xhrError = function (e) {
|
||||
const { progressSubscriber, subscriber, request } = xhrError;
|
||||
if (progressSubscriber) {
|
||||
progressSubscriber.error(e);
|
||||
}
|
||||
const ajaxError = new AjaxError('ajax error', this, request);
|
||||
if (ajaxError.response === errorObject) {
|
||||
subscriber.error(errorObject.e);
|
||||
}
|
||||
else {
|
||||
subscriber.error(ajaxError);
|
||||
}
|
||||
};
|
||||
xhr.onerror = xhrError;
|
||||
xhrError.request = request;
|
||||
xhrError.subscriber = this;
|
||||
xhrError.progressSubscriber = progressSubscriber;
|
||||
}
|
||||
function xhrReadyStateChange(e) {
|
||||
return;
|
||||
}
|
||||
xhr.onreadystatechange = xhrReadyStateChange;
|
||||
xhrReadyStateChange.subscriber = this;
|
||||
xhrReadyStateChange.progressSubscriber = progressSubscriber;
|
||||
xhrReadyStateChange.request = request;
|
||||
function xhrLoad(e) {
|
||||
const { subscriber, progressSubscriber, request } = xhrLoad;
|
||||
if (this.readyState === 4) {
|
||||
let status = this.status === 1223 ? 204 : this.status;
|
||||
let response = (this.responseType === 'text' ? (this.response || this.responseText) : this.response);
|
||||
if (status === 0) {
|
||||
status = response ? 200 : 0;
|
||||
}
|
||||
if (status < 400) {
|
||||
if (progressSubscriber) {
|
||||
progressSubscriber.complete();
|
||||
}
|
||||
subscriber.next(e);
|
||||
subscriber.complete();
|
||||
}
|
||||
else {
|
||||
if (progressSubscriber) {
|
||||
progressSubscriber.error(e);
|
||||
}
|
||||
const ajaxError = new AjaxError('ajax error ' + status, this, request);
|
||||
if (ajaxError.response === errorObject) {
|
||||
subscriber.error(errorObject.e);
|
||||
}
|
||||
else {
|
||||
subscriber.error(ajaxError);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
xhr.onload = xhrLoad;
|
||||
xhrLoad.subscriber = this;
|
||||
xhrLoad.progressSubscriber = progressSubscriber;
|
||||
xhrLoad.request = request;
|
||||
}
|
||||
unsubscribe() {
|
||||
const { done, xhr } = this;
|
||||
if (!done && xhr && xhr.readyState !== 4 && typeof xhr.abort === 'function') {
|
||||
xhr.abort();
|
||||
}
|
||||
super.unsubscribe();
|
||||
}
|
||||
}
|
||||
export class AjaxResponse {
|
||||
constructor(originalEvent, xhr, request) {
|
||||
this.originalEvent = originalEvent;
|
||||
this.xhr = xhr;
|
||||
this.request = request;
|
||||
this.status = xhr.status;
|
||||
this.responseType = xhr.responseType || request.responseType;
|
||||
this.response = parseXhrResponse(this.responseType, xhr);
|
||||
}
|
||||
}
|
||||
function AjaxErrorImpl(message, xhr, request) {
|
||||
Error.call(this);
|
||||
this.message = message;
|
||||
this.name = 'AjaxError';
|
||||
this.xhr = xhr;
|
||||
this.request = request;
|
||||
this.status = xhr.status;
|
||||
this.responseType = xhr.responseType || request.responseType;
|
||||
this.response = parseXhrResponse(this.responseType, xhr);
|
||||
return this;
|
||||
}
|
||||
AjaxErrorImpl.prototype = Object.create(Error.prototype);
|
||||
export const AjaxError = AjaxErrorImpl;
|
||||
function parseJson(xhr) {
|
||||
if ('response' in xhr) {
|
||||
return xhr.responseType ? xhr.response : JSON.parse(xhr.response || xhr.responseText || 'null');
|
||||
}
|
||||
else {
|
||||
return JSON.parse(xhr.responseText || 'null');
|
||||
}
|
||||
}
|
||||
function parseXhrResponse(responseType, xhr) {
|
||||
switch (responseType) {
|
||||
case 'json':
|
||||
return tryCatch(parseJson)(xhr);
|
||||
case 'xml':
|
||||
return xhr.responseXML;
|
||||
case 'text':
|
||||
default:
|
||||
return ('response' in xhr) ? xhr.response : xhr.responseText;
|
||||
}
|
||||
}
|
||||
function AjaxTimeoutErrorImpl(xhr, request) {
|
||||
AjaxError.call(this, 'ajax timeout', xhr, request);
|
||||
this.name = 'AjaxTimeoutError';
|
||||
return this;
|
||||
}
|
||||
export const AjaxTimeoutError = AjaxTimeoutErrorImpl;
|
||||
//# sourceMappingURL=AjaxObservable.js.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"AjaxObservable.js","sources":["../../../../src/internal/observable/dom/AjaxObservable.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,iBAAiB,CAAC;AACvC,OAAO,EAAE,QAAQ,EAAE,MAAM,qBAAqB,CAAC;AAC/C,OAAO,EAAE,WAAW,EAAE,MAAM,wBAA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G,EAAE,KAAK,EAAE,QAAQ,EAAE,OAAO,EAAE,IAAI,EAAE,EAC/D,GAAG,IAAI,CAAC;QACT,MAAM,SAAS,GAAG,OAAO,CAAC,SAAS,CAAC;QACpC,MAAM,GAAG,GAAmB,QAAQ,CAAC,SAAS,CAAC,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;QAE9D,IAAS,GAAG,KAAK,WAAW,EAAE;YAC5B,IAAI,CAAC,KAAK,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC;SAC3B;aAAM;YACL,IAAI,CAAC,GAAG,GAAG,GAAG,CAAC;YAMf,IAAI,CAAC,WAAW,CAAC,GAAG,EAAE,OAAO,CAAC,CAAC;YAE/B,IAAI,MAAW,CAAC;YAChB,IAAI,IAAI,EAAE;gBACR,MAAM,GAAG,QAAQ,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC,IAAI,CAAC,GAAG,EAAE,MAAM,EAAE,GAAG,EAAE,KAAK,EAAE,IAAI,EAAE,QAAQ,CAAC,CAAC;aAC3E;iBAAM;gBACL,MAAM,GAAG,QAAQ,CAAC,GAAG,CAACLine truncated
|
||||
+210
@@ -0,0 +1,210 @@
|
||||
import { Subject, AnonymousSubject } from '../../Subject';
|
||||
import { Subscriber } from '../../Subscriber';
|
||||
import { Observable } from '../../Observable';
|
||||
import { Subscription } from '../../Subscription';
|
||||
import { ReplaySubject } from '../../ReplaySubject';
|
||||
import { tryCatch } from '../../util/tryCatch';
|
||||
import { errorObject } from '../../util/errorObject';
|
||||
const DEFAULT_WEBSOCKET_CONFIG = {
|
||||
url: '',
|
||||
deserializer: (e) => JSON.parse(e.data),
|
||||
serializer: (value) => JSON.stringify(value),
|
||||
};
|
||||
const WEBSOCKETSUBJECT_INVALID_ERROR_OBJECT = 'WebSocketSubject.error must be called with an object with an error code, and an optional reason: { code: number, reason: string }';
|
||||
export class WebSocketSubject extends AnonymousSubject {
|
||||
constructor(urlConfigOrSource, destination) {
|
||||
super();
|
||||
if (urlConfigOrSource instanceof Observable) {
|
||||
this.destination = destination;
|
||||
this.source = urlConfigOrSource;
|
||||
}
|
||||
else {
|
||||
const config = this._config = Object.assign({}, DEFAULT_WEBSOCKET_CONFIG);
|
||||
this._output = new Subject();
|
||||
if (typeof urlConfigOrSource === 'string') {
|
||||
config.url = urlConfigOrSource;
|
||||
}
|
||||
else {
|
||||
for (let key in urlConfigOrSource) {
|
||||
if (urlConfigOrSource.hasOwnProperty(key)) {
|
||||
config[key] = urlConfigOrSource[key];
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!config.WebSocketCtor && WebSocket) {
|
||||
config.WebSocketCtor = WebSocket;
|
||||
}
|
||||
else if (!config.WebSocketCtor) {
|
||||
throw new Error('no WebSocket constructor can be found');
|
||||
}
|
||||
this.destination = new ReplaySubject();
|
||||
}
|
||||
}
|
||||
lift(operator) {
|
||||
const sock = new WebSocketSubject(this._config, this.destination);
|
||||
sock.operator = operator;
|
||||
sock.source = this;
|
||||
return sock;
|
||||
}
|
||||
_resetState() {
|
||||
this._socket = null;
|
||||
if (!this.source) {
|
||||
this.destination = new ReplaySubject();
|
||||
}
|
||||
this._output = new Subject();
|
||||
}
|
||||
multiplex(subMsg, unsubMsg, messageFilter) {
|
||||
const self = this;
|
||||
return new Observable((observer) => {
|
||||
const result = tryCatch(subMsg)();
|
||||
if (result === errorObject) {
|
||||
observer.error(errorObject.e);
|
||||
}
|
||||
else {
|
||||
self.next(result);
|
||||
}
|
||||
let subscription = self.subscribe(x => {
|
||||
const result = tryCatch(messageFilter)(x);
|
||||
if (result === errorObject) {
|
||||
observer.error(errorObject.e);
|
||||
}
|
||||
else if (result) {
|
||||
observer.next(x);
|
||||
}
|
||||
}, err => observer.error(err), () => observer.complete());
|
||||
return () => {
|
||||
const result = tryCatch(unsubMsg)();
|
||||
if (result === errorObject) {
|
||||
observer.error(errorObject.e);
|
||||
}
|
||||
else {
|
||||
self.next(result);
|
||||
}
|
||||
subscription.unsubscribe();
|
||||
};
|
||||
});
|
||||
}
|
||||
_connectSocket() {
|
||||
const { WebSocketCtor, protocol, url, binaryType } = this._config;
|
||||
const observer = this._output;
|
||||
let socket = null;
|
||||
try {
|
||||
socket = protocol ?
|
||||
new WebSocketCtor(url, protocol) :
|
||||
new WebSocketCtor(url);
|
||||
this._socket = socket;
|
||||
if (binaryType) {
|
||||
this._socket.binaryType = binaryType;
|
||||
}
|
||||
}
|
||||
catch (e) {
|
||||
observer.error(e);
|
||||
return;
|
||||
}
|
||||
const subscription = new Subscription(() => {
|
||||
this._socket = null;
|
||||
if (socket && socket.readyState === 1) {
|
||||
socket.close();
|
||||
}
|
||||
});
|
||||
socket.onopen = (e) => {
|
||||
const { openObserver } = this._config;
|
||||
if (openObserver) {
|
||||
openObserver.next(e);
|
||||
}
|
||||
const queue = this.destination;
|
||||
this.destination = Subscriber.create((x) => {
|
||||
if (socket.readyState === 1) {
|
||||
const { serializer } = this._config;
|
||||
const msg = tryCatch(serializer)(x);
|
||||
if (msg === errorObject) {
|
||||
this.destination.error(errorObject.e);
|
||||
return;
|
||||
}
|
||||
socket.send(msg);
|
||||
}
|
||||
}, (e) => {
|
||||
const { closingObserver } = this._config;
|
||||
if (closingObserver) {
|
||||
closingObserver.next(undefined);
|
||||
}
|
||||
if (e && e.code) {
|
||||
socket.close(e.code, e.reason);
|
||||
}
|
||||
else {
|
||||
observer.error(new TypeError(WEBSOCKETSUBJECT_INVALID_ERROR_OBJECT));
|
||||
}
|
||||
this._resetState();
|
||||
}, () => {
|
||||
const { closingObserver } = this._config;
|
||||
if (closingObserver) {
|
||||
closingObserver.next(undefined);
|
||||
}
|
||||
socket.close();
|
||||
this._resetState();
|
||||
});
|
||||
if (queue && queue instanceof ReplaySubject) {
|
||||
subscription.add(queue.subscribe(this.destination));
|
||||
}
|
||||
};
|
||||
socket.onerror = (e) => {
|
||||
this._resetState();
|
||||
observer.error(e);
|
||||
};
|
||||
socket.onclose = (e) => {
|
||||
this._resetState();
|
||||
const { closeObserver } = this._config;
|
||||
if (closeObserver) {
|
||||
closeObserver.next(e);
|
||||
}
|
||||
if (e.wasClean) {
|
||||
observer.complete();
|
||||
}
|
||||
else {
|
||||
observer.error(e);
|
||||
}
|
||||
};
|
||||
socket.onmessage = (e) => {
|
||||
const { deserializer } = this._config;
|
||||
const result = tryCatch(deserializer)(e);
|
||||
if (result === errorObject) {
|
||||
observer.error(errorObject.e);
|
||||
}
|
||||
else {
|
||||
observer.next(result);
|
||||
}
|
||||
};
|
||||
}
|
||||
_subscribe(subscriber) {
|
||||
const { source } = this;
|
||||
if (source) {
|
||||
return source.subscribe(subscriber);
|
||||
}
|
||||
if (!this._socket) {
|
||||
this._connectSocket();
|
||||
}
|
||||
this._output.subscribe(subscriber);
|
||||
subscriber.add(() => {
|
||||
const { _socket } = this;
|
||||
if (this._output.observers.length === 0) {
|
||||
if (_socket && _socket.readyState === 1) {
|
||||
_socket.close();
|
||||
}
|
||||
this._resetState();
|
||||
}
|
||||
});
|
||||
return subscriber;
|
||||
}
|
||||
unsubscribe() {
|
||||
const { source, _socket } = this;
|
||||
if (_socket && _socket.readyState === 1) {
|
||||
_socket.close();
|
||||
this._resetState();
|
||||
}
|
||||
super.unsubscribe();
|
||||
if (!source) {
|
||||
this.destination = new ReplaySubject();
|
||||
}
|
||||
}
|
||||
}
|
||||
//# sourceMappingURL=WebSocketSubject.js.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"WebSocketSubject.js","sources":["../../../../src/internal/observable/dom/WebSocketSubject.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,gBAAgB,EAAE,MAAM,eAAe,CAAC;AAC1D,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAC9C,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAC9C,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAElD,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AAEpD,OAAO,EAAE,QAAQ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,EAAE;gBACH,MAAM,EAAE,eAAe,EAAE,GAAG,IAAI,CAAC,OAAO,CAAC;gBACzC,IAAI,eAAe,EAAE;oBACnB,eAAe,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC;iBACjC;gBACD,MAAM,CAAC,KAAK,EAAE,CAAC;gBACf,IAAI,CAAC,WAAW,EAAE,CAAC;YACrB,CAAC,CACiB,CAAC;YAErB,IAAI,KAAK,IAAI,KAAK,YAAY,aAAa,EAAE;gBAC3C,YAAY,CAAC,GAAG,CAAoB,KAAM,CAAC,SAAS,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC,CAAC;aACzE;QACH,CAAC,CAAC;QAEF,MAAM,CAAC,OAAO,GAAG,CAAC,CAAQ,EAAE,EAAE;YAC5B,IAAI,CAAC,WAAW,EAAE,CAAC;YACnB,QAAQ,CAAC,KAAK,Line truncated
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
import { AjaxObservable } from './AjaxObservable';
|
||||
export const ajax = AjaxObservable.create;
|
||||
//# sourceMappingURL=ajax.js.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"ajax.js","sources":["../../../../src/internal/observable/dom/ajax.ts"],"names":[],"mappings":"AAAA,OAAO,EAAG,cAAc,EAAuB,MAAM,kBAAkB,CAAC;AAkBxE,MAAM,CAAC,MAAM,IAAI,GAAuB,cAAc,CAAC,MAAM,CAAC"}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
import { WebSocketSubject } from './WebSocketSubject';
|
||||
export function webSocket(urlConfigOrSource) {
|
||||
return new WebSocketSubject(urlConfigOrSource);
|
||||
}
|
||||
//# sourceMappingURL=webSocket.js.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"webSocket.js","sources":["../../../../src/internal/observable/dom/webSocket.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAA0B,MAAM,oBAAoB,CAAC;AA4J9E,MAAM,UAAU,SAAS,CAAI,iBAAqD;IAChF,OAAO,IAAI,gBAAgB,CAAI,iBAAiB,CAAC,CAAC;AACpD,CAAC"}
|
||||
Reference in new issue
Block a user