导航菜单

  • 0.api
  • 0.Async
  • 0.module
  • 1.ES2015
  • 2.Promise
  • 3.Node
  • 4.NodeInstall
  • 5.REPL
  • 6.NodeCore
  • 7.module&NPM
  • 8.Encoding
  • 9.Buffer
  • 10.fs
  • 11.Stream-1
  • 11.Stream-2
  • 11.Stream-3
  • 11.Stream-4
  • 12-Network-2
  • 12.NetWork-3
  • 12.Network-1
  • 13.tcp
  • 14.http-1
  • 14.http-2
  • 15.compress
  • 16.crypto
  • 17.process
  • 18.yargs
  • 19.cache
  • 20.action
  • 21.https
  • 22.cookie
  • 23.session
  • 24.express-1
  • 24.express-2
  • 24.express-3
  • 24.express-4
  • 25.koa-1
  • 26.webpack-1-basic
  • 26.webpack-2-optimize
  • 26.webpack-3-file
  • 26.webpack-4.tapable
  • 26.webpack-5-AST
  • 26.webpack-6-sources
  • 26.webpack-7-loader
  • 26.webpack-8-plugin
  • 26.webpack-9-hand
  • 26.webpack-10-prepare
  • 28.redux
  • 28.redux-jwt-back
  • 28.redux-jwt-front
  • 29.mongodb-1
  • 29.mongodb-2
  • 29.mongodb-3
  • 29.mongodb-4
  • 29.mongodb-5
  • 29.mongodb-6
  • 30.cms-1-mysql
  • 30.cms-2-mysql
  • 30.cms-3-mysql
  • 30.cms-4-nunjucks
  • 30.cms-5-mock
  • 30.cms-6-egg
  • 30.cms-7-api
  • 30.cms-8-roadhog
  • 30.cms-9-yaml
  • 30.cms-10-umi
  • 30.cms-12-dva
  • 30.cms-13-dva-ant
  • 30.cms-14-front
  • 30.cms-15-deploy
  • 31.dva
  • 31.cms-13-dva-antdesign
  • 33.redis
  • 34.unittest
  • 35.jwt
  • 36.websocket-1
  • 36.websocket-2
  • 38.chat-api-1
  • 38.chat-api-2
  • 38.chat-3
  • 38.chat-api-3
  • 38.chat
  • 38.chat2
  • 38.chat2
  • 39.crawl-0
  • 39.crawl-1
  • 39.crawl-2
  • 40.deploy
  • 41.safe
  • 42.test
  • 43.nginx
  • 44.enzyme
  • 45.docker
  • 46.elastic
  • 47.oauth
  • 48.wxpay
  • index
  • 52.UML
  • 53.design
  • index
  • 54.linux
  • 57.ts
  • 56.react-ssr
  • 58.ts_react
  • 59.ketang
  • 59.ketang2
  • 61.1.devops-linux
  • 61.2.devops-vi
  • 61.3.devops-user
  • 61.4.devops-auth
  • 61.5.devops-shell
  • 61.6.devops-install
  • 61.7.devops-system
  • 61.8.devops-service
  • 61.9.devops-network
  • 61.10.devops-nginx
  • 61.11.devops-docker
  • 61.12.devops-jekins
  • 61.13.devops-groovy
  • 61.14.devops-php
  • 61.15.devops-java
  • 61.16.devops-node
  • 61.17.devops-k8s
  • 62.1.react-basic
  • 62.2.react-state
  • 62.3.react-high
  • 62.4.react-optimize
  • 62.5.react-hooks
  • 62.6.react-immutable
  • 62.7.react-mobx
  • 62.8.react-source
  • 63.1.redux
  • 63.2.redux-middleware
  • 63.3.redux-hooks
  • 63.4.redux-saga
  • 63.5.redux-saga-hand
  • 64.1.router
  • 64.2.router-connected
  • 65.1.typescript
  • 65.2.typescript
  • 65.3.typescript
  • 65.4.antd
  • 65.4.definition
  • 66-1.vue-base
  • 66-2.vue-component
  • 66-3.vue-cli3.0
  • 66-4.$message组件
  • 66-5.Form组件
  • 66-6.tree
  • 66-7.vue-router-apply
  • 66-8.axios-apply
  • 66-9.vuex-apply
  • 66-10.jwt-vue
  • 66-11.vue-ssr
  • 66-12.nuxt-apply
  • 66-13.pwa
  • 66-14.vue单元测试
  • 66-15.权限校验
  • 67-1-network
  • 68-2-wireshark
  • 7.npm2
  • 69-hooks
  • 70-deploy
  • 71-hmr
  • 72.deploy
  • 73.import
  • 74.mobile
  • 75.webpack-1.文件分析
  • 75.webpack-2.loader
  • 75.webpack-3.源码流程
  • 75.webpack-4.tapable
  • 75.webpack-5.prepare
  • 75.webpack-6.resolve
  • 75.webpack-7.loader
  • 75.webpack-8.module
  • 75.webpack-9.chunk
  • 75.webpack-10.asset
  • 75.webpack-11.实现
  • 76.react_optimize
  • 77.ts_ketang_back
  • 77.ts_ketang_front
  • 78.vue-domdiff
  • 79.grammar
  • 80.tree
  • 81.axios
  • 82.1.react
  • 82.2.react-high
  • 82.3.react-router
  • 82.4.redux
  • 82.5.redux_middleware
  • 82.6.connected
  • 82.7.saga
  • 82.8.dva
  • 82.8.dva-source
  • 82.9.roadhog
  • 82.10.umi
  • 82.11.antdesign
  • 82.12.ketang-front
  • 82.12.ketang-back
  • 83.upload
  • 84.graphql
  • 85.antpro
  • 86.1.uml
  • 86.2.design
  • 87.postcss
  • 88.react16-1
  • 89.nextjs
  • 90.react-test
  • 91.react-ts
  • 92.rbac
  • 93.tsnode
  • 94.1.JavaScript
  • 94.2.JavaScript
  • 94.3.MODULE
  • 94.4.EventLoop
  • 94.5.文件上传
  • 94.6.https
  • 94.7. nginx
  • 95.1. react
  • 95.2.react
  • 96.1.react16
  • 96.2.fiber
  • 96.3.fiber
  • 97.serverless
  • 98.websocket
  • 100.1.react-basic
  • 101.1.monitor
  • 101.2.monitor
  • 102.java
  • 103.1.webpack-usage
  • 103.2.webpack-bundle
  • 103.3.webpack-ast
  • 103.4.webpack-flow
  • 103.5.webpack-loader
  • 103.6.webpack-tapable
  • 103.7.webpack-plugin
  • 103.8.webpack-optimize1
  • 103.9.webpack-optimize2
  • 103.10.webpack-hand
  • 103.11.webpack-hmr
  • 103.11.webpack5
  • 103.13.splitChunks
  • 103.14.webpack-sourcemap
  • 103.15.webpack-compiler1
  • 103.15.webpack-compiler2
  • 103.16.rollup.1
  • 103.16.rollup.2
  • 103.16.rollup.3
  • 103.16.vite.basic
  • 103.16.vite.source
  • 103.16.vite.plugin
  • 103.16.vite.1
  • 103.16.vite.2
  • 103.17.polyfill
  • 104.1.binary
  • 104.2.binary
  • 105.skeleton
  • 106.1.react
  • 106.2.react_hooks
  • 106.3.react_router
  • 106.4.redux
  • 106.5.redux_middleware
  • 106.6.connected-react-router
  • 106.6.redux-first-history
  • 106.7.redux-saga
  • 106.8.dva
  • 106.9.umi
  • 106.10.ketang
  • 106.11.antdesign
  • 106.12.antpro
  • 106.13.router-6
  • 106.14.ssr
  • 106.15.nextjs
  • 106.16.1.cms
  • 106.16.2.cms
  • 106.16.3.cms
  • 106.16.4.cms
  • 106.16.mobx
  • 106.17.fomily
  • 107.fiber
  • 108.http
  • 109.1.webpack_usage
  • 109.2.webpack_source
  • 109.3.dll
  • 110.nest.js
  • 111.xstate
  • 112.Form
  • 113.redux-saga
  • 114.react+typescript
  • 115.immer
  • 116.pro5
  • 117.css-loader
  • 118.1.umi-core
  • 119.2.module-federation
  • 119.1.module-federation
  • 120.create-react-app
  • 121.react-scripts
  • 122.react-optimize
  • 123.jsx-runtime
  • 124.next.js
  • 125.1.linux
  • 125.2.linux-vi
  • 125.3.linux-user
  • 125.4.linux-auth
  • 125.5.linux-shell
  • 125.6.linux-install
  • 125.7.linux-system
  • 125.8.linux-service
  • 125.9.linux-network
  • 125.10.nginx
  • 125.11.docker
  • 125.12.ci
  • 125.13.k8s
  • 125.14.k8s
  • 125.15.k8s
  • 125.16.k8s
  • 126.11.react-1
  • 126.12.react-2
  • 126.12.react-3
  • 126.12.react-4
  • 126.12.react-5
  • 126.12.react-6
  • 126.12.react-7
  • 126.12.react-8
  • 127.frontend
  • 128.rollup
  • 129.px2rem-loader
  • 130.health
  • 131.hooks
  • 132.keepalive
  • 133.vue-cli
  • 134.react18
  • 134.2.react18
  • 134.3.react18
  • 135.function
  • 136.toolkit
  • 137.lerna
  • 138.create-vite
  • 139.cli
  • 140.antd
  • 141.react-dnd
  • 142.1.link
  • 143.1.gulp
  • 143.2.stream
  • 143.3.gulp
  • 144.1.closure
  • 144.2.v8
  • 144.3.gc
  • 145.react-router-v6
  • 146.browser
  • 147.lighthouse
  • 148.1.basic
  • 148.2.basic
  • 148.3.basic
  • 148.4.basic
  • 148.5.basic
  • 149.1.vite
  • 149.2.vite
  • 149.3.vite
  • 149.4.vite
  • 150.react-window
  • 151.react-query
  • 152.useRequest
  • 153.transition
  • 154.emotion
  • 155.1.formily
  • 155.2.formily
  • 155.3.formily
  • 155.3.1.mobx.usage
  • 155.3.2.mobx.source
  • 156.vue-loader
  • 103.11.mf
  • 157.1.react18
  • 158.umi4
  • 159.rxjs
  • 159.rxjs2
  • 160.bff
  • 161.zustand
  • 162.vscode
  • 163.emp
  • 164.cors
  • 7.初次渲染
    • 7.1 src\index.js
    • 7.2 src\react-dom\index.js
    • 7.3 ReactFiberRoot.js
    • 7.4 ReactFiber.js
    • 7.5 ReactUpdateQueue.js
    • 7.6 ReactFiberReconciler.js
    • 7.7 ReactFiberWorkLoop.js
    • 7.8 ReactWorkTags.js
  • 8.同步渲染级
    • 8.1 ReactFiberReconciler.js
    • 8.2 ReactFiber.js
    • 8.3 ReactUpdateQueue.js
    • 8.4 ReactFiberWorkLoop.js
    • 8.5 ReactFiberLane.js
  • 9.异步渲染
    • 9.1 ReactFiberWorkLoop.js
    • 9.2 Scheduler.js
    • 9.3 ReactFiberLane.js
    • 9.4 ReactFiberRoot.js
    • 9.5 ReactTypes.js
    • 9.6 ReactDOMEventListener.js
    • 9.7 ReactDOMEventListener.js
    • 9.8 SchedulerWithReactIntegration.js

7.初次渲染 #

7.1 src\index.js #

import React from 'react';
import ReactDOM from './react-dom';
ReactDOM.render(<h1>hello</h1>, document.getElementById('root'));

7.2 src\react-dom\index.js #

src\react-dom\index.js

import { createFiberRoot } from '../react-reconciler/ReactFiberRoot';
import { updateContainer } from '../react-reconciler/ReactFiberReconciler';
function render(element, container) {
    let fiberRoot = createFiberRoot(container);
    updateContainer(element, fiberRoot);
}
const ReactDOM = {
    render
}
export default ReactDOM;

7.3 ReactFiberRoot.js #

src\react-reconciler\ReactFiberRoot.js

import { createHostRootFiber } from './ReactFiber';
import { initializeUpdateQueue } from './ReactUpdateQueue';
export function createFiberRoot(containerInfo) {
    const root = new FiberRootNode(containerInfo);
    const hostRootFiber = createHostRootFiber();
    root.current = hostRootFiber;
    hostRootFiber.stateNode = root;
    initializeUpdateQueue(hostRootFiber);
    return root;
}

function FiberRootNode(containerInfo) {
    this.containerInfo = containerInfo;
}

7.4 ReactFiber.js #

src\react-reconciler\ReactFiber.js

import { HostRoot } from './ReactWorkTags';
/**
 * 创建根fiber
 * @returns 根fiber
 */
export function createHostRootFiber() {
    return createFiber(HostRoot);
}
/**
 * 创建fiber
 * @param {*} tag fiber类型
 * @param {*} pendingProps 新属性对象
 * @param {*} key 唯一标识
 * @returns 创建的fiber
 */
const createFiber = function (tag, pendingProps, key) {
    return new FiberNode(tag, pendingProps, key);
};
/**
 * fiber构建函数
 * @param {*} tag fiber类型
 * @param {*} pendingProps  新属性对象
 * @param {*} key 唯一标识
 */
function FiberNode(tag, pendingProps, key) {
    this.tag = tag;
    this.pendingProps = pendingProps;
    this.key = key;
}

7.5 ReactUpdateQueue.js #

src\react-reconciler\ReactUpdateQueue.js

/**
 * 初始化fiber节点上的更新队列 
 * @param {*} fiber 
 */
export function initializeUpdateQueue(fiber) {
    const queue = {
        //这是一个环状链表,存放着等待生效的更新
        shared: {
            pending: null
        }
    };
    fiber.updateQueue = queue;
}
/**
 * 创建更新对象
 * @returns 更新对象
 */
export function createUpdate() {
    return {};
}
/**
 * 把更新对象添加到fiber的更新队列中
 * @param {*} fiber fiber节点
 * @param {*} update 新的更新对象
 */
export function enqueueUpdate(fiber, update) {
    //取出fiber上的更新队列
    const updateQueue = fiber.updateQueue;
    const sharedQueue = updateQueue.shared;
    //取出等待生效的更新环状链表
    const pending = sharedQueue.pending;
    //如果环状链表为空
    if (!pending) {
        //构建环状链表 
        update.next = update;
    } else {
        //让新的更新的next指向第一个更新
        update.next = pending.next;
        //让原来的pending指向新的更新
        pending.next = update;
    }
    //sharedQueue的pending指向新的更新
    sharedQueue.pending = update;
}

7.6 ReactFiberReconciler.js #

src\react-reconciler\ReactFiberReconciler.js

import { createUpdate, enqueueUpdate } from './ReactUpdateQueue';
import { scheduleUpdateOnFiber } from './ReactFiberWorkLoop';
/**
 * 把element元素渲染到容器中
 * @param {*} element 要渲染的虚拟DOM
 * @param {*} container 容器
 */
export function updateContainer(element, container) {
    //获取HostRootFiber
    const current = container.current;
    //创建一个更新对象
    const update = createUpdate();
    //更新对象的payload为{ element }
    update.payload = { element };
    //把更新对象添加到更新队列中
    enqueueUpdate(current, update);
    //开始从HostRootFiber调度更新
    scheduleUpdateOnFiber(current);
}

7.7 ReactFiberWorkLoop.js #

src\react-reconciler\ReactFiberWorkLoop.js

import { HostRoot } from './ReactWorkTags';
/**
 * 向上获取HostRoot节点
 * @param {*} sourceFiber 更新来源fiber
 * @returns 
 */
function markUpdateLaneFromFiberToRoot(sourceFiber) {
    let node = sourceFiber;
    let parent = node.return;
    //一直向上找父亲,找不到为止
    while (parent) {
        node = parent;
        parent = parent.return;
    }
    //如果找到的是HostRoot就返回FiberRootNode,其实就是容器div#root
    if (node.tag === HostRoot) {
        return node.stateNode;
    }
}

export function scheduleUpdateOnFiber(fiber) {
    //向上获取HostRoot节点
    const root = markUpdateLaneFromFiberToRoot(fiber);
    //执行HostRoot上的更新
    performSyncWorkOnRoot(root);
}
/**
 * 开始执行FiberRootNode上的工作
 * @param {*} root  FiberRootNode
 */
function performSyncWorkOnRoot(root) {
    console.log(root);
}

7.8 ReactWorkTags.js #

src\react-reconciler\ReactWorkTags.js

//根fiber,对应的其实是容器containerInfo
export const HostRoot = 3;

8.同步渲染级 #

8.1 ReactFiberReconciler.js #

src\react-reconciler\ReactFiberReconciler.js

import { createUpdate, enqueueUpdate } from './ReactUpdateQueue';
+import { scheduleUpdateOnFiber, requestUpdateLane, requestEventTime } from './ReactFiberWorkLoop';
/**
 * 把element元素渲染到容器中
 * @param {*} element 要渲染的虚拟DOM
 * @param {*} container 容器
 */
export function updateContainer(element, container) {
    //获取HostRootFiber
    const current = container.current;
+   //获取事件开始时间,一般是performance.now()
+   const eventTime = requestEventTime();
+   //获取更新优先级
+   const lane = requestUpdateLane(current);
    //创建一个更新对象
+   const update = createUpdate(eventTime, lane);
    //更新对象的payload为{ element }
    update.payload = { element };
    //把更新对象添加到更新队列中
    enqueueUpdate(current, update);
    //开始从HostRootFiber调度更新
+   scheduleUpdateOnFiber(current, lane, eventTime);
}

8.2 ReactFiber.js #

src\react-reconciler\ReactFiber.js

import { HostRoot } from './ReactWorkTags';
/**
 * 创建根fiber
 * @returns 根fiber
 */
export function createHostRootFiber() {
    return createFiber(HostRoot);
}
/**
 * 创建fiber
 * @param {*} tag fiber类型
 * @param {*} pendingProps 新属性对象
 * @param {*} key 唯一标识
 * @returns 创建的fiber
 */
const createFiber = function (tag, pendingProps, key) {
    return new FiberNode(tag, pendingProps, key);
};
/**
 * fiber构建函数
 * @param {*} tag fiber类型
 * @param {*} pendingProps  新属性对象
 * @param {*} key 唯一标识
 */
function FiberNode(tag, pendingProps, key) {
    this.tag = tag;
    this.pendingProps = pendingProps;
    this.key = key;
}
+/**
+ * 基于老的current创建新的workInProgress
+ * @param {*} current 老的fiber
+ * @returns 
+*/
+export function createWorkInProgress(current, pendingProps) {
+    let workInProgress = createFiber(current.tag, pendingProps, current.key);
+    workInProgress.childLanes = current.childLanes;
+    workInProgress.lanes = current.lanes;
+    current.alternate = workInProgress;
+    return workInProgress;
+}

8.3 ReactUpdateQueue.js #

src\react-reconciler\ReactUpdateQueue.js

/**
 * 初始化fiber节点上的更新队列 
 * @param {*} fiber 
 */
export function initializeUpdateQueue(fiber) {
    const queue = {
        //这是一个环状链表,存放着等待生效的更新
        shared: {
            pending: null
        }
    };
    fiber.updateQueue = queue;
}
/**
 * 创建更新对象
 * @returns 更新对象
 */
export function createUpdate(eventTime, lane) {
    return {
+       // 任务时间,通过performance.now()获取的毫秒数
+       eventTime,
+       // 更新优先级
+       lane,
+       //更新所携带的状态
+       //根组件中为React.element,即ReactDOM.render的第一个参数
+       payload: null,
+       // 指向下一个update
+       next: null
    };
}
/**
 * 把更新对象添加到fiber的更新队列中
 * @param {*} fiber fiber节点
 * @param {*} update 新的更新对象
 */
