invite/inviteManager.js

ModuleBase.define("InviteManager", ["Invite","Error"], function(Invite, Error) {

     /**
     * @desc InviteManager构造函数。
     * @constructor
     * @alias InviteManager
     */
	var InviteManager = function() {
	    /**
         * @desc InviteManager级别回调的枚举
         * @readonly
         * @enum {string}
         */
		this.InviteManagerCallbackEnum = {
			CALL_NOTIFY: 'call.notify',
		};
		
		this.invites = {}; 
		
		this.eventEmitter = new EventEmitter();
	};
	
	
    /**
	 * @desc 创建邀请对象
	 * @param {String} fromUserId - 邀请发起方用户id
     * @param {String} toUserId - 邀请接受方用户id
	 * @param {Object} options -参数
	 */
	InviteManager.prototype.createInvite = function(fromUserId,toUserId,options){
		   var invite = new Invite(fromUserId,toUserId,options);
		   this.invites[invite.inviteId] = invite;
		   return invite;
	}
	
	/**
	 * @desc 通过ID获取Invite对象
	 * @param {string} InviteId - InviteId
	 */
	InviteManager.prototype.getInvite = function(InviteId){
		 var invite = this.invites[InviteId];
		 return invite;
	}
	
	
	
	/**
	 * @desc 发启邀请
	 * @param {Object} callInvite -邀请对象
	 */
	InviteManager.prototype.call = function(callInvite){
		 var deferred = when.defer();
		 
		 callInvite.callInner().then(function() {
		 	  log.debug("===InviteManager.call(),success");
		 	  deferred.resolve();
		 }).otherwise(function(error) {
		 	  log.debug("===InviteManager.call(),error:",error);
		 	  deferred.reject(error);
		 });
		return deferred.promise;
	}
	
	
	/**
	 * @desc emitCallNotify
	 * @ignore
	 */
	InviteManager.prototype.emitCallNotify = function(inviteId,fromUserId,toUserId,options){
		 var optionsObj = null;
		 if(options){
		 	 optionsObj = JSON.parse(options);
		 }
		 var invite = new Invite(toUserId,fromUserId,optionsObj); //接受到提请的invite对象,把fromUserId与toUserId反一下
		 invite.inviteId = inviteId;
		 this.invites[invite.inviteId] = invite;
		 invite.onCallTimeOutInner();
		 this.eventEmitter.emit(this.InviteManagerCallbackEnum.CALL_NOTIFY,invite);
	}
	
	
	/**
	 * @desc addCallback
	 * @param {Object} type
	 * @param {Object} callback
	 * 
	 * @example
	 * inviteManager.addCallback(inviteManager.InviteManagerCallbackEnum.CALL_NOTIFY, onCall);
	 * 
     * 邀请回调
     * param:invite - 邀请对象
     * function onCall(invite){
     * }
	 *
	 *
	 */
	InviteManager.prototype.addCallback = function(type, callback) {
		 this.eventEmitter.on(type, callback);
	};
	
	return InviteManager;

});