I am trying to hook up my app with in app purchases using this: https://github.com/chirag04/react-native-in-app-utils
I have an epic where I want to emit success if it succeeds, and failure if it fails. Something like this:
import 'rxjs';
import { InAppUtils } from 'NativeModules';
import * as packagesActions from '../ducks/packages';
import * as subscriptionActions from '../ducks/subscription';
export default function createSubscription(action$, store) {
return action$.ofType(packagesActions.SELECT)
.mergeMap(action => {
const productId = action.payload;
InAppUtils.purchaseProduct(productId, (error, response) => {
if(response && response.productIdentifier) {
return subscriptionActions.subscribeSuccess();
} else {
return subscriptionActions.subscribeFailure();
}
});
});
};
However I'm not sure how to write the contents of mergeMap
. Is there a way to do this?
InAppUtils.purchaseProduct
appears to use a Node-style callback. There is an RxJS static method that can be used to create an observable from such an API call: bindNodeCallback
.
Within your mergeMap
, you should be able to do something like this
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/observable/bindNodeCallback';
import 'rxjs/add/observable/of';
...
.mergeMap(action => {
const productId = action.payload;
const bound = Observable.bindNodeCallback((callback) => InAppUtils.purchaseProduct(productId, callback));
return bound()
.map(response => subscriptionActions.subscribeSuccess())
.catch(error => Observable.of(subscriptionActions.subscribeFailure()));
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With