export function enqueueUpdate(fiber, update) {
    //取出fiber上的更新队列
    const updateQueue = fiber.updateQueue;
    const sharedQueue = updateQueue.shared;
    //取出等待生效的更新环状链表
    const pending = sharedQueue.pending;
    //如果环状链表为空
    if (!pending) {
        //构建环状链表 
        update.next = update;
    } else {
        //让新的更新的next指向第一个更新
        update.next = pending.next;
        //让原来的pending指向新的更新
        pending.next = update;
    }
    //sharedQueue的pending指向新的更新
    sharedQueue.pending = update;
}

8.4 ReactFiberWorkLoop.js #

src\react-reconciler\ReactFiberWorkLoop.js

import { HostRoot } from './ReactWorkTags';
+import { NoTimestamp, SyncLane, mergeLanes, markRootUpdated, NoLanes, getNextLanes, +includesSomeLane } from './ReactFiberLane';
+import { now } from '../scheduler';
+import { createWorkInProgress } from './ReactFiber';
+let currentEventTime = NoTimestamp;
+let workInProgress = null;
+export let subtreeRenderLanes = NoLanes;
/**
 * 从触发状态更新的fiber通过一直往上找return得到rootFiber
 * 找的过程都会将lane收集到每个parent.childLanes上
 * @param {*} sourceFiber 更新来源fiber
 * @returns 
 */
+function markUpdateLaneFromFiberToRoot(sourceFiber, lane) {
+   //更新现有fiber上的lanes
+   sourceFiber.lanes = mergeLanes(sourceFiber.lanes, lane);
    let node = sourceFiber;
    // 从产生更新的fiber节点开始,向上收集childLanes
    let parent = node.return;
    //到rootFiber,其parent为null,则会跳出while
    while (parent) {
+       parent.childLanes = mergeLanes(parent.childLanes, lane);
        node = parent;
        parent = parent.return;
    }
    //如果找到的是HostRoot就返回FiberRootNode,其实就是容器div#root
    if (node.tag === HostRoot) {
        return node.stateNode;
    }
}

+export function scheduleUpdateOnFiber(fiber, lane, eventTime) {
+    //向上获取HostRoot节点并向上收集fiber.childLanes
+    const root = markUpdateLaneFromFiberToRoot(fiber, lane);
+    //在root上标记更新,将update的lane放到root.pendingLane
+    markRootUpdated(root, lane, eventTime);
+    if (lane === SyncLane) {
+        //执行HostRoot上的更新
+        performSyncWorkOnRoot(root);
+    }
+}

/**
 * 开始执行FiberRootNode上的工作
 * @param {*} root  FiberRootNode
 */
function performSyncWorkOnRoot(root) {
+   let lanes = getNextLanes(root, NoLanes);
+   renderRootSync(root, lanes);
}
+/**
+ * 刷新栈帧: 重置FiberRoot上的全局属性和fiber树构造循环过程中的全局变量
+ * @param {*} root 
+ * @param {*} lanes 
+ */
+function prepareFreshStack(root, lanes) {
+    root.finishedWork = null;
+    root.finishedLanes = NoLanes;
+    workInProgress = createWorkInProgress(root.current, null);
+    subtreeRenderLanes = lanes;
+}
+
+function renderRootSync(root, lanes) {
+    prepareFreshStack(root, lanes);
+    workLoopSync();
+}
+function workLoopSync() {
+    while (workInProgress) {
+        performUnitOfWork(workInProgress);
+    }
+}
+function performUnitOfWork(unitOfWork) {
+    if (includesSomeLane(subtreeRenderLanes, unitOfWork.lanes)) {
+        console.log('处理', unitOfWork);
+        workInProgress = null;
+    } else {
+        workInProgress = null;
+    }
+}
+export function requestEventTime() {
+    currentEventTime = now();
+    return currentEventTime;
+}
+
+export function requestUpdateLane(fiber) {
+    return SyncLane;
+}

8.5 ReactFiberLane.js #

src\react-reconciler\ReactFiberLane.js


export const NoLanePriority = 0;
export const DefaultLanePriority = 8;

export const NoLanes = 0b0000000000000000000000000000000;
//同步车道
export const SyncLane = 0b0000000000000000000000000000001;

export const NoTimestamp = -1;

/**
 * 把 a 和 b合并成一个Lanes(优先级分组)
 * 生成一个新的优先级范围
 */
export function mergeLanes(a, b) {
    return a | b;
}

export function markRootUpdated(root, updateLane) {
    //将本次更新的lane放入root的pendingLanes
    root.pendingLanes |= updateLane;
}

export function getNextLanes(root, wipLanes) {
    // 该函数从root.pendingLanes中找出优先级最高的lane
    const pendingLanes = root.pendingLanes;
    let nextLanes = NoLanes;
    nextLanes = getHighestPriorityLanes(pendingLanes);
    nextLanes = pendingLanes & getEqualOrHigherPriorityLanes(nextLanes);
    return nextLanes;
}
/**
 * 找到对应优先级范围内优先级最高的那一批lanes
 * @param {*} lanes 
 */
function getHighestPriorityLanes(lanes) {
    if ((SyncLane & lanes) !== NoLanes) {
        return SyncLane;
    }
}

function getEqualOrHigherPriorityLanes(lanes) {
    return (getLowestPriorityLane(lanes) << 1) - 1;
}
/**
 * 找到lanes中优先级最低的那一个lane
 * @param {*} lanes 
 * @returns 
 */
function getLowestPriorityLane(lanes) {
    const index = 31 - Math.clz32(lanes);
    return index < 0 ? NoLanes : 1 << index;
}

export function includesSomeLane(a, b) {
    return (a & b) !== NoLanes;
}

9.异步渲染 #

9.1 ReactFiberWorkLoop.js #

react-reconciler\ReactFiberWorkLoop.js

import { HostRoot } from './ReactWorkTags';
+import {
+   NoTimestamp, SyncLane, mergeLanes, markRootUpdated, NoLanes, getNextLanes,
+   includesSomeLane, schedulerPriorityToLanePriority, findUpdateLane, returnNextLanesPriority,
+   lanePriorityToSchedulerPriority, markStarvedLanesAsExpired, markRootFinished
+} from './ReactFiberLane';
import { now } from '../scheduler';
import { createWorkInProgress } from './ReactFiber';
+import {
+    scheduleCallback, getCurrentPriorityLevel, shouldYield,
+    ImmediatePriority as ImmediateSchedulerPriority,
+    runWithPriority
+} from './SchedulerWithReactIntegration';
let currentEventTime = NoTimestamp;
let workInProgress = null;
//表示需要更新的fiber节点的lane的集合,在后面更新fiber节点的时候会根据这个值判断是否需要更新
export let subtreeRenderLanes = NoLanes;
+let currentEventWipLanes = NoLanes;
+let workInProgressRootIncludedLanes = NoLanes;
+//是在任务执行阶段赋予的需要更新的fiber节点上的lane的值
+//当新的更新任务产生时,workInProgressRootRenderLanes不为空,则表示有任务正在执行
+//那么则直接返回这个正在执行的任务的lane,那么当前新的任务则会和现有的任务进行一次批量更新
+//表示当前是否有任务正在执行,有值则表示有任务正在执行,反之则没有任务在执行
+let workInProgressRootRenderLanes = NoLanes;

