有没有更好的方法来形成这个代码示例?

Is there a better way to form this code example?

我是 rxjs 的新手,正在使用 redux-observable。简而言之,当我获得连接然后输出结果时,我需要发出几个承诺请求。我想知道是否有一种方法可以在最后将其加入到单个地图中,而不必多次调用 store.dispatch 并让每个单独的读取都进行重试。提前感谢您的评论。

export const handleBleConnectionSuccess = (action$,store,{bleCommunicator}) =>
  action$.ofType(c.BLE_CONNECTION_SUCCESS)
    .do((a)=>{
      Observable.fromPromise(bleCommunicator.readCharacteristic(a.device.id,gattInfo.uuid,gattInfo.firmwareRevision.uuid))
        .do((value)=>store.dispatch({type:c.DEVICE_FIRMWARE_VERSION,device:{...a.device,firmwareVersion:value}}))
        .retry(3);

      Observable.fromPromise(bleCommunicator.readCharacteristic(a.device.id,gattInfo.uuid,gattInfo.modelNumber.uuid))
        .do(value=>store.dispatch({type:c.DEVICE_MODEL_NUMBER,device:{...a.device,modelNumber:value}}))
        .retry(3);
    })
    .mapTo({type:'DEVICE_INFORMATION_REQUESTED'});

I'm wondering if there is a way to join this into a single map at the end and not have to call store.dispatch multiple times and have the retry work for each individual read

是的,有更好的方法,而且可以为所欲为。
从语法来看,我猜你使用的是 ngrx (effects)(而不是 redux-observable)。

所以对于 ngrx/effects 你可以这样做:

export const handleBleConnectionSuccess = (
  action$,
  store,
  { bleCommunicator }
) =>
  action$.ofType(c.BLE_CONNECTION_SUCCESS).switchMap(a => {
    const readCharacteristic = deviceOrFirmwareUuid =>
      bleCommunicator.readCharacteristic(a.device.id, gattInfo.uuid, deviceOrFirmwareUuid);

    return Observable.merge(
      readCharacteristic(gattInfo.firmwareRevision.uuid)
        .map(value => ({
          type: c.DEVICE_FIRMWARE_VERSION,
          device: { ...a.device, firmwareVersion: value },
        }))
        .retry(3),

      readCharacteristic(gattInfo.modelNumber.uuid)
        .map(value => ({
          type: c.DEVICE_MODEL_NUMBER,
          device: { ...a.device, modelNumber: value },
        }))
        .retry(3),

      { type: 'DEVICE_INFORMATION_REQUESTED' }
    );
  });