-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use the fetch queues for any horizon-related request (#1125)
- Loading branch information
Showing
2 changed files
with
72 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { Observable, Subscription, SubscriptionObserver, unsubscribe } from "observable-fns" | ||
|
||
type AsyncObservableInitializer<T> = ( | ||
observer: SubscriptionObserver<T> | ||
) => Promise<Subscription<T> | (() => void) | void> | ||
|
||
export function observableFromAsyncFactory<T>(init: AsyncObservableInitializer<T>): Observable<T> { | ||
return new Observable<T>(observer => { | ||
let downstreamUnsubscribe: Subscription<T> | (() => void) | void | ||
let receivedUnsubscribe = false | ||
|
||
init(observer).then( | ||
returned => { | ||
downstreamUnsubscribe = returned | ||
|
||
if (receivedUnsubscribe) { | ||
unsubscribe(downstreamUnsubscribe) | ||
} | ||
}, | ||
error => { | ||
observer.error(error) | ||
} | ||
) | ||
|
||
const upstreamUnsubscribe = () => { | ||
receivedUnsubscribe = true | ||
unsubscribe(downstreamUnsubscribe) | ||
} | ||
return upstreamUnsubscribe | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters