##// END OF EJS Templates
removed obsolete code
cin -
r12:80e4b5709788 propose cancellat...
parent child
Show More
@@ -1,96 +1,95
1 import { IActivationController } from './IActivationController';
1 import { IActivationController } from './IActivationController';
2 import { IActivatable } from './IActivatable';
2 import { IActivatable } from './IActivatable';
3 import { AsyncComponent } from './AsyncComponent';
3 import { AsyncComponent } from './AsyncComponent';
4 import { ICancellation } from '../ICancellation';
4 import { ICancellation } from '../ICancellation';
5 import { EmptyCancellation } from '../EmptyCancellation';
5 import { EmptyCancellation } from '../EmptyCancellation';
6 import * as TraceSource from '../log/TraceSource';
6 import * as TraceSource from '../log/TraceSource';
7
7
8 type Constructor<T = {}> = new (...args: any[]) => T;
8 type Constructor<T = {}> = new (...args: any[]) => T;
9
9
10 const log = TraceSource.get('@implab/core/components/ActivatableMixin');
10 const log = TraceSource.get('@implab/core/components/ActivatableMixin');
11
11
12 function ActivatableMixin<TBase extends Constructor<AsyncComponent>>(Base: TBase) {
12 function ActivatableMixin<TBase extends Constructor<AsyncComponent>>(Base: TBase) {
13 return class extends Base implements IActivatable {
13 return class extends Base implements IActivatable {
14 _controller: IActivationController;
14 _controller: IActivationController;
15
15
16 _active: boolean;
16 _active: boolean;
17
17
18 isActive() {
18 isActive() {
19 return this._active;
19 return this._active;
20 }
20 }
21
21
22 getActivationController() {
22 getActivationController() {
23 return this._controller;
23 return this._controller;
24 }
24 }
25
25
26 setActivationController(controller: IActivationController) {
26 setActivationController(controller: IActivationController) {
27 this._controller = controller;
27 this._controller = controller;
28 }
28 }
29
29
30 async onActivating(ct: ICancellation) {
30 async onActivating(ct: ICancellation) {
31 if (this._controller)
31 if (this._controller)
32 await this._controller.activating(this, ct);
32 await this._controller.activating(this, ct);
33 }
33 }
34
34
35 async onActivated(ct: ICancellation) {
35 async onActivated(ct: ICancellation) {
36 if (this._controller)
36 if (this._controller)
37 await this._controller.activated(this, ct);
37 await this._controller.activated(this, ct);
38 }
38 }
39
39
40 async activate(ct: ICancellation = EmptyCancellation.default) {
40 async activate(ct: ICancellation = EmptyCancellation.default) {
41 if (this.isActive())
41 if (this.isActive())
42 return;
42 return;
43 ct = this.startOperation(ct);
43 ct = this.startOperation(ct);
44 try {
44 try {
45 await this.onActivating(ct);
45 await this.onActivating(ct);
46 this._active = true;
46 this._active = true;
47 try {
47 try {
48 await this.onActivated(ct);
48 await this.onActivated(ct);
49 } catch(e) {
49 } catch(e) {
50 log.error(e);
50 log.error("Suppressed onActivated error: {0}", e);
51 // TODO log error
52 }
51 }
53 this.completeSuccess();
52 this.completeSuccess();
54 } catch (e) {
53 } catch (e) {
55 this.completeFail(e);
54 this.completeFail(e);
56 }
55 }
57 return this.getCompletion();
56 return this.getCompletion();
58 }
57 }
59
58
60 async onDeactivating(ct: ICancellation) {
59 async onDeactivating(ct: ICancellation) {
61 if (this._controller)
60 if (this._controller)
62 await this._controller.deactivating(this, ct);
61 await this._controller.deactivating(this, ct);
63 }
62 }
64
63
65 async onDeactivated(ct: ICancellation) {
64 async onDeactivated(ct: ICancellation) {
66 if (this._controller)
65 if (this._controller)
67 await this._controller.deactivated(this, ct);
66 await this._controller.deactivated(this, ct);
68 }
67 }
69
68
70 async deactivate(ct: ICancellation = EmptyCancellation.default) {
69 async deactivate(ct: ICancellation = EmptyCancellation.default) {
71 if (!this.isActive())
70 if (!this.isActive())
72 return;
71 return;
73 ct = this.startOperation(ct);
72 ct = this.startOperation(ct);
74 try {
73 try {
75 await this.onDeactivating(ct);
74 await this.onDeactivating(ct);
76 this._active = false;
75 this._active = false;
77 try {
76 try {
78 await this.onDeactivated(ct);
77 await this.onDeactivated(ct);
79 } catch {
78 } catch(e) {
80 // TODO log error
79 log.error("Suppressed onDeactivated error: {0}", e);
81 }
80 }
82 this.completeSuccess();
81 this.completeSuccess();
83 } catch (e) {
82 } catch (e) {
84 this.completeFail(e);
83 this.completeFail(e);
85 }
84 }
86 return this.getCompletion();
85 return this.getCompletion();
87 }
86 }
88
87
89 }
88 }
90 }
89 }
91
90
92 namespace ActivatableMixin {
91 namespace ActivatableMixin {
93
92 export const traceSource = log;
94 }
93 }
95
94
96 export = ActivatableMixin; No newline at end of file
95 export = ActivatableMixin;
@@ -1,175 +1,207
1 import * as format from '../text/format'
1 import * as format from '../text/format'
2 import { argumentNotNull } from '../safe';
2 import { argumentNotNull } from '../safe';
3
3
4 interface TraceEventHandler {
4 interface TraceEventHandler {
5 (sender: TraceSource, level: number, arg: any): void;
5 (sender: TraceSource, level: number, arg: any): void;
6 }
6 }
7
7
8 interface TraceSourceHandler {
8 interface TraceSourceHandler {
9 (source: TraceSource): void;
9 (source: TraceSource): void;
10 }
10 }
11
11
12 interface Destroyable {
12 interface Destroyable {
13 destroy();
13 destroy();
14 }
14 }
15
15
16 class Registry {
16 class Registry {
17 static readonly instance = new Registry();
17 static readonly instance = new Registry();
18
18
19 private _registry: object = new Object();
19 private _registry: object = new Object();
20 private _listeners: object = new Object();
20 private _listeners: object = new Object();
21 private _nextCookie: number = 1;
21 private _nextCookie: number = 1;
22
22
23 get(id: any): TraceSource {
23 get(id: any): TraceSource {
24 argumentNotNull(id, "id");
24 argumentNotNull(id, "id");
25
25
26 if (this._registry[id])
26 if (this._registry[id])
27 return this._registry[id];
27 return this._registry[id];
28
28
29 var source = new TraceSource(id);
29 var source = new TraceSource(id);
30 this._registry[id] = source;
30 this._registry[id] = source;
31 this._onNewSource(source);
31 this._onNewSource(source);
32
32
33 return source;
33 return source;
34 }
34 }
35
35
36 add(id: any, source: TraceSource) {
36 add(id: any, source: TraceSource) {
37 argumentNotNull(id, "id");
37 argumentNotNull(id, "id");
38 argumentNotNull(source, "source");
38 argumentNotNull(source, "source");
39
39
40 this._registry[id] = source;
40 this._registry[id] = source;
41 this._onNewSource(source);
41 this._onNewSource(source);
42 }
42 }
43
43
44 _onNewSource(source: TraceSource) {
44 _onNewSource(source: TraceSource) {
45 for (let i in this._listeners)
45 for (let i in this._listeners)
46 this._listeners[i].call(null, source);
46 this._listeners[i].call(null, source);
47 }
47 }
48
48
49 on(handler: TraceSourceHandler): Destroyable {
49 on(handler: TraceSourceHandler): Destroyable {
50 argumentNotNull(handler, "handler");
50 argumentNotNull(handler, "handler");
51 var me = this;
51 var me = this;
52
52
53 var cookie = this._nextCookie++;
53 var cookie = this._nextCookie++;
54
54
55 this._listeners[cookie] = handler;
55 this._listeners[cookie] = handler;
56
56
57 for (let i in this._registry)
57 for (let i in this._registry)
58 handler(this._registry[i]);
58 handler(this._registry[i]);
59
59
60 return {
60 return {
61 destroy() {
61 destroy() {
62 delete me._listeners[cookie];
62 delete me._listeners[cookie];
63 }
63 }
64 };
64 };
65 }
65 }
66 }
66 }
67
67
68 class TraceSource {
68 class TraceSource {
69
69
70 readonly id: any
70 readonly id: any
71
71
72 // using array will provide faster iteration the with object
72 // using array will provide faster iteration the with object
73 private _handlers: Array<TraceEventHandler> = new Array<TraceEventHandler>();
73 private _handlers: Array<TraceEventHandler> = new Array<TraceEventHandler>();
74
74
75 level: number
75 level: number
76
76
77 constructor(id: any) {
77 constructor(id: any) {
78 this.id = id || new Object();
78 this.id = id || new Object();
79 }
79 }
80
80
81 on(handler: TraceEventHandler): Destroyable {
81 on(handler: TraceEventHandler): Destroyable {
82 argumentNotNull(handler, "handler");
82 argumentNotNull(handler, "handler");
83 var me = this;
83 var me = this;
84 me._handlers.push(handler);
84 me._handlers.push(handler);
85
85
86 return {
86 return {
87 destroy() {
87 destroy() {
88 me.remove(handler);
88 me.remove(handler);
89 }
89 }
90 }
90 }
91 }
91 }
92
92
93 remove(handler: TraceEventHandler): void {
93 remove(handler: TraceEventHandler): void {
94 let i = this._handlers.indexOf(handler);
94 let i = this._handlers.indexOf(handler);
95 if (i >= 0)
95 if (i >= 0)
96 this._handlers.splice(i, 1);
96 this._handlers.splice(i, 1);
97 }
97 }
98
98
99 protected emit(level: number, arg: any) {
99 protected emit(level: number, arg: any) {
100 this._handlers.forEach(h => {
100 this._handlers.forEach(h => {
101 try {
101 try {
102 h(this, level, arg);
102 h(this, level, arg);
103 } catch (e) {
103 } catch (e) {
104 // suppress error in log handlers
104 // suppress error in log handlers
105 }
105 }
106 });
106 });
107 }
107 }
108
108
109 isDebugEnabled() {
109 isDebugEnabled() {
110 return this.level >= TraceSource.DebugLevel;
110 return this.level >= TraceSource.DebugLevel;
111 }
111 }
112
112
113 debug(msg: string, ...args: any[]) {
113 debug(msg: string, ...args: any[]) {
114 if (this.isEnabled(TraceSource.DebugLevel))
114 if (this.isEnabled(TraceSource.DebugLevel))
115 this.emit(TraceSource.DebugLevel, format(msg, args));
115 this.emit(TraceSource.DebugLevel, format(msg, args));
116 }
116 }
117
117
118 isLogEnabled() {
118 isLogEnabled() {
119 return this.level >= TraceSource.LogLevel;
119 return this.level >= TraceSource.LogLevel;
120 }
120 }
121
121
122 log(msg: string, ...args: any[]) {
122 log(msg: string, ...args: any[]) {
123 if (this.isEnabled(TraceSource.LogLevel))
123 if (this.isEnabled(TraceSource.LogLevel))
124 this.emit(TraceSource.LogLevel, format(msg, args));
124 this.emit(TraceSource.LogLevel, format(msg, args));
125 }
125 }
126
126
127 isWarnEnabled() {
127 isWarnEnabled() {
128 return this.level >= TraceSource.WarnLevel;
128 return this.level >= TraceSource.WarnLevel;
129 }
129 }
130
130
131 warn(msg: string, ...args: any[]) {
131 warn(msg: string, ...args: any[]) {
132 if (this.isEnabled(TraceSource.WarnLevel))
132 if (this.isEnabled(TraceSource.WarnLevel))
133 this.emit(TraceSource.WarnLevel, format(msg, args));
133 this.emit(TraceSource.WarnLevel, format(msg, args));
134 }
134 }
135
135
136 /**
137 * returns true if errors will be recorded.
138 */
136 isErrorEnabled() {
139 isErrorEnabled() {
137 return this.level >= TraceSource.ErrorLevel;
140 return this.level >= TraceSource.ErrorLevel;
138 }
141 }
139
142
143 /**
144 * Traces a error.
145 *
146 * @param msg the message.
147 * @param args parameters which will be substituted in the message.
148 */
140 error(msg: string, ...args: any[]) {
149 error(msg: string, ...args: any[]) {
141 if (this.isEnabled(TraceSource.ErrorLevel))
150 if (this.isEnabled(TraceSource.ErrorLevel))
142 this.emit(TraceSource.ErrorLevel, format(msg, args));
151 this.emit(TraceSource.ErrorLevel, format(msg, args));
143 }
152 }
144
153
154 /**
155 * Checks whether the specified level is enabled for this
156 * trace source.
157 *
158 * @param level the trace level which should be checked.
159 */
145 isEnabled(level: number) {
160 isEnabled(level: number) {
146 return (this.level >= level);
161 return (this.level >= level);
147 }
162 }
148
163
164 /**
165 * Traces a raw event, passing data as it is to the underlying listeners
166 *
167 * @param level the level of the event
168 * @param arg the data of the event, can be a simple string or any object.
169 */
149 traceEvent(level: number, arg: any) {
170 traceEvent(level: number, arg: any) {
150 if (this.isEnabled(level))
171 if (this.isEnabled(level))
151 this.emit(level, arg);
172 this.emit(level, arg);
152 }
173 }
153
174
175 /**
176 * Register the specified handler to be called for every new and already
177 * created trace source.
178 *
179 * @param handler the handler which will be called for each trace source
180 */
154 static on(handler: TraceSourceHandler) {
181 static on(handler: TraceSourceHandler) {
155 return Registry.instance.on(handler);
182 return Registry.instance.on(handler);
156 }
183 }
157
184
185 /**
186 * Creates or returns already created trace source for the specified id.
187 *
188 * @param id the id for the trace source
189 */
158 static get(id: any) {
190 static get(id: any) {
159 return Registry.instance.get(id);
191 return Registry.instance.get(id);
160 }
192 }
161 }
193 }
162
194
163 namespace TraceSource {
195 namespace TraceSource {
164 export const DebugLevel = 400;
196 export const DebugLevel = 400;
165
197
166 export const LogLevel = 300;
198 export const LogLevel = 300;
167
199
168 export const WarnLevel = 200;
200 export const WarnLevel = 200;
169
201
170 export const ErrorLevel = 100;
202 export const ErrorLevel = 100;
171
203
172 export const SilentLevel = 0;
204 export const SilentLevel = 0;
173 }
205 }
174
206
175 export = TraceSource; No newline at end of file
207 export = TraceSource;
@@ -1,1 +1,1
1 define(["./ActivatableTests", "./trace-test"]); No newline at end of file
1 define(["./ActivatableTests", "./trace-test", "./TraceSourceTests"]); No newline at end of file
@@ -1,14 +1,22
1 import * as TraceSource from '@implab/core/log/TraceSource'
1 import * as TraceSource from '@implab/core/log/TraceSource'
2 import * as tape from 'tape';
2 import * as tape from 'tape';
3
3
4 const sourceId = 'test/TraceSourceTests';
4 const sourceId = 'test/TraceSourceTests';
5
5
6 tape('', t => {
6 tape('', t => {
7 let trace = TraceSource.get(sourceId);
7 let trace = TraceSource.get(sourceId);
8
8
9 let h = trace.on((sender,level,msg) => {
9 trace.level = TraceSource.DebugLevel;
10
10
11 let h = trace.on((sender,level,msg) => {
12 t.equal(sender, trace, "sender should be the current trace source");
13 t.equal(TraceSource.DebugLevel, level, "level should be debug level");
14 t.equal(msg, "Hello, World!", "The message should be formatted correctly");
15
16 t.end();
11 });
17 });
12
18
19 trace.debug("Hello, {0}!", "World");
20
13 h.destroy();
21 h.destroy();
14 }); No newline at end of file
22 });
1 NO CONTENT: file was removed
NO CONTENT: file was removed
1 NO CONTENT: file was removed
NO CONTENT: file was removed
1 NO CONTENT: file was removed
NO CONTENT: file was removed
General Comments 0
You need to be logged in to leave comments. Login now