Skip to content

Commit

Permalink
feat(SnapshotPlugin): Add keptDuration option.
Browse files Browse the repository at this point in the history
  • Loading branch information
cartant committed Oct 31, 2017
1 parent 6d95a2e commit 723de5b
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 19 deletions.
8 changes: 7 additions & 1 deletion source/detector-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ import { find, spy } from "./spy";

import "rxjs/add/operator/mergeMap";

const options = {
keptDuration: -1,
keptValues: 4,
warning: false
};

describe("detector", () => {

let detector: Detector;
Expand All @@ -29,7 +35,7 @@ describe("detector", () => {

beforeEach(() => {

teardown = spy({ warning: false });
teardown = spy({ ...options });
detector = new Detector(find(SnapshotPlugin));
});

Expand Down
24 changes: 23 additions & 1 deletion source/plugin/snapshot-plugin-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import "../add/operator/tag";

describe("SnapshotPlugin", () => {

const keptDuration = 10;
const keptValues = 2;
let plugin: SnapshotPlugin;
let teardown: () => void;
Expand All @@ -36,7 +37,7 @@ describe("SnapshotPlugin", () => {

beforeEach(() => {

plugin = new SnapshotPlugin({ keptValues });
plugin = new SnapshotPlugin({ keptDuration, keptValues });
teardown = spy({ plugins: [new GraphPlugin(), plugin], warning: false });
});

Expand Down Expand Up @@ -139,6 +140,27 @@ describe("SnapshotPlugin", () => {
expect(subscriberSnapshot.values).to.have.length(2);
expect(subscriberSnapshot.valuesFlushed).to.equal(2);
});

it("should automatically flush old, unsubscribed snapshots", (callback) => {

const subject = new Subject<number>();
const subscription = subject.subscribe((value) => {}, (error) => {});

subject.next(1);

let snapshot = plugin.snapshotAll();
expect(snapshot.observables).to.have.property("size", 1);

subject.complete();

setTimeout(() => {

snapshot = plugin.snapshotAll();
expect(snapshot.observables).to.have.property("size", 0);
callback();

}, keptDuration + 10);
});
});

describe("snapshotAll", () => {
Expand Down
16 changes: 14 additions & 2 deletions source/plugin/snapshot-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,26 +78,37 @@ export interface SubscriptionSnapshot {

export class SnapshotPlugin extends BasePlugin {

private rootSubscriptionRefs_: Map<SubscriptionRef, boolean>;
private keptDuration_: number;
private keptValues_: number;
private lastFlush_: number;
private rootSubscriptionRefs_: Map<SubscriptionRef, boolean>;

constructor({
keptDuration = 30000,
keptValues = 4
}: {
keptDuration?: number,
keptValues?: number
} = {}) {

super();

this.rootSubscriptionRefs_ = new Map<SubscriptionRef, boolean>();
this.keptDuration_ = keptDuration;
this.keptValues_ = keptValues;
this.lastFlush_ = Date.now();
this.rootSubscriptionRefs_ = new Map<SubscriptionRef, boolean>();
}

afterUnsubscribe(ref: SubscriptionRef): void {

const snapshotRef = getSnapshotRef(ref);
snapshotRef.tick = tick();
snapshotRef.unsubscribed = true;

const { keptDuration_, rootSubscriptionRefs_ } = this;
if ((keptDuration_ >= 0) && rootSubscriptionRefs_.has(ref)) {
setTimeout(() => rootSubscriptionRefs_.delete(ref), keptDuration_);
}
}

beforeComplete(ref: SubscriptionRef): void {
Expand Down Expand Up @@ -175,6 +186,7 @@ export class SnapshotPlugin extends BasePlugin {
rootSubscriptionRefs_.delete(ref);
}
});
this.lastFlush_ = Date.now();
}

snapshotAll({
Expand Down
26 changes: 16 additions & 10 deletions source/spy-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ import { tick } from "./tick";

import "rxjs/add/operator/mapTo";

const options = {
keptDuration: -1,
keptValues: 4,
warning: false
};

describe("spy", () => {

let teardown: () => void;
Expand All @@ -33,7 +39,7 @@ describe("spy", () => {
beforeEach(() => {

plugin = stubPlugin();
teardown = spy({ plugins: [plugin], warning: false });
teardown = spy({ plugins: [plugin], ...options });
});

it("should call the plugin's flush method", () => {
Expand All @@ -47,7 +53,7 @@ describe("spy", () => {

it("should apply the selector to the tagged observable", () => {

teardown = spy({ plugins: [], warning: false });
teardown = spy({ plugins: [], ...options });
_let("people", (source) => source.mapTo("bob"));

const values: any[] = [];
Expand All @@ -63,7 +69,7 @@ describe("spy", () => {

it("should log the tagged observable", () => {

teardown = spy({ plugins: [], warning: false });
teardown = spy({ plugins: [], ...options });

const subject = new Subject<string>();
let calls: any[][] = [];
Expand Down Expand Up @@ -92,7 +98,7 @@ describe("spy", () => {

it("should log all/any tagged observables", () => {

teardown = spy({ plugins: [], warning: false });
teardown = spy({ plugins: [], ...options });

const subject = new Subject<string>();
const calls: any[][] = [];
Expand All @@ -111,7 +117,7 @@ describe("spy", () => {

it("should pause the tagged observable's subscriptions", () => {

teardown = spy({ plugins: [], warning: false });
teardown = spy({ plugins: [], ...options });
const deck = pause("people");

const values: any[] = [];
Expand All @@ -127,7 +133,7 @@ describe("spy", () => {

it("should resume upon teardown", () => {

teardown = spy({ plugins: [], warning: false });
teardown = spy({ plugins: [], ...options });
const deck = pause("people");

const values: any[] = [];
Expand All @@ -149,7 +155,7 @@ describe("spy", () => {
beforeEach(() => {

plugin = stubPlugin();
teardown = spy({ plugins: [plugin], warning: false });
teardown = spy({ plugins: [plugin], ...options });
});

it("should call the plugin subscribe/next/unsubscribe methods", () => {
Expand Down Expand Up @@ -308,7 +314,7 @@ describe("spy", () => {

it("should show snapshotted information for the tagged observable", () => {

teardown = spy({ warning: false });
teardown = spy({ ...options });

const calls: any[][] = [];
const subject = new Subject<number>();
Expand All @@ -325,7 +331,7 @@ describe("spy", () => {

it("should show snapshotted information all/any tagged observables", () => {

teardown = spy({ warning: false });
teardown = spy({ ...options });

const calls: any[][] = [];
const subject = new Subject<number>();
Expand All @@ -345,7 +351,7 @@ describe("spy", () => {

it("should increment with each subscription and value, etc.", () => {

teardown = spy({ plugins: [], warning: false });
teardown = spy({ plugins: [], ...options });

const subject = new Subject<string>();

Expand Down
10 changes: 5 additions & 5 deletions source/spy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,14 +274,14 @@ export function show(match: any, partialLogger: PartialLogger = defaultLogger):
logger.groupEnd();
}

export function spy({
plugins,
warning = true
}: {
export function spy(options: {
[key: string]: any,
plugins?: Plugin[]
warning?: boolean
} = {}): () => void {

const { plugins, warning } = options;

if (Observable.prototype.subscribe !== subscribeBase) {
throw new Error("Already spying on Observable.prototype.subscribe.");
}
Expand All @@ -298,7 +298,7 @@ export function spy({
plugins_ = [
new StackTracePlugin(),
new GraphPlugin(),
new SnapshotPlugin(),
new SnapshotPlugin(options as { [key: string]: any }),
new DevToolsPlugin()
];
}
Expand Down

0 comments on commit 723de5b

Please sign in to comment.