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