JavaScript类中的最佳重构和简洁代码实践

时间:2018-06-27 18:56:22

标签: javascript refactoring

我想知道是否有任何方法(我确定有)更好地重构小类并提高其在JavaScript中的理解力。

我正在努力提高我的重构和简洁的编码技能。我希望能够编写最短的代码,同时仍然易于理解。

我会非常感谢您的帮助,以改善这一状况。我真的很想成为顶级程序员。

以下是我的课程模块; 任何批评都受到欢迎,并被接受

const tls = require('tls');
const fs = require('fs');


/**
 * 
 * 
 * NetTalk Class:
 * Creates a NetTalk Object for making request to a NetTalk Service in WPP (Whole Packet Protocol).
 * 
 */
class NetTalk {
    /**
     * Creates an instance of NetTalk.
     * @param {Number} port Port through which you wish to connect to NetTalk.
     * @param {String} server Server to which to connect.
     * @param {String} certificate Route where the certificate file (*.pem) is located
     * @param {String} [verify = '0'] verify the certificate? (default="0")
     * @memberof NetTalk
     */
    constructor (port, server, certificate, verify = "0") {
        this.port = port;
        this.server = server;
        this.options = {ca:[ fs.readFileSync(certificate) ], rejectUnauthorized: false};
        this.msgSize = 0;
        this.wholePackage = '';
        this.socket = null;
        this.verify = verify;
    }

    /**
     * 
     * Connects to the NetTalk Service.
     * @memberof NetTalk
     */
    connect() {
        //Connecting to NetTalk server.
        this.socket = tls.connect(this.port, this.server, this.options, () => {
            process.stdin.pipe(this.socket);
            process.stdin.resume();
        });

        //set TimeOut.
        this.socket.setTimeout(12000)
        //Setting Encoding
        this.socket.setEncoding('ascii');

        //on Server Terminated.
        this.socket.on('end', () => {
            console.log('Connection terminated.');
        });

        //On TimeOut
        this.socket.on('timeout', ()=> {
            console.log('Connection timed out.');
            this.socket.destroy();
        });

        //return promise with resolve and reject.
        return new Promise((resolve, reject) => {    
            //if connection established.
            this.socket.on('ready', () => {
                console.log(`client connected on port ${this.port} to server ${this.server}`);
                resolve();
            });
            //if error:
            this.socket.on('error', (err) => {
                reject({err:err, func:'connect'});
            });
        });
    }

    /**
     * sends the request.
     * 
     * @param {String} reqMsg Request Message to be sent to the service.
     * @returns {Promise} {Promise.<resolve(data)|reject(error)>}
     * @memberof NetTalk
     */
    request(reqMsg) {
        return new Promise((resolve, reject) => {
            /*Adding 32 bit length Integer to the beginning of stream 
            for compatibility with WPP (Whole Packet Protocol).*/
            var message = new Buffer(reqMsg, 'binary');
            var stream = new Buffer(4 + message.length); //declares the stream to be sent.

            stream.writeInt32LE(message.length, 0); //Must be in LittleEndian for compatibility.
            message.copy(stream, 4);

            this.socket.write(stream);

            //receiving function.
            this.socket.on('data', (data) => {
                let wholePackageSize = null;

                this.wholePackage += data;
                wholePackageSize = new Buffer(this.wholePackage, 'binary');
                this.msgSize += data.length;

                if (wholePackageSize.readInt32LE(0) <= this.msgSize){
                    this.socket.end();
                    resolve(this.wholePackage.slice(4, this.wholePackage.length));
                }
            });

            //timeOut function:
            this.socket.on('timeout', ()=> {
                this.socket.destroy();
                reject({err:'Error!, server do not respond', func:'request'})
            });

            //error function.
            this.socket.on('error', err => {
                reject({err:err, func:'request'});
            })
        });
    }
}

module.exports = {NetTalk};</script>

再次感谢您的帮助!

0 个答案:

没有答案