changeset 7:9c0943c68a90

minor fixes added safe.async(fn, thisArg)
author cin
date Tue, 20 Jun 2017 19:45:15 +0300
parents 9663631cbdb9
children f0035923ff3e 37e9e6bbe87a
files src/implab/components/ConsoleLogChannel.js src/implab/components/_LogMixin.js src/implab/guard.js src/implab/safe.js
diffstat 4 files changed, 47 insertions(+), 94 deletions(-) [+]
line wrap: on
line diff
--- a/src/implab/components/ConsoleLogChannel.js	Tue Jun 20 00:33:15 2017 +0300
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,26 +0,0 @@
-define(["dojo/_base/declare", "../text/format"], function (declare, format) {
-    return declare(null, {
-        name: null,
-
-        constructor: function (name) {
-            this.name = name;
-        },
-
-        log: function () {
-            console.log(this._makeMsg(arguments));
-        },
-
-        warn: function () {
-            console.warn(this._makeMsg(arguments));
-        },
-
-        error: function () {
-            console.error(this._makeMsg(arguments));
-        },
-
-        _makeMsg: function (args) {
-            return this.name ? this.name + " " +
-                format.apply(null, args) : format.apply(null, args);
-        }
-    });
-});
\ No newline at end of file
--- a/src/implab/components/_LogMixin.js	Tue Jun 20 00:33:15 2017 +0300
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,68 +0,0 @@
-define([
-    "implab/declare", "implab/declare/override"
-], function (declare, override) {
-    var cls = declare(null, {
-        _logChannel: null,
-
-        _logLevel: 1,
-
-        constructor: function (opts) {
-            if (typeof opts == "object") {
-                if ("logChannel" in opts)
-                    this._logChannel = opts.logChannel;
-                if ("logLevel" in opts)
-                    this._logLevel = opts.logLevel;
-            }
-        },
-
-        getLogChannel: function () {
-            return this._logChannel;
-        },
-
-        setLogChannel: function (v) {
-            this._logChannel = v;
-        },
-
-        getLogLevel: function () {
-            return this._logLevel;
-        },
-
-        setLogLevel: function (v) {
-            this._logLevel = v;
-        },
-
-        log: function () {
-            if (this._logChannel && this._logLevel > 2)
-                this._logChannel.log.apply(this._logChannel, arguments);
-        },
-        warn: function () {
-            if (this._logChannel && this._logLevel > 1)
-                this._logChannel.warn.apply(this._logChannel, arguments);
-        },
-        error: function () {
-            if (this._logChannel && this._logLevel > 0)
-                this._logChannel.error.apply(this._logChannel, arguments);
-        },
-
-        /**
-         * Used to by widgets
-         */
-        startup: override( /** @this */ function (inherited) {
-            var me = this,
-                parent;
-            if (!me.getLogChannel()) {
-                parent = me;
-                while ((parent = parent.getParent())) {
-                    if (parent.getLogChannel) {
-                        me.setLogChannel(parent.getLogChannel());
-                        if (parent.getLogLevel)
-                            me.setLogLevel(parent.getLogLevel());
-                        break;
-                    }
-                }
-            }
-            return inherited();
-        })
-    });
-    return cls;
-});
\ No newline at end of file
--- a/src/implab/guard.js	Tue Jun 20 00:33:15 2017 +0300
+++ b/src/implab/guard.js	Tue Jun 20 19:45:15 2017 +0300
@@ -19,6 +19,7 @@
      *                         также должен быть задан
      * @param{Array} args Параметры для вызова метода, не обязательно.
      * @returns{dojo/promise}
+     * @deprecated use <code>safe.async() + when()</code> instead.
      */
     return function(o, m, args) {
         if (arguments.length == 1) {
--- a/src/implab/safe.js	Tue Jun 20 00:33:15 2017 +0300
+++ b/src/implab/safe.js	Tue Jun 20 19:45:15 2017 +0300
@@ -138,6 +138,52 @@
                 return dest;
             },
 
+            /** Wraps the specified function to emulate an asynchronous execution.
+             * @param{Object} thisArg [Optional] Object which will be passed as 'this' to the function.
+             * @param{Function|String} fn [Required] Function wich will be wrapped.
+             */
+            async: function (fn, thisArg) {
+                if (arguments.length == 2)
+                    fn = thisArg[fn];
+
+                if (fn == null)
+                    throw new Error("The function must be specified");
+
+                function wrapresult(x, e) {
+                    if (e) {
+                        return {
+                            then: function (cb, eb) {
+                                try {
+                                    return eb ? wrapresult(eb(e)) : this;
+                                } catch (e2) {
+                                    return wrapresult(null, e2);
+                                }
+                            }
+                        };
+                    } else {
+                        if (x && x.then)
+                            return x;
+                        return {
+                            then : function(cb) {
+                                try {
+                                    return cb ? wrapresult(cb(x)) : this;
+                                } catch(e2) {
+                                    return wrapresult(e2);
+                                }
+                            }
+                        };
+                    }
+                }
+
+                return /** @this */ function () {
+                    try {
+                        return wrapresult(fn.apply(this, arguments));
+                    } catch (e) {
+                        return wrapresult(null, e);
+                    }
+                };
+            },
+
             create: function () {
                 if (console && console.warn)
                     console.warn("implab/safe::create is deprecated use Object.create instead");