Skip to content

Instantly share code, notes, and snippets.

@victorporof
Last active May 31, 2022 16:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save victorporof/363f5417c754799bd94691907c12fb66 to your computer and use it in GitHub Desktop.
Save victorporof/363f5417c754799bd94691907c12fb66 to your computer and use it in GitHub Desktop.
RxJS + Async Stack Tagging
<!-- macOS: open -a Google\ Chrome\ Canary --args --js-flags="--experimental-async-stack-tagging-api" -->
<!-- Linux: chrome --js-flags="--experimental-async-stack-tagging-api" -->
<!DOCTYPE html>
<html lang="en">
<head>
<script type="module">
import * as rxjs from "https://unpkg.com/rxjs@7?module";
// Wraps `rxjs::from` to also schedule an async task.
// Returns the task and the observable.
function from(name, ...args) {
const task = console.scheduleAsyncTask(name, true);
const observable = rxjs.from(...args);
return { task, observable };
}
// Wraps `Observable::subscribe` to run an async task with attribution.
function subscribe(observable, task, f) {
return observable.subscribe({
next(...args) {
console.startAsyncTask(task);
f(...args);
console.finishAsyncTask(task);
},
complete() {
console.cancelAsyncTask(task);
},
});
}
// Example adapted from https://www.learnrxjs.io/learn-rxjs/operators/filtering/sample
const sequence = from("Hello Task Attribution!", ["foo", "bar", "baz"]);
const zipped = rxjs.zip(sequence.observable, rxjs.interval(1000));
const piped = zipped.pipe(rxjs.sample(rxjs.interval(1500)));
subscribe(piped, sequence.task, (value) => {
console.trace(value);
debugger;
});
</script>
</head>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment