사용자:Hsl0/연구소/숫자야구 live/실시간.js
보이기
참고: 설정을 저장한 후에 바뀐 점을 확인하기 위해서는 브라우저의 캐시를 새로 고쳐야 합니다.
- 파이어폭스 / 사파리: 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('stopped');
const RUNNING = Symbol('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) {
Object.assign(this.interval, interval);
const client = this;
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) client[TIMEOUT] = setTimeout(load, client.interval.required, {
params: {
maxage: 0
}
});
}
let throttle = true;
client[TIMEOUT] = null;
client[RUNNING] = true;
setTimeout(() => {
throttle = false;
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;
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() {
this.interval = null;
if(this[TIMEOUT]) clearTimeout(this[TIMEOUT]);
else this[STOPPED] = true;
return this;
}
reset(from) {
const interval = this.interval;
stop();
if(from) {
this.now = from;
start(interval);
}
return this;
}
dispatchEvent() {
throw new TypeError('이벤트를 임의로 발생시킬 수 없습니다');
}
}