/**
 * 从触发状态更新的fiber通过一直往上找return得到rootFiber
 * 找的过程都会将lane收集到每个parent.childLanes上
 * @param {*} sourceFiber 更新来源fiber
 * @returns 
 */
function markUpdateLaneFromFiberToRoot(sourceFiber, lane) {
    //更新现有fiber上的lanes
    sourceFiber.lanes = mergeLanes(sourceFiber.lanes, lane);
    let node = sourceFiber;
    // 从产生更新的fiber节点开始,向上收集childLanes
    let parent = node.return;
    //到rootFiber,其parent为null,则会跳出while
    while (parent) {
        parent.childLanes = mergeLanes(parent.childLanes, lane);
        node = parent;
        parent = parent.return;
    }
    //如果找到的是HostRoot就返回FiberRootNode,其实就是容器div#root
    if (node.tag === HostRoot) {
        return node.stateNode;
    }
}

export function scheduleUpdateOnFiber(fiber, lane, eventTime) {
    //向上获取HostRoot节点并向上收集fiber.childLanes
    const root = markUpdateLaneFromFiberToRoot(fiber, lane);
    //在root上标记更新,将update的lane放到root.pendingLane
    markRootUpdated(root, lane, eventTime);
    if (lane === SyncLane) {
        //执行HostRoot上的更新
        performSyncWorkOnRoot(root);
+   } else {
+       ensureRootIsScheduled(root, eventTime);
+   }
}

+function ensureRootIsScheduled(root, currentTime) {
+    //为当前任务根据优先级添加过期时间
+    //并检查未执行的任务中是否有任务过期,有任务过期则expiredLanes中添加该任务的lane
+    //在后续任务执行中以同步模式执行,避免饥饿问题
+    markStarvedLanesAsExpired(root, currentTime);
+    //获取优先级最高的任务的优先级
+    const nextLanes = getNextLanes(root, workInProgressRootRenderLanes);
+    //如果nextLanes为空则表示没有任务需要执行,则直接中断更新
+    if (nextLanes === NoLanes) {
+        return;
+    }
+    const newCallbackPriority = returnNextLanesPriority();
+    const schedulerPriorityLevel = lanePriorityToSchedulerPriority(newCallbackPriority);
+    let newCallbackNode = scheduleCallback(schedulerPriorityLevel, performConcurrentWorkOnRoot.bind+(null, root));
+    root.callbackPriority = newCallbackPriority;
+    root.callbackNode = newCallbackNode;
+}
+function performConcurrentWorkOnRoot(root) {
+    currentEventTime = NoTimestamp;
+    currentEventWipLanes = NoLanes;
+    const originalCallbackNode = root.callbackNode;
+    //获取本次渲染的优先级
+    let lanes = getNextLanes(root, workInProgressRootRenderLanes);
+    //构造fiber树
+    let exitStatus = renderRootConcurrent(root, lanes);
+    const finishedWork = root.current.alternate;
+    root.finishedWork = finishedWork;
+    root.finishedLanes = lanes;
+    //渲染fiber树
+    finishConcurrentRender(root, exitStatus, lanes);
+    //退出前再次检测, 是否还有其他更新, 是否需要发起新调度
+    if (root.callbackNode === originalCallbackNode) {
+        //渲染被阻断, 返回一个新的performConcurrentWorkOnRoot函数, 等待下一次调用
+        return performConcurrentWorkOnRoot.bind(null, root);
+    }
+    return null;
+}
+function finishConcurrentRender(root, exitStatus, lanes) {
+    commitRoot(root);
+}
+function commitRoot(root) {
+    const renderPriorityLevel = getCurrentPriorityLevel();
+    runWithPriority(ImmediateSchedulerPriority, commitRootImpl.bind(null, root, renderPriorityLevel));
+    return null;
+}
+function commitRootImpl(root, renderPriorityLevel) {
+    //设置局部变量
+    const finishedWork = root.finishedWork;
+    const lanes = root.finishedLanes;
+    //清空FiberRoot对象上的属性
+    root.finishedWork = null;
+    root.finishedLanes = NoLanes;
+    root.callbackNode = null;
+    //将finishedWork.lanes和finishedWork.childLanes进行合并操作,获取到剩下还需要做更新的lanes
+    let remainingLanes = mergeLanes(finishedWork.lanes, finishedWork.childLanes);
+    //然后调用markRootFinished清空掉已经执行完成的lanes的数据,将剩下的lanes重新挂载到pendingLanes上,准备下一+次的执行
+    markRootFinished(root, remainingLanes);
+}
+function renderRootConcurrent(root, lanes) {
+    prepareFreshStack(root, lanes);
+    workLoopConcurrent();
+}
+function workLoopConcurrent() {
+    while (workInProgress !== null && !shouldYield()) {
+        performUnitOfWork(workInProgress);
+    }
+}
+
+
+/**
+ * 开始执行FiberRootNode上的工作
+ * @param {*} root  FiberRootNode
+ */
+function performSyncWorkOnRoot(root) {
+    let lanes = getNextLanes(root, NoLanes);
+    let exitStatus = renderRootSync(root, lanes);
+    const finishedWork = root.current.alternate;
+    root.finishedWork = finishedWork;
+    root.finishedLanes = lanes;
+    commitRoot(root);
+}
+/**
+ * 刷新栈帧: 重置FiberRoot上的全局属性和fiber树构造循环过程中的全局变量
+ * @param {*} root 
+ * @param {*} lanes 
+ */
+function prepareFreshStack(root, lanes) {
+    root.finishedWork = null;
+    root.finishedLanes = NoLanes;
+    workInProgress = createWorkInProgress(root.current, null);
+    subtreeRenderLanes = workInProgressRootIncludedLanes = lanes;
+}
+
+function renderRootSync(root, lanes) {
+    prepareFreshStack(root, lanes);
+    workLoopSync();
+}
+function workLoopSync() {
+    while (workInProgress) {
+        performUnitOfWork(workInProgress);
+    }
+}
+function performUnitOfWork(unitOfWork) {
+    if (includesSomeLane(subtreeRenderLanes, unitOfWork.lanes)) {
+        console.log('处理', unitOfWork);
+        workInProgress = null;
+    } else {
+        workInProgress = null;
+    }
+}
+export function requestEventTime() {
+    currentEventTime = now();
+    return currentEventTime;
+}
+
+export function requestUpdateLane(fiber) {
+    if (currentEventWipLanes === NoLanes) {
+        currentEventWipLanes = workInProgressRootIncludedLanes;
+    }
+    const schedulerPriority = getCurrentPriorityLevel();//97
+    let lane;
+    const schedulerLanePriority = schedulerPriorityToLanePriority(schedulerPriority);//8
+    lane = findUpdateLane(schedulerLanePriority, currentEventWipLanes);
+    return lane;
+}

