Code coverage report for lib/socket.io/namespace.js

Statements: 88.89% (48 / 54)      Branches: 28.57% (4 / 14)      Functions: 81.82% (9 / 11)      Lines: 88.46% (46 / 52)      Ignored: none     

All files » lib/socket.io/ » namespace.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140                        1 1 1 1 1                   1 11 11 11     11 11 11 11     11   11               11                   1 27 27                     1 6 6             1           1 11 11 11   11 11   11       11 11   11   11 11     11 11                               1 27 15   15 1 5 5 5     10        
/*!
 * koa.io - lib/socket.io/namespace.js
 * Copyright(c) 2014 dead_horse <dead_horse@qq.com>
 * MIT Licensed
 */
 
'use strict';
 
/**
 * Module dependencies.
 */
 
var debug = require('debug')('koa.io:socket.io:namespace');
var compose = require('koa-compose');
var Socket = require('./socket');
var Router = require('./router');
var co = require('co');
 
/**
 * Adds a new client.
 * rewrite socket.io's namespace.prototype.add
 *
 * @return {Socket}
 * @api private
 */
 
exports.add = function (client, fn) {
  debug('adding socket to nsp %s', this.name);
  var socket = Socket(this, client);
  socket._fn = fn;
 
  // koa style middleware support
  Eif (!this.gen) {
    this._onconnect = onconnect(this);
    debug('compose middlewares');
    this.gen = compose(this.fns.concat([this.router.middleware(), this._onconnect]));
  }
 
  var self = this;
 
  co(this.gen).call(socket, function (err) {
    if (client.conn.readyState === 'open') {
      if (err) {
        return socket.socket.error(err.data || err.message);
      }
    }
  });
 
  return socket.socket;
};
 
 
/**
 * wrap a fn to a middleware function
 *
 * @param {GeneratorFunction} fn
 * @api public
 */
exports.use = function (fn) {
  this.fns.push(createMiddleware(fn, this));
  return this;
};
 
/**
 * add route for socket event
 *
 * @param {String|RegExp} event
 * @param {GeneratorFunction} handler
 * @return {this}
 */
 
exports.route = function (event, handler) {
  this.router.route(event, handler);
  return this;
};
 
/**
 * init the router
 */
 
exports.router = Router();
 
/**
 * onconnect middleware
 */
 
function onconnect(nsp) {
  return function* () {
    debug('on connect');
    yield process.nextTick;
 
    var socket = this.socket;
    var fn = this._fn;
 
    Iif (socket.client.conn.readyState !== 'open') {
      debug('next called after client was closed - ignoring socket');
    }
 
    nsp.sockets.push(socket);
    socket.onconnect();
 
    Eif(fn) fn();
 
    nsp.emit('connect', socket);
    nsp.emit('connection', socket);
 
    // after socket emit disconnect, resume middlewares
    yield function ondisconnect(done) {
      socket.once('disconnect', function (reason) {
        debug('socket disconnect by %s', reason);
        done(null, reason);
      });
    }
  };
}
 
/**
 * wrap a generatorFunction to koa.io's middleware
 *
 * @param {GeneratorFunction} fn
 * @return {GeneratorFunction}
 * @api private
 */
 
function createMiddleware(fn, nsp) {
  return function* (next) {
    var done = true;
 
    yield* fn.call(this, _next.call(this));
    function* _next() {
      debug('yield next, continue middlewares');
      done = false;
      yield* next;
    }
 
    Eif (done) yield* nsp._onconnect.call(this);
  }
}