Implementing a Custom Function in Observable for Subscribers (defer)
I have created an Observable using event streams, specifically Bluetooth notifications.
My goal is to execute a function (startNotifictions) only when the Observable has a subscriber.
This piece of code used to work on previous versions with Ionic3 framework. It introduced a new operator that would run when subscribed. However, now there are type errors popping up, complaining about the absence of .doOnSubscribe on typedef Observable any> and <{}>.
Does anyone know how to correct this typing issue? Perhaps through extension? I attempted to utilize .defer directly, but it didn't work.
// add operator doOnSubscribe to the event observable
Observable.prototype.doOnSubscribe = function(onSubscribe) {
let source = this;
return Observable.defer(() => {
onSubscribe();
return source;
});
};
// return the Observable for the notify char, with startNotify on first subscribe
getUartDataNote( Observable.fromEvent( this.uartChar, 'characteristicvaluechanged' )
.doOnSubscribe(() => {
console.log('starting note');
this.uartChar.startNotifications();
})
.map( value => String.fromCharCode.apply( null, new Uint8Array( this.uartChar.value.buffer )))
.takeUntil( Observable.fromEvent( this.gatt.device, 'gattserverdisconnected' ))
.finally(() => {
console.log( 'stream disconnected ');
// not necessary: return this.uartChar.stopNotifications()
})
.share()
);