9.2 Scheduler.js #

src\scheduler\src\Scheduler.js

import { requestHostCallback, shouldYieldToHost, getCurrentTime, requestHostTimeout } from './SchedulerHostConfig';
import { push, pop, peek } from './SchedulerMinHeap';
import { ImmediatePriority, UserBlockingPriority, NormalPriority, LowPriority, IdlePriority } from './SchedulerPriorities';
// 不同优先级对应的不同的任务过期时间间隔
let maxSigned31BitInt = 1073741823;
let IMMEDIATE_PRIORITY_TIMEOUT = -1;//立即执行的优先级,级别最高
let USER_BLOCKING_PRIORITY_TIMEOUT = 250;//用户阻塞级别的优先级
let NORMAL_PRIORITY_TIMEOUT = 5000;//正常的优先级
let LOW_PRIORITY_TIMEOUT = 10000;//较低的优先级
let IDLE_PRIORITY_TIMEOUT = maxSigned31BitInt;//优先级最低,表示任务可以闲置
//下一个任务ID编号
let taskIdCounter = 1;
//任务队列
let taskQueue = [];
//延迟队列
let timerQueue = [];
let currentTask;
let currentPriorityLevel = NormalPriority;

/**
 * 调度一个任务
 * @param {*} callback 要执行的任务
 */
export function scheduleCallback(priorityLevel, callback, options) {
    // 获取当前时间,它是计算任务开始时间、过期时间和判断任务是否过期的依据
    let currentTime = getCurrentTime();
    // 确定任务开始时间
    let startTime;
    if (typeof options === 'object' && options !== null) {
        var delay = options.delay;
        if (typeof delay === 'number' && delay > 0) {
            startTime = currentTime + delay;
        } else {
            startTime = currentTime;
        }
    } else {
        startTime = currentTime;
    }
    // 计算过期时间
    let timeout;
    switch (priorityLevel) {
        case ImmediatePriority://1
            timeout = IMMEDIATE_PRIORITY_TIMEOUT;//-1
            break;
        case UserBlockingPriority://2
            timeout = USER_BLOCKING_PRIORITY_TIMEOUT;//250
            break;
        case IdlePriority://5
            timeout = IDLE_PRIORITY_TIMEOUT;//1073741823
            break;
        case LowPriority://4
            timeout = LOW_PRIORITY_TIMEOUT;//10000
            break;
        case NormalPriority://3
        default:
            timeout = NORMAL_PRIORITY_TIMEOUT;//5000
            break;
    }
    //计算超时时间
    let expirationTime = startTime + timeout;
    //创建新任务
    let newTask = {
        id: taskIdCounter++,//任务ID
        callback,//真正的任务函数
        priorityLevel,//任务优先级,参与计算任务过期时间
        startTime,
        expirationTime,//表示任务何时过期,影响它在taskQueue中的排序
        //为小顶堆的队列提供排序依据
        sortIndex: -1
    };
    if (startTime > currentTime) {
        newTask.sortIndex = startTime;
        push(timerQueue, newTask);
        if (peek(taskQueue) === null && newTask === peek(timerQueue)) {
            requestHostTimeout(handleTimeout, startTime - currentTime);
        }
    } else {
        newTask.sortIndex = expirationTime;
        //把此工作添加到任务队列中
        push(taskQueue, newTask);
        //taskQueue.push(callback);
        //开始调度flushWork
        requestHostCallback(flushWork);
    }
    return newTask;
}
/**
 * 处理超时任务
 * @param {*} currentTime 
 */
function handleTimeout(currentTime) {
    advanceTimers(currentTime);
    if (peek(taskQueue) !== null) {
        requestHostCallback(flushWork);
    } else {
        const firstTimer = peek(timerQueue);
        if (firstTimer !== null) {
            requestHostTimeout(handleTimeout, firstTimer.startTime - currentTime);
        }
    }
}
function advanceTimers(currentTime) {
    let timer = peek(timerQueue);
    while (timer !== null) {
        if (timer.callback === null) {
            pop(timerQueue);
        } else if (timer.startTime <= currentTime) {
            pop(timerQueue);
            timer.sortIndex = timer.expirationTime;
            push(taskQueue, timer);
        } else {
            return;
        }
        timer = peek(timerQueue);
    }
}
/**
 * 清空任务队列
 * @returns 队列中是否还有任务
 */
function flushWork(initialTime) {
    return workLoop(initialTime);
}
/**
 * 清空任务队列
 * workLoop是通过判断任务函数的返回值去识别任务的完成状态的
 * @returns 队列中是否还有任务
 */
function workLoop(initialTime) {
    //当前时间
    let currentTime = initialTime;
    //取出第一个任务
    currentTask = peek(taskQueue);
    //如果任务存在
    while (currentTask) {
        //如果当前任务的过期时间大于当前时间,并且当前的时间片到期了,退出工作循环
        if (currentTask.expirationTime > currentTime && shouldYieldToHost()) {
            break;
        }
        //执行当前的工作
        const callback = currentTask.callback;
        if (typeof callback === 'function') {
            currentTask.callback = null;
            const didUserCallbackTimeout = currentTask.expirationTime <= currentTime;
            const continuationCallback = callback(didUserCallbackTimeout);
            //如果返回函数说明任务尚未结束,下次还执行它
            if (typeof continuationCallback === 'function') {
                //currentTask = continuationCallback;
                currentTask.callback = continuationCallback;
            } else {
                //否则表示此任务执行结束,可以把此任务移除队列
                pop(taskQueue);
            }
        } else {
            pop(taskQueue);
        }
        //还取第一个任务
        currentTask = peek(taskQueue);
    }
    if (currentTask !== null) {
        return true;
    } else {
        const firstTimer = peek(timerQueue);
        if (firstTimer !== null) {
            requestHostTimeout(handleTimeout, firstTimer.startTime - currentTime);
        }
        return false;
    }
}

export function cancelCallback(task) {
    //清空回调以表示任务已取消
    //无法从队列中删除,因为无法从基于数组的堆中删除任意节点,只能删除第一个节点
    task.callback = null;
}
+/**
+ * 
+ * @param {*} priorityLevel 
+ * @param {*} eventHandler 
+ * @returns 
+ */
+function runWithPriority(priorityLevel, eventHandler) {
+    switch (priorityLevel) {
+        case ImmediatePriority:
+        case UserBlockingPriority:
+        case NormalPriority:
+        case LowPriority:
+        case IdlePriority:
+            break;
+        default:
+            priorityLevel = NormalPriority;
+    }
+    var previousPriorityLevel = currentPriorityLevel;
+    currentPriorityLevel = priorityLevel;
+    try {
+        return eventHandler();
+    } finally {
+        currentPriorityLevel = previousPriorityLevel;
+    }
+}
export function getCurrentPriorityLevel() {
    return currentPriorityLevel;
}
export {
    shouldYieldToHost as shouldYield,
    ImmediatePriority,
    UserBlockingPriority,
    NormalPriority,
    IdlePriority,
    LowPriority,
    getCurrentTime as now,
+   runWithPriority
}

9.3 ReactFiberLane.js #

src\react-reconciler\ReactFiberLane.js

import { ImmediatePriority as ImmediateSchedulerPriority, UserBlockingPriority as UserBlockingSchedulerPriority, NormalPriority as NormalSchedulerPriority, LowPriority as LowSchedulerPriority, IdlePriority as IdleSchedulerPriority, NoPriority as NoSchedulerPriority } from './SchedulerWithReactIntegration';
+export const SyncLanePriority = 15;
+export const InputDiscreteLanePriority = 12;
+export const InputContinuousLanePriority = 10;
export const DefaultLanePriority = 8;
+export const TransitionPriority = 6;
+export const IdleLanePriority = 2;
+export const NoLanePriority = 0;

+//lane使用31位二进制来表示优先级车道共31条, 位数越小(1的位置越靠右)表示优先级越高
+const TotalLanes = 31;
+//没有优先级
export const NoLanes = 0b0000000000000000000000000000000;
+//同步优先级,表示同步的任务一次只能执行一个,例如:用户的交互事件产生的更新任务
export const SyncLane = 0b0000000000000000000000000000001;
+// 连续触发优先级,例如:滚动事件,拖动事件等
+export const InputContinuousHydrationLane = 0b0000000000000000000000000000010;
+export const InputContinuousLane = 0b0000000000000000000000000000100;
+// 默认优先级,例如使用setTimeout,请求数据返回等造成的更新
+export const DefaultLanes = 0b0000000000000000000111000000000;
+const IdleLanes = 0b0110000000000000000000000000000;
+export const NoTimestamp = -1;
+let return_highestLanePriority = DefaultLanePriority;

/**
 * 把 a 和 b合并成一个Lanes(优先级分组)
 * 生成一个新的优先级范围
 */
export function mergeLanes(a, b) {
    return a | b;
}

export function markRootUpdated(root, updateLane) {
    //将本次更新的lane放入root的pendingLanes
    root.pendingLanes |= updateLane;
}
+/**
+ * 获取优先级最高的任务的优先级
+ * @param {*} root 
+ * @param {*} wipLanes 
+ * @returns 
+ */
+export function getNextLanes(root, wipLanes) {
+    // 该函数从root.pendingLanes中找出优先级最高的lane
+    const pendingLanes = root.pendingLanes;
+    let nextLanes = NoLanes;
+    let nextLanePriority = NoLanePriority;
+    const expiredLanes = root.expiredLanes;
+    if (expiredLanes !== NoLanes) {
+        nextLanes = expiredLanes;
+        nextLanePriority = return_highestLanePriority = SyncLanePriority;
+    } else {
+        nextLanes = getHighestPriorityLanes(pendingLanes);
+        nextLanePriority = return_highestLanePriority;
+    }
+    if (nextLanes === NoLanes) {
+        return NoLanes;
+    }
+    nextLanes = pendingLanes & getEqualOrHigherPriorityLanes(nextLanes);
+    return nextLanes;
+}
+/**
+ * 找到对应优先级范围内优先级最高的那一批lanes
+ * @param {*} lanes 
+ */
+function getHighestPriorityLanes(lanes) {
+    if ((SyncLane & lanes) !== NoLanes) {
+        return SyncLane;
+    }
+    const defaultLanes = DefaultLanes & lanes;
+    if (defaultLanes !== NoLanes) {
+        return_highestLanePriority = DefaultLanePriority;
+        return defaultLanes;
+    }
+
+    const idleLanes = IdleLanes & lanes;
+
+    if (idleLanes !== NoLanes) {
+        return_highestLanePriority = IdleLanePriority;
+        return idleLanes;
+    }
+    return_highestLanePriority = DefaultLanePriority;
+    return lanes;
+}
+function getHighestPriorityLane(lanes) {
+    return lanes & -lanes;
+}
+
+function getEqualOrHigherPriorityLanes(lanes) {
+    return (getLowestPriorityLane(lanes) << 1) - 1;
+}
+/**
+ * 找到lanes中优先级最低的那一个lane
+ * @param {*} lanes 
+ * @returns 
+ */
+function getLowestPriorityLane(lanes) {
+    const index = 31 - Math.clz32(lanes);
+    return index < 0 ? NoLanes : 1 << index;
+}
+
+export function includesSomeLane(a, b) {
+    return (a & b) !== NoLanes;
+}
+
+export function schedulerPriorityToLanePriority(schedulerPriorityLevel) {
+    switch (schedulerPriorityLevel) {
+        case ImmediateSchedulerPriority:
+            return SyncLanePriority;
+        case UserBlockingSchedulerPriority:
+            return InputContinuousLanePriority;
+        case NormalSchedulerPriority:
+        case LowSchedulerPriority:
+            return DefaultLanePriority;
+        case IdleSchedulerPriority:
+            return IdleLanePriority;
+        default:
+            return NoLanePriority;
+    }
+}
+
+export function findUpdateLane(lanePriority, wipLanes) {
+    switch (lanePriority) {
+        case DefaultLanePriority:
+            {
+                let lane = pickArbitraryLane(DefaultLanes & ~wipLanes);//512
+                return lane;
+            }
+        default:
+            break;
+    }
+}
+export function pickArbitraryLane(lanes) {
+    return getHighestPriorityLane(lanes);
+}
+
+export function markStarvedLanesAsExpired(root, currentTime) {
+    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;
+    }
+}
+function pickArbitraryLaneIndex(lanes) {
+    return 31 - Math.clz32(lanes);
+}
+
+function computeExpirationTime(lane, currentTime) {
+    getHighestPriorityLanes(lane);
+    const priority = return_highestLanePriority;
+    if (priority >= InputContinuousLanePriority) {
+        return currentTime + 250;
+    } else if (priority >= TransitionPriority) {
+        return currentTime + 5000;
+    } else {
+        return NoTimestamp;
+    }
+}
+export function lanePriorityToSchedulerPriority(lanePriority) {
+    switch (lanePriority) {
+        case SyncLanePriority:
+            return ImmediateSchedulerPriority;
+        case InputDiscreteLanePriority:
+        case InputContinuousLanePriority:
+            return UserBlockingSchedulerPriority;
+        case DefaultLanePriority:
+            return NormalSchedulerPriority;
+        case IdleLanePriority:
+            return IdleSchedulerPriority;
+        case NoLanePriority:
+            return NoSchedulerPriority;
+        default:
+            break;
+    }
+}
+
+export function returnNextLanesPriority() {
+    return return_highestLanePriority;
+}
+
+export function createLaneMap(initial) {
+    const laneMap = [];
+    for (let i = 0; i < TotalLanes; i++) {
+        laneMap.push(initial);
+    }
+    return laneMap;
+}
+export function markRootFinished(root, remainingLanes) {
+    //从pendingLanes中删除还未执行的lanes,那么就找到了已经执行过的lanes
+    const noLongerPendingLanes = root.pendingLanes & ~remainingLanes;
+    // 将剩下的lanes重新挂载到pendingLanes上,准备下一次的执行
+    root.pendingLanes = remainingLanes;
+    // 从expiredLanes中删除掉已经执行的lanes
+    root.expiredLanes &= remainingLanes;
+    const expirationTimes = root.expirationTimes;
+    const eventTimes = root.eventTimes;
+    let lanes = noLongerPendingLanes;
+    //取出已经执行的lane,清空它们所有的数据
+    //eventTimes中的事件触发时间,expirationTimes中的任务过期时间等
+    while (lanes > 0) {
+        const index = pickArbitraryLaneIndex(lanes);
+        const lane = 1 << index;
+        eventTimes[index] = NoTimestamp;
+        expirationTimes[index] = NoTimestamp;
+        lanes &= ~lane;
+    }
+}

9.4 ReactFiberRoot.js #

src\react-reconciler\ReactFiberRoot.js

import { createHostRootFiber } from './ReactFiber';
import { initializeUpdateQueue } from './ReactUpdateQueue';
+import { NoTimestamp, createLaneMap, NoLanes } from './ReactFiberLane';
export function createFiberRoot(containerInfo) {
    const root = new FiberRootNode(containerInfo);
    const hostRootFiber = createHostRootFiber();
    root.current = hostRootFiber;
    hostRootFiber.stateNode = root;
    initializeUpdateQueue(hostRootFiber);
    return root;
}
function FiberRootNode(containerInfo) {
    this.containerInfo = containerInfo;
+   this.eventTimes = createLaneMap(NoLanes);
+   this.expirationTimes = createLaneMap(NoTimestamp);
}

9.5 ReactTypes.js #

src\react\shared\ReactTypes.js

export const DiscreteEvent = 0;
export const UserBlockingEvent = 1;
export const ContinuousEvent = 2;

9.6 ReactDOMEventListener.js #

src\react-dom\ReactDOMEventListener.js

import * as Scheduler from 'scheduler';
const { UserBlockingPriority, runWithPriority } = Scheduler;
export function createEventListenerWrapperWithPriority(targetContainer, domEventName, eventSystemFlags) {
    const eventPriority = getEventPriorityForPluginSystem(domEventName);
    let listenerWrapper;
    switch (eventPriority) {
        case DiscreteEvent:
            listenerWrapper = dispatchDiscreteEvent;
            break;
        case UserBlockingEvent:
            listenerWrapper = dispatchUserBlockingUpdate;
            break;
        case ContinuousEvent:
        default:
            listenerWrapper = dispatchEvent;
            break;
    }
    return listenerWrapper.bind(null, domEventName, eventSystemFlags, targetContainer);
}
function dispatchUserBlockingUpdate(domEventName, eventSystemFlags, container, nativeEvent) {
    runWithPriority(UserBlockingPriority, dispatchEvent.bind(null, domEventName, eventSystemFlags, container, nativeEvent));
}

9.7 ReactDOMEventListener.js #

src\react-dom\ReactDOMEventListener.js

import * as Scheduler from 'scheduler';
const { UserBlockingPriority, runWithPriority } = Scheduler;
export function createEventListenerWrapperWithPriority(targetContainer, domEventName, eventSystemFlags) {
    const eventPriority = getEventPriorityForPluginSystem(domEventName);
    let listenerWrapper;
    switch (eventPriority) {
        case DiscreteEvent:
            listenerWrapper = dispatchDiscreteEvent;
            break;
        case UserBlockingEvent:
            listenerWrapper = dispatchUserBlockingUpdate;
            break;
        case ContinuousEvent:
        default:
            listenerWrapper = dispatchEvent;
            break;
    }
    return listenerWrapper.bind(null, domEventName, eventSystemFlags, targetContainer);
}
function dispatchUserBlockingUpdate(domEventName, eventSystemFlags, container, nativeEvent) {
    runWithPriority(UserBlockingPriority, dispatchEvent.bind(null, domEventName, eventSystemFlags, container, nativeEvent));
}

9.8 SchedulerWithReactIntegration.js #

src\react-reconciler\SchedulerWithReactIntegration.js

import * as Scheduler from '../scheduler';
const {
    getCurrentPriorityLevel: Scheduler_getCurrentPriorityLevel,
    ImmediatePriority: Scheduler_ImmediatePriority,
    UserBlockingPriority: Scheduler_UserBlockingPriority,
    NormalPriority: Scheduler_NormalPriority,
    LowPriority: Scheduler_LowPriority,
    IdlePriority: Scheduler_IdlePriority,
    scheduleCallback: Scheduler_scheduleCallback,
    shouldYield: Scheduler_shouldYield,
    runWithPriority: Scheduler_runWithPriority
} = Scheduler;

export const ImmediatePriority = 99;
export const UserBlockingPriority = 98;
export const NormalPriority = 97;
export const LowPriority = 96;
export const IdlePriority = 95;
export const NoPriority = 90;

export function getCurrentPriorityLevel() {
    switch (Scheduler_getCurrentPriorityLevel()) {
        case Scheduler_ImmediatePriority:
            return ImmediatePriority;
        case Scheduler_UserBlockingPriority:
            return UserBlockingPriority;
        case Scheduler_NormalPriority:
            return NormalPriority;
        case Scheduler_LowPriority:
            return LowPriority;
        case Scheduler_IdlePriority:
            return IdlePriority;
        default:
            break;
    }
}

export function scheduleCallback(reactPriorityLevel, callback, options) {
    const priorityLevel = reactPriorityToSchedulerPriority(reactPriorityLevel);
    return Scheduler_scheduleCallback(priorityLevel, callback, options);
}

function reactPriorityToSchedulerPriority(reactPriorityLevel) {
    switch (reactPriorityLevel) {
        case ImmediatePriority:
            return Scheduler_ImmediatePriority;
        case UserBlockingPriority:
            return Scheduler_UserBlockingPriority;
        case NormalPriority:
            return Scheduler_NormalPriority;
        case LowPriority:
            return Scheduler_LowPriority;
        case IdlePriority:
            return Scheduler_IdlePriority;
        default:
            break;
    }
}
export function runWithPriority(reactPriorityLevel, fn) {
    const priorityLevel = reactPriorityToSchedulerPriority(reactPriorityLevel);
    return Scheduler_runWithPriority(priorityLevel, fn);
}
export const shouldYield = Scheduler_shouldYield;

访问验证

请输入访问令牌

Token不正确,请重新输入