An example using the console API
rxjs-spy exposes a module API intended to be called from code and a console API intended for interactive use via the browser’s console.
The documentation for the APIs is in the GitHub README.
This page includes an example showing what can be done with its console API, so open up a DevTools console and give it a go.
The example’s source
Have a look at the this page’s source:
<script>
rxjsSpy.create();
</script>
<script>
(function () {
var interval = rxjs.interval(2000).pipe(
rxjsSpy.operators.tag("interval")
);
var people = interval.pipe(
rxjs.operators.map(function (value) {
const names = ["alice", "bob"];
return names[value % names.length];
}),
rxjsSpy.operators.tag("people")
).subscribe();
})();
</script>
Note that there are two script elements. The first configures the spy and the second represents the application code that will be spied upon.
The observables in the second script element are enclosed in an IIFE, so they are not accessible from the console. However, they are tagged, so they can be inspected and manipulated using the spy’s console API.
The example
-
To inspect the current snapshots of all tagged observables, run the following in the console:
spy.show();You’ll see a listing of snapshots for all tagged observables. There are two: an interval observable; and an observable that emits people’s names.
To inspect a specific tagged observable, you can pass a string or a regular expression to
show. For example:spy.show("people"); -
Logging can be enabled using the API’s
logmethod. For example:spy.log("people");This will enable logging for the specified tag.
Run the following to log the interval observable’s values, too:
spy.log("interval"); -
Calls made to the spy API can be undone using the
undomethod. To obtain a list of calls, run the following:spy.undo();It will display a list of numbered calls. Undo the interval logging by passing the appropriate call number:
spy.undo(3); -
The spy API can modify the a tagged observable using the
letmethod. The method behaves in a similar manner to the RxJSletoperator.For example, the following call will replace the value emitted from the people observable:
spy.let("people", source => source.mapTo("mallory"));Note that the changes will be seen by both current and future subscribers to the observable.
Undo the change with an
undocall:spy.undo(3); -
The spy API can pause tagged observables using the
pausemethod. For example, the following call pauses the people observable so that it’s emissions can be controlled from the console:spy.pause("interval");pausereturns aDeckinstance that can be used to control the observable and return value should be assigned to a variable. It’s easy to forget to do this, so there is adeckmethod that will display a numbered list ofpausecalls. Passing a call number todeckwill return the instance associated with the call. For example:spy.deck(); var deck = spy.deck(1);The deck can be used to inspect and control the observable’s emissions. For example, calling its
logmethod will show the notifications that have been paused:deck.log();Its
stepmethod will release a single paused notification:deck.step();Its
skipmethod will skip a single paused notification:deck.skip();And its
resumemethod will release all paused notifications and will resume the observable’s emissions:deck.resume();Calling its
pausemethod will return the observable to a paused state:deck.pause(); -
All API calls that manipulate observables can be undone, so
undocan be used to undo the pausing of an observable. When undone, any paused notifications are resumed:spy.undo(3);