Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Question - wrapping sc client #111

Open
happilymarrieddad opened this issue Apr 9, 2018 · 2 comments
Open

Question - wrapping sc client #111

happilymarrieddad opened this issue Apr 9, 2018 · 2 comments
Labels

Comments

@happilymarrieddad
Copy link

happilymarrieddad commented Apr 9, 2018

@jondubois Hey man, if I do this would it cause any issues? I've tested it locally and it works fine. Basically, I'm trying to wrap promises around emit and publish for the client.

Thanks!

import * as SC from 'socketcluster-client'
import * as Emitter from 'component-emitter'

export function install (_Vue,options) {
    function vue2SocketclusterInit() {
        var opts = this.$options

        if (opts.socket) {
            this.$socket = opts.socket
        } else if (opts.parent && opts.parent.$socket) {
            this.$socket = opts.parent.$socket
        } else {
            let soc = SC.connect(options)

            soc.emit = function(event,data,callback) {
                return new Promise((resolve,reject) => {
                    if (soc._localEvents[event] == null) {
                        return soc._emit(event,data,(err,res) => {
                            callback && callback(err,res)
                            if (err) return reject(err)
                            return resolve(res)
                        })
                    }

                    Emitter.prototype.emit.call(soc, event, data)
                })
            }

            soc.publish = function(channelName,data,callback) {
                return new Promise((resolve,reject) => {
                    let pubData = {
                        channel:soc._decorateChannelName(channelName),
                        data
                    }

                    soc._emit('#publish',pubData,(err,res) => {
                        callback && callback(err,res)
                        if (err) return reject(err)
                        return resolve(res)
                    })
                })
            }

            this.$socket = soc
        }

    }

    var usesInit = _Vue.config._lifecycleHooks.indexOf('init') > -1
    _Vue.mixin(usesInit ? { init: vue2SocketclusterInit } : { beforeCreate: vue2SocketclusterInit })
}
@happilymarrieddad
Copy link
Author

Here are the original funcs for quick reference.

SCSocket.prototype.emit = function (event, data, callback) {
  if (this._localEvents[event] == null) {
    this._emit(event, data, callback);
  } else {
    Emitter.prototype.emit.call(this, event, data);
  }
};

SCSocket.prototype.publish = function (channelName, data, callback) {
  var pubData = {
    channel: this._decorateChannelName(channelName),
    data: data
  };
  this.emit('#publish', pubData, callback);
};

@happilymarrieddad
Copy link
Author

I can always do this in a class or something but then I need to pass all the funcs/props to the class.. I don't like doing that.

@MegaGM MegaGM added the question label Jun 1, 2018
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants