Skip to content

Commit

Permalink
Fix Flow Types in RelayObservable
Browse files Browse the repository at this point in the history
Summary:
: Leveraging latest Flow features fix the `isObservable` and `isPromise` functions to now work correctly.

This allows us to finally fix `ObservableFromValue` so that it can use `Subscribable<T>` instead of `RelayObservable<T>`.

Reviewed By: captbaritone

Differential Revision: D54539298

fbshipit-source-id: bff95e07e2ac75c460e38c6d62df3eb490c5d067
  • Loading branch information
Jerry Francois authored and facebook-github-bot committed Mar 6, 2024
1 parent 5d40e95 commit 448aa67
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 12 deletions.
12 changes: 4 additions & 8 deletions packages/relay-runtime/network/RelayObservable.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,7 @@ export interface Subscribable<+T> {
subscribe(observer: Observer<T> | Sink<T>): Subscription;
}

// Note: This should accept Subscribable<T> instead of RelayObservable<T>,
// however Flow cannot yet distinguish it from T.
export type ObservableFromValue<+T> = RelayObservable<T> | Promise<T> | T;
export type ObservableFromValue<+T> = Subscribable<T> | Promise<T> | T;

let hostReportError:
| ((Error, isUncaughtThrownError: boolean) => mixed)
Expand Down Expand Up @@ -144,9 +142,9 @@ class RelayObservable<+T> implements Subscribable<T> {
* useful for accepting the result of a user-provided FetchFunction.
*/
static from<V>(obj: ObservableFromValue<V>): RelayObservable<V> {
return isObservable(obj)
return isObservable<V>(obj)
? fromObservable(obj)
: isPromise(obj)
: isPromise<V>(obj)
? fromPromise(obj)
: fromValue(obj);
}
Expand Down Expand Up @@ -447,9 +445,7 @@ class RelayObservable<+T> implements Subscribable<T> {
}

// Use declarations to teach Flow how to check isObservable.
// $FlowFixMe[deprecated-type]
declare function isObservable(p: mixed): boolean %checks(p instanceof
RelayObservable);
declare function isObservable<T>(obj: mixed): obj is Subscribable<T>;

function isObservable(obj: mixed) {
return (
Expand Down
7 changes: 3 additions & 4 deletions packages/relay-runtime/util/isPromise.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@

'use strict';

// $FlowFixMe[deprecated-type]
declare function isPromise(p: mixed): boolean %checks(p instanceof Promise);
declare function isPromise<T>(p: mixed): p is Promise<T>;

function isPromise(p: $FlowFixMe): boolean {
return !!p && typeof p.then === 'function';
function isPromise(p: mixed) {
return p != null && typeof p === 'object' && typeof p.then === 'function';
}

module.exports = isPromise;

0 comments on commit 448aa67

Please sign in to comment.