Skip to content

Commit

Permalink
Add Historian.instance
Browse files Browse the repository at this point in the history
  • Loading branch information
vmihailenco committed Sep 21, 2018
1 parent 75f5a34 commit 71936d3
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 14 deletions.
2 changes: 1 addition & 1 deletion karma.conf.js
Expand Up @@ -79,6 +79,6 @@ module.exports = function(config) {

// Concurrency level
// how many browser should be started simultaneous
concurrency: Infinity
concurrency: 1
})
}
16 changes: 9 additions & 7 deletions src/client.ts
Expand Up @@ -21,7 +21,7 @@ import nodeReporter from './reporter/node';
import xhrReporter from './reporter/xhr';
import jsonpReporter from './reporter/jsonp';

import {historian, getHistory} from './historian';
import Historian from './historian';


declare const VERSION: string;
Expand All @@ -36,6 +36,7 @@ interface Todo {

class Client {
private opts: any;
private historian: Historian;

private processor: Processor;
private reporter: Reporter;
Expand Down Expand Up @@ -98,17 +99,18 @@ class Client {
this.addFilter(nodeFilter);
}

historian.registerNotifier(this);
this.historian = Historian.instance();
this.historian.registerNotifier(this);
if (opts.unwrapConsole || isDevEnv(opts)) {
historian.unwrapConsole();
this.historian.unwrapConsole();
}
}

close(): void {
for (let fn of this.onClose) {
fn();
}
historian.unregisterNotifier(this);
this.historian.unregisterNotifier(this);
}

private setReporter(name: string|Reporter): void {
Expand Down Expand Up @@ -179,7 +181,7 @@ class Client {
});
}

let history = getHistory();
let history = this.historian.getHistory();
if (history.length > 0) {
notice.context.history = history;
}
Expand Down Expand Up @@ -224,7 +226,7 @@ class Client {
return fn.apply(this, wrappedArgs);
} catch (err) {
client.notify({error: err, params: {arguments: fnArgs}});
historian.ignoreNextWindowError();
this.historian.ignoreNextWindowError();
throw err;
}
} as FuncWrapper;
Expand Down Expand Up @@ -262,7 +264,7 @@ class Client {
}

onerror(): void {
historian.onerror.apply(historian, arguments);
this.historian.onerror.apply(this.historian, arguments);
}

private onOnline(): void {
Expand Down
15 changes: 9 additions & 6 deletions src/historian.ts
Expand Up @@ -13,6 +13,8 @@ interface XMLHttpRequestWithState extends XMLHttpRequest {


export default class Historian {
private static _instance: Historian;

private historyMaxLen = 20;

private notifiers: Notifier[] = [];
Expand Down Expand Up @@ -89,6 +91,13 @@ export default class Historian {
}
}

static instance(): Historian {
if (!Historian._instance) {
Historian._instance = new Historian();
}
return Historian._instance;
}

registerNotifier(notifier: Notifier) {
this.notifiers.push(notifier);

Expand Down Expand Up @@ -393,9 +402,3 @@ export default class Historian {
this.lastLocation = url;
}
}

export let historian = new Historian();

export function getHistory(): any[] {
return historian.getHistory();
}
1 change: 1 addition & 0 deletions test/unit/jsonify_notice_test.ts
Expand Up @@ -2,6 +2,7 @@ import Notice from '../../src/notice';
import jsonifyNotice from '../../src/jsonify_notice';
import { expect } from './sinon_chai';


describe('jsonify_notice', () => {
const maxLength = 30000;

Expand Down

0 comments on commit 71936d3

Please sign in to comment.