17
17
* limitations under the License.
18
18
*/
19
19
20
+ import { observer } from '.'
20
21
import Record from '../record'
21
22
import ResultSummary from '../result-summary'
22
23
@@ -34,7 +35,7 @@ interface StreamObserver {
34
35
* to it's onError method, otherwise set instance variable _error.
35
36
* @param {Object } error - An error object
36
37
*/
37
- onError ? : ( error : Error ) => void
38
+ onError : ( error : Error ) => void
38
39
onCompleted ?: ( meta : any ) => void
39
40
}
40
41
@@ -58,9 +59,9 @@ interface ResultObserver {
58
59
59
60
/**
60
61
* Called when the result is fully received
61
- * @param {ResultSummary } summary The result summary
62
+ * @param {ResultSummary| any } summary The result summary
62
63
*/
63
- onCompleted ?: ( summary : ResultSummary ) => void
64
+ onCompleted ?: ( summary : ResultSummary | any ) => void
64
65
65
66
/**
66
67
* Called when some error occurs during the result proccess or query execution
@@ -105,4 +106,74 @@ export interface ResultStreamObserver extends StreamObserver {
105
106
subscribe ( observer : ResultObserver ) : void
106
107
}
107
108
108
- // @todo implement observers
109
+ export class CompletedObserver implements ResultStreamObserver {
110
+ subscribe ( observer : ResultObserver ) : void {
111
+ apply ( observer , observer . onKeys , [ ] )
112
+ apply ( observer , observer . onCompleted , { } )
113
+ }
114
+
115
+ cancel ( ) : void {
116
+ // do nothing
117
+ }
118
+
119
+ prepareToHandleSingleResponse ( ) : void {
120
+ // do nothing
121
+ }
122
+
123
+ markCompleted ( ) : void {
124
+ // do nothing
125
+ }
126
+
127
+ onError ( error : Error ) : void {
128
+ // nothing to do, already finished
129
+ throw Error ( 'CompletedObserver not supposed to call onError' )
130
+ }
131
+ }
132
+
133
+ export class FailedObserver implements ResultStreamObserver {
134
+ private _error : Error
135
+ private _beforeError ?: ( error : Error ) => void
136
+ private _observers : ResultObserver [ ]
137
+
138
+ constructor ( {
139
+ error,
140
+ onError
141
+ } : {
142
+ error : Error
143
+ onError ?: ( error : Error ) => void | Promise < void >
144
+ } ) {
145
+ this . _error = error
146
+ this . _beforeError = onError
147
+ this . _observers = [ ]
148
+ this . onError ( error )
149
+ }
150
+
151
+ subscribe ( observer : ResultObserver ) : void {
152
+ apply ( observer , observer . onError , this . _error )
153
+ this . _observers . push ( observer )
154
+ }
155
+
156
+ onError ( error : Error ) : void {
157
+ Promise . resolve ( apply ( this , this . _beforeError , error ) ) . then ( ( ) =>
158
+ this . _observers . forEach ( o => apply ( o , o . onError , error ) )
159
+ )
160
+ }
161
+
162
+ cancel ( ) : void {
163
+ // do nothing
164
+ }
165
+
166
+ prepareToHandleSingleResponse ( ) : void {
167
+ // do nothing
168
+ }
169
+
170
+ markCompleted ( ) : void {
171
+ // do nothing
172
+ }
173
+ }
174
+
175
+ function apply < T > ( thisArg : any , func ?: ( param : T ) => void , param ?: T ) : void {
176
+ if ( func ) {
177
+ func . bind ( thisArg ) ( param )
178
+ }
179
+ }
0 commit comments