Skip to content

Commit

Permalink
issue(SDKFetch.get): 令多个先后不重叠的订阅能共享一个网络请求
Browse files Browse the repository at this point in the history
避免网络请求的共享随其间订阅数降为 0 (因为订阅之间没有时间重叠,所以
必然存在一个订阅结束之后,订阅数降为 0;后续订阅者进来,订阅数又升至大
于 0)而失效。

另外,由于请求必然会 complete 或者 error,所以不存在资源泄漏的问题。
  • Loading branch information
chuan6 committed Aug 14, 2019
1 parent 28e5811 commit 0d7e7d7
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 11 deletions.
1 change: 1 addition & 0 deletions src/Net/Http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'rxjs/add/observable/dom/ajax'
import 'rxjs/add/observable/empty'
import 'rxjs/add/operator/catch'
import 'rxjs/add/operator/map'
import 'rxjs/add/operator/publishReplay'
import { AjaxError } from 'rxjs/observable/dom/AjaxObservable'
import { Observable } from 'rxjs/Observable'
import { Observer } from 'rxjs/Observer'
Expand Down
5 changes: 2 additions & 3 deletions src/SDKFetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'rxjs/add/observable/defer'
import 'rxjs/add/observable/throw'
import 'rxjs/add/operator/catch'
import 'rxjs/add/operator/map'
import 'rxjs/add/operator/publishReplay'
import 'rxjs/add/operator/shareReplay'
import 'rxjs/add/operator/finally'
import { Observable } from 'rxjs/Observable'
import { Http, HttpErrorMessage, HttpResponseWithHeaders, getHttpWithResponseHeaders } from './Net/Http'
Expand Down Expand Up @@ -119,8 +119,7 @@ export class SDKFetch {
const tail = SDKFetch.fetchTail || Date.now()
const urlWithTail = appendQueryString(urlWithQuery, `_=${ tail }`)
dist = Observable.defer(() => http.setUrl(urlWithTail).get()['request'])
.publishReplay<any>(1)
.refCount()
.shareReplay<any>(1)
.finally(() => {
SDKFetch.FetchStack.delete(urlWithQuery)
})
Expand Down
23 changes: 18 additions & 5 deletions test/SDKFetch.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,23 @@ describe('SDKFetch', () => {
})
})

it('shouldn not re-use a matching request A if A is in finished', function* () {
it('should re-use a matching request A even if subscriber count has dropped to zero previously', function* () {
fetchMock.mock(urlMatcher, Observable.of({ body: { 'A': 'aaaa' } }).delay(10).toPromise())

const getA = sdkFetch.get(path, { value: 'A' })
const getANext = sdkFetch.get(path, { value: 'A' })

yield Observable.from([ getA, getANext ], Scheduler.asap)
.switch() // 确保 getA 的订阅与 getANext 的订阅在时间上是隔离的,
// 也就是在 getANext 被订阅前,请求源的订阅数降到了 0
.do((response) => {
expect(response).to.deep.equal({ 'A': 'aaaa' })
})

expect(fetchMock.calls(urlMatcher).length).to.equal(1)
})

it('should not re-use a matching request A if A is finished', function* () {
fetchMock.mock(urlMatcher, { body: { 'A': 'aaaa' } })

const getA = sdkFetch.get(path, { value: 'A' })
Expand All @@ -180,10 +196,7 @@ describe('SDKFetch', () => {
.take(3)
.subscribeOn(Scheduler.asap)

yield Observable.of(fetchMock.calls(urlMatcher).length)
.do((numberOfRequestsReceived) => {
expect(numberOfRequestsReceived).to.equal(2)
})
expect(fetchMock.calls(urlMatcher).length).to.equal(2)
})

it('should only apply `Http` object `mapFn`(internal-use only) once for each request sent', function* () {
Expand Down
6 changes: 3 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2694,9 +2694,9 @@ rxjs-marbles@^2.4.1:
lodash-es "^4.0.0"

rxjs@^5.4.3:
version "5.5.6"
resolved "https://registry.npmjs.org/rxjs/-/rxjs-5.5.6.tgz#e31fb96d6fd2ff1fd84bcea8ae9c02d007179c02"
integrity sha512-v4Q5HDC0FHAQ7zcBX7T2IL6O5ltl1a2GX4ENjPXg6SjDY69Cmx9v4113C99a4wGF16ClPv5Z8mghuYorVkg/kg==
version "5.5.12"
resolved "https://registry.npmjs.org/rxjs/-/rxjs-5.5.12.tgz#6fa61b8a77c3d793dbaf270bee2f43f652d741cc"
integrity sha512-xx2itnL5sBbqeeiVgNPVuQQ1nC8Jp2WfNJhXWHmElW9YmrpS9UVnNzhP3EH3HFqexO5Tlp8GhYY+WEcqcVMvGw==
dependencies:
symbol-observable "1.0.1"

Expand Down

0 comments on commit 0d7e7d7

Please sign in to comment.