import { Task } from './task' ; import { sortBy } from './common' ; /**
 * 任务优先级。数据越小，优先级越高
 */ export enum TaskPriority { /**
     * 用户行为（比如：各种交互行为）
     */ UserAction = 0 , /**
     * 更新领域数据
     */ UpdateState = 1 , /**
     * 更新 React 组件
     */ RenderReact = 2 , /**
     * 更新场景视图
     *
     * 暂定高优先级，因为基于之后场景组件化的机制，这一帧绘制的应该是上一帧的结果
     */ RenderScene = - 1 } /**
 * TODO:
 * 1. 收集这里的任务排队执行情况
 * 2. 根据搜集数据+行业中的任务切片方案来优化这边的任务执行
 */ let tasks = [ ] ; let readyToExecute = false ; /**
 * 执行任务队列
 */ function executeTask ( ) { if ( readyToExecute ) { const currentTasks = sortBy ( tasks , task => task . priority ) ; tasks = [ ] ; currentTasks . forEach ( ( task ) => { const { fn } = task ; fn ( ) ; } ) ; readyToExecute = false ; } } param fn param priority function pushTask ( fn , priority ) { tasks . push ( Task . createTask ( fn , priority ) ) ; if ( ! readyToExecute ) { readyToExecute = true ; requestAnimationFrame ( executeTask ) ; } } param func param priority export function throttleInAFrame < T extends ( ... args : any [ ] ) => void > ( func , priority = TaskPriority . UserAction ) { let cacheArgs ; let isPending = false ; function execute ( ) { isPending = false ; func ( ... cacheArgs ) ; } return function ( ... args ) { cacheArgs = args ; if ( ! isPending ) { isPending = true ; pushTask ( execute , priority ) ; } } as T ; }