VaKeR CYBER ARMY
Logo of a company Server : Apache/2.4.41 (Ubuntu)
System : Linux absol.cf 5.4.0-198-generic #218-Ubuntu SMP Fri Sep 27 20:18:53 UTC 2024 x86_64
User : www-data ( 33)
PHP Version : 7.4.33
Disable Function : pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Directory :  /var/www/html/libs/absol-form/js/blocks/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : /var/www/html/libs/absol-form/js/blocks/CBFunction.js
import {AssemblerInstance} from "../core/Assembler";
import BaseBlock from "../core/BaseBlock";
import inheritComponentClass from "../core/inheritComponentClass";
import noop from "absol/src/Code/noop";
import safeThrow from "absol/src/Code/safeThrow";

function CBFunction() {
    BaseBlock.call(this);
    this.func = noop;
    this.attributes.args = [];
    this.attributes.body = '';
    this.pinHandlers = Object.assign({},CBFunction.prototype.pinHandlers );
    this.receivedArgValues = {};
    this.result = undefined;
}

inheritComponentClass(CBFunction, BaseBlock);

CBFunction.prototype.tag = 'function';
CBFunction.prototype.menuIcon = 'span.mdi.mdi-function';


CBFunction.prototype.attributeHandlers.body = {
    set: function (value, ref) {
        ref.set(value);
        if (value && this.attributes.args) {
            this.rebuildFunction();
        }
        return value;
    },
    descriptor:{
        type:'text'
    }
};

CBFunction.prototype.attributeHandlers.args = {
    set: function (args, ref){
        ref.set(args ||[]);
        this.rebuildFunction();
        return ref.get();
    },
    descriptor:{
        type:'text[]'
    }
};

CBFunction.prototype.attributeHandlers.defaultArgs = {
    set: function (value) {
        Object.assign(this.receivedArgValues, value);
        return value;
    },
    descriptor: {
        type: 'object'
    }
};

CBFunction.prototype.pinHandlers.exec = {
    receives:function (){
        this.exec();
    },
    descriptor:{
        type:'bool'
    }
};


CBFunction.prototype.pinHandlers.result = {
    get: function () {
        return this.result;
    }
};

CBFunction.prototype.rebuildFunction = function () {
    var self = this;
    try {
        this.pinHandlers = this.attributes.args.reduce(function (ac, cr) {
            ac[cr] = {
                receives: function (value) {
                    if (value && value.then) {
                        value.then(function () {
                            self.receivedArgValues[cr] = value;
                            self.exec();
                        });
                    }
                    else {
                        self.receivedArgValues[cr] = value;
                        self.exec();
                    }
                }
            };
            return ac;
        }, {});


        Object.assign(this.pinHandlers, CBFunction.prototype.pinHandlers);

        this.func = (new Function([
            `return function(${this.attributes.args.join(', ')}){`,
            this.attributes.body,
            '}'
        ].join('\n')))();
    } catch (error) {
        safeThrow(error);
        this.func = noop;
    }
};

CBFunction.prototype.exec = function () {
    var self = this;
    var receivedArgValues = this.receivedArgValues;
    var completeArg = true;
    var result;
    var args = this.attributes.args.map(function (name) {
        if (name in receivedArgValues) {
            return receivedArgValues[name];
        }
        else completeArg = false;
    });
    if (completeArg) {
        try{
            result = this.func.apply(this, args);
        }
        catch (err){
            safeThrow(err);
            console.error("Execute script error", this, err);
            return;
        }


        if (result && result.then) {
            result.then(function (result) {
                self.result = result;
                self.pinFire('result');
            }).catch(function (err){
                safeThrow(err);
                console.error("Execute script error", this, err);
            }.bind(this));
        }
        else {
            this.result = result;
            this.pinFire('result');
        }
    }
};


AssemblerInstance.addClass(CBFunction);

export default CBFunction;

VaKeR 2022