# HG changeset patch # User cin # Date 2018-09-02 22:33:00 # Node ID 57344e243cdf36c198fcb587a8346fe8f2f8ae44 # Parent c1c00bfb5487f0f6612fa9fdf6e206325b857de3 rewritten TraceSource diff --git a/package-lock.json b/package-lock.json --- a/package-lock.json +++ b/package-lock.json @@ -133,7 +133,7 @@ }, "minimist": { "version": "0.0.5", - "resolved": "https://registry.npmjs.org/minimist/-/minimist-0.0.5.tgz", + "resolved": "http://registry.npmjs.org/minimist/-/minimist-0.0.5.tgz", "integrity": "sha1-16oye87PUY+RBqxrjwA/o7zqhWY=", "dev": true }, @@ -319,9 +319,9 @@ } }, "requirejs": { - "version": "2.3.5", - "resolved": "https://registry.npmjs.org/requirejs/-/requirejs-2.3.5.tgz", - "integrity": "sha512-svnO+aNcR/an9Dpi44C7KSAy5fFGLtmPbaaCeQaklUz8BQhS64tWWIIlvEA5jrWICzlO/X9KSzSeXFnZdBu8nw==", + "version": "2.3.6", + "resolved": "https://registry.npmjs.org/requirejs/-/requirejs-2.3.6.tgz", + "integrity": "sha512-ipEzlWQe6RK3jkzikgCupiTbTvm4S0/CAU5GlgptkN5SO6F3u0UD0K18wy6ErDqiCyP4J4YYe1HuAShvsxePLg==", "dev": true }, "resolve": { @@ -413,9 +413,9 @@ } }, "typescript": { - "version": "2.9.2", - "resolved": "https://registry.npmjs.org/typescript/-/typescript-2.9.2.tgz", - "integrity": "sha512-Gr4p6nFNaoufRIY4NMdpQRNmgxVIGMs4Fcu/ujdYk3nAZqk7supzBE9idmvfZIlH/Cuj//dvi+019qEue9lV0w==", + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-3.0.3.tgz", + "integrity": "sha512-kk80vLW9iGtjMnIv11qyxLqZm20UklzuR2tL0QAnDIygIUIemcZMxlMWudl9OOt76H3ntVzcTiddQ1/pAAJMYg==", "dev": true }, "wrappy": { diff --git a/run-amd-tests.js b/run-amd-tests.js --- a/run-amd-tests.js +++ b/run-amd-tests.js @@ -14,6 +14,10 @@ requirejs.config({ { name: "test", location: "build/test" + }, + { + name: "dojo", + location: "node_modules/dojo" } ], nodeRequire: require diff --git a/src/js/log/trace.js b/src/js/log/trace.js --- a/src/js/log/trace.js +++ b/src/js/log/trace.js @@ -1,116 +1,50 @@ -define(["../text/format"], function (format) { +define(["./TraceSource"], function (TraceSource) { 'use strict'; - var listeners = []; - var channels = {}; - - var Trace = function (name) { - this.name = name; - this._subscribers = []; - }; - - Trace.prototype.debug = function () { - if (Trace.level >= 4) - this.notify("debug", format.apply(null, arguments)); - }; - - Trace.prototype.log = function () { - if (Trace.level >= 3) - this.notify("log", format.apply(null, arguments)); - }; - - Trace.prototype.warn = function () { - if (Trace.level >= 2) - this.notify("warn", format.apply(null, arguments)); - - }; + return { - Trace.prototype.error = function () { - if (Trace.level >= 1) - this.notify("error", format.apply(null, arguments)); - }; - - Trace.prototype.notify = function (name, msg) { - var me = this; - me._subscribers.forEach(function (cb) { - cb(me, name, msg); - }); - }; - - Trace.prototype.subscribe = function (cb) { - this._subscribers.push(cb); - }; - - Trace.prototype.toString = function () { - return this.name; - }; - - Trace.createChannel = function (type, name, cb) { - var chId = name; - if (channels[chId]) - return channels[chId]; - - var channel = new type(chId); - channels[chId] = channel; - - Trace._onNewChannel(chId, channel); - cb(channel); - }; + on: function (filter, cb) { + if (arguments.length == 1) { + cb = filter; + filter = undefined; + } + var test; + if (filter instanceof RegExp) { + test = function (chId) { + return filter.test(chId); + }; + } else if (filter instanceof Function) { + test = filter; + } else if (filter) { + test = function (chId) { + return chId == filter; + }; + } - Trace._onNewChannel = function (chId, ch) { - listeners.forEach(function (listener) { - listener(chId, ch); - }); - }; - - Trace.on = function (filter, cb) { - if (arguments.length == 1) { - cb = filter; - filter = undefined; - } - var d, test; - if (filter instanceof RegExp) { - test = function (chId) { - return filter.test(chId); - }; - } else if (filter instanceof Function) { - test = filter; - } else if (filter) { - test = function (chId) { - return chId == filter; - }; - } + if (test) { + TraceSource.on(function (source) { + if (test(source.id)) + source.on(cb); + }); + } else { + TraceSource.on(function (source) { + source.on(cb); + }); + } + }, - if (test) { - d = function(chId, ch) { - if(test(chId)) - ch.subscribe(cb); - }; - } else { - d = function(chId, ch) { - ch.subscribe(cb); - }; - } - listeners.push(d); - - for(var chId in channels) - d(chId,channels[chId]); - }; + load: function (id, require, cb) { + if (id) { + cb(TraceSource.get(id)); + } else if (require.module && require.module.mid) { + cb(TraceSource.get(require.module.mid)); + } else { + require(['module'], function (module) { + cb(TraceSource.get(module && module.id)); + }); + } + }, - Trace.load = function (id, require, cb) { - if (id) - Trace.createChannel(Trace, id, cb); - else if (require.module && require.module.mid) - Trace.createChannel(Trace, require.module.mid, cb); - else - require(['module'], function (module) { - Trace.createChannel(Trace, module && module.id, cb); - }); + dynamic: true, }; - - Trace.dynamic = true; - - Trace.level = 4; - - return Trace; }); \ No newline at end of file diff --git a/src/ts/components/ActivatableMixin.ts b/src/ts/components/ActivatableMixin.ts --- a/src/ts/components/ActivatableMixin.ts +++ b/src/ts/components/ActivatableMixin.ts @@ -3,6 +3,7 @@ import { IActivatable } from './IActivat import { AsyncComponent } from './AsyncComponent'; import { ICancellation } from '../ICancellation'; import { EmptyCancellation } from '../EmptyCancellation'; +import * as log from '@implab/core/log/trace!'; type Constructor = new (...args: any[]) => T; @@ -43,7 +44,8 @@ function ActivatableMixin + // using array will provide faster iteration the with object + private _handlers: Array = new Array(); level: number constructor(id: any) { this.id = id || new Object(); - this._handlers = new Array(); } - on(handler: Handler): Destroyable { - if (!handler) - throw new Error("A handler must be specified"); + on(handler: TraceEventHandler): Destroyable { + argumentNotNull(handler, "handler"); + var me = this; + me._handlers.push(handler); - let d = new HandlerDescriptor(this, handler) - - this._handlers.push(d); - - return d; + return { + destroy() { + me.remove(handler); + } + } } - remove(cookie: any): void { - let i = this._handlers.indexOf(cookie); + remove(handler: TraceEventHandler): void { + let i = this._handlers.indexOf(handler); if (i >= 0) this._handlers.splice(i, 1); } - protected emit(level: number, msg: string, ...args: any[]) { - if (level <= this.level) { - let event = new TraceEventArgs(this, format(msg, args)); - - this._handlers.forEach(d => { - try { - d.handler.call(null, event); - } catch { - // suppress error in log handlers - } - }); - } + protected emit(level: number, arg: any) { + this._handlers.forEach(h => { + try { + h(this, level, arg); + } catch (e) { + // suppress error in log handlers + } + }); } isDebugEnabled() { return this.level >= TraceSource.DebugLevel; } - debug(msg: string, ...args: any[]): void { - this.emit(TraceSource.DebugLevel, msg, args); + debug(msg: string, ...args: any[]) { + if (this.isEnabled(TraceSource.DebugLevel)) + this.emit(TraceSource.DebugLevel, format(msg, args)); } isLogEnabled() { return this.level >= TraceSource.LogLevel; } - log(msg: string, ...args: any[]): void { - this.emit(TraceSource.LogLevel, msg, args); + log(msg: string, ...args: any[]) { + if (this.isEnabled(TraceSource.LogLevel)) + this.emit(TraceSource.LogLevel, format(msg, args)); } isWarnEnabled() { return this.level >= TraceSource.WarnLevel; } - warn(msg: string, ...args: any[]): void { - this.emit(TraceSource.WarnLevel, msg, args); + warn(msg: string, ...args: any[]) { + if (this.isEnabled(TraceSource.WarnLevel)) + this.emit(TraceSource.WarnLevel, format(msg, args)); } isErrorEnabled() { return this.level >= TraceSource.ErrorLevel; } - error(msg: string, ...args: any[]): void { - this.emit(TraceSource.ErrorLevel, msg, args); + error(msg: string, ...args: any[]) { + if (this.isEnabled(TraceSource.ErrorLevel)) + this.emit(TraceSource.ErrorLevel, format(msg, args)); + } + + isEnabled(level: number) { + return (this.level >= level); + } + + traceEvent(level: number, arg: any) { + if (this.isEnabled(level)) + this.emit(level, arg); + } + + static on(handler: TraceSourceHandler) { + Registry.instance.on(handler); + } + + static get(id: any) { + return Registry.instance.get(id); } } diff --git a/src/ts/log/trace.ts b/src/ts/log/trace.ts new file mode 100644 --- /dev/null +++ b/src/ts/log/trace.ts @@ -0,0 +1,12 @@ +declare interface ILog { + debug(...args: any[]): void; + log(...args: any[]): void; + warn(...args: any[]): void; + error(...args: any[]): void; +} + +declare module "@implab/core/log/trace!*" { + const channel: ILog; + + export = channel; +} \ No newline at end of file diff --git a/src/ts/safe.d.ts b/src/ts/safe.d.ts new file mode 100644 --- /dev/null +++ b/src/ts/safe.d.ts @@ -0,0 +1,1 @@ +export declare function argumentNotNull(v: any, paramName: string); \ No newline at end of file