语法相关
枚举
|
1 2 3 4 5 |
enum loop_type { static = 0, dynamic = 1, compose = 2 } |
接口
|
1 2 3 4 5 6 |
interface list_item { tsk_type: number; cache_id: string; path: string; // ... } |
类型注解
|
1 2 3 4 |
private interval: number; private timer: NodeJS.Timeout | null = null; private is_valid(item: list_item): boolean { } private Internal_get_cache_id(res_type: number, id: string) { } |
联合类型
|
1 |
private last_loop_type: number | null; |
继承
|
1 2 3 4 5 |
class WpLoopCompose extends ApiEventBase { constructor() { super("loop_compose"); } } |
async/await
|
1 2 3 |
private async start_sock() { await this.schedule_sock(); } |
功能
任务队列
- 定义,初始化
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
private tasks : list_item[]; constructor() { super("loop_compose"); this.tasks = []; this.new_tasks = []; this.free_tasks = []; this.sock_tasks = []; this.tasks_map = new Map(); this.tasks_map_sock = new Map(); this.tasks_map_free = new Map(); this.interval = 10 * 1000; this.interval_sock = 60 * 1000; this.last_loop_type = null; } |
- 队列循环
- 从某个队列头取任务,再放到尾
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
private Internal_UpdateTasks() : list_item { let first_item: list_item; const free_tsk = this.Internal_GetFreeTask(); if (free_tsk === undefined) { if (this.new_tasks.length === 0) { first_item = this.tasks.shift(); } else { first_item = this.new_tasks.shift(); } if (first_item === undefined) { this.Internal_UpdateCfg(); return undefined; } const tmp_cache_id = first_item.cache_id; this.tasks_map.set(tmp_cache_id, true); if (this.tasks_map.size < tsk_limit.em_limit) { const all_emited = [...this.tasks_map.values()].every(val => val === true); if (all_emited) { console.log("update tsk from all_emited."); this.tasks_map.forEach((value, key) => this.tasks_map.set(key, false)); this.tasks_map_free.forEach((value, key) => this.tasks_map_free.set(key, false)); this.Internal_UpdateCfg(); } } if (first_item != undefined) { this.tasks.push(first_item); return first_item; } } else { first_item = free_tsk; const tmp = free_tsk.cache_id; this.tasks_map_free.set(tmp, true); return first_item; } return undefined; } |
任务标记
- 使用
map
|
1 2 3 |
private tasks_map : Map<string, boolean>; this.tasks_map = new Map(); |
- 添加任务时,检测任务是否已添加
|
1 2 3 4 5 6 7 8 9 10 11 |
if (this.tasks_map.has(tmp_cache_id)) { console.log('tasks_map ${tmp_cache_id} is exist already.'); return; } if (this.tasks_map.size >= tsk_limit.em_limit) { console.log('tasks_map is tsk_limit.em_limit, add new_tsk failed. '); return; } // 默认没有执行 this.tasks_map.set(tmp_cache_id, false); |
- 删除某个任务时,使用
delete清标记
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
private remove_wp_tsks_cate(cate: string) { for (let i = this.new_tasks.length - 1; i >= 0; i--) { if (this.new_tasks[i].cate === cate) { const id = this.new_tasks[i].cache_id; this.tasks_map.delete(id); this.new_tasks.splice(i, 1); } } for (let i = this.tasks.length - 1; i >= 0; i--) { if (this.tasks[i].cate === cate) { const id = this.tasks[i].cache_id; this.tasks_map.delete(id); this.tasks.splice(i, 1); } } if (this.new_tasks.length === 0 && this.tasks.length === 0) { this.stop_loop(); console.log('stop loop from remove_wp_tsks_cate'); } } |
- 标记已执行
|
1 2 |
const tmp_cache_id = first_item.cache_id; this.tasks_map.set(tmp_cache_id, true); |
- 检查所有任务是否都已执行
- 重置当前所有任务标记为
false - 任务数量未满之前,再请求新的任务
- 重置当前所有任务标记为
|
1 2 3 4 5 6 7 |
const all_emited = [...this.tasks_map.values()].every(val => val === true); if (all_emited) { console.log("update tsk from all_emited."); this.tasks_map.forEach((value, key) => this.tasks_map.set(key, false)); this.tasks_map_free.forEach((value, key) => this.tasks_map_free.set(key, false)); this.Internal_UpdateCfg(); } |
声明:本文为原创文章,版权归Aet所有,欢迎分享本文,转载请保留出处!