Skip to content

Commit

Permalink
chore(spy): Rename rxSpy global to spy.
Browse files Browse the repository at this point in the history
Keeping, but deprecating the rxSpy global.
  • Loading branch information
cartant committed Sep 3, 2018
1 parent bf2f978 commit 8dab99b
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 20 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)

<a name="console-undo"></a>

Expand Down
24 changes: 12 additions & 12 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand All @@ -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:

Expand Down
14 changes: 13 additions & 1 deletion source/spy-console.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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();
},

Expand All @@ -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();
},

Expand Down
22 changes: 20 additions & 2 deletions source/spy-core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import { isObservable, toSubscriber } from "./util";

declare const __RX_SPY_VERSION__: string;
const observableSubscribe = Observable.prototype.subscribe;
const previousWindow: Record<string, any> = {};

export class SpyCore implements Spy {

Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 8dab99b

Please sign in to comment.