import { FiberRoot } from './ReactInternalTypes' ; export const TotalLanes = 31 ; export type Lanes = number ; export type Lane = number ; export type LaneMap < T > = Array < T > ; export const NoLanes = 0b0000000000000000000000000000000 ; export const NoLane = 0b0000000000000000000000000000000 ; export const SyncLane = 0b0000000000000000000000000000001 ; export const InputContinuousLane = 0b0000000000000000000000000000100 ; export const DefaultLane = 0b0000000000000000000000000010000 ; export const IdleLane = 0b0100000000000000000000000000000 ; const NonIdleLanes = 0b0001111111111111111111111111111 ; export const NoTimestamp = - 1 ; const clz32 = Math . clz32 ; const pickArbitraryLaneIndex = ( lanes ) : number => { return 31 - clz32 ( lanes ) ; } ; param lane param currentTime returns const computeExpirationTime = ( lane , currentTime ) : number => { switch ( lane ) { case SyncLane : return currentTime + 250 ; case DefaultLane : return currentTime + 5000 ; default : { throw new Error ( 'Not Implement' ) ; } } } ; param root param currentTime export const markStarvedLanesAsExpired = ( root , currentTime ) : void => { const pendingLanes = root . pendingLanes ; const expirationTimes = root . expirationTimes ; let lanes = pendingLanes ; while ( lanes > 0 ) { const index = pickArbitraryLaneIndex ( lanes ) ; const lane = 1 << index ; const expirationTime = expirationTimes [ index ] ; if ( expirationTime === NoTimestamp ) { /**
             * 还没有相关的时间戳，帮他计算一个
             */ expirationTimes [ index ] = computeExpirationTime ( lane , currentTime ) ; } else if ( expirationTime <= currentTime ) { root . expiredLanes |= lane ; } lanes &= ~ lane ; } } ; param lanes returns export const getHighestPriorityLane = ( lanes ) : Lane => { return lanes & - lanes ; } ; param lanes returns const getHighestPriorityLanes = ( lanes ) : Lanes => { switch ( getHighestPriorityLane ( lanes ) ) { case SyncLane : return SyncLane ; case DefaultLane : return DefaultLane ; default : { throw new Error ( 'Not Implement' ) ; } } } ; param root param wipLanes returns export const getNextLanes = ( root , wipLanes ) : Lanes => { const pendingLanes = root . pendingLanes ; if ( pendingLanes === NoLanes ) return NoLanes ; let nextLanes = NoLanes ; const nonIdlePendingLanes = pendingLanes & NonIdleLanes ; if ( nonIdlePendingLanes !== NoLanes ) { nextLanes = getHighestPriorityLanes ( nonIdlePendingLanes ) ; } else { throw new Error ( 'Not Implement' ) ; } if ( nextLanes === NoLanes ) { return NoLanes ; } /**
     * 如果已经处于render阶段，切换lanes会导致丢失进度
     * 我们只应该在新的lane拥有更高的优先级的时候这样做
     */ if ( wipLanes !== NoLanes && wipLanes !== nextLanes ) { const nextLane = getHighestPriorityLane ( nextLanes ) ; const wipLane = getHighestPriorityLane ( wipLanes ) ; if ( nextLane >= wipLane ) { return wipLanes ; } } return nextLanes ; } ; export const includesSomeLane = ( a , b ) : boolean => { return ( a & b ) !== NoLanes ; } ; export const mergeLanes = ( a , b ) : Lanes => { return a | b ; } ; param lane returns const laneToIndex = ( lane ) : number => { return pickArbitraryLaneIndex ( lane ) ; } ; param root param updateLane param eventTime export const markRootUpdated = ( root , updateLane , eventTime ) : void => { root . pendingLanes |= updateLane ; const eventTimes = root . eventTimes ; const index = laneToIndex ( updateLane ) ; eventTimes [ index ] = eventTime ; } ; export const createLaneMap = < T > ( initial ) : LaneMap < T > => { const laneMap = [ ] ; for ( let i = 0 ; i < TotalLanes ; ++ i ) { laneMap . push ( initial ) ; } return laneMap ; } ; param set param subset returns export const isSubsetOfLanes = ( set , subset ) => { return ( set & subset ) === subset ; } ; export const includesNonIdleWork = ( lanes ) : boolean => { return ( lanes & NonIdleLanes ) !== NonIdleLanes ; } ; /**
 * 是否开启默认同步模式
 */ const enableSyncDefaultUpdates = false ; param root param lanes returns export const shouldTimeSlice = ( root , lanes ) : boolean => { if ( ( lanes & root . expiredLanes ) !== NoLanes ) { return false ; } if ( enableSyncDefaultUpdates ) { const SyncDefaultLanes = InputContinuousLane | DefaultLane ; return ( lanes & SyncDefaultLanes ) === NoLanes ; } else { return true ; } } ; param root param remainingLanes export const markRootFinished = ( root , remainingLanes ) => { const noLongerPendingLanes = root . pendingLanes & ~ remainingLanes ; root . pendingLanes = remainingLanes ; root . expiredLanes &= remainingLanes ; const eventTimes = root . eventTimes ; const expirationTimes = root . expirationTimes ; let lanes = noLongerPendingLanes ; while ( lanes > 0 ) { const index = pickArbitraryLaneIndex ( lanes ) ; const lane = 1 << index ; eventTimes [ index ] = NoTimestamp ; expirationTimes [ index ] = NoTimestamp ; lanes &= ~ lane ; } } ; export const removeLanes = ( set , subset ) : Lanes => { return set & ~ subset ; } ;