I am currently exploring how to wrap an event callback from a library to an RxJS Observable in a unique way.
This library I am working with sets up handlers for events like so:
const someHandler = (args) => {};
const resultCallback = (result) => {};
Library.addHandler('SOME_EVENT', someHandler, resultCallback);
Library.removeHandler('SOME_EVENT', someHandler, resultCallback);
The resultCallback
here is a callback that confirms if the handler was successfully registered.
My goal is to pass the handler in as a parameter of the function, and then emit its result.
I am currently facing a challenge in figuring out how to emit the value of the handler from this function, while also retaining an object reference for handler removal.
addEventHandlerObservable<T>(handler: any): Observable<T> {
return new Observable(observer => {
SomeLibrary.addHandler('SOME_EVENT', handler,
(result) => {
// was it registered successfully?
if(result.failed) {
observer.error();
observer.complete();
}
});
});
}