Last active
October 1, 2021 12:24
-
-
Save sashaaro/f0f4e1df5f2804d9e253418b56af1a71 to your computer and use it in GitHub Desktop.
rxjs operator AsyncLocalStorage
This file contains hidden or 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
import {AsyncLocalStorage} from "async_hooks"; | |
import {MonoTypeOperatorFunction, Observable, Subscription} from "rxjs"; | |
export function withinContext<T, S = any>(storage: AsyncLocalStorage<S>, store: () => S): MonoTypeOperatorFunction<T> { | |
return function (source: Observable<T>) { | |
return new Observable((observer) => { | |
let sub: Subscription; | |
storage.run(store(),() => { | |
sub = source.subscribe(observer); | |
}) | |
// @ts-ignore | |
return sub; | |
}); | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment