发现remote组件,无法用es6风格重写,其它组件目前未发现问题

发现remote组件,无法用es6风格重写,其它组件目前未发现问题,希望官方尽快更新pomelo,使其在node6及以上,es6 es7下完美运行,改写后的代码附上

class ChatRemote {
constructor(app) {
this.app = app;
this.channelService = app.get('channelService');
}

/*
* Add user into chat channel.
*
* @param {String} uid unique id for user
* @param {String} sid server id
* @param {String} name channel name
* @param {boolean} flag channel parameter
*
*/
add(uid, sid, name, flag, cb) {
const channel = this.channelService.getChannel(name, flag);
const username = uid.split('
')[0];
const param = {
route: 'onAdd',
user: username
};
channel.pushMessage(param);

if(!!channel) {
  channel.add(uid, sid);
}

cb(this.get(name, flag));

};

/*
* Get user from chat channel.
*
* @param {Object} opts parameters for request
* @param {String} name channel name
* @param {boolean} flag channel parameter
* @return {Array} users uids in channel
*
*/
get(name, flag) {
let users = [];
const channel = this.channelService.getChannel(name, flag);
if(!!channel) {
users = channel.getMembers();
}
for(let i = 0; i < users.length; i++) {
users[i] = users[i].split('
')[0];
}
return users;
};

/*
* Kick user out chat channel.
*
* @param {String} uid unique id for user
* @param {String} sid server id
* @param {String} name channel name
*
*/
kick(uid, sid, name, cb) {
const channel = this.channelService.getChannel(name, false);
// leave channel
if(!!channel) {
channel.leave(uid, sid);
}
const username = uid.split('
')[0];
const param = {
route: 'onLeave',
user: username
};
channel.pushMessage(param);
cb();
};
}

module.exports = (app) => {
return new ChatRemote(app);
};

标签: pomelo 建议与改进 开发求助
Sailor 在 4-25 16:32发布 分享到 weibo
5 回复
#1 {2} MissLee 4-27 09:15 回复

早就发现了,prototype定义的function是可枚举的,class内部的function是不可枚举的

Sailor 5-1 17:51 回复

忍痛放弃class

MissLee 5-3 09:15 回复

@Sailor 含泪放弃class

#2 MissLee 5-3 11:24 回复

借你提醒,又研究了一下,挂接rpc是用了pomelo-rpc组件,主要原理是用了for in遍历remote的原型属性,由于for in只能遍历可枚举属性,class的方法是不可枚举的,所以不行

/**
* Add proxy into array.
*
* @param proxies {Object} rpc proxies
* @param namespace {String} rpc namespace sys/user
* @param serverType {String} rpc remote server type
* @param proxy {Object} rpc proxy
*
* @api private
*/
var insertProxy = function(proxies, namespace, serverType, proxy) {
proxies[namespace] = proxies[namespace] || {};
if (proxies[namespace][serverType]) {
for (var attr in proxy) {
proxies[namespace][serverType][attr] = proxy[attr];
}
} else {
proxies[namespace][serverType] = proxy;
}
};
#3 MissLee 5-3 17:57 回复

用class后,把每个函数加上这句就可以了

Object.defineProperty(Remote.prototype, &apos;auth&apos;, {
    enumerable: true
});
#4 {1} MissLee 5-3 18:00 回复

Remote完整代码

const Token = require(&apos;../../../util/Token&apos;);
const UserDao = require(&apos;../../../dao/UserDao&apos;);
const User = require(&apos;../../../entity/User&apos;);
const CODE = require(&apos;../../../const/code&apos;);
const ObjectId = require(&apos;mongodb&apos;).ObjectId;

const DEFAULT_SECRET = &apos;pomelo_session_secret&apos;;
const DEFAULT_EXPIRE = 60 * 60 * 1000; // default session expire time: 1 hours

class Remote {

    /**
     * Constructor.
     *
     * @constructor
     * @param {object} app - Pomelo app.
     */
    constructor(app) {
        this.app = app;
        const session = app.get(&apos;session&apos;) || {};
        this.secret = session.secret || DEFAULT_SECRET;
        this.expire = session.expire || DEFAULT_EXPIRE;
    }

    async auth(token, cb) {
        const res = Token.parse(token, this.secret);
        if (!res) {
            cb({code: CODE.AUTH.CE_TOKEN_INVALID});
            return;
        }

        if (!Remote._checkExpire(res, this.expire)) {
            cb({code: CODE.AUTH.FA_TOKEN_EXPIRE});
            return;
        }

        const user = await UserDao.getUser({_id: ObjectId(res.uid)});

        if (!user) {
            cb({code: CODE.AUTH.FA_USER_NOT_EXIST});
        } else {
            cb({code: CODE.OK, user: user});
        }
    }

    static _checkExpire(token, expire) {
        if (expire &lt; 0) {
            // negative expire means never expire
            return true;
        }
        return (Date.now() - token.timestamp) &lt; expire;
    }
}

Object.defineProperty(Remote.prototype, &apos;auth&apos;, {
    enumerable: true
});

module.exports = function(app) {
    return new Remote(app);
};
Sailor 5-7 19:00 回复

谢啦,我试试

#5 {1} wangxy 5-4 19:22 回复

可以搞,用 Proxy

Sailor 5-7 19:00 回复

怎么讲?能具体点吗

回到顶部