|
| 1 | +import PromisePolyfill from './Promise'; |
| 2 | +import { requestAnimFrame } from './util'; |
| 3 | +import { isFunction, isNil, isNumber } from './util/common'; |
| 4 | +import { getGlobalWorkerPool } from './worker/WorkerPool'; |
| 5 | +import Browser from './Browser'; |
| 6 | +import globalConfig from '../globalConfig'; |
| 7 | + |
| 8 | +let tasks = []; |
| 9 | + |
| 10 | +/** |
| 11 | + * |
| 12 | + * @param {Object|Function} task - a micro task(Promise) |
| 13 | + * @param {Number} task.count - task run count |
| 14 | + * @param {Function} task.run - task run function |
| 15 | + * @return {Promise} |
| 16 | + * @example |
| 17 | + * const run =()=>{ |
| 18 | + * //do some things |
| 19 | + * }; |
| 20 | + * runTaskAsync({count:4,run}).then(result=>{}) |
| 21 | + * runTaskAsync(run).then(result=>{}) |
| 22 | + */ |
| 23 | +export function runTaskAsync(task) { |
| 24 | + startTasks(); |
| 25 | + const promise = new PromisePolyfill((resolve, reject) => { |
| 26 | + if (!task) { |
| 27 | + reject(new Error('task is null')); |
| 28 | + return; |
| 29 | + } |
| 30 | + if (isFunction(task)) { |
| 31 | + task = { count: 1, run: task }; |
| 32 | + } |
| 33 | + if (!task.run) { |
| 34 | + reject(new Error('task.run is null')); |
| 35 | + return; |
| 36 | + } |
| 37 | + if (isNil(task.count)) { |
| 38 | + task.count = 1; |
| 39 | + } |
| 40 | + task.count = Math.ceil(task.count); |
| 41 | + if (!isNumber(task.count)) { |
| 42 | + reject(new Error('task.count is not number')); |
| 43 | + return; |
| 44 | + } |
| 45 | + task.results = []; |
| 46 | + tasks.push(task); |
| 47 | + task.resolve = resolve; |
| 48 | + }); |
| 49 | + return promise; |
| 50 | +} |
| 51 | + |
| 52 | +function executeMicroTasks() { |
| 53 | + if (tasks.length === 0) { |
| 54 | + return; |
| 55 | + } |
| 56 | + const runingTasks = [], endTasks = []; |
| 57 | + let len = tasks.length; |
| 58 | + for (let i = 0; i < len; i++) { |
| 59 | + const task = tasks[i]; |
| 60 | + task.count--; |
| 61 | + if (task.count === -1) { |
| 62 | + endTasks.push(task); |
| 63 | + } else { |
| 64 | + runingTasks.push(task); |
| 65 | + const result = task.run(); |
| 66 | + task.results.push(result); |
| 67 | + } |
| 68 | + } |
| 69 | + tasks = runingTasks; |
| 70 | + len = endTasks.length; |
| 71 | + for (let i = 0; i < len; i++) { |
| 72 | + const task = endTasks[i]; |
| 73 | + if (task.resolve) { |
| 74 | + task.resolve(task.results); |
| 75 | + } |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +let broadcastIdleMessage = true; |
| 80 | +function loop() { |
| 81 | + if (broadcastIdleMessage) { |
| 82 | + getGlobalWorkerPool().broadcastIdleMessage(); |
| 83 | + } else { |
| 84 | + getGlobalWorkerPool().commit(); |
| 85 | + } |
| 86 | + executeMicroTasks(); |
| 87 | + broadcastIdleMessage = !broadcastIdleMessage; |
| 88 | +} |
| 89 | + |
| 90 | +function frameLoop(deadline) { |
| 91 | + const { idleTimeRemaining, idleLog, idleTimeout } = globalConfig; |
| 92 | + if (Browser.requestIdleCallback) { |
| 93 | + if (deadline && deadline.timeRemaining) { |
| 94 | + const t = deadline.timeRemaining(); |
| 95 | + if (t > idleTimeRemaining || deadline.didTimeout) { |
| 96 | + if (deadline.didTimeout && idleLog) { |
| 97 | + console.error('idle timeout in', idleTimeout); |
| 98 | + } |
| 99 | + loop(); |
| 100 | + } else if (t <= idleTimeRemaining && idleLog) { |
| 101 | + console.warn('currrent page is busy,the timeRemaining is', t); |
| 102 | + } |
| 103 | + } |
| 104 | + requestIdleCallback(frameLoop, { timeout: idleTimeout }); |
| 105 | + } else { |
| 106 | + loop(); |
| 107 | + // Fallback to requestAnimFrame |
| 108 | + requestAnimFrame(frameLoop); |
| 109 | + if (idleLog) { |
| 110 | + console.warn('current env not support requestIdleCallback. Fallback to requestAnimFrame'); |
| 111 | + } |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +let started = false; |
| 116 | +export function startTasks() { |
| 117 | + if (started) { |
| 118 | + return; |
| 119 | + } |
| 120 | + started = true; |
| 121 | + const { idleTimeout } = globalConfig; |
| 122 | + if (Browser.requestIdleCallback) { |
| 123 | + requestIdleCallback(frameLoop, { timeout: idleTimeout }); |
| 124 | + } else { |
| 125 | + requestAnimFrame(frameLoop); |
| 126 | + } |
| 127 | +} |
0 commit comments