##// END OF EJS Templates
minor refactoring of TraceSource, code linting
cin -
r53:5fd4258909e8 di-typescript
parent child
Show More
@@ -0,0 +1,55
1 import { TraceSource } from "./TraceSource";
2 import { argumentNotNull } from "../safe";
3 import { IDestroyable } from "../interfaces";
4
5 export class Registry {
6 static readonly instance = new Registry();
7
8 private _registry: object = new Object();
9 private _listeners: object = new Object();
10 private _nextCookie: number = 1;
11
12 get(id: any): TraceSource {
13 argumentNotNull(id, "id");
14
15 if (this._registry[id])
16 return this._registry[id];
17
18 const source = new TraceSource(id);
19 this._registry[id] = source;
20 this._onNewSource(source);
21
22 return source;
23 }
24
25 add(id: any, source: TraceSource) {
26 argumentNotNull(id, "id");
27 argumentNotNull(source, "source");
28
29 this._registry[id] = source;
30 this._onNewSource(source);
31 }
32
33 _onNewSource(source: TraceSource) {
34 for (const i in this._listeners)
35 this._listeners[i].call(null, source);
36 }
37
38 on(handler: (source: TraceSource) => void): IDestroyable {
39 argumentNotNull(handler, "handler");
40 const me = this;
41
42 const cookie = this._nextCookie++;
43
44 this._listeners[cookie] = handler;
45
46 for (const i in this._registry)
47 handler(this._registry[i]);
48
49 return {
50 destroy() {
51 delete me._listeners[cookie];
52 }
53 };
54 }
55 }
@@ -1,7 +1,6
1 import * as format from "../text/format";
1 import * as format from "../text/format";
2 import { argumentNotNull } from "../safe";
3 import { Observable } from "../Observable";
2 import { Observable } from "../Observable";
4 import { IDestroyable } from "../interfaces";
3 import { Registry } from "./Registry";
5
4
6 export const DebugLevel = 400;
5 export const DebugLevel = 400;
7
6
@@ -13,91 +12,33 export const ErrorLevel = 100;
13
12
14 export const SilentLevel = 0;
13 export const SilentLevel = 0;
15
14
16 export class TraceEvent {
15 export interface TraceEvent {
17 readonly source: TraceSource;
16 readonly source: TraceSource;
18
17
19 readonly level: number;
18 readonly level: number;
20
19
21 readonly arg: any;
20 readonly arg: any;
22
23 constructor(source: TraceSource, level: number, arg: any) {
24 this.source = source;
25 this.level = level;
26 this.arg = arg;
27 }
28 }
29
30 class Registry {
31 static readonly instance = new Registry();
32
33 private _registry: object = new Object();
34 private _listeners: object = new Object();
35 private _nextCookie: number = 1;
36
37 get(id: any): TraceSource {
38 argumentNotNull(id, "id");
39
40 if (this._registry[id])
41 return this._registry[id];
42
43 var source = new TraceSource(id);
44 this._registry[id] = source;
45 this._onNewSource(source);
46
47 return source;
48 }
49
50 add(id: any, source: TraceSource) {
51 argumentNotNull(id, "id");
52 argumentNotNull(source, "source");
53
54 this._registry[id] = source;
55 this._onNewSource(source);
56 }
57
58 _onNewSource(source: TraceSource) {
59 for (let i in this._listeners)
60 this._listeners[i].call(null, source);
61 }
62
63 on(handler: (source: TraceSource) => void): IDestroyable {
64 argumentNotNull(handler, "handler");
65 var me = this;
66
67 var cookie = this._nextCookie++;
68
69 this._listeners[cookie] = handler;
70
71 for (let i in this._registry)
72 handler(this._registry[i]);
73
74 return {
75 destroy() {
76 delete me._listeners[cookie];
77 }
78 };
79 }
80 }
21 }
81
22
82 export class TraceSource {
23 export class TraceSource {
83 readonly id: any
24 readonly id: any;
84
25
85 level: number
26 level: number;
86
27
87 readonly events: Observable<TraceEvent>
28 readonly events: Observable<TraceEvent>;
88
29
89 _notifyNext: (arg: TraceEvent) => void
30 _notifyNext: (arg: TraceEvent) => void;
90
31
91 constructor(id: any) {
32 constructor(id: any) {
92
33
93 this.id = id || new Object();
34 this.id = id || new Object();
94 this.events = new Observable((next) => {
35 this.events = new Observable(next => {
95 this._notifyNext = next;
36 this._notifyNext = next;
96 })
37 });
97 }
38 }
98
39
99 protected emit(level: number, arg: any) {
40 protected emit(level: number, arg: any) {
100 this._notifyNext(new TraceEvent(this, level, arg));
41 this._notifyNext({ source: this, level, arg});
101 }
42 }
102
43
103 isDebugEnabled() {
44 isDebugEnabled() {
General Comments 0
You need to be logged in to leave comments. Login now