@@ -112,6 +112,26 @@ export function share<T>(options?: ShareConfig<T>): OperatorFunction<T, T> {
112
112
// Create the subject if we don't have one yet.
113
113
subject = subject ?? connector ( ) ;
114
114
115
+ // Add the teardown directly to the subscriber - instead of returning it -
116
+ // so that the handling of the subscriber's unsubscription will be wired
117
+ // up _before_ the subscription to the source occurs. This is done so that
118
+ // the assignment to the source connection's `closed` property will be seen
119
+ // by synchronous firehose sources.
120
+ subscriber . add ( ( ) => {
121
+ refCount -- ;
122
+
123
+ // If we're resetting on refCount === 0, and it's 0, we only want to do
124
+ // that on "unsubscribe", really. Resetting on error or completion is a different
125
+ // configuration.
126
+ if ( resetOnRefCountZero && ! refCount && ! hasErrored && ! hasCompleted ) {
127
+ // We need to capture the connection before
128
+ // we reset (if we need to reset).
129
+ const conn = connection ;
130
+ reset ( ) ;
131
+ conn ?. unsubscribe ( ) ;
132
+ }
133
+ } ) ;
134
+
115
135
// The following line adds the subscription to the subscriber passed.
116
136
// Basically, `subscriber === subject.subscribe(subscriber)` is `true`.
117
137
subject . subscribe ( subscriber ) ;
@@ -147,21 +167,5 @@ export function share<T>(options?: ShareConfig<T>): OperatorFunction<T, T> {
147
167
} ) ;
148
168
from ( source ) . subscribe ( connection ) ;
149
169
}
150
-
151
- // This is also added to `subscriber`, technically.
152
- return ( ) => {
153
- refCount -- ;
154
-
155
- // If we're resetting on refCount === 0, and it's 0, we only want to do
156
- // that on "unsubscribe", really. Resetting on error or completion is a different
157
- // configuration.
158
- if ( resetOnRefCountZero && ! refCount && ! hasErrored && ! hasCompleted ) {
159
- // We need to capture the connection before
160
- // we reset (if we need to reset).
161
- const conn = connection ;
162
- reset ( ) ;
163
- conn ?. unsubscribe ( ) ;
164
- }
165
- } ;
166
170
} ) ;
167
171
}
0 commit comments