1717 * limitations under the License.
1818 */
1919
20+ import { observer } from '.'
2021import Record from '../record'
2122import ResultSummary from '../result-summary'
2223
@@ -34,7 +35,7 @@ interface StreamObserver {
3435 * to it's onError method, otherwise set instance variable _error.
3536 * @param {Object } error - An error object
3637 */
37- onError ? : ( error : Error ) => void
38+ onError : ( error : Error ) => void
3839 onCompleted ?: ( meta : any ) => void
3940}
4041
@@ -58,9 +59,9 @@ interface ResultObserver {
5859
5960 /**
6061 * Called when the result is fully received
61- * @param {ResultSummary } summary The result summary
62+ * @param {ResultSummary| any } summary The result summary
6263 */
63- onCompleted ?: ( summary : ResultSummary ) => void
64+ onCompleted ?: ( summary : ResultSummary | any ) => void
6465
6566 /**
6667 * Called when some error occurs during the result proccess or query execution
@@ -105,4 +106,74 @@ export interface ResultStreamObserver extends StreamObserver {
105106 subscribe ( observer : ResultObserver ) : void
106107}
107108
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