smartuniverse/ts/smartuniverse.classes.reactionresult.ts

53 lines
1.5 KiB
TypeScript
Raw Normal View History

2019-09-10 21:55:20 +00:00
import * as plugins from './smartuniverse.plugins';
import { ReactionResponse } from './smartuniverse.classes.reactionresponse';
export class ReactionResult<T extends plugins.typedrequestInterfaces.ITypedRequest> {
2019-09-17 11:57:34 +00:00
private resultReplaySubject = new plugins.smartrx.rxjs.ReplaySubject<T['response']>();
2019-09-10 21:55:20 +00:00
private endResult: Array<T['response']> = [];
private completeDeferred = plugins.smartpromise.defer<Array<T['response']>>();
2019-11-09 12:00:30 +00:00
constructor() {
2020-09-24 18:17:52 +00:00
this.resultSubscribe((responseArg) => {
2019-09-10 21:55:20 +00:00
this.endResult.push(responseArg);
});
}
public resultSubscribe(observerArg: (responseArg: T['response']) => void) {
2019-09-17 11:57:34 +00:00
return this.resultReplaySubject.subscribe(observerArg);
2019-09-10 21:55:20 +00:00
}
2019-09-17 10:46:35 +00:00
/**
* gets the end result as an array of all results
*/
2019-09-10 21:55:20 +00:00
public async getEndResult() {
const result = await this.completeDeferred.promise;
return result;
}
2019-09-17 10:46:35 +00:00
/**
* if there is a single respondant, or you are only interested in the first result
*/
public async getFirstResult() {
const done = plugins.smartpromise.defer<T['response']>();
2020-09-24 18:17:52 +00:00
const subscription = this.resultReplaySubject.subscribe((result) => {
2019-09-17 10:46:35 +00:00
done.resolve(result);
subscription.unsubscribe();
});
return await done.promise;
}
2019-09-10 21:55:20 +00:00
/**
* push a reactionResponse
*/
public async pushReactionResponse(responseArg: T['response']) {
2019-09-17 11:57:34 +00:00
this.resultReplaySubject.next(responseArg);
2019-09-10 21:55:20 +00:00
}
2019-11-09 12:00:30 +00:00
2019-09-10 21:55:20 +00:00
/**
* completes the ReactionResult
*/
public async complete() {
this.completeDeferred.resolve(this.endResult);
}
}