remote 现在可以用ES6 Class写?
cold-lic opened this issue · 7 comments
cold-lic commented
hi,
之前的issue:remote的写法支持es6的class么
我看已经close掉了。
但是npm上的2.2.5的包,用class写remote还是会找不到函数。
请问现在remote写法可以支持class了吗? 如果可以的话,我应该用哪个版本或者分支?
谢谢!!!
whtiehack commented
不支持~要改源码。 想尝试新的东西可以用 @node-pinus
SteveCruise commented
我早就用class写了
cold-lic commented
How? 求指点。
SteveCruise commented
很久远的事了,你看源码会发现,用了for in,这个无法扫描到class中的function,因为它们是不可枚举的,只要加上下面这段就行了。
Object.defineProperties(TeamRemote.prototype, {
leaveTeam: {
enumerable: true
},
rename: {
enumerable: true
},
matchSuccess: {
enumerable: true
}
});
贴个例子
const CODE = require('../../../consts/CODE');
/**
* @class
*/
class TeamRemote {
/**
* Constructor.
*
* @param {Application} app - Pomelo app.
*/
constructor(app) {
/** @type {Application} */ this.app = app;
/** @type {TeamManager} */ this.teamManager = app.get('teamManager');
}
/**
* Tell match success and all team info has been cleared in match queue.
* So don't need to clear info in match queue when user offline.
*
* @param {number} teamId
* @param {number} roomType
* @param {number} gameType
* @param {function} cb
*/
matchSuccess(teamId, roomType, gameType, cb) {
const team = this.teamManager.getTeam(teamId, roomType, gameType);
if (team) {
team.isMatching = false;
cb(null, { code: CODE.OK });
} else {
cb(null, { code: CODE.ROOM.CE_INPUT_INVALID });
}
}
/**
* Leave team, used when user offline, not need set session teamId null.
*
* @param {number} uid
* @param {number} teamId
* @param {number} roomType
* @param {number} gameType
* @param {function} cb
*/
async leaveTeam(uid, teamId, roomType, gameType, cb) {
if (await this.teamManager.leaveTeam(uid, teamId, roomType, gameType)) {
cb(null, { code: CODE.OK });
} else {
cb(null, { code: CODE.ROOM.CE_INPUT_INVALID });
}
}
/**
* Leave team, used when user offline, not need set session teamId null.
*
* @param {number} uid
* @param {string} name
* @param {number} teamId
* @param {number} roomType
* @param {number} gameType
* @param {function} cb
*/
async rename(uid, name, teamId, roomType, gameType, cb) {
const team = this.teamManager.getTeam(teamId, roomType, gameType);
if (team) {
team.rename(uid, name);
cb(null, { code: CODE.OK });
} else {
cb(null, { code: CODE.ROOM.CE_INPUT_INVALID });
}
}
}
Object.defineProperties(TeamRemote.prototype, {
leaveTeam: {
enumerable: true
},
rename: {
enumerable: true
},
matchSuccess: {
enumerable: true
}
});
module.exports = app => new TeamRemote(app);
SteveCruise commented
不要谢我,我还有100个优化技巧,自己研究吧。
cold-lic commented
@SteveCruise 厉害!! 非常感谢!! 100个优化技巧希望分享~
raoyaoiau commented
100个,哈哈