/** * Copyright (c) 2017-present, 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 * @format */ /** * Expected tries to mimic llvm's Expected class. * This is specially useful for Observables that can return a stream of errors instead of closing * the subscription. */ type ExpectedError = { isError: true, isPending: false, error: Error, getOrDefault: (def: T) => T, }; type ExpectedValue = { isError: false, isPending: false, value: T, getOrDefault: (def: T) => T, }; type ExpectedPendingValue = { isError: false, isPending: true, value: T, getOrDefault: (def: T) => T, }; export type Expected = | ExpectedError | ExpectedValue | ExpectedPendingValue; export class Expect { static error(error: Error): ExpectedError { return { isError: true, isPending: false, error, getOrDefault(def: T): T { return def; }, }; } static value(value: T): ExpectedValue { return { isError: false, isPending: false, value, getOrDefault(def: T): T { return this.value; }, }; } static pendingValue(value: T): ExpectedPendingValue { return { isError: false, isPending: true, value, getOrDefault(def: T): T { return this.value; }, }; } }