| @@ -0,0 +1,39 | |||
|
|
1 | import { IObservable, ICancellation, IDestroyable } from "@implab/core/interfaces"; | |
|
|
2 | import * as TraceEvent from '@implab/core/log/TraceEvent'; | |
|
|
3 | import { Cancellation } from "@implab/core/Cancellation"; | |
|
|
4 | import * as TraceSource from "@implab/core/log/TraceSource"; | |
|
|
5 | import * as tape from 'tape'; | |
|
|
6 | import { argumentNotNull } from "@implab/core/safe"; | |
|
|
7 | ||
|
|
8 | export class TapeWriter implements IDestroyable { | |
|
|
9 | readonly _tape: tape.Test | |
|
|
10 | ||
|
|
11 | _subscriptions = new Array<IDestroyable>(); | |
|
|
12 | ||
|
|
13 | constructor(tape: tape.Test) { | |
|
|
14 | argumentNotNull(tape, "tape"); | |
|
|
15 | this._tape = tape; | |
|
|
16 | } | |
|
|
17 | ||
|
|
18 | writeEvents(source: IObservable<TraceEvent>, ct: ICancellation = Cancellation.none) { | |
|
|
19 | let subscription = source.on(this.writeEvent.bind(this)); | |
|
|
20 | if (ct.isSupported()) { | |
|
|
21 | ct.register(subscription.destroy.bind(subscription)); | |
|
|
22 | } | |
|
|
23 | this._subscriptions.push(subscription); | |
|
|
24 | } | |
|
|
25 | ||
|
|
26 | writeEvent(next: TraceEvent) { | |
|
|
27 | if (next.level >= TraceSource.LogLevel) { | |
|
|
28 | this._tape.comment("LOG " + next.arg); | |
|
|
29 | } else if(next.level >= TraceSource.WarnLevel) { | |
|
|
30 | this._tape.comment("WARN " + next.arg); | |
|
|
31 | } else { | |
|
|
32 | this._tape.comment("ERROR " + next.arg); | |
|
|
33 | } | |
|
|
34 | } | |
|
|
35 | ||
|
|
36 | destroy() { | |
|
|
37 | this._subscriptions.forEach(x => x.destroy()); | |
|
|
38 | } | |
|
|
39 | } No newline at end of file | |
| @@ -1,445 +1,445 | |||
|
|
1 | 1 | { |
|
|
2 | 2 | "name": "@implab/core", |
|
|
3 | 3 | "version": "0.0.1-dev", |
|
|
4 | 4 | "lockfileVersion": 1, |
|
|
5 | 5 | "requires": true, |
|
|
6 | 6 | "dependencies": { |
|
|
7 | 7 | "@types/node": { |
|
|
8 | 8 | "version": "10.5.1", |
|
|
9 | 9 | "resolved": "https://registry.npmjs.org/@types/node/-/node-10.5.1.tgz", |
|
|
10 | 10 | "integrity": "sha512-AFLl1IALIuyt6oK4AYZsgWVJ/5rnyzQWud7IebaZWWV3YmgtPZkQmYio9R5Ze/2pdd7XfqF5bP+hWS11mAKoOQ==", |
|
|
11 | 11 | "dev": true |
|
|
12 | 12 | }, |
|
|
13 | 13 | "@types/tape": { |
|
|
14 | 14 | "version": "4.2.32", |
|
|
15 | 15 | "resolved": "https://registry.npmjs.org/@types/tape/-/tape-4.2.32.tgz", |
|
|
16 | 16 | "integrity": "sha512-xil0KO5wkPoixdBWGIGolPv9dekf6dVkjjJLAFYchfKcd4DICou67rgGCIO7wAh3i5Ff/6j9IDgZz+GU9cMaqQ==", |
|
|
17 | 17 | "dev": true, |
|
|
18 | 18 | "requires": { |
|
|
19 | 19 | "@types/node": "10.5.1" |
|
|
20 | 20 | } |
|
|
21 | 21 | }, |
|
|
22 | 22 | "balanced-match": { |
|
|
23 | 23 | "version": "1.0.0", |
|
|
24 | 24 | "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.0.tgz", |
|
|
25 | 25 | "integrity": "sha1-ibTRmasr7kneFk6gK4nORi1xt2c=", |
|
|
26 | 26 | "dev": true |
|
|
27 | 27 | }, |
|
|
28 | 28 | "brace-expansion": { |
|
|
29 | 29 | "version": "1.1.11", |
|
|
30 | 30 | "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz", |
|
|
31 | 31 | "integrity": "sha512-iCuPHDFgrHX7H2vEI/5xpz07zSHB00TpugqhmYtVmMO6518mCuRMoOYFldEBl0g187ufozdaHgWKcYFb61qGiA==", |
|
|
32 | 32 | "dev": true, |
|
|
33 | 33 | "requires": { |
|
|
34 | 34 | "balanced-match": "1.0.0", |
|
|
35 | 35 | "concat-map": "0.0.1" |
|
|
36 | 36 | } |
|
|
37 | 37 | }, |
|
|
38 | 38 | "concat-map": { |
|
|
39 | 39 | "version": "0.0.1", |
|
|
40 | 40 | "resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz", |
|
|
41 | 41 | "integrity": "sha1-2Klr13/Wjfd5OnMDajug1UBdR3s=", |
|
|
42 | 42 | "dev": true |
|
|
43 | 43 | }, |
|
|
44 | 44 | "core-util-is": { |
|
|
45 | 45 | "version": "1.0.2", |
|
|
46 | 46 | "resolved": "https://registry.npmjs.org/core-util-is/-/core-util-is-1.0.2.tgz", |
|
|
47 | 47 | "integrity": "sha1-tf1UIgqivFq1eqtxQMlAdUUDwac=", |
|
|
48 | 48 | "dev": true |
|
|
49 | 49 | }, |
|
|
50 | 50 | "deep-equal": { |
|
|
51 | 51 | "version": "1.0.1", |
|
|
52 | 52 | "resolved": "https://registry.npmjs.org/deep-equal/-/deep-equal-1.0.1.tgz", |
|
|
53 | 53 | "integrity": "sha1-9dJgKStmDghO/0zbyfCK0yR0SLU=", |
|
|
54 | 54 | "dev": true |
|
|
55 | 55 | }, |
|
|
56 | 56 | "define-properties": { |
|
|
57 | 57 | "version": "1.1.2", |
|
|
58 | 58 | "resolved": "https://registry.npmjs.org/define-properties/-/define-properties-1.1.2.tgz", |
|
|
59 | 59 | "integrity": "sha1-g6c/L+pWmJj7c3GTyPhzyvbUXJQ=", |
|
|
60 | 60 | "dev": true, |
|
|
61 | 61 | "requires": { |
|
|
62 | 62 | "foreach": "2.0.5", |
|
|
63 | 63 | "object-keys": "1.0.12" |
|
|
64 | 64 | } |
|
|
65 | 65 | }, |
|
|
66 | 66 | "defined": { |
|
|
67 | 67 | "version": "1.0.0", |
|
|
68 | 68 | "resolved": "https://registry.npmjs.org/defined/-/defined-1.0.0.tgz", |
|
|
69 | 69 | "integrity": "sha1-yY2bzvdWdBiOEQlpFRGZ45sfppM=", |
|
|
70 | 70 | "dev": true |
|
|
71 | 71 | }, |
|
|
72 | 72 | "dojo": { |
|
|
73 | 73 | "version": "1.13.0", |
|
|
74 | 74 | "resolved": "https://registry.npmjs.org/dojo/-/dojo-1.13.0.tgz", |
|
|
75 | 75 | "integrity": "sha512-mGoGvsXAbPkUrBnxCoO7m6CFH8jvWq7rAL7fP7jrhJEOyswA/bZwWdXwEH0ovs68t8S0+xOpV/3V7addYbaiAA==" |
|
|
76 | 76 | }, |
|
|
77 | 77 | "duplexer": { |
|
|
78 | 78 | "version": "0.1.1", |
|
|
79 | 79 | "resolved": "https://registry.npmjs.org/duplexer/-/duplexer-0.1.1.tgz", |
|
|
80 | 80 | "integrity": "sha1-rOb/gIwc5mtX0ev5eXessCM0z8E=", |
|
|
81 | 81 | "dev": true |
|
|
82 | 82 | }, |
|
|
83 | 83 | "es-abstract": { |
|
|
84 | 84 | "version": "1.12.0", |
|
|
85 | 85 | "resolved": "https://registry.npmjs.org/es-abstract/-/es-abstract-1.12.0.tgz", |
|
|
86 | 86 | "integrity": "sha512-C8Fx/0jFmV5IPoMOFPA9P9G5NtqW+4cOPit3MIuvR2t7Ag2K15EJTpxnHAYTzL+aYQJIESYeXZmDBfOBE1HcpA==", |
|
|
87 | 87 | "dev": true, |
|
|
88 | 88 | "requires": { |
|
|
89 | 89 | "es-to-primitive": "1.1.1", |
|
|
90 | 90 | "function-bind": "1.1.1", |
|
|
91 | 91 | "has": "1.0.3", |
|
|
92 | 92 | "is-callable": "1.1.3", |
|
|
93 | 93 | "is-regex": "1.0.4" |
|
|
94 | 94 | } |
|
|
95 | 95 | }, |
|
|
96 | 96 | "es-to-primitive": { |
|
|
97 | 97 | "version": "1.1.1", |
|
|
98 | 98 | "resolved": "https://registry.npmjs.org/es-to-primitive/-/es-to-primitive-1.1.1.tgz", |
|
|
99 | 99 | "integrity": "sha1-RTVSSKiJeQNLZ5Lhm7gfK3l13Q0=", |
|
|
100 | 100 | "dev": true, |
|
|
101 | 101 | "requires": { |
|
|
102 | 102 | "is-callable": "1.1.3", |
|
|
103 | 103 | "is-date-object": "1.0.1", |
|
|
104 | 104 | "is-symbol": "1.0.1" |
|
|
105 | 105 | } |
|
|
106 | 106 | }, |
|
|
107 | 107 | "faucet": { |
|
|
108 | 108 | "version": "0.0.1", |
|
|
109 | 109 | "resolved": "https://registry.npmjs.org/faucet/-/faucet-0.0.1.tgz", |
|
|
110 | 110 | "integrity": "sha1-WX3PHSGJosBiMhtZHo8VHtIDnZw=", |
|
|
111 | 111 | "dev": true, |
|
|
112 | 112 | "requires": { |
|
|
113 | 113 | "defined": "0.0.0", |
|
|
114 | 114 | "duplexer": "0.1.1", |
|
|
115 | 115 | "minimist": "0.0.5", |
|
|
116 | 116 | "sprintf": "0.1.5", |
|
|
117 | 117 | "tap-parser": "0.4.3", |
|
|
118 | 118 | "tape": "2.3.3", |
|
|
119 | 119 | "through2": "0.2.3" |
|
|
120 | 120 | }, |
|
|
121 | 121 | "dependencies": { |
|
|
122 | 122 | "deep-equal": { |
|
|
123 | 123 | "version": "0.1.2", |
|
|
124 | 124 | "resolved": "https://registry.npmjs.org/deep-equal/-/deep-equal-0.1.2.tgz", |
|
|
125 | 125 | "integrity": "sha1-skbCuApXCkfBG+HZvRBw7IeLh84=", |
|
|
126 | 126 | "dev": true |
|
|
127 | 127 | }, |
|
|
128 | 128 | "defined": { |
|
|
129 | 129 | "version": "0.0.0", |
|
|
130 | 130 | "resolved": "https://registry.npmjs.org/defined/-/defined-0.0.0.tgz", |
|
|
131 | 131 | "integrity": "sha1-817qfXBekzuvE7LwOz+D2SFAOz4=", |
|
|
132 | 132 | "dev": true |
|
|
133 | 133 | }, |
|
|
134 | 134 | "minimist": { |
|
|
135 | 135 | "version": "0.0.5", |
|
|
136 | "resolved": "http://registry.npmjs.org/minimist/-/minimist-0.0.5.tgz", | |
|
|
136 | "resolved": "https://registry.npmjs.org/minimist/-/minimist-0.0.5.tgz", | |
|
|
137 | 137 | "integrity": "sha1-16oye87PUY+RBqxrjwA/o7zqhWY=", |
|
|
138 | 138 | "dev": true |
|
|
139 | 139 | }, |
|
|
140 | 140 | "tape": { |
|
|
141 | 141 | "version": "2.3.3", |
|
|
142 | 142 | "resolved": "https://registry.npmjs.org/tape/-/tape-2.3.3.tgz", |
|
|
143 | 143 | "integrity": "sha1-Lnzgox3wn41oUWZKcYQuDKUFevc=", |
|
|
144 | 144 | "dev": true, |
|
|
145 | 145 | "requires": { |
|
|
146 | 146 | "deep-equal": "0.1.2", |
|
|
147 | 147 | "defined": "0.0.0", |
|
|
148 | 148 | "inherits": "2.0.3", |
|
|
149 | 149 | "jsonify": "0.0.0", |
|
|
150 | 150 | "resumer": "0.0.0", |
|
|
151 | 151 | "through": "2.3.8" |
|
|
152 | 152 | } |
|
|
153 | 153 | } |
|
|
154 | 154 | } |
|
|
155 | 155 | }, |
|
|
156 | 156 | "for-each": { |
|
|
157 | 157 | "version": "0.3.3", |
|
|
158 | 158 | "resolved": "https://registry.npmjs.org/for-each/-/for-each-0.3.3.tgz", |
|
|
159 | 159 | "integrity": "sha512-jqYfLp7mo9vIyQf8ykW2v7A+2N4QjeCeI5+Dz9XraiO1ign81wjiH7Fb9vSOWvQfNtmSa4H2RoQTrrXivdUZmw==", |
|
|
160 | 160 | "dev": true, |
|
|
161 | 161 | "requires": { |
|
|
162 | 162 | "is-callable": "1.1.3" |
|
|
163 | 163 | } |
|
|
164 | 164 | }, |
|
|
165 | 165 | "foreach": { |
|
|
166 | 166 | "version": "2.0.5", |
|
|
167 | 167 | "resolved": "https://registry.npmjs.org/foreach/-/foreach-2.0.5.tgz", |
|
|
168 | 168 | "integrity": "sha1-C+4AUBiusmDQo6865ljdATbsG5k=", |
|
|
169 | 169 | "dev": true |
|
|
170 | 170 | }, |
|
|
171 | 171 | "fs.realpath": { |
|
|
172 | 172 | "version": "1.0.0", |
|
|
173 | 173 | "resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz", |
|
|
174 | 174 | "integrity": "sha1-FQStJSMVjKpA20onh8sBQRmU6k8=", |
|
|
175 | 175 | "dev": true |
|
|
176 | 176 | }, |
|
|
177 | 177 | "function-bind": { |
|
|
178 | 178 | "version": "1.1.1", |
|
|
179 | 179 | "resolved": "https://registry.npmjs.org/function-bind/-/function-bind-1.1.1.tgz", |
|
|
180 | 180 | "integrity": "sha512-yIovAzMX49sF8Yl58fSCWJ5svSLuaibPxXQJFLmBObTuCr0Mf1KiPopGM9NiFjiYBCbfaa2Fh6breQ6ANVTI0A==", |
|
|
181 | 181 | "dev": true |
|
|
182 | 182 | }, |
|
|
183 | 183 | "glob": { |
|
|
184 | 184 | "version": "7.1.2", |
|
|
185 | 185 | "resolved": "https://registry.npmjs.org/glob/-/glob-7.1.2.tgz", |
|
|
186 | 186 | "integrity": "sha512-MJTUg1kjuLeQCJ+ccE4Vpa6kKVXkPYJ2mOCQyUuKLcLQsdrMCpBPUi8qVE6+YuaJkozeA9NusTAw3hLr8Xe5EQ==", |
|
|
187 | 187 | "dev": true, |
|
|
188 | 188 | "requires": { |
|
|
189 | 189 | "fs.realpath": "1.0.0", |
|
|
190 | 190 | "inflight": "1.0.6", |
|
|
191 | 191 | "inherits": "2.0.3", |
|
|
192 | 192 | "minimatch": "3.0.4", |
|
|
193 | 193 | "once": "1.4.0", |
|
|
194 | 194 | "path-is-absolute": "1.0.1" |
|
|
195 | 195 | } |
|
|
196 | 196 | }, |
|
|
197 | 197 | "has": { |
|
|
198 | 198 | "version": "1.0.3", |
|
|
199 | 199 | "resolved": "https://registry.npmjs.org/has/-/has-1.0.3.tgz", |
|
|
200 | 200 | "integrity": "sha512-f2dvO0VU6Oej7RkWJGrehjbzMAjFp5/VKPp5tTpWIV4JHHZK1/BxbFRtf/siA2SWTe09caDmVtYYzWEIbBS4zw==", |
|
|
201 | 201 | "dev": true, |
|
|
202 | 202 | "requires": { |
|
|
203 | 203 | "function-bind": "1.1.1" |
|
|
204 | 204 | } |
|
|
205 | 205 | }, |
|
|
206 | 206 | "inflight": { |
|
|
207 | 207 | "version": "1.0.6", |
|
|
208 | 208 | "resolved": "https://registry.npmjs.org/inflight/-/inflight-1.0.6.tgz", |
|
|
209 | 209 | "integrity": "sha1-Sb1jMdfQLQwJvJEKEHW6gWW1bfk=", |
|
|
210 | 210 | "dev": true, |
|
|
211 | 211 | "requires": { |
|
|
212 | 212 | "once": "1.4.0", |
|
|
213 | 213 | "wrappy": "1.0.2" |
|
|
214 | 214 | } |
|
|
215 | 215 | }, |
|
|
216 | 216 | "inherits": { |
|
|
217 | 217 | "version": "2.0.3", |
|
|
218 | 218 | "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.3.tgz", |
|
|
219 | 219 | "integrity": "sha1-Yzwsg+PaQqUC9SRmAiSA9CCCYd4=", |
|
|
220 | 220 | "dev": true |
|
|
221 | 221 | }, |
|
|
222 | 222 | "is-callable": { |
|
|
223 | 223 | "version": "1.1.3", |
|
|
224 | 224 | "resolved": "https://registry.npmjs.org/is-callable/-/is-callable-1.1.3.tgz", |
|
|
225 | 225 | "integrity": "sha1-hut1OSgF3cM69xySoO7fdO52BLI=", |
|
|
226 | 226 | "dev": true |
|
|
227 | 227 | }, |
|
|
228 | 228 | "is-date-object": { |
|
|
229 | 229 | "version": "1.0.1", |
|
|
230 | 230 | "resolved": "https://registry.npmjs.org/is-date-object/-/is-date-object-1.0.1.tgz", |
|
|
231 | 231 | "integrity": "sha1-mqIOtq7rv/d/vTPnTKAbM1gdOhY=", |
|
|
232 | 232 | "dev": true |
|
|
233 | 233 | }, |
|
|
234 | 234 | "is-regex": { |
|
|
235 | 235 | "version": "1.0.4", |
|
|
236 | 236 | "resolved": "https://registry.npmjs.org/is-regex/-/is-regex-1.0.4.tgz", |
|
|
237 | 237 | "integrity": "sha1-VRdIm1RwkbCTDglWVM7SXul+lJE=", |
|
|
238 | 238 | "dev": true, |
|
|
239 | 239 | "requires": { |
|
|
240 | 240 | "has": "1.0.3" |
|
|
241 | 241 | } |
|
|
242 | 242 | }, |
|
|
243 | 243 | "is-symbol": { |
|
|
244 | 244 | "version": "1.0.1", |
|
|
245 | 245 | "resolved": "https://registry.npmjs.org/is-symbol/-/is-symbol-1.0.1.tgz", |
|
|
246 | 246 | "integrity": "sha1-PMWfAAJRlLarLjjbrmaJJWtmBXI=", |
|
|
247 | 247 | "dev": true |
|
|
248 | 248 | }, |
|
|
249 | 249 | "isarray": { |
|
|
250 | 250 | "version": "0.0.1", |
|
|
251 | 251 | "resolved": "https://registry.npmjs.org/isarray/-/isarray-0.0.1.tgz", |
|
|
252 | 252 | "integrity": "sha1-ihis/Kmo9Bd+Cav8YDiTmwXR7t8=", |
|
|
253 | 253 | "dev": true |
|
|
254 | 254 | }, |
|
|
255 | 255 | "jsonify": { |
|
|
256 | 256 | "version": "0.0.0", |
|
|
257 | 257 | "resolved": "https://registry.npmjs.org/jsonify/-/jsonify-0.0.0.tgz", |
|
|
258 | 258 | "integrity": "sha1-LHS27kHZPKUbe1qu6PUDYx0lKnM=", |
|
|
259 | 259 | "dev": true |
|
|
260 | 260 | }, |
|
|
261 | 261 | "minimatch": { |
|
|
262 | 262 | "version": "3.0.4", |
|
|
263 | 263 | "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.0.4.tgz", |
|
|
264 | 264 | "integrity": "sha512-yJHVQEhyqPLUTgt9B83PXu6W3rx4MvvHvSUvToogpwoGDOUQ+yDrR0HRot+yOCdCO7u4hX3pWft6kWBBcqh0UA==", |
|
|
265 | 265 | "dev": true, |
|
|
266 | 266 | "requires": { |
|
|
267 | 267 | "brace-expansion": "1.1.11" |
|
|
268 | 268 | } |
|
|
269 | 269 | }, |
|
|
270 | 270 | "minimist": { |
|
|
271 | 271 | "version": "1.2.0", |
|
|
272 | 272 | "resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.0.tgz", |
|
|
273 | 273 | "integrity": "sha1-o1AIsg9BOD7sH7kU9M1d95omQoQ=", |
|
|
274 | 274 | "dev": true |
|
|
275 | 275 | }, |
|
|
276 | 276 | "object-inspect": { |
|
|
277 | 277 | "version": "1.6.0", |
|
|
278 | 278 | "resolved": "https://registry.npmjs.org/object-inspect/-/object-inspect-1.6.0.tgz", |
|
|
279 | 279 | "integrity": "sha512-GJzfBZ6DgDAmnuaM3104jR4s1Myxr3Y3zfIyN4z3UdqN69oSRacNK8UhnobDdC+7J2AHCjGwxQubNJfE70SXXQ==", |
|
|
280 | 280 | "dev": true |
|
|
281 | 281 | }, |
|
|
282 | 282 | "object-keys": { |
|
|
283 | 283 | "version": "1.0.12", |
|
|
284 | 284 | "resolved": "https://registry.npmjs.org/object-keys/-/object-keys-1.0.12.tgz", |
|
|
285 | 285 | "integrity": "sha512-FTMyFUm2wBcGHnH2eXmz7tC6IwlqQZ6mVZ+6dm6vZ4IQIHjs6FdNsQBuKGPuUUUY6NfJw2PshC08Tn6LzLDOag==", |
|
|
286 | 286 | "dev": true |
|
|
287 | 287 | }, |
|
|
288 | 288 | "once": { |
|
|
289 | 289 | "version": "1.4.0", |
|
|
290 | 290 | "resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz", |
|
|
291 | 291 | "integrity": "sha1-WDsap3WWHUsROsF9nFC6753Xa9E=", |
|
|
292 | 292 | "dev": true, |
|
|
293 | 293 | "requires": { |
|
|
294 | 294 | "wrappy": "1.0.2" |
|
|
295 | 295 | } |
|
|
296 | 296 | }, |
|
|
297 | 297 | "path-is-absolute": { |
|
|
298 | 298 | "version": "1.0.1", |
|
|
299 | 299 | "resolved": "https://registry.npmjs.org/path-is-absolute/-/path-is-absolute-1.0.1.tgz", |
|
|
300 | 300 | "integrity": "sha1-F0uSaHNVNP+8es5r9TpanhtcX18=", |
|
|
301 | 301 | "dev": true |
|
|
302 | 302 | }, |
|
|
303 | 303 | "path-parse": { |
|
|
304 | 304 | "version": "1.0.5", |
|
|
305 | 305 | "resolved": "https://registry.npmjs.org/path-parse/-/path-parse-1.0.5.tgz", |
|
|
306 | 306 | "integrity": "sha1-PBrfhx6pzWyUMbbqK9dKD/BVxME=", |
|
|
307 | 307 | "dev": true |
|
|
308 | 308 | }, |
|
|
309 | 309 | "readable-stream": { |
|
|
310 | 310 | "version": "1.1.14", |
|
|
311 | 311 | "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-1.1.14.tgz", |
|
|
312 | 312 | "integrity": "sha1-fPTFTvZI44EwhMY23SB54WbAgdk=", |
|
|
313 | 313 | "dev": true, |
|
|
314 | 314 | "requires": { |
|
|
315 | 315 | "core-util-is": "1.0.2", |
|
|
316 | 316 | "inherits": "2.0.3", |
|
|
317 | 317 | "isarray": "0.0.1", |
|
|
318 | 318 | "string_decoder": "0.10.31" |
|
|
319 | 319 | } |
|
|
320 | 320 | }, |
|
|
321 | 321 | "requirejs": { |
|
|
322 |
"version": "2.3. |
|
|
|
323 |
"resolved": "https://registry.npmjs.org/requirejs/-/requirejs-2.3. |
|
|
|
324 | "integrity": "sha512-ipEzlWQe6RK3jkzikgCupiTbTvm4S0/CAU5GlgptkN5SO6F3u0UD0K18wy6ErDqiCyP4J4YYe1HuAShvsxePLg==", | |
|
|
322 | "version": "2.3.5", | |
|
|
323 | "resolved": "https://registry.npmjs.org/requirejs/-/requirejs-2.3.5.tgz", | |
|
|
324 | "integrity": "sha512-svnO+aNcR/an9Dpi44C7KSAy5fFGLtmPbaaCeQaklUz8BQhS64tWWIIlvEA5jrWICzlO/X9KSzSeXFnZdBu8nw==", | |
|
|
325 | 325 | "dev": true |
|
|
326 | 326 | }, |
|
|
327 | 327 | "resolve": { |
|
|
328 | 328 | "version": "1.7.1", |
|
|
329 | 329 | "resolved": "https://registry.npmjs.org/resolve/-/resolve-1.7.1.tgz", |
|
|
330 | 330 | "integrity": "sha512-c7rwLofp8g1U+h1KNyHL/jicrKg1Ek4q+Lr33AL65uZTinUZHe30D5HlyN5V9NW0JX1D5dXQ4jqW5l7Sy/kGfw==", |
|
|
331 | 331 | "dev": true, |
|
|
332 | 332 | "requires": { |
|
|
333 | 333 | "path-parse": "1.0.5" |
|
|
334 | 334 | } |
|
|
335 | 335 | }, |
|
|
336 | 336 | "resumer": { |
|
|
337 | 337 | "version": "0.0.0", |
|
|
338 | 338 | "resolved": "https://registry.npmjs.org/resumer/-/resumer-0.0.0.tgz", |
|
|
339 | 339 | "integrity": "sha1-8ej0YeQGS6Oegq883CqMiT0HZ1k=", |
|
|
340 | 340 | "dev": true, |
|
|
341 | 341 | "requires": { |
|
|
342 | 342 | "through": "2.3.8" |
|
|
343 | 343 | } |
|
|
344 | 344 | }, |
|
|
345 | 345 | "sprintf": { |
|
|
346 | 346 | "version": "0.1.5", |
|
|
347 | 347 | "resolved": "https://registry.npmjs.org/sprintf/-/sprintf-0.1.5.tgz", |
|
|
348 | 348 | "integrity": "sha1-j4PjmpMXwaUCy324BQ5Rxnn27c8=", |
|
|
349 | 349 | "dev": true |
|
|
350 | 350 | }, |
|
|
351 | 351 | "string.prototype.trim": { |
|
|
352 | 352 | "version": "1.1.2", |
|
|
353 | 353 | "resolved": "https://registry.npmjs.org/string.prototype.trim/-/string.prototype.trim-1.1.2.tgz", |
|
|
354 | 354 | "integrity": "sha1-0E3iyJ4Tf019IG8Ia17S+ua+jOo=", |
|
|
355 | 355 | "dev": true, |
|
|
356 | 356 | "requires": { |
|
|
357 | 357 | "define-properties": "1.1.2", |
|
|
358 | 358 | "es-abstract": "1.12.0", |
|
|
359 | 359 | "function-bind": "1.1.1" |
|
|
360 | 360 | } |
|
|
361 | 361 | }, |
|
|
362 | 362 | "string_decoder": { |
|
|
363 | 363 | "version": "0.10.31", |
|
|
364 | 364 | "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-0.10.31.tgz", |
|
|
365 | 365 | "integrity": "sha1-YuIDvEF2bGwoyfyEMB2rHFMQ+pQ=", |
|
|
366 | 366 | "dev": true |
|
|
367 | 367 | }, |
|
|
368 | 368 | "tap-parser": { |
|
|
369 | 369 | "version": "0.4.3", |
|
|
370 | 370 | "resolved": "https://registry.npmjs.org/tap-parser/-/tap-parser-0.4.3.tgz", |
|
|
371 | 371 | "integrity": "sha1-pOrhkMENdsehEZIf84u+TVjwnuo=", |
|
|
372 | 372 | "dev": true, |
|
|
373 | 373 | "requires": { |
|
|
374 | 374 | "inherits": "2.0.3", |
|
|
375 | 375 | "readable-stream": "1.1.14" |
|
|
376 | 376 | } |
|
|
377 | 377 | }, |
|
|
378 | 378 | "tape": { |
|
|
379 | 379 | "version": "4.9.1", |
|
|
380 | 380 | "resolved": "https://registry.npmjs.org/tape/-/tape-4.9.1.tgz", |
|
|
381 | 381 | "integrity": "sha512-6fKIXknLpoe/Jp4rzHKFPpJUHDHDqn8jus99IfPnHIjyz78HYlefTGD3b5EkbQzuLfaEvmfPK3IolLgq2xT3kw==", |
|
|
382 | 382 | "dev": true, |
|
|
383 | 383 | "requires": { |
|
|
384 | 384 | "deep-equal": "1.0.1", |
|
|
385 | 385 | "defined": "1.0.0", |
|
|
386 | 386 | "for-each": "0.3.3", |
|
|
387 | 387 | "function-bind": "1.1.1", |
|
|
388 | 388 | "glob": "7.1.2", |
|
|
389 | 389 | "has": "1.0.3", |
|
|
390 | 390 | "inherits": "2.0.3", |
|
|
391 | 391 | "minimist": "1.2.0", |
|
|
392 | 392 | "object-inspect": "1.6.0", |
|
|
393 | 393 | "resolve": "1.7.1", |
|
|
394 | 394 | "resumer": "0.0.0", |
|
|
395 | 395 | "string.prototype.trim": "1.1.2", |
|
|
396 | 396 | "through": "2.3.8" |
|
|
397 | 397 | } |
|
|
398 | 398 | }, |
|
|
399 | 399 | "through": { |
|
|
400 | 400 | "version": "2.3.8", |
|
|
401 | 401 | "resolved": "https://registry.npmjs.org/through/-/through-2.3.8.tgz", |
|
|
402 | 402 | "integrity": "sha1-DdTJ/6q8NXlgsbckEV1+Doai4fU=", |
|
|
403 | 403 | "dev": true |
|
|
404 | 404 | }, |
|
|
405 | 405 | "through2": { |
|
|
406 | 406 | "version": "0.2.3", |
|
|
407 | 407 | "resolved": "https://registry.npmjs.org/through2/-/through2-0.2.3.tgz", |
|
|
408 | 408 | "integrity": "sha1-6zKE2k6jEbbMis42U3SKUqvyWj8=", |
|
|
409 | 409 | "dev": true, |
|
|
410 | 410 | "requires": { |
|
|
411 | 411 | "readable-stream": "1.1.14", |
|
|
412 | 412 | "xtend": "2.1.2" |
|
|
413 | 413 | } |
|
|
414 | 414 | }, |
|
|
415 | 415 | "typescript": { |
|
|
416 |
"version": " |
|
|
|
417 |
"resolved": "https://registry.npmjs.org/typescript/-/typescript- |
|
|
|
418 | "integrity": "sha512-kk80vLW9iGtjMnIv11qyxLqZm20UklzuR2tL0QAnDIygIUIemcZMxlMWudl9OOt76H3ntVzcTiddQ1/pAAJMYg==", | |
|
|
416 | "version": "2.9.2", | |
|
|
417 | "resolved": "https://registry.npmjs.org/typescript/-/typescript-2.9.2.tgz", | |
|
|
418 | "integrity": "sha512-Gr4p6nFNaoufRIY4NMdpQRNmgxVIGMs4Fcu/ujdYk3nAZqk7supzBE9idmvfZIlH/Cuj//dvi+019qEue9lV0w==", | |
|
|
419 | 419 | "dev": true |
|
|
420 | 420 | }, |
|
|
421 | 421 | "wrappy": { |
|
|
422 | 422 | "version": "1.0.2", |
|
|
423 | 423 | "resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz", |
|
|
424 | 424 | "integrity": "sha1-tSQ9jz7BqjXxNkYFvA0QNuMKtp8=", |
|
|
425 | 425 | "dev": true |
|
|
426 | 426 | }, |
|
|
427 | 427 | "xtend": { |
|
|
428 | 428 | "version": "2.1.2", |
|
|
429 | 429 | "resolved": "https://registry.npmjs.org/xtend/-/xtend-2.1.2.tgz", |
|
|
430 | 430 | "integrity": "sha1-bv7MKk2tjmlixJAbM3znuoe10os=", |
|
|
431 | 431 | "dev": true, |
|
|
432 | 432 | "requires": { |
|
|
433 | 433 | "object-keys": "0.4.0" |
|
|
434 | 434 | }, |
|
|
435 | 435 | "dependencies": { |
|
|
436 | 436 | "object-keys": { |
|
|
437 | 437 | "version": "0.4.0", |
|
|
438 | 438 | "resolved": "https://registry.npmjs.org/object-keys/-/object-keys-0.4.0.tgz", |
|
|
439 | 439 | "integrity": "sha1-KKaq50KN0sOpLz2V8hM13SBOAzY=", |
|
|
440 | 440 | "dev": true |
|
|
441 | 441 | } |
|
|
442 | 442 | } |
|
|
443 | 443 | } |
|
|
444 | 444 | } |
|
|
445 | 445 | } |
| @@ -1,101 +1,101 | |||
|
|
1 | 1 | define( |
|
|
2 | 2 | [], |
|
|
3 | 3 | function() { |
|
|
4 | 4 | var map = { |
|
|
5 | 5 | "\\{" : "&curlopen;", |
|
|
6 | 6 | "\\}" : "&curlclose;", |
|
|
7 | 7 | "&" : "&", |
|
|
8 | 8 | "\\:" : ":" |
|
|
9 | 9 | }; |
|
|
10 | 10 | |
|
|
11 | 11 | var rev = { |
|
|
12 | 12 | curlopen : "{", |
|
|
13 | 13 | curlclose : "}", |
|
|
14 | 14 | amp : "&", |
|
|
15 | 15 | colon : ":" |
|
|
16 | 16 | }; |
|
|
17 | 17 | |
|
|
18 | 18 | var espaceString = function(s) { |
|
|
19 | 19 | if (!s) |
|
|
20 | 20 | return s; |
|
|
21 | return "'" + s.replace(/('|\\)/g, "\\$1") + "'"; | |
|
|
21 | return "'" + s.replace(/('|\\)/g, "\\$1").replace("\n","\\n") + "'"; | |
|
|
22 | 22 | }; |
|
|
23 | 23 | |
|
|
24 | 24 | var encode = function(s) { |
|
|
25 | 25 | if (!s) |
|
|
26 | 26 | return s; |
|
|
27 | return s.replace(/\\{|\\}|&|\\:/g, function(m) { | |
|
|
27 | return s.replace(/\\{|\\}|&|\\:|\n/g, function(m) { | |
|
|
28 | 28 | return map[m] || m; |
|
|
29 | 29 | }); |
|
|
30 | 30 | }; |
|
|
31 | 31 | |
|
|
32 | 32 | var decode = function(s) { |
|
|
33 | 33 | if (!s) |
|
|
34 | 34 | return s; |
|
|
35 | 35 | return s.replace(/&(\w+);/g, function(m, $1) { |
|
|
36 | 36 | return rev[$1] || m; |
|
|
37 | 37 | }); |
|
|
38 | 38 | }; |
|
|
39 | 39 | |
|
|
40 | 40 | var subst = function(s) { |
|
|
41 | 41 | var i = s.indexOf(":"), name, pattern; |
|
|
42 | 42 | if (i >= 0) { |
|
|
43 | 43 | name = s.substr(0, i); |
|
|
44 | 44 | pattern = s.substr(i + 1); |
|
|
45 | 45 | } else { |
|
|
46 | 46 | name = s; |
|
|
47 | 47 | } |
|
|
48 | 48 | |
|
|
49 | 49 | if (pattern) |
|
|
50 | 50 | return [ |
|
|
51 | 51 | espaceString(decode(name)), |
|
|
52 | 52 | espaceString(decode(pattern)) ]; |
|
|
53 | 53 | else |
|
|
54 | 54 | return [ espaceString(decode(name)) ]; |
|
|
55 | 55 | }; |
|
|
56 | 56 | |
|
|
57 | 57 | var compile = function(str) { |
|
|
58 | 58 | if (!str) |
|
|
59 | 59 | return function() {}; |
|
|
60 | 60 | |
|
|
61 | 61 | var chunks = encode(str).split("{"), chunk; |
|
|
62 | 62 | |
|
|
63 | 63 | var code = [ "var result=[];" ]; |
|
|
64 | 64 | |
|
|
65 | 65 | for (var i = 0; i < chunks.length; i++) { |
|
|
66 | 66 | chunk = chunks[i]; |
|
|
67 | 67 | |
|
|
68 | 68 | if (i === 0) { |
|
|
69 | 69 | if (chunk) |
|
|
70 | 70 | code.push("result.push(" + espaceString(decode(chunk)) + |
|
|
71 | 71 | ");"); |
|
|
72 | 72 | } else { |
|
|
73 | 73 | var len = chunk.indexOf("}"); |
|
|
74 | 74 | if (len < 0) |
|
|
75 | 75 | throw new Error("Unbalanced substitution #" + i); |
|
|
76 | 76 | |
|
|
77 | 77 | code.push("result.push(subst(" + |
|
|
78 | 78 | subst(chunk.substr(0, len)).join(",") + "));"); |
|
|
79 | 79 | if (chunk.length > len + 1) |
|
|
80 | 80 | code.push("result.push(" + |
|
|
81 | 81 | espaceString(decode(chunk.substr(len + 1))) + ");"); |
|
|
82 | 82 | } |
|
|
83 | 83 | } |
|
|
84 | 84 | |
|
|
85 | 85 | code.push("return result.join('');"); |
|
|
86 | 86 | |
|
|
87 | 87 | /* jshint -W054 */ |
|
|
88 | 88 | return new Function("subst", code.join("\n")); |
|
|
89 | 89 | }; |
|
|
90 | 90 | |
|
|
91 | 91 | var cache = {}; |
|
|
92 | 92 | |
|
|
93 | 93 | return function(template) { |
|
|
94 | 94 | var compiled = cache[template]; |
|
|
95 | 95 | if (!compiled) { |
|
|
96 | 96 | compiled = compile(template); |
|
|
97 | 97 | cache[template] = compiled; |
|
|
98 | 98 | } |
|
|
99 | 99 | return compiled; |
|
|
100 | 100 | }; |
|
|
101 | 101 | }); No newline at end of file |
| @@ -1,94 +1,215 | |||
|
|
1 | 1 | import { IObservable, IDestroyable, ICancellation } from '../interfaces'; |
|
|
2 | 2 | import { Cancellation } from '../Cancellation' |
|
|
3 | 3 | import { argumentNotNull } from '../safe'; |
|
|
4 | 4 | |
|
|
5 | 5 | |
|
|
6 | 6 | interface Handler<T> { |
|
|
7 |
(x:T) |
|
|
|
7 | (x: T): void | |
|
|
8 | 8 | } |
|
|
9 | 9 | |
|
|
10 | 10 | interface Initializer<T> { |
|
|
11 | (notify: Handler<T>) : (() => void) | void; | |
|
|
11 | (notify: Handler<T>, error?: (e: any) => void, complete?: () => void): (() => void) | void; | |
|
|
12 | 12 | } |
|
|
13 | 13 | |
|
|
14 | // TODO: think about to move this interfaces.ts and make it public | |
|
|
15 | interface IObserver<T> { | |
|
|
16 | next(event: T): void | |
|
|
17 | ||
|
|
18 | error(e: any): void | |
|
|
19 | ||
|
|
20 | complete(): void | |
|
|
21 | } | |
|
|
14 | 22 | |
|
|
15 | 23 | class Observable<T> implements IObservable<T>, IDestroyable { |
|
|
16 |
private _once = new Array< |
|
|
|
24 | private _once = new Array<IObserver<T>>(); | |
|
|
25 | ||
|
|
26 | private _observers = new Array<IObserver<T>>(); | |
|
|
17 | 27 | |
|
|
18 | private readonly _observers = new Array<Handler<T>>(); | |
|
|
28 | private _cleanup: (() => void) | void; | |
|
|
19 | 29 | |
|
|
20 | private readonly _cleanup : (() => void) | void; | |
|
|
30 | private _complete: boolean | |
|
|
31 | ||
|
|
32 | private _error: any | |
|
|
21 | 33 | |
|
|
22 | 34 | constructor(func?: Initializer<T>) { |
|
|
23 |
this._cleanup = func && func( |
|
|
|
35 | this._cleanup = func && func( | |
|
|
36 | this._notifyNext.bind(this), | |
|
|
37 | this._notifyError.bind(this), | |
|
|
38 | this._notifyCompleted.bind(this) | |
|
|
39 | ); | |
|
|
24 | 40 | } |
|
|
25 | 41 | |
|
|
26 | on(observer: Handler<T>, error?: Handler<any>, complete?: () => void): IDestroyable { | |
|
|
27 | argumentNotNull(observer, "observer"); | |
|
|
28 | ||
|
|
29 | this._observers.push(observer); | |
|
|
42 | /** | |
|
|
43 | * Registers handlers for the current observable object. | |
|
|
44 | * | |
|
|
45 | * @param next the handler for events | |
|
|
46 | * @param error the handler for a error | |
|
|
47 | * @param complete the handler for a completion | |
|
|
48 | * @returns {IDestroyable} the handler for the current subscription, this | |
|
|
49 | * handler can be used to unsubscribe from events. | |
|
|
50 | * | |
|
|
51 | */ | |
|
|
52 | on(next: Handler<T>, error?: Handler<any>, complete?: () => void): IDestroyable { | |
|
|
53 | argumentNotNull(next, "next"); | |
|
|
30 | 54 | |
|
|
31 | 55 | let me = this; |
|
|
32 | return { | |
|
|
56 | ||
|
|
57 | let observer: IObserver<T> & IDestroyable = { | |
|
|
58 | next: next, | |
|
|
59 | ||
|
|
60 | error(e: any) { | |
|
|
61 | if (error) | |
|
|
62 | error(e); | |
|
|
63 | }, | |
|
|
64 | ||
|
|
65 | complete() { | |
|
|
66 | if (complete) | |
|
|
67 | complete(); | |
|
|
68 | }, | |
|
|
69 | ||
|
|
33 | 70 | destroy() { |
|
|
34 |
me._removeObserver( |
|
|
|
71 | me._removeObserver(this); | |
|
|
35 | 72 | } |
|
|
36 | 73 | } |
|
|
74 | ||
|
|
75 | this._addObserver(observer); | |
|
|
76 | ||
|
|
77 | ||
|
|
78 | return observer; | |
|
|
79 | } | |
|
|
80 | ||
|
|
81 | private _addObserver(observer: IObserver<T>) { | |
|
|
82 | if (this._complete) { | |
|
|
83 | try { | |
|
|
84 | if (this._error) | |
|
|
85 | observer.error(this._error); | |
|
|
86 | else | |
|
|
87 | observer.complete(); | |
|
|
88 | } catch (e) { | |
|
|
89 | this.onObserverException(e); | |
|
|
90 | } | |
|
|
91 | } else { | |
|
|
92 | this._observers.push(observer); | |
|
|
93 | } | |
|
|
37 | 94 | } |
|
|
38 | 95 | |
|
|
96 | /** | |
|
|
97 | * Waits for the next event. This method can't be used to read messages | |
|
|
98 | * as a sequence since it can skip some messages between calls. | |
|
|
99 | * | |
|
|
100 | * @param ct a cancellation token | |
|
|
101 | */ | |
|
|
39 | 102 | next(ct: ICancellation = Cancellation.none): Promise<T> { |
|
|
40 | 103 | return new Promise<T>((resolve, reject) => { |
|
|
41 | this._once.push(resolve); | |
|
|
42 | if (ct.isSupported()) { | |
|
|
104 | let observer: IObserver<T> = { | |
|
|
105 | next: resolve, | |
|
|
106 | error: reject, | |
|
|
107 | complete: () => reject("No more events are available") | |
|
|
108 | }; | |
|
|
109 | ||
|
|
110 | if (this._addOnce(observer) && ct.isSupported()) { | |
|
|
43 | 111 | ct.register((e) => { |
|
|
44 |
this._removeOnce( |
|
|
|
112 | this._removeOnce(observer); | |
|
|
45 | 113 | reject(e); |
|
|
46 | 114 | }); |
|
|
47 | 115 | } |
|
|
48 | 116 | }); |
|
|
49 | 117 | } |
|
|
50 | 118 | |
|
|
119 | private _addOnce(observer: IObserver<T>) { | |
|
|
120 | if (this._complete) { | |
|
|
121 | try { | |
|
|
122 | if (this._error) | |
|
|
123 | observer.error(this._error); | |
|
|
124 | else | |
|
|
125 | observer.complete(); | |
|
|
126 | } catch (e) { | |
|
|
127 | this.onObserverException(e); | |
|
|
128 | } | |
|
|
129 | return false; | |
|
|
130 | } | |
|
|
131 | ||
|
|
132 | this._once.push(observer); | |
|
|
133 | return true; | |
|
|
134 | } | |
|
|
135 | ||
|
|
51 | 136 | destroy() { |
|
|
52 |
if(this._c |
|
|
|
53 |
this._ |
|
|
|
137 | if (this._complete) | |
|
|
138 | this._notifyCompleted(); | |
|
|
139 | ||
|
|
140 | let cleanup = this._cleanup; | |
|
|
141 | if (cleanup) { | |
|
|
142 | this._cleanup = null; | |
|
|
143 | cleanup(); | |
|
|
144 | } | |
|
|
54 | 145 | } |
|
|
55 | 146 | |
|
|
56 | 147 | protected onObserverException(e: any) { |
|
|
57 | 148 | } |
|
|
58 | 149 | |
|
|
59 |
private _removeOnce(d: |
|
|
|
150 | private _removeOnce(d: IObserver<T>) { | |
|
|
60 | 151 | let i = this._once.indexOf(d); |
|
|
61 | 152 | if (i >= 0) |
|
|
62 | 153 | this._once.splice(i); |
|
|
63 | 154 | } |
|
|
64 | 155 | |
|
|
65 |
private _removeObserver(d: |
|
|
|
156 | private _removeObserver(d: IObserver<T>) { | |
|
|
66 | 157 | let i = this._observers.indexOf(d); |
|
|
67 | 158 | if (i >= 0) |
|
|
68 | 159 | this._observers.splice(i); |
|
|
69 | 160 | } |
|
|
70 | 161 | |
|
|
71 | protected _notify(evt: T) { | |
|
|
72 | let guard = (observer: Handler<T>) => { | |
|
|
73 | try { | |
|
|
74 | observer(evt); | |
|
|
75 | } catch (e) { | |
|
|
76 | this.onObserverException(e); | |
|
|
77 | } | |
|
|
78 | } | |
|
|
79 | ||
|
|
162 | private _notify(guard: (observer: IObserver<T>) => void) { | |
|
|
80 | 163 | if (this._once.length) { |
|
|
81 | 164 | for (let i = 0; i < this._once.length; i++) |
|
|
82 | 165 | guard(this._once[i]); |
|
|
83 | 166 | this._once = []; |
|
|
84 | 167 | } |
|
|
85 | 168 | |
|
|
86 | 169 | for (let i = 0; i < this._observers.length; i++) |
|
|
87 | 170 | guard(this._observers[i]); |
|
|
88 | 171 | } |
|
|
172 | ||
|
|
173 | protected _notifyNext(evt: T) { | |
|
|
174 | let guard = (observer: IObserver<T>) => { | |
|
|
175 | try { | |
|
|
176 | observer.next(evt); | |
|
|
177 | } catch (e) { | |
|
|
178 | this.onObserverException(e); | |
|
|
179 | } | |
|
|
180 | } | |
|
|
181 | ||
|
|
182 | this._notify(guard); | |
|
|
183 | } | |
|
|
184 | ||
|
|
185 | protected _notifyError(e: any) { | |
|
|
186 | let guard = (observer: IObserver<T>) => { | |
|
|
187 | try { | |
|
|
188 | observer.error(e); | |
|
|
189 | } catch (e) { | |
|
|
190 | this.onObserverException(e); | |
|
|
191 | } | |
|
|
192 | } | |
|
|
193 | ||
|
|
194 | this._notify(guard); | |
|
|
195 | this._observers = []; | |
|
|
196 | } | |
|
|
197 | ||
|
|
198 | protected _notifyCompleted() { | |
|
|
199 | let guard = (observer: IObserver<T>) => { | |
|
|
200 | try { | |
|
|
201 | observer.complete(); | |
|
|
202 | } catch (e) { | |
|
|
203 | this.onObserverException(e); | |
|
|
204 | } | |
|
|
205 | } | |
|
|
206 | ||
|
|
207 | this._notify(guard); | |
|
|
208 | this._observers = []; | |
|
|
209 | } | |
|
|
89 | 210 | } |
|
|
90 | 211 | |
|
|
91 | 212 | namespace Observable { |
|
|
92 | 213 | } |
|
|
93 | 214 | |
|
|
94 | 215 | export = Observable; No newline at end of file |
| @@ -1,171 +1,171 | |||
|
|
1 | 1 | import * as format from '../text/format' |
|
|
2 | 2 | import { argumentNotNull } from '../safe'; |
|
|
3 | 3 | import * as Observable from '../components/Observable' |
|
|
4 | 4 | import { IDestroyable } from '../interfaces'; |
|
|
5 | 5 | import * as TraceEvent from './TraceEvent' |
|
|
6 | 6 | |
|
|
7 | 7 | class Registry { |
|
|
8 | 8 | static readonly instance = new Registry(); |
|
|
9 | 9 | |
|
|
10 | 10 | private _registry: object = new Object(); |
|
|
11 | 11 | private _listeners: object = new Object(); |
|
|
12 | 12 | private _nextCookie: number = 1; |
|
|
13 | 13 | |
|
|
14 | 14 | get(id: any): TraceSource { |
|
|
15 | 15 | argumentNotNull(id, "id"); |
|
|
16 | 16 | |
|
|
17 | 17 | if (this._registry[id]) |
|
|
18 | 18 | return this._registry[id]; |
|
|
19 | 19 | |
|
|
20 | 20 | var source = new TraceSource(id); |
|
|
21 | 21 | this._registry[id] = source; |
|
|
22 | 22 | this._onNewSource(source); |
|
|
23 | 23 | |
|
|
24 | 24 | return source; |
|
|
25 | 25 | } |
|
|
26 | 26 | |
|
|
27 | 27 | add(id: any, source: TraceSource) { |
|
|
28 | 28 | argumentNotNull(id, "id"); |
|
|
29 | 29 | argumentNotNull(source, "source"); |
|
|
30 | 30 | |
|
|
31 | 31 | this._registry[id] = source; |
|
|
32 | 32 | this._onNewSource(source); |
|
|
33 | 33 | } |
|
|
34 | 34 | |
|
|
35 | 35 | _onNewSource(source: TraceSource) { |
|
|
36 | 36 | for (let i in this._listeners) |
|
|
37 | 37 | this._listeners[i].call(null, source); |
|
|
38 | 38 | } |
|
|
39 | 39 | |
|
|
40 | 40 | on(handler: (source: TraceSource) => void): IDestroyable { |
|
|
41 | 41 | argumentNotNull(handler, "handler"); |
|
|
42 | 42 | var me = this; |
|
|
43 | 43 | |
|
|
44 | 44 | var cookie = this._nextCookie++; |
|
|
45 | 45 | |
|
|
46 | 46 | this._listeners[cookie] = handler; |
|
|
47 | 47 | |
|
|
48 | 48 | for (let i in this._registry) |
|
|
49 | 49 | handler(this._registry[i]); |
|
|
50 | 50 | |
|
|
51 | 51 | return { |
|
|
52 | 52 | destroy() { |
|
|
53 | 53 | delete me._listeners[cookie]; |
|
|
54 | 54 | } |
|
|
55 | 55 | }; |
|
|
56 | 56 | } |
|
|
57 | 57 | } |
|
|
58 | 58 | |
|
|
59 | 59 | class TraceSource extends Observable<TraceEvent> { |
|
|
60 | 60 | readonly id: any |
|
|
61 | 61 | |
|
|
62 | 62 | level: number |
|
|
63 | 63 | |
|
|
64 | 64 | constructor(id: any) { |
|
|
65 | 65 | super(); |
|
|
66 | 66 | this.id = id || new Object(); |
|
|
67 | 67 | } |
|
|
68 | 68 | |
|
|
69 | 69 | protected emit(level: number, arg: any) { |
|
|
70 | this._notify(new TraceEvent(this, level, arg)); | |
|
|
70 | this._notifyNext(new TraceEvent(this, level, arg)); | |
|
|
71 | 71 | } |
|
|
72 | 72 | |
|
|
73 | 73 | isDebugEnabled() { |
|
|
74 | 74 | return this.level >= TraceSource.DebugLevel; |
|
|
75 | 75 | } |
|
|
76 | 76 | |
|
|
77 | 77 | debug(msg: string, ...args: any[]) { |
|
|
78 | 78 | if (this.isEnabled(TraceSource.DebugLevel)) |
|
|
79 | 79 | this.emit(TraceSource.DebugLevel, format(msg, args)); |
|
|
80 | 80 | } |
|
|
81 | 81 | |
|
|
82 | 82 | isLogEnabled() { |
|
|
83 | 83 | return this.level >= TraceSource.LogLevel; |
|
|
84 | 84 | } |
|
|
85 | 85 | |
|
|
86 | 86 | log(msg: string, ...args: any[]) { |
|
|
87 | 87 | if (this.isEnabled(TraceSource.LogLevel)) |
|
|
88 | 88 | this.emit(TraceSource.LogLevel, format(msg, args)); |
|
|
89 | 89 | } |
|
|
90 | 90 | |
|
|
91 | 91 | isWarnEnabled() { |
|
|
92 | 92 | return this.level >= TraceSource.WarnLevel; |
|
|
93 | 93 | } |
|
|
94 | 94 | |
|
|
95 | 95 | warn(msg: string, ...args: any[]) { |
|
|
96 | 96 | if (this.isEnabled(TraceSource.WarnLevel)) |
|
|
97 | 97 | this.emit(TraceSource.WarnLevel, format(msg, args)); |
|
|
98 | 98 | } |
|
|
99 | 99 | |
|
|
100 | 100 | /** |
|
|
101 | 101 | * returns true if errors will be recorded. |
|
|
102 | 102 | */ |
|
|
103 | 103 | isErrorEnabled() { |
|
|
104 | 104 | return this.level >= TraceSource.ErrorLevel; |
|
|
105 | 105 | } |
|
|
106 | 106 | |
|
|
107 | 107 | /** |
|
|
108 | 108 | * Traces a error. |
|
|
109 | 109 | * |
|
|
110 | 110 | * @param msg the message. |
|
|
111 | 111 | * @param args parameters which will be substituted in the message. |
|
|
112 | 112 | */ |
|
|
113 | 113 | error(msg: string, ...args: any[]) { |
|
|
114 | 114 | if (this.isEnabled(TraceSource.ErrorLevel)) |
|
|
115 | 115 | this.emit(TraceSource.ErrorLevel, format(msg, args)); |
|
|
116 | 116 | } |
|
|
117 | 117 | |
|
|
118 | 118 | /** |
|
|
119 | 119 | * Checks whether the specified level is enabled for this |
|
|
120 | 120 | * trace source. |
|
|
121 | 121 | * |
|
|
122 | 122 | * @param level the trace level which should be checked. |
|
|
123 | 123 | */ |
|
|
124 | 124 | isEnabled(level: number) { |
|
|
125 | 125 | return (this.level >= level); |
|
|
126 | 126 | } |
|
|
127 | 127 | |
|
|
128 | 128 | /** |
|
|
129 | 129 | * Traces a raw event, passing data as it is to the underlying listeners |
|
|
130 | 130 | * |
|
|
131 | 131 | * @param level the level of the event |
|
|
132 | 132 | * @param arg the data of the event, can be a simple string or any object. |
|
|
133 | 133 | */ |
|
|
134 | 134 | traceEvent(level: number, arg: any) { |
|
|
135 | 135 | if (this.isEnabled(level)) |
|
|
136 | 136 | this.emit(level, arg); |
|
|
137 | 137 | } |
|
|
138 | 138 | |
|
|
139 | 139 | /** |
|
|
140 | 140 | * Register the specified handler to be called for every new and already |
|
|
141 | 141 | * created trace source. |
|
|
142 | 142 | * |
|
|
143 | 143 | * @param handler the handler which will be called for each trace source |
|
|
144 | 144 | */ |
|
|
145 | 145 | static on(handler: (source: TraceSource) => void) { |
|
|
146 | 146 | return Registry.instance.on(handler); |
|
|
147 | 147 | } |
|
|
148 | 148 | |
|
|
149 | 149 | /** |
|
|
150 | 150 | * Creates or returns already created trace source for the specified id. |
|
|
151 | 151 | * |
|
|
152 | 152 | * @param id the id for the trace source |
|
|
153 | 153 | */ |
|
|
154 | 154 | static get(id: any) { |
|
|
155 | 155 | return Registry.instance.get(id); |
|
|
156 | 156 | } |
|
|
157 | 157 | } |
|
|
158 | 158 | |
|
|
159 | 159 | namespace TraceSource { |
|
|
160 | 160 | export const DebugLevel = 400; |
|
|
161 | 161 | |
|
|
162 | 162 | export const LogLevel = 300; |
|
|
163 | 163 | |
|
|
164 | 164 | export const WarnLevel = 200; |
|
|
165 | 165 | |
|
|
166 | 166 | export const ErrorLevel = 100; |
|
|
167 | 167 | |
|
|
168 | 168 | export const SilentLevel = 0; |
|
|
169 | 169 | } |
|
|
170 | 170 | |
|
|
171 | 171 | export = TraceSource; No newline at end of file |
| @@ -1,29 +1,35 | |||
|
|
1 | import { IObservable } from "../../interfaces"; | |
|
|
1 | import { IObservable, IDestroyable, ICancellation } from "../../interfaces"; | |
|
|
2 | 2 | import * as TraceEvent from '../TraceEvent'; |
|
|
3 | import { ICancellation } from "../../interfaces"; | |
|
|
4 | 3 | import { Cancellation } from "../../Cancellation"; |
|
|
5 | 4 | import * as TraceSource from "../TraceSource"; |
|
|
6 | 5 | |
|
|
7 | class ConsoleWriter { | |
|
|
8 | async write(source: IObservable<TraceEvent>, ct: ICancellation = Cancellation.none) { | |
|
|
9 | let next; | |
|
|
10 | while(next = await source.next(ct)) { | |
|
|
11 |
|
|
|
|
6 | class ConsoleWriter implements IDestroyable { | |
|
|
7 | readonly _subscriptions = new Array<IDestroyable>(); | |
|
|
8 | ||
|
|
9 | writeEvents(source: IObservable<TraceEvent>, ct: ICancellation = Cancellation.none) { | |
|
|
10 | var subscription = source.on(this.writeEvent.bind(this)); | |
|
|
11 | if (ct.isSupported()) { | |
|
|
12 | ct.register(subscription.destroy.bind(subscription)); | |
|
|
13 | } | |
|
|
14 | this._subscriptions.push(subscription); | |
|
|
15 | } | |
|
|
16 | ||
|
|
17 | writeEvent(next: TraceEvent) { | |
|
|
18 | if (next.level >= TraceSource.LogLevel) { | |
|
|
19 | console.log(next.source.id.toString(), next.arg); | |
|
|
20 | } else if(next.level >= TraceSource.WarnLevel) { | |
|
|
21 | console.warn(next.source.id.toString(), next.arg); | |
|
|
22 | } else { | |
|
|
23 | console.error(next.source.id.toString(), next.arg); | |
|
|
12 | 24 | } |
|
|
13 | 25 | } |
|
|
14 | 26 | |
|
|
15 | private _writeEvent(next: TraceEvent) { | |
|
|
16 | if (next.level >= TraceSource.LogLevel) { | |
|
|
17 | console.log(next.source.toString(), next.arg); | |
|
|
18 | } else if(next.level >= TraceSource.WarnLevel) { | |
|
|
19 | console.warn(next.source.toString(), next.arg); | |
|
|
20 | } else { | |
|
|
21 | console.error(next.source.toString(), next.arg); | |
|
|
22 | } | |
|
|
27 | destroy() { | |
|
|
28 | this._subscriptions.forEach(x => x.destroy()); | |
|
|
23 | 29 | } |
|
|
24 | 30 | } |
|
|
25 | 31 | |
|
|
26 | 32 | namespace ConsoleWriter { |
|
|
27 | 33 | } |
|
|
28 | 34 | |
|
|
29 | 35 | export = ConsoleWriter; No newline at end of file |
| @@ -1,62 +1,70 | |||
|
|
1 | 1 | import * as TraceSource from '@implab/core/log/TraceSource' |
|
|
2 | 2 | import * as tape from 'tape'; |
|
|
3 | 3 | import * as ConsoleWriter from '@implab/core/log/writers/ConsoleWriter'; |
|
|
4 | import { TapeWriter } from './TestTraits'; | |
|
|
4 | 5 | |
|
|
5 | 6 | const sourceId = 'test/TraceSourceTests'; |
|
|
6 | 7 | |
|
|
7 | 8 | tape('trace message', t => { |
|
|
8 | 9 | let trace = TraceSource.get(sourceId); |
|
|
9 | 10 | |
|
|
10 | 11 | trace.level = TraceSource.DebugLevel; |
|
|
11 | 12 | |
|
|
12 | 13 | let h = trace.on((ev) => { |
|
|
13 | 14 | t.equal(ev.source, trace, "sender should be the current trace source"); |
|
|
14 | 15 | t.equal(ev.level, TraceSource.DebugLevel, "level should be debug level"); |
|
|
15 | 16 | t.equal(ev.arg, "Hello, World!", "The message should be a formatted message"); |
|
|
16 | 17 | |
|
|
17 | 18 | t.end(); |
|
|
18 | 19 | }); |
|
|
19 | 20 | |
|
|
20 | 21 | trace.debug("Hello, {0}!", "World"); |
|
|
21 | 22 | |
|
|
22 | 23 | h.destroy(); |
|
|
23 | 24 | }); |
|
|
24 | 25 | |
|
|
25 | 26 | tape('trace event', t => { |
|
|
26 | 27 | let trace = TraceSource.get(sourceId); |
|
|
27 | 28 | |
|
|
28 | 29 | trace.level = TraceSource.DebugLevel; |
|
|
29 | 30 | |
|
|
30 | 31 | let event = { |
|
|
31 | 32 | name: "custom event" |
|
|
32 | 33 | }; |
|
|
33 | 34 | |
|
|
34 | 35 | let h = trace.on((ev) => { |
|
|
35 | 36 | t.equal(ev.source, trace, "sender should be the current trace source"); |
|
|
36 | 37 | t.equal(ev.level, TraceSource.DebugLevel, "level should be debug level"); |
|
|
37 | 38 | t.equal(ev.arg, event, "The message should be the specified object"); |
|
|
38 | 39 | |
|
|
39 | 40 | t.end(); |
|
|
40 | 41 | }); |
|
|
41 | 42 | |
|
|
42 | 43 | trace.traceEvent(TraceSource.DebugLevel, event); |
|
|
43 | 44 | |
|
|
44 | 45 | h.destroy(); |
|
|
45 | 46 | }); |
|
|
46 | 47 | |
|
|
47 |
tape(' |
|
|
|
48 |
let writer = new |
|
|
|
48 | tape('tape comment writer', async t => { | |
|
|
49 | let writer = new TapeWriter(t); | |
|
|
50 | ||
|
|
51 | TraceSource.on(ts => { | |
|
|
52 | writer.writeEvents(ts); | |
|
|
53 | }); | |
|
|
49 | 54 | |
|
|
50 | 55 | let trace = TraceSource.get(sourceId); |
|
|
51 | 56 | trace.level = TraceSource.DebugLevel; |
|
|
52 | 57 | |
|
|
53 | let p = writer.write(trace); | |
|
|
54 | ||
|
|
55 | 58 | trace.log("Hello, {0}!", 'World'); |
|
|
59 | trace.log("Multi\n line"); | |
|
|
56 | 60 | trace.warn("Look at me!"); |
|
|
57 | 61 | trace.error("DIE!"); |
|
|
58 | 62 | |
|
|
59 | console.log("DONE"); | |
|
|
63 | writer.destroy(); | |
|
|
64 | ||
|
|
65 | trace.log("You shouldn't see it!"); | |
|
|
66 | ||
|
|
67 | t.comment("DONE"); | |
|
|
60 | 68 | |
|
|
61 | 69 | t.end(); |
|
|
62 | 70 | }); No newline at end of file |
General Comments 0
You need to be logged in to leave comments.
Login now
