5

我正在尝试使用以下方法将我的应用与应用内购买挂钩:https ://github.com/chirag04/react-native-in-app-utils

我有一个史诗,如果它成功,我想发出成功,如果失败,我想发出失败。像这样的东西:

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();
        }
      });
    });
};

但是我不确定如何编写mergeMap. 有没有办法做到这一点?

4

1 回答 1

3

InAppUtils.purchaseProduct似乎使用了节点样式的回调。有一个 RxJS 静态方法可以用来从这样的 API 调用创建一个 observable bindNodeCallback:.

在你的mergeMap,你应该能够做这样的事情

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()));
});
于 2017-04-02T05:23:05.260 回答