@@ -32,47 +32,49 @@ var getSubscriptionInternalState = getterFor(SUBSCRIPTION);
32
32
var getSubscriptionObserverInternalState = getterFor ( SUBSCRIPTION_OBSERVER ) ;
33
33
var Array = global . Array ;
34
34
35
- var cleanupSubscription = function ( subscriptionState ) {
36
- var cleanup = subscriptionState . cleanup ;
37
- if ( cleanup ) {
38
- subscriptionState . cleanup = undefined ;
39
- try {
40
- cleanup ( ) ;
41
- } catch ( error ) {
42
- hostReportErrors ( error ) ;
43
- }
44
- }
45
- } ;
46
-
47
- var subscriptionClosed = function ( subscriptionState ) {
48
- return subscriptionState . observer === undefined ;
35
+ var SubscriptionState = function ( observer ) {
36
+ this . observer = anObject ( observer ) ;
37
+ this . cleanup = undefined ;
38
+ this . subscriptionObserver = undefined ;
49
39
} ;
50
40
51
- var close = function ( subscriptionState ) {
52
- var subscription = subscriptionState . facade ;
53
- if ( ! DESCRIPTORS ) {
54
- subscription . closed = true ;
55
- var subscriptionObserver = subscriptionState . subscriptionObserver ;
56
- if ( subscriptionObserver ) subscriptionObserver . closed = true ;
57
- } subscriptionState . observer = undefined ;
41
+ SubscriptionState . prototype = {
42
+ type : SUBSCRIPTION ,
43
+ clean : function ( ) {
44
+ var cleanup = this . cleanup ;
45
+ if ( cleanup ) {
46
+ this . cleanup = undefined ;
47
+ try {
48
+ cleanup ( ) ;
49
+ } catch ( error ) {
50
+ hostReportErrors ( error ) ;
51
+ }
52
+ }
53
+ } ,
54
+ close : function ( ) {
55
+ if ( ! DESCRIPTORS ) {
56
+ var subscription = this . facade ;
57
+ var subscriptionObserver = this . subscriptionObserver ;
58
+ subscription . closed = true ;
59
+ if ( subscriptionObserver ) subscriptionObserver . closed = true ;
60
+ } this . observer = undefined ;
61
+ } ,
62
+ isClosed : function ( ) {
63
+ return this . observer === undefined ;
64
+ }
58
65
} ;
59
66
60
67
var Subscription = function ( observer , subscriber ) {
61
- var subscriptionState = setInternalState ( this , {
62
- type : SUBSCRIPTION ,
63
- cleanup : undefined ,
64
- observer : anObject ( observer ) ,
65
- subscriptionObserver : undefined
66
- } ) ;
68
+ var subscriptionState = setInternalState ( this , new SubscriptionState ( observer ) ) ;
67
69
var start ;
68
70
if ( ! DESCRIPTORS ) this . closed = false ;
69
71
try {
70
72
if ( start = getMethod ( observer , 'start' ) ) call ( start , observer , this ) ;
71
73
} catch ( error ) {
72
74
hostReportErrors ( error ) ;
73
75
}
74
- if ( subscriptionClosed ( subscriptionState ) ) return ;
75
- var subscriptionObserver = subscriptionState . subscriptionObserver = new SubscriptionObserver ( this ) ;
76
+ if ( subscriptionState . isClosed ( ) ) return ;
77
+ var subscriptionObserver = subscriptionState . subscriptionObserver = new SubscriptionObserver ( subscriptionState ) ;
76
78
try {
77
79
var cleanup = subscriber ( subscriptionObserver ) ;
78
80
var subscription = cleanup ;
@@ -82,38 +84,38 @@ var Subscription = function (observer, subscriber) {
82
84
} catch ( error ) {
83
85
subscriptionObserver . error ( error ) ;
84
86
return ;
85
- } if ( subscriptionClosed ( subscriptionState ) ) cleanupSubscription ( subscriptionState ) ;
87
+ } if ( subscriptionState . isClosed ( ) ) subscriptionState . clean ( ) ;
86
88
} ;
87
89
88
90
Subscription . prototype = redefineAll ( { } , {
89
91
unsubscribe : function unsubscribe ( ) {
90
92
var subscriptionState = getSubscriptionInternalState ( this ) ;
91
- if ( ! subscriptionClosed ( subscriptionState ) ) {
92
- close ( subscriptionState ) ;
93
- cleanupSubscription ( subscriptionState ) ;
93
+ if ( ! subscriptionState . isClosed ( ) ) {
94
+ subscriptionState . close ( ) ;
95
+ subscriptionState . clean ( ) ;
94
96
}
95
97
}
96
98
} ) ;
97
99
98
100
if ( DESCRIPTORS ) defineProperty ( Subscription . prototype , 'closed' , {
99
101
configurable : true ,
100
102
get : function ( ) {
101
- return subscriptionClosed ( getSubscriptionInternalState ( this ) ) ;
103
+ return getSubscriptionInternalState ( this ) . isClosed ( ) ;
102
104
}
103
105
} ) ;
104
106
105
- var SubscriptionObserver = function ( subscription ) {
107
+ var SubscriptionObserver = function ( subscriptionState ) {
106
108
setInternalState ( this , {
107
109
type : SUBSCRIPTION_OBSERVER ,
108
- subscription : subscription
110
+ subscriptionState : subscriptionState
109
111
} ) ;
110
112
if ( ! DESCRIPTORS ) this . closed = false ;
111
113
} ;
112
114
113
115
SubscriptionObserver . prototype = redefineAll ( { } , {
114
116
next : function next ( value ) {
115
- var subscriptionState = getSubscriptionInternalState ( getSubscriptionObserverInternalState ( this ) . subscription ) ;
116
- if ( ! subscriptionClosed ( subscriptionState ) ) {
117
+ var subscriptionState = getSubscriptionObserverInternalState ( this ) . subscriptionState ;
118
+ if ( ! subscriptionState . isClosed ( ) ) {
117
119
var observer = subscriptionState . observer ;
118
120
try {
119
121
var nextMethod = getMethod ( observer , 'next' ) ;
@@ -124,38 +126,38 @@ SubscriptionObserver.prototype = redefineAll({}, {
124
126
}
125
127
} ,
126
128
error : function error ( value ) {
127
- var subscriptionState = getSubscriptionInternalState ( getSubscriptionObserverInternalState ( this ) . subscription ) ;
128
- if ( ! subscriptionClosed ( subscriptionState ) ) {
129
+ var subscriptionState = getSubscriptionObserverInternalState ( this ) . subscriptionState ;
130
+ if ( ! subscriptionState . isClosed ( ) ) {
129
131
var observer = subscriptionState . observer ;
130
- close ( subscriptionState ) ;
132
+ subscriptionState . close ( ) ;
131
133
try {
132
134
var errorMethod = getMethod ( observer , 'error' ) ;
133
135
if ( errorMethod ) call ( errorMethod , observer , value ) ;
134
136
else hostReportErrors ( value ) ;
135
137
} catch ( err ) {
136
138
hostReportErrors ( err ) ;
137
- } cleanupSubscription ( subscriptionState ) ;
139
+ } subscriptionState . clean ( ) ;
138
140
}
139
141
} ,
140
142
complete : function complete ( ) {
141
- var subscriptionState = getSubscriptionInternalState ( getSubscriptionObserverInternalState ( this ) . subscription ) ;
142
- if ( ! subscriptionClosed ( subscriptionState ) ) {
143
+ var subscriptionState = getSubscriptionObserverInternalState ( this ) . subscriptionState ;
144
+ if ( ! subscriptionState . isClosed ( ) ) {
143
145
var observer = subscriptionState . observer ;
144
- close ( subscriptionState ) ;
146
+ subscriptionState . close ( ) ;
145
147
try {
146
148
var completeMethod = getMethod ( observer , 'complete' ) ;
147
149
if ( completeMethod ) call ( completeMethod , observer ) ;
148
150
} catch ( error ) {
149
151
hostReportErrors ( error ) ;
150
- } cleanupSubscription ( subscriptionState ) ;
152
+ } subscriptionState . clean ( ) ;
151
153
}
152
154
}
153
155
} ) ;
154
156
155
157
if ( DESCRIPTORS ) defineProperty ( SubscriptionObserver . prototype , 'closed' , {
156
158
configurable : true ,
157
159
get : function ( ) {
158
- return subscriptionClosed ( getSubscriptionInternalState ( getSubscriptionObserverInternalState ( this ) . subscription ) ) ;
160
+ return getSubscriptionObserverInternalState ( this ) . subscriptionState . isClosed ( ) ;
159
161
}
160
162
} ) ;
161
163
0 commit comments