UNPKG

2ch

Version:

A JavaScript library for comfortable 2ch watching.

169 lines (142 loc) 5.15 kB
(function() { var Bbs, BbsMenu, BbsMenuFactory, EventEmitter, Thread, ThreadWatcher, bbsMenuFactory, _, __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }, __hasProp = {}.hasOwnProperty, __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }; _ = require('lodash'); EventEmitter = require('events').EventEmitter; BbsMenu = require('./bbs_menu'); Bbs = require('./bbs'); Thread = require('./thread'); BbsMenuFactory = (function() { function BbsMenuFactory() {} BbsMenuFactory._bbsMenu = null; BbsMenuFactory.prototype.get = function() { if (!this._bbsMenu) { this._bbsMenu = new BbsMenu(); } return this._bbsMenu; }; return BbsMenuFactory; })(); bbsMenuFactory = new BbsMenuFactory(); module.exports = ThreadWatcher = (function(_super) { __extends(ThreadWatcher, _super); function ThreadWatcher(bbsName, query, interval, bbsMenu) { this.bbsName = bbsName; this.query = query; this.interval = interval != null ? interval : 600000; this.bbsMenu = bbsMenu; this._run = __bind(this._run, this); if (this.interval < 5000) { this.interval = 5000; } if (this.bbsMenu == null) { this.bbsMenu = bbsMenuFactory.get(); } this.bbs = new Bbs(this.bbsMenu, this.bbsName); this.thread = null; this._watching = false; this._initializeEvents(); } ThreadWatcher.prototype._initializeEvents = function() { var _this = this; this.events = { 'thread error': function(error) { return _this.emit('error', error); }, 'thread update': function(messages) { return _this.emit('update', messages); }, 'thread reload': function(title) { return _this.emit('reload', title); }, 'thread begin': function(title) { return _this.emit('begin', title); }, 'thread end': function(title) { return _this.emit('end', title); } }; return this.delegateEvents(); }; ThreadWatcher.prototype.delegateEvents = function() { var _this = this; return _.forEach(this.events, function(fn, key) { var eventName, propName, _ref, _ref1; _ref = key.split(/\s+/), propName = _ref[0], eventName = _ref[1]; return (_ref1 = _this[propName]) != null ? _ref1.on(eventName, fn) : void 0; }); }; ThreadWatcher.prototype.undelegateEvents = function() { var _this = this; return _.forEach(this.events, function(fn, key) { var eventName, propName, _ref, _ref1; _ref = key.split(/\s+/), propName = _ref[0], eventName = _ref[1]; return (_ref1 = _this[propName]) != null ? _ref1.off(eventName, fn) : void 0; }); }; ThreadWatcher.prototype.isWatching = function() { return this._watching; }; ThreadWatcher.prototype.start = function() { if (this._watching) { return; } this._watching = true; return this._run(); }; ThreadWatcher.prototype._run = function() { var _this = this; if (!this._watching) { return; } return this.update(function() { return setTimeout(_this._run, _this.interval); }); }; ThreadWatcher.prototype.stop = function() { return this._watching = false; }; ThreadWatcher.prototype.update = function(done) { var _this = this; if ((this.thread != null) && !this.thread.isEnded()) { return this.thread.update(function(error, messages) { return typeof done === "function" ? done(null, messages) : void 0; }); } else { return this.findNewThread(function(error, thread) { if (error) { return typeof done === "function" ? done(error) : void 0; } if (thread == null) { return typeof done === "function" ? done(null, []) : void 0; } _this.setThread(thread); return _this.thread.update(function(error, messages) { return typeof done === "function" ? done(null, messages) : void 0; }); }); } }; ThreadWatcher.prototype.setThread = function(thread) { this.undelegateEvents(); this.thread = thread; return this.delegateEvents(); }; ThreadWatcher.prototype.findNewThread = function(done) { var _this = this; return this.bbs.fetchThreadHeader(this.query, function(error, header) { if (error) { _this.emit('error', error); return done(error); } else if (!header) { _this.emit('notfound'); return done(null, null); } return done(null, new Thread(_this.bbs, header.number, header.title)); }); }; return ThreadWatcher; })(EventEmitter); }).call(this);