@@ -56,8 +56,14 @@ export interface ShareReplayConfig {
56
56
* @method shareReplay
57
57
* @owner Observable
58
58
*/
59
- export function shareReplay < T > ( config : ShareReplayConfig ) : MonoTypeOperatorFunction < T > ;
60
- export function shareReplay < T > ( bufferSize ?: number , windowTime ?: number , scheduler ?: SchedulerLike ) : MonoTypeOperatorFunction < T > ;
59
+ export function shareReplay < T > (
60
+ config : ShareReplayConfig
61
+ ) : MonoTypeOperatorFunction < T > ;
62
+ export function shareReplay < T > (
63
+ bufferSize ?: number ,
64
+ windowTime ?: number ,
65
+ scheduler ?: SchedulerLike
66
+ ) : MonoTypeOperatorFunction < T > ;
61
67
export function shareReplay < T > (
62
68
configOrBufferSize ?: ShareReplayConfig | number ,
63
69
windowTime ?: number ,
@@ -71,7 +77,7 @@ export function shareReplay<T>(
71
77
bufferSize : configOrBufferSize as number | undefined ,
72
78
windowTime,
73
79
refCount : false ,
74
- scheduler
80
+ scheduler,
75
81
} ;
76
82
}
77
83
return ( source : Observable < T > ) => source . lift ( shareReplayOperator ( config ) ) ;
@@ -81,23 +87,28 @@ function shareReplayOperator<T>({
81
87
bufferSize = Number . POSITIVE_INFINITY ,
82
88
windowTime = Number . POSITIVE_INFINITY ,
83
89
refCount : useRefCount ,
84
- scheduler
90
+ scheduler,
85
91
} : ShareReplayConfig ) {
86
92
let subject : ReplaySubject < T > | undefined ;
87
93
let refCount = 0 ;
88
94
let subscription : Subscription | undefined ;
89
95
let hasError = false ;
90
96
let isComplete = false ;
91
97
92
- return function shareReplayOperation ( this : Subscriber < T > , source : Observable < T > ) {
98
+ return function shareReplayOperation (
99
+ this : Subscriber < T > ,
100
+ source : Observable < T >
101
+ ) {
93
102
refCount ++ ;
94
103
let innerSub : Subscription ;
95
104
if ( ! subject || hasError ) {
96
105
hasError = false ;
97
106
subject = new ReplaySubject < T > ( bufferSize , windowTime , scheduler ) ;
98
107
innerSub = subject . subscribe ( this ) ;
99
108
subscription = source . subscribe ( {
100
- next ( value ) { subject . next ( value ) ; } ,
109
+ next ( value ) {
110
+ subject . next ( value ) ;
111
+ } ,
101
112
error ( err ) {
102
113
hasError = true ;
103
114
subject . error ( err ) ;
@@ -108,13 +119,22 @@ function shareReplayOperator<T>({
108
119
subject . complete ( ) ;
109
120
} ,
110
121
} ) ;
122
+
123
+ // Here we need to check to see if the source synchronously completed. Although
124
+ // we're setting `subscription = undefined` in the completion handler, if the source
125
+ // is synchronous, that will happen *before* subscription is set by the return of
126
+ // the `subscribe` call.
127
+ if ( isComplete ) {
128
+ subscription = undefined ;
129
+ }
111
130
} else {
112
131
innerSub = subject . subscribe ( this ) ;
113
132
}
114
133
115
134
this . add ( ( ) => {
116
135
refCount -- ;
117
136
innerSub . unsubscribe ( ) ;
137
+ innerSub = undefined ;
118
138
if ( subscription && ! isComplete && useRefCount && refCount === 0 ) {
119
139
subscription . unsubscribe ( ) ;
120
140
subscription = undefined ;
0 commit comments