diff --git a/CHANGELOG.md b/CHANGELOG.md
index 63d8add..08ab1b4 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -66,7 +66,7 @@
* The `subscribers` map has been removed from the `observable` snapshot and replaced with a `subscriptions` map.
* In the snapshots, `merges` has been renamed to `flattenings`.
* It's no longer possible to undo the entire spy via the console.
-* The console global - `rxSpy` - won't exist until `create` has been called.
+* The console global - `spy` - won't exist until `create` has been called.
* `undefined` is favoured for return values, etc. rather than `null`.
### Fixes:
diff --git a/README.md b/README.md
index e17fea4..39c3247 100644
--- a/README.md
+++ b/README.md
@@ -85,7 +85,7 @@ spy.log(/^some-tag$/);
spy.log(tag => tag === "some-tag");
```
-`rxjs-spy` exposes a module API intended to be called from code and a console API - via the `rxSpy` global - intended for interactive use via the browser's console.
+`rxjs-spy` exposes a module API intended to be called from code and a console API - via the `spy` global - intended for interactive use via the browser's console.
## Module API
@@ -389,12 +389,12 @@ import { detect } from "rxjs-spy/detect";
## Console API
-The methods in the console API are callable via the `rxSpy` global (note the lower-case `r`) and are intended to be used interactively in the browser's console.
+The methods in the console API are callable via the `spy` global and are intended to be used interactively in the browser's console.
They are identical to the methods in the spy instances created using the module API except for the fact that they do not return teardown functions. Instead, calls can be undone using the `undo` API method.
-* [`rxSpy.undo`](#console-undo)
-* [`rxSpy.deck`](#console-deck)
+* [`spy.undo`](#console-undo)
+* [`spy.deck`](#console-deck)
diff --git a/docs/index.md b/docs/index.md
index bc8f5f6..81c8993 100644
--- a/docs/index.md
+++ b/docs/index.md
@@ -42,52 +42,52 @@ The observables in the second `script` element are enclosed in an IIFE, so they
1. To inspect the current snapshots of all tagged observables, run the following in the console:
- rxSpy.show();
+ 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:
- rxSpy.show("people");
+ spy.show("people");
1. Logging can be enabled using the API's `log`method. For example:
- rxSpy.log("people");
+ spy.log("people");
This will enable logging for the specified tag.
Run the following to log the interval observable's values, too:
- rxSpy.log("interval");
+ spy.log("interval");
1. Calls made to the spy API can be undone using the `undo` method. To obtain a list of calls, run the following:
- rxSpy.undo();
+ spy.undo();
It will display a list of numbered calls. Undo the interval logging by passing the appropriate call number:
- rxSpy.undo(3);
+ spy.undo(3);
1. The spy API can modify the a tagged observable using the `let` method. The method behaves in a similar manner to the RxJS `let` operator.
For example, the following call will replace the value emitted from the people observable:
- rxSpy.let("people", source => source.mapTo("mallory"));
+ 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 `undo` call:
- rxSpy.undo(3);
+ spy.undo(3);
1. The spy API can pause tagged observables using the `pause` method. For example, the following call pauses the people observable so that it's emissions can be controlled from the console:
- rxSpy.pause("interval");
+ spy.pause("interval");
`pause` returns a `Deck` instance 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 a `deck` method that will display a numbered list of `pause` calls. Passing a call number to `deck` will return the instance associated with the call. For example:
- rxSpy.deck();
- var deck = rxSpy.deck(1);
+ spy.deck();
+ var deck = spy.deck(1);
The deck can be used to inspect and control the observable's emissions. For example, calling its `log` method will show the notifications that have been paused:
@@ -111,7 +111,7 @@ The observables in the second `script` element are enclosed in an IIFE, so they
1. All API calls that manipulate observables can be undone, so `undo` can be used to undo the pausing of an observable. When undone, any paused notifications are resumed:
- rxSpy.undo(3);
+ spy.undo(3);
## There are more examples in the following articles:
diff --git a/source/spy-console.ts b/source/spy-console.ts
index 640f814..18f21b1 100644
--- a/source/spy-console.ts
+++ b/source/spy-console.ts
@@ -9,12 +9,16 @@ import { PausePlugin } from "./plugin";
import { SpyCore } from "./spy-core";
import { inferPath, inferType } from "./util";
-export function wrap(core: SpyCore): any {
+export function wrap(
+ core: SpyCore,
+ deprecation: () => void = () => {}
+): any {
return {
deck(call?: number): any {
+ deprecation();
const pausePlugins = core.findAll(PausePlugin);
if (call === undefined) {
const logger = toLogger(defaultLogger);
@@ -29,16 +33,19 @@ export function wrap(core: SpyCore): any {
debug(...args: any[]): void {
+ deprecation();
core.debug.apply(core, args);
},
detect(id: string = ""): void {
+ deprecation();
detect(id);
},
flush(): void {
+ deprecation();
core.flush();
},
@@ -47,26 +54,31 @@ export function wrap(core: SpyCore): any {
let(...args: any[]): void {
+ deprecation();
core.let.apply(core, args);
},
log(...args: any[]): void {
+ deprecation();
core.log.apply(core, args);
},
pause(...args: any[]): any {
+ deprecation();
return core.pause.apply(core, args);
},
show(...args: any[]): void {
+ deprecation();
core.show.apply(core, args);
},
stats(): void {
+ deprecation();
core.stats();
},
diff --git a/source/spy-core.ts b/source/spy-core.ts
index e75a1b3..30d45d3 100644
--- a/source/spy-core.ts
+++ b/source/spy-core.ts
@@ -43,6 +43,7 @@ import { isObservable, toSubscriber } from "./util";
declare const __RX_SPY_VERSION__: string;
const observableSubscribe = Observable.prototype.subscribe;
+const previousWindow: Record = {};
export class SpyCore implements Spy {
@@ -103,13 +104,30 @@ export class SpyCore implements Spy {
hook((id) => this.detect_(id, detector));
if (typeof window !== "undefined") {
- window["rxSpy"] = wrap(this);
+ ["rxSpy", "spy"].forEach(key => {
+ if (window.hasOwnProperty(key)) {
+ this.defaultLogger_.log(`Overwriting window.${key}`);
+ previousWindow[key] = window[key];
+ }
+ window[key] = wrap(this, key === "spy" ?
+ undefined :
+ () => this.warnOnce(this.defaultLogger_, `${key} is deprecated; use spy instead`)
+ );
+ });
}
this.teardown_ = () => {
if (typeof window !== "undefined") {
- delete window["rxSpy"];
+ ["rxSpy", "spy"].forEach(key => {
+ if (previousWindow.hasOwnProperty(key)) {
+ this.defaultLogger_.log(`Restoring window.${key}`);
+ window[key] = previousWindow[key];
+ delete previousWindow[key];
+ } else {
+ delete window[key];
+ }
+ });
}
hook(undefined);