본문으로 이동

사용자:Hsl0/연구소/숫자야구 live/실시간.js

리버티게임, 모두가 만들어가는 자유로운 게임
imported>Hsl0님의 2020년 8월 19일 (수) 01:14 판

참고: 설정을 저장한 후에 바뀐 점을 확인하기 위해서는 브라우저의 캐시를 새로 고쳐야 합니다.

  • 파이어폭스 / 사파리: Shift 키를 누르면서 새로 고침을 클릭하거나, Ctrl-F5 또는 Ctrl-R을 입력 (Mac에서는 ⌘-R)
  • 구글 크롬: Ctrl-Shift-R키를 입력 (Mac에서는 ⌘-Shift-R)
  • 엣지: Ctrl 키를 누르면서 새로 고침을 클릭하거나, Ctrl-F5를 입력.
const api = new mw.Api();
const TIMEOUT = Symbol('timeout_id');
const STOPPED = Symbol('is_stopped');
const RUNNING = Symbol('is_running');

export class LiveClient extends EventTarget {
	constructor(title, options = {}) {
		if(!title) throw new TypeError('문서를 지정해 주세요');
		
		super();
		this.src = title;
		this.interval = options.interval || {
			throttle: null,
			required: null
		};
		this.loadParams = options.loadParams || {
			action: "query",
			curtimestamp: true,
			prop: "revisions",
			titles: this.src,
			formatversion: 2,
			rvprop: ['ids', 'timestamp', 'user', 'content'],
			rvlimit: "max",
			rvdir: "newer",
		};
		this.postParams = options.postParams || {
			action: "edit",
			curtimestamp: true,
			title: this.src,
			minor: true
		};
		this.now = options.from || {
			id: null,
			timestamp: null
		};
	}
	load(from, options = {}) {
		const params = {};
		switch(typeof from) {
			case 'string':
				params.rvcontinue = from;
				break;
			case 'object':
				if(from.timestamp) {
					if(from.id) params.rvcontinue = from.timestamp + '|' + from.id;
					else params.rvfrom = from.timestamp;
				} else if(from.id) params.rvfromid = from.id;
				break;
		}
		
		return api.get(Object.assign(params, this.loadParams, options.params), options.ajax).then(response => {
			if(response.error) {
				throw {
					type: 'api',
					error: response.error,
					target: from,
					response
				};
			} else {
				const revisions = response.query && response.query.pages[0].revisions;
				const result = {
					response,
					revisions,
					target: from
				};
				
				if(!options.silent) setTimeout(() => super.dispatchEvent(new CustomEvent('update', {
					detail: result
				})), 0);
				
				return result;
			}
		}, error => {
			throw {
				type: 'fetch',
				error,
				target: from,
				response: null
			};
		});
	}
	post(content) {
		return api.postWithEditToken(Object.assign({
			text: content
		}, this.postParams));
	}
	start(interval) {
		const client = this;
		this[RUNNING] = true;
		Object.assign(this.interval, interval);
		
		if(typeof this.interval !== 'object' && (typeof this.interval.throttle !== 'number' || typeof this.interval.required !== 'number')) {
			throw new TypeError('정상적인 간격이 지정되지 않았습니다');
		}
		
		function load(options) {
			function next() {
				if(throttle && request) client[TIMEOUT] = setTimeout(load, client.interval.required, {
					params: {
						maxage: 0
					}
				});
			}
			
			let throttle = false;
			let request = false;
			
			client[TIMEOUT] = null;
			client[RUNNING] = true;
			
			setTimeout(() => {
				throttle = true;
				next();
			}, client.timeout.throttle);
			
			client.load(client.now, options).then(result => {
				if(client[STOPPED]) client[STOPPED] = false;
				else {
					const response = result.response;
					const revisions = result.revisions;
					
					request = true;
					
					result.error = false;
					result.updated = Boolean(revisions);
					delete result.revisions;
					super.dispatchEvent(new CustomEvent('fetch', {
						detail: result
					}));
					
					if(revisions) {
						if(response.continue) {
							client.now = response.continue.rvcontinue;
							
							return load();
						} else {
							const last = revisions[revisions.length - 1];
							client.now = {
								id: last.revid,
								timestamp: last.timestamp
							};
							client.now.id++;
						}
					}
					
					next();
				}
			}, error => {
				if(client[STOPPED]) client[STOPPED] = false;
				else {
					super.dispatchEvent(new CustomEvent('error', {
						detail: error
					}));
					
					if(error.type === 'api') super.dispatchEvent(new CustomEvent('fetch', {
						detail: {
							response: error.response,
							error: true,
							updated: false
						}
					}));
				}
			});
			
			super.dispatchEvent(new CustomEvent('request', {
				detail: {
					target: client.now,
					params: options && options.params,
					ajax: options && options.ajax
				}
			}));
		}
		if(!this[RUNNING]) load();
		
		return this;
	}
	stop() {
		if(this[RUNNING]) {
			if(this[TIMEOUT]) {
				clearTimeout(this[TIMEOUT]);
				this[TIMEOUT] = null;
			} else this[STOPPED] = true;
			
			this[RUNNING] = false;
		}
		
		return this;
	}
	reset(from) {
		stop();
		
		if(from) {
			this.now = from;
			start();
		}
		
		return this;
	}
	dispatchEvent() {
		throw new TypeError('이벤트를 임의로 발생시킬 수 없습니다');
	}
}