Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(dependency): 升级 rxjs 6 #544

Open
wants to merge 3 commits into
base: release
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,14 @@
"moment": "^2.18.1",
"node-watch": "^0.5.8",
"nyc": "^11.2.1",
"reactivedb": "~0.10.2",
"reactivedb": "0.11.1-alpha.0-rxnotreeshake",
"rollup": "^0.60.5",
"rollup-plugin-alias": "^1.3.1",
"rollup-plugin-commonjs": "^8.4.1",
"rollup-plugin-node-resolve": "^3.0.0",
"rrule": "2.2.0",
"rxjs": "^5.4.3",
"rxjs-marbles": "^2.4.1",
"rxjs": "6",
"rxjs-marbles": "^4.3.1",
"semver": "^5.5.0",
"sinon": "^4.0.0",
"sinon-chai": "^2.14.0",
Expand Down
88 changes: 41 additions & 47 deletions src/Net/Http.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
import 'rxjs/add/observable/throw'
import 'rxjs/add/observable/dom/ajax'
import 'rxjs/add/observable/empty'
import 'rxjs/add/operator/catch'
import 'rxjs/add/operator/map'
import { AjaxError } from 'rxjs/observable/dom/AjaxObservable'
import { Observable } from 'rxjs/Observable'
import { Observer } from 'rxjs/Observer'
import { Subject } from 'rxjs/Subject'
import * as rx from '../rx'
import { parseHeaders } from '../utils/index'
import { testable } from '../testable'
import { forEach } from '../utils'
Expand All @@ -30,7 +22,7 @@ type MethodParams = {
url: string,
body?: any,
_opts: any,
errorAdapter$: Subject<HttpErrorMessage>,
errorAdapter$: rx.Subject<HttpErrorMessage>,
includeHeaders: boolean
}

Expand All @@ -40,9 +32,9 @@ const rxAjaxDefaultHeaderKey2NormKey = {
}

/**
* Observable.ajax 目前的实现,对请求头字段的已有设置检查没有遵循
* rxjs ajax 目前的实现,对请求头字段的已有设置检查没有遵循
* 头字段 key 不区分大小写的原则,比如:如果用户已經设置 `content-type`,
* Observable.ajax 内部会发现 `Content-Type` 没有设置,结果会
* rxjs ajax 内部会发现 `Content-Type` 没有设置,结果会
* 额外添加一个 `Content-Type` 字段,结果导致浏览器发现请求头字段里
* 既有 `content-type` 又有 `Content-Type`,出现问题。
*/
Expand All @@ -55,46 +47,48 @@ const coverRxAjaxHeadersBug = (normHeaders: {}) => {
})
}

export const HttpError$ = new Subject<HttpErrorMessage>() as any as Observable<HttpErrorMessage>
export const HttpError$ = new rx.Subject<HttpErrorMessage>() as any as rx.Observable<HttpErrorMessage>

export const createMethod = (method: AllowedHttpMethod) => (params: MethodParams): Observable<any> => {
export const createMethod = (method: AllowedHttpMethod) => (params: MethodParams): rx.Observable<any> => {
const { url, body, _opts, errorAdapter$, includeHeaders } = params

/* istanbul ignore if */
if (testable.UseXMLHTTPRequest && typeof window !== 'undefined') {
coverRxAjaxHeadersBug(_opts.headers)
return Observable.ajax({
return rx.ajax({
url, body, method,
headers: _opts.headers,
withCredentials: _opts.credentials === 'include',
responseType: _opts.responseType || 'json',
crossDomain: typeof _opts.crossDomain !== 'undefined' ? !!_opts.crossDomain : true
})
.map(value => {
const respBody = value.response
if (!includeHeaders) {
return respBody
}
const respHeaders = parseHeaders(value.xhr.getAllResponseHeaders())
return { headers: respHeaders, body: respBody }
})
.catch((e: AjaxError) => {
const headers = e.xhr.getAllResponseHeaders()
const errorResponse = new Response(new Blob([JSON.stringify(e.xhr.response)]), {
status: e.xhr.status,
statusText: e.xhr.statusText,
headers: headers.length ? parseHeaders(headers) : new Headers()
.pipe(
rx.map(value => {
const respBody = value.response
if (!includeHeaders) {
return respBody
}
const respHeaders = parseHeaders(value.xhr.getAllResponseHeaders())
return { headers: respHeaders, body: respBody }
}),
rx.catch((e: rx.AjaxError) => {
const headers = e.xhr.getAllResponseHeaders()
const errorResponse = new Response(new Blob([JSON.stringify(e.xhr.response)]), {
status: e.xhr.status,
statusText: e.xhr.statusText,
headers: headers.length ? parseHeaders(headers) : new Headers()
})
const requestInfo = { method, url, body }
const errorResponseClone = errorResponse.clone()

setTimeout(() => {
errorAdapter$.next({ ...requestInfo, error: errorResponseClone })
}, 10)
return rx.throw({ ...requestInfo, error: errorResponse })
})
const requestInfo = { method, url, body }
const errorResponseClone = errorResponse.clone()

setTimeout(() => {
errorAdapter$.next({ ...requestInfo, error: errorResponseClone })
}, 10)
return Observable.throw({ ...requestInfo, error: errorResponse })
})
)
} else { // 测试用分支
return Observable.create((observer: Observer<any>) => {
return new rx.Observable((observer) => {
const _options = {
... _opts,
method: method
Expand Down Expand Up @@ -138,16 +132,16 @@ export const createMethod = (method: AllowedHttpMethod) => (params: MethodParams

export const getHttpWithResponseHeaders = <T>(
url?: string,
errorAdapter$?: Subject<HttpErrorMessage>
errorAdapter$?: rx.Subject<HttpErrorMessage>
): Http<HttpResponseWithHeaders<T>> => {
return new Http<HttpResponseWithHeaders<T>>(url, errorAdapter$, true)
}

export class Http<T> {
private errorAdapter$: Subject<HttpErrorMessage>
private errorAdapter$: rx.Subject<HttpErrorMessage>
private cloned = false
private request: Observable<T> | undefined
public mapFn: (v$: Observable<T>) => Observable<any> = (dist$ => dist$)
private request: rx.Observable<T> | undefined
public mapFn: (v$: rx.Observable<T>) => rx.Observable<any> = (dist$ => dist$)

private static get = createMethod('get')
private static put = createMethod('put')
Expand All @@ -161,13 +155,13 @@ export class Http<T> {

constructor(
private url: string = '',
errorAdapter$?: Subject<HttpErrorMessage>,
errorAdapter$?: rx.Subject<HttpErrorMessage>,
private readonly includeHeaders: boolean = false
) {
if (errorAdapter$) {
this.errorAdapter$ = errorAdapter$
} else {
this.errorAdapter$ = HttpError$ as Subject<HttpErrorMessage>
this.errorAdapter$ = HttpError$ as rx.Subject<HttpErrorMessage>
}
}

Expand Down Expand Up @@ -225,14 +219,14 @@ export class Http<T> {
return this
}

send(): Observable<T> {
return this.request ? this.mapFn(this.request) : Observable.empty()
send(): rx.Observable<T> {
return this.request ? this.mapFn(this.request) : rx.empty()
}

clone() {
const result = new Http<T>(this.url, this.errorAdapter$)
if (!this.cloned && this.request) {
this.request = this.request.publishReplay(1).refCount()
this.request = this.request.pipe(rx.publishReplay(1), rx.refCount())
this.cloned = true
result.cloned = true
}
Expand Down
Loading