사용자:Hsl0/연구소/3.js: 두 판 사이의 차이
imported>Hsl0 (내용을 "→* * 즉시 갱신 ([[틀:갱신]]) * 제작자: [[사용자:hsl0]] *: function purgeNow() { if(document.querySelector('.purgepage')) $.post('/w/api.php', { acti..."(으)로 바꿈) |
imported>Hsl0 편집 요약 없음 |
||
1번째 줄: | 1번째 줄: | ||
var TASKS = Symbol('tasks_queue'); | |||
var WAITING = Symbol('waiting_queue'); | |||
function Tasker() { | |||
this[TASKS] = new Set(); | |||
this[WAITING] = new Set(); | |||
} | } | ||
$( | Tasker.prototype.push = function push() { | ||
var tasker = this; | |||
var items = arguments; | |||
if(Array.isArray(items[0])) items = items[0]; | |||
else items = Array.from(items); | |||
items.forEach(function(item) { | |||
if(typeof item === 'function') this[TASKS].add(item); | |||
else this[WAITING].add(item); | |||
}); | |||
return this; | |||
}; | |||
Tasker.prototype.wait = function wait() { | |||
var tasker = this; | |||
return new Promise(function(resolve) { | |||
function capture(queue) { | |||
var arr = Array.from(queue); | |||
queue.clear(); | |||
return arr; | |||
} | |||
Promise.all(capture(tasker[WAITING])).then(function handle() { | |||
var captured = capture(tasker[WAITING]); | |||
if(captured.length) return Promise.all(captured).then(handle); | |||
else resolve(); | |||
}); | |||
}); | |||
}; | |||
Tasker.prototype.run = function run() { | |||
var tasker = this; | |||
this[TASKS].forEach(function(task) { | |||
tasker[WAITING].add(task()); | |||
}); | |||
return this; | |||
}; | |||
window.Tasker = Tasker; | |||
var taskers = { | |||
renderer: new Tasker(), | |||
handler: new Tasker(), | |||
trigger: new Tasker() | |||
}; | |||
function runTask() { | |||
var tasker = this; | |||
this[TASKS].forEach(function(task) { | |||
if(typeof task === 'string') tasker[TASKS].add(mw.loader.using(task)); | |||
else tasker[TASKS].add(task()); | |||
}); | |||
return this; | |||
} | |||
taskers.renderer.push = function() { | |||
Tasker.prototype.apply(this, arguments); | |||
if(documentReady) this.run(); | |||
return this; | |||
}; | |||
taskers.handler.push = function() { | |||
Tasker.prototype.apply(this, arguments); | |||
if(rendererDone) this.run(); | |||
return this; | |||
}; | |||
taskers.trigger.push = function() { | |||
Tasker.prototype.apply(this, arguments); | |||
if(hendlerDone) this.run(); | |||
return this; | |||
}; | |||
taskers.renderer.run = runTask; | |||
taskers.handler.run = runTask; | |||
taskers.trigger.run = runTask; | |||
var documentReady = false; | |||
var rendererDone = false; | |||
var handlerDone = false; | |||
window.registerRenderer = function registerRenderer() { | |||
var renderers = arguments; | |||
if(Array.isArray(renderers[0])) renderers = renderers[0]; | |||
taskers.renderer.push(arguments); | |||
}; | |||
window.registerHandler = function registerHandler() { | |||
var handlers = arguments; | |||
if(Array.isArray(handlers[0])) handlers = handlers[0]; | |||
taskers.handler.push(arguments); | |||
}; | |||
window.registerTrigger = function registerTrigger() { | |||
var triggers = arguments; | |||
if(Array.isArray(triggers[0])) triggers = triggers[0]; | |||
taskers.trigger.push(arguments); | |||
}; | |||
$(function() { | |||
documentReady = true; | |||
taskers.renderer.run().wait().then(function() { | |||
rendererDone = true; | |||
return taskers.handler.run().wait(); | |||
}).then(function() { | |||
handlerDone = true; | |||
taskers.trigger.run(); | |||
}); | |||
}); |
2021년 1월 3일 (일) 03:41 판
var TASKS = Symbol('tasks_queue');
var WAITING = Symbol('waiting_queue');
function Tasker() {
this[TASKS] = new Set();
this[WAITING] = new Set();
}
Tasker.prototype.push = function push() {
var tasker = this;
var items = arguments;
if(Array.isArray(items[0])) items = items[0];
else items = Array.from(items);
items.forEach(function(item) {
if(typeof item === 'function') this[TASKS].add(item);
else this[WAITING].add(item);
});
return this;
};
Tasker.prototype.wait = function wait() {
var tasker = this;
return new Promise(function(resolve) {
function capture(queue) {
var arr = Array.from(queue);
queue.clear();
return arr;
}
Promise.all(capture(tasker[WAITING])).then(function handle() {
var captured = capture(tasker[WAITING]);
if(captured.length) return Promise.all(captured).then(handle);
else resolve();
});
});
};
Tasker.prototype.run = function run() {
var tasker = this;
this[TASKS].forEach(function(task) {
tasker[WAITING].add(task());
});
return this;
};
window.Tasker = Tasker;
var taskers = {
renderer: new Tasker(),
handler: new Tasker(),
trigger: new Tasker()
};
function runTask() {
var tasker = this;
this[TASKS].forEach(function(task) {
if(typeof task === 'string') tasker[TASKS].add(mw.loader.using(task));
else tasker[TASKS].add(task());
});
return this;
}
taskers.renderer.push = function() {
Tasker.prototype.apply(this, arguments);
if(documentReady) this.run();
return this;
};
taskers.handler.push = function() {
Tasker.prototype.apply(this, arguments);
if(rendererDone) this.run();
return this;
};
taskers.trigger.push = function() {
Tasker.prototype.apply(this, arguments);
if(hendlerDone) this.run();
return this;
};
taskers.renderer.run = runTask;
taskers.handler.run = runTask;
taskers.trigger.run = runTask;
var documentReady = false;
var rendererDone = false;
var handlerDone = false;
window.registerRenderer = function registerRenderer() {
var renderers = arguments;
if(Array.isArray(renderers[0])) renderers = renderers[0];
taskers.renderer.push(arguments);
};
window.registerHandler = function registerHandler() {
var handlers = arguments;
if(Array.isArray(handlers[0])) handlers = handlers[0];
taskers.handler.push(arguments);
};
window.registerTrigger = function registerTrigger() {
var triggers = arguments;
if(Array.isArray(triggers[0])) triggers = triggers[0];
taskers.trigger.push(arguments);
};
$(function() {
documentReady = true;
taskers.renderer.run().wait().then(function() {
rendererDone = true;
return taskers.handler.run().wait();
}).then(function() {
handlerDone = true;
taskers.trigger.run();
});
});