diff --git a/src/js/components/ActivationController.js b/src/js/components/ActivationController.js deleted file mode 100644 --- a/src/js/components/ActivationController.js +++ /dev/null @@ -1,120 +0,0 @@ -define(["dojo/_base/declare", "../guard", "../safe", "../log/_LogMixin"], function (declare, guard, safe, _LogMixin) { - "use strict"; - return declare([_LogMixin], { - - _current: null, - - _pending: false, - - getCurrent: function () { - return this._current; - }, - - _start: function () { - if (this._pending) - throw new Error("The activation/decativation is already pending"); - this._pending = true; - }, - - _await: function (d) { - var me = this; - return d.then(function (x) { - me._pending = false; - return x; - }, function (e) { - me._pending = false; - throw e; - }); - }, - - activate: function (component) { - safe.argumentNotNull(component, "component"); - var me = this; - if (component.getController() !== this) - throw new Error("The specified component doesn't belong to this controller"); - - return me._await(guard(me, "_start").then(function () { - me._activate(component); - })); - }, - - _activate: function (component) { - var me = this; - if (me._current === component) - return guard(false); - - // before activation hook - return guard(me, "onActivating", [component]).then(function () { - // deactivate curent - if (me._current) - return me._current.deactivate(true).then(function () { - try { - me._current.onDeactivated(); - } catch (err) { - me.error(err); - } - // HACK raise deactivated event - try { - me.onDeactivated(me._current, component); - } catch (err) { - // deactivated shouldn't affect the process - me.error(err); - } - me._current = null; - - }); - }).then(function () { - return component.activate(true); - }).then(function () { - me._current = component; - try { - me.onActivated(component); - } catch (err) { - me.error(err); - } - - }); - - }, - - /** - * Деактивирует текущую компоненту. - * - * @async - * @returns true - компонента была деактивирована, либо нет активной - * компоненты. false - запрос на деактивацию - отклонен. - */ - deactivate: function () { - var me = this; - return me._await(guard(me, "_start").then(function () { - return me._deactivate(); - })); - }, - - _deactivate: function () { - var me = this; - if (!me._current) - return guard(false); - - return guard(me, "onDeactivating").then(function () { - return me._current.deactivate(true); - }).then(function () { - // HACK raise deactivated event - try { - me.onDeactivated(me._current); - } catch (err) { - me.error(err); - } - me._current = null; - }); - }, - - onActivating: function (component) {}, - - onDeactivating: function (component) {}, - - onDeactivated: function (component, next) {}, - - onActivated: function (component) {} - }); -}); \ No newline at end of file diff --git a/src/js/components/StateMachine.js b/src/js/components/StateMachine.js deleted file mode 100644 --- a/src/js/components/StateMachine.js +++ /dev/null @@ -1,34 +0,0 @@ -define([ "dojo/_base/declare", "../safe", "../text/format" ], function(declare, safe, format) { - return declare(null, { - states : null, - - current : null, - - constructor : function(opts) { - safe.argumentNotNull(opts, "opts"); - safe.argumentNotNull(opts.states, "opts.states"); - safe.argumentNotNull(opts.initial, "opts.initial"); - - this.states = opts.states; - this.current = opts.initial; - - if (safe.isNull(this.states[this.current])) - throw new Error("Invalid initial state " + this.current); - }, - - move : function(input, noThrow) { - safe.argumentNotNull(input, "input"); - - var next = this.states[this.current][input]; - if(safe.isNull(next)) { - if (noThrow) - return false; - else - throw new Error(format("Invalid transition {0}-{1}->?", this.current, input)); - } else { - this.current = next; - return true; - } - } - }); -}); \ No newline at end of file diff --git a/src/js/components/_ActivatableMixin.js b/src/js/components/_ActivatableMixin.js deleted file mode 100644 --- a/src/js/components/_ActivatableMixin.js +++ /dev/null @@ -1,153 +0,0 @@ -define(["dojo/_base/declare", "../guard", "./StateMachine", "../log/_LogMixin", ], function (declare, guard, StateMachine, _LogMixin) { - - var states = { - inactive: { - activate: "activating" - }, - activating: { - success: "active", - failed: "inactive" - }, - active: { - deactivate: "deactivating" - }, - deactivating: { - success: "inactive", - failed: "active" - } - }; - - return declare([_LogMixin], { - _controller: null, - - _active: null, - - constructor: function () { - this._active = new StateMachine({ - states: states, - initial: "inactive" - }); - }, - - /** - * @returns {Object} контроллер для активации текущей компоненты - */ - getController: function () { - return this._controller; - }, - - /** - * @param {Object} - * v Контроллер для активации текущей компоненты - */ - setController: function (v) { - this._controller = v; - }, - - /** - * @returns {Boolean} текущая компонента активна - */ - isActive: function () { - return this._active.current == "active"; - }, - - assertActive: function () { - if (!this.isActive()) - throw new Error("The object must be active to perform the operation"); - }, - - /** - * Активирует текущую компоненту, если у текущей компоненты задан - * контроллер, то активация будет осуществляться через него - * - * @async - * @param{Boolean} - * direct вызов должен осуществится напрямую, без участия - * контроллера. - * @return{Boolean} успешно/неуспешно - */ - activate: function (direct) { - var me = this; - if (!direct && this._controller) - return me._controller.activate(me).then(function () { - me.onActivated(); - }); - - me._active.move("activate"); - return guard(me, "onActivating").then(function () { - me.log("Activated"); - me._active.move("success"); - if (!me._controller) - me.onActivated(); - }, function (err) { - console.error(err); - me.error("Activation failed: {0}", err); - me._active.move("failed"); - throw err; - }); - }, - - /** - * Деактивирует текущую компоненту, если у компоненты задан контроллер, - * то деактивация будет осуществляться через него. - * - * @async - * @param{Boolean} direct вызов должен осуществится напрямую, без - * участия контроллера. - * - */ - deactivate: function (direct) { - var me = this; - if (!direct && me._controller) - return me._controller.deactivate(me).then(function () { - me.onDeactivated(); - }); - - me._active.move("deactivate"); - return guard(me, "onDeactivating").then(function () { - me.log("Deactivated"); - me._active.move("success"); - if (!me._controller) - me.onDeactivated(); - }, function (err) { - console.error(err); - me.error("Deactivation failed: {0}", err); - me.move("failed"); - throw err; - }); - - }, - - toogleActive: function () { - var me = this; - return (me.isActive() ? me.deactivate() : me.activate()).then(function () { - return me.isActive(); - }); - }, - - /** - * Событие вызывается перед активацией текущей компоненты - * - * @returns{Boolean|undefined} если false - активация будет отменена - */ - onActivating: function () {}, - - /** - * Событие вызывается перед деактивацией текущей компоненты - * - * @returns {Boolean|undefined} если false - деактивация будет отменена - */ - onDeactivating: function () {}, - - /** - * Событие вызывается после активации текущей компоненты - */ - onActivated: function () {}, - - /** - * Событие вызывается после деактивации текущей компоненты - */ - onDeactivated: function () {} - - }); -}); \ 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 @@ -47,8 +47,7 @@ function ActivatableMixin= TraceSource.ErrorLevel; } + /** + * Traces a error. + * + * @param msg the message. + * @param args parameters which will be substituted in the message. + */ error(msg: string, ...args: any[]) { if (this.isEnabled(TraceSource.ErrorLevel)) this.emit(TraceSource.ErrorLevel, format(msg, args)); } + /** + * Checks whether the specified level is enabled for this + * trace source. + * + * @param level the trace level which should be checked. + */ isEnabled(level: number) { return (this.level >= level); } + /** + * Traces a raw event, passing data as it is to the underlying listeners + * + * @param level the level of the event + * @param arg the data of the event, can be a simple string or any object. + */ traceEvent(level: number, arg: any) { if (this.isEnabled(level)) this.emit(level, arg); } + /** + * Register the specified handler to be called for every new and already + * created trace source. + * + * @param handler the handler which will be called for each trace source + */ static on(handler: TraceSourceHandler) { return Registry.instance.on(handler); } + /** + * Creates or returns already created trace source for the specified id. + * + * @param id the id for the trace source + */ static get(id: any) { return Registry.instance.get(id); } diff --git a/test/js/plan.js b/test/js/plan.js --- a/test/js/plan.js +++ b/test/js/plan.js @@ -1,1 +1,1 @@ -define(["./ActivatableTests", "./trace-test"]); \ No newline at end of file +define(["./ActivatableTests", "./trace-test", "./TraceSourceTests"]); \ No newline at end of file diff --git a/test/ts/TraceSourceTests.ts b/test/ts/TraceSourceTests.ts --- a/test/ts/TraceSourceTests.ts +++ b/test/ts/TraceSourceTests.ts @@ -6,9 +6,17 @@ const sourceId = 'test/TraceSourceTests' tape('', t => { let trace = TraceSource.get(sourceId); - let h = trace.on((sender,level,msg) => { + trace.level = TraceSource.DebugLevel; + let h = trace.on((sender,level,msg) => { + t.equal(sender, trace, "sender should be the current trace source"); + t.equal(TraceSource.DebugLevel, level, "level should be debug level"); + t.equal(msg, "Hello, World!", "The message should be formatted correctly"); + + t.end(); }); + trace.debug("Hello, {0}!", "World"); + h.destroy(); }); \ No newline at end of file