Skip to content
This repository has been archived by the owner on Sep 25, 2023. It is now read-only.

remote 现在可以用ES6 Class写? #1086

Closed
cold-lic opened this issue Jul 11, 2018 · 7 comments
Closed

remote 现在可以用ES6 Class写? #1086

cold-lic opened this issue Jul 11, 2018 · 7 comments

Comments

@cold-lic
Copy link

hi,

之前的issue:remote的写法支持es6的class么
我看已经close掉了。

但是npm上的2.2.5的包,用class写remote还是会找不到函数。

请问现在remote写法可以支持class了吗? 如果可以的话,我应该用哪个版本或者分支?

谢谢!!!

@whtiehack
Copy link
Contributor

不支持~要改源码。 想尝试新的东西可以用 @node-pinus

@SteveCruise
Copy link

我早就用class写了

@cold-lic
Copy link
Author

@SteveCruise

How? 求指点。

@SteveCruise
Copy link

SteveCruise commented Jul 21, 2018

很久远的事了,你看源码会发现,用了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
Copy link

不要谢我,我还有100个优化技巧,自己研究吧。

@cold-lic
Copy link
Author

@SteveCruise 厉害!! 非常感谢!! 100个优化技巧希望分享~

@raoyaoiau
Copy link

100个,哈哈

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants