All Downloads are FREE. Search and download functionalities are using the official Maven repository.

node_modules.graphql.subscription.mapAsyncIterator.js.flow Maven / Gradle / Ivy

The newest version!
/**
 * Copyright (c) 2017, Facebook, Inc.
 * All rights reserved.
 *
 * This source code is licensed under the BSD-style license found in the
 * LICENSE file in the root directory of this source tree. An additional grant
 * of patent rights can be found in the PATENTS file in the same directory.
 *
 * @flow
 */

import { $$asyncIterator, getAsyncIterator } from 'iterall';

/**
 * Given an AsyncIterable and a callback function, return an AsyncIterator
 * which produces values mapped via calling the callback function.
 */
export default function mapAsyncIterator(
  iterable: AsyncIterable,
  callback: (value: T) => Promise | U
): AsyncGenerator {
  const iterator = getAsyncIterator(iterable);
  let $return;
  let abruptClose;
  if (typeof iterator.return === 'function') {
    $return = iterator.return;
    abruptClose = error => {
      const rethrow = () => Promise.reject(error);
      return $return.call(iterator).then(rethrow, rethrow);
    };
  }

  function mapResult(result) {
    return result.done ?
      result :
      asyncMapValue(result.value, callback).then(iteratorResult, abruptClose);
  }

  return {
    next() {
      return iterator.next().then(mapResult);
    },
    return() {
      return $return ?
        $return.call(iterator).then(mapResult) :
        Promise.resolve({ value: undefined, done: true });
    },
    throw(error) {
      if (typeof iterator.throw === 'function') {
        return iterator.throw(error).then(mapResult);
      }
      return Promise.reject(error).catch(abruptClose);
    },
    [$$asyncIterator]() {
      return this;
    },
  };
}

function asyncMapValue(
  value: T,
  callback: (T) => Promise | U
): Promise {
  return new Promise(resolve => resolve(callback(value)));
}

function iteratorResult(value: T): IteratorResult {
  return { value, done: false };
}