node-nicovideo-api
Version:
nicovideo api (video, live, etc..) wrapper package for node.js
123 lines (100 loc) • 3.77 kB
JavaScript
// Generated by CoffeeScript 1.10.0
(function() {
var NicoLiveApi, NicoLiveInfo, NsenChannel, NsenChannels, _;
_ = require("lodash");
NicoLiveInfo = require("./NicoLiveInfo");
NsenChannels = require("./NsenChannels");
NsenChannel = require("./NsenChannel");
module.exports = NicoLiveApi = (function() {
NicoLiveApi.prototype._session = null;
NicoLiveApi.prototype._nsenChannelInstances = null;
NicoLiveApi.prototype._nicoLiveInstances = null;
/**
* @param {NicoSession} _session
*/
function NicoLiveApi(_session) {
this._session = _session;
this._nsenChannelInstances = {};
this._nicoLiveInstances = {};
}
/**
* 指定された放送の情報を取得します。
*
* 番組情報が取得できればNicoLiveInfoオブジェクトとともにresolveされます。
* 取得中にエラーが発生した場合、エラーメッセージとともにrejectされます。
*
* @param {string} liveId 放送ID
* @return {Promise}
*/
NicoLiveApi.prototype.getLiveInfo = function(liveId) {
if (typeof liveId !== "string" || liveId === "") {
throw new TypeError("liveId must bea string");
}
if (this._nicoLiveInstances[liveId] != null) {
return Promise.resolve(this._nicoLiveInstances[liveId]);
}
return NicoLiveInfo.instanceFor(liveId, this._session).then((function(_this) {
return function(liveInfo) {
_this._nicoLiveInstances[liveId] = liveInfo;
return Promise.resolve(liveInfo);
};
})(this));
};
/**
* NsenChannelのインスタンスを取得します。
*
* @param {String} channel
* @param {Object} [options]
* @param {Boolean} [options.connect=false] NsenChannel生成時にコメントサーバーへ自動接続するか指定します。
* @param {Number} [options.firstGetComments] 接続時に取得するコメント数
* @param {Number} [options.timeoutMs] タイムアウトまでのミリ秒
* @return {Promise}
*/
NicoLiveApi.prototype.getNsenChannelHandlerFor = function(channel, options) {
var isValidChannel;
if (options == null) {
options = {};
}
isValidChannel = _.select(NsenChannels, {
'id': channel
}).length === 1;
if (!isValidChannel) {
throw new RangeError("Invalid Nsen channel: " + channel);
}
if (this._nsenChannelInstances[channel] != null) {
return Promise.resolve(this._nsenChannelInstances[channel]);
}
return this.getLiveInfo(channel).then((function(_this) {
return function(live) {
return NsenChannel.instanceFor(live, options, _this._session);
};
})(this)).then((function(_this) {
return function(instance) {
_this._nsenChannelInstances[channel] = instance;
instance.onWillDispose(function() {
return delete _this._nsenChannelInstances[channel];
});
return Promise.resolve(instance);
};
})(this));
};
/**
* 現在のインスタンスおよび、関連するオブジェクトを破棄し、利用不能にします。
*/
NicoLiveApi.prototype.dispose = function() {
var i, instance, j, len, len1, ref, ref1;
ref = this._nsenChannelInstances;
for (i = 0, len = ref.length; i < len; i++) {
instance = ref[i];
instance.dispose();
}
ref1 = this._nicoLiveInstances;
for (j = 0, len1 = ref1.length; j < len1; j++) {
instance = ref1[j];
instance.dispose();
}
return DisposeHelper.wrapAllMembers(this);
};
return NicoLiveApi;
})();
}).call(this);