From 4712e8a621f3178f30b70a3d90eee6f3d7dbe6d1 Mon Sep 17 00:00:00 2001 From: * <*> Date: Tue, 10 Jan 2023 19:55:01 +0800 Subject: [PATCH 1/4] Match-id-8c8edc92392dcae4fa155fe80ecae735f6f4d0fc --- libs/horizon/src/renderer/components/BaseClassComponent.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libs/horizon/src/renderer/components/BaseClassComponent.ts b/libs/horizon/src/renderer/components/BaseClassComponent.ts index fff07c8d..8a931e04 100644 --- a/libs/horizon/src/renderer/components/BaseClassComponent.ts +++ b/libs/horizon/src/renderer/components/BaseClassComponent.ts @@ -29,7 +29,7 @@ class Component { this.context = context; } - setState(state: S) { + setState(state: S, callback?: any) { if (isDev) { console.error('Cant not call `this.setState` in the constructor of class component, it will do nothing'); } From 1c3c678883fa3aeb5afb8c987429bc9151326a5e Mon Sep 17 00:00:00 2001 From: * <*> Date: Tue, 10 Jan 2023 20:09:54 +0800 Subject: [PATCH 2/4] Match-id-9952e9ebc64f02cfbab95841eee1b1788348e9f6 --- libs/horizon/src/renderer/Types.ts | 2 ++ libs/horizon/src/renderer/UpdateHandler.ts | 4 +--- libs/horizon/src/renderer/components/BaseClassComponent.ts | 4 +++- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/libs/horizon/src/renderer/Types.ts b/libs/horizon/src/renderer/Types.ts index 1503fee9..91b6bfbd 100644 --- a/libs/horizon/src/renderer/Types.ts +++ b/libs/horizon/src/renderer/Types.ts @@ -77,3 +77,5 @@ export type Source = { fileName: string; lineNumber: number; }; + +export type Callback = () => void; diff --git a/libs/horizon/src/renderer/UpdateHandler.ts b/libs/horizon/src/renderer/UpdateHandler.ts index c906bd1c..5fd17bb1 100644 --- a/libs/horizon/src/renderer/UpdateHandler.ts +++ b/libs/horizon/src/renderer/UpdateHandler.ts @@ -13,7 +13,7 @@ * See the Mulan PSL v2 for more details. */ -import type { VNode } from './Types'; +import type { VNode, Callback } from './Types'; import { FlagUtils, ShouldCapture } from './vnode/VNodeFlags'; export type Update = { @@ -22,8 +22,6 @@ export type Update = { callback: Callback | null; }; -export type Callback = () => any; - export type Updates = Array | null; export enum UpdateState { diff --git a/libs/horizon/src/renderer/components/BaseClassComponent.ts b/libs/horizon/src/renderer/components/BaseClassComponent.ts index 8a931e04..de4ff351 100644 --- a/libs/horizon/src/renderer/components/BaseClassComponent.ts +++ b/libs/horizon/src/renderer/components/BaseClassComponent.ts @@ -13,6 +13,8 @@ * See the Mulan PSL v2 for more details. */ +import {Callback} from '../Types'; + /** * Component的api setState和forceUpdate在实例生成阶段实现 */ @@ -29,7 +31,7 @@ class Component { this.context = context; } - setState(state: S, callback?: any) { + setState(state: S, callback?: Callback) { if (isDev) { console.error('Cant not call `this.setState` in the constructor of class component, it will do nothing'); } From cadaabfd3b863bb76438f2a980548ee115ab78d1 Mon Sep 17 00:00:00 2001 From: * <*> Date: Thu, 12 Jan 2023 17:48:28 +0800 Subject: [PATCH 3/4] Match-id-1f58ff819dfbd01d0b80fd9bfaa198bcb73f0b50 --- .../src/renderer/taskExecutor/BrowserAsync.ts | 33 ++++++++++++++----- .../src/renderer/taskExecutor/TaskExecutor.ts | 7 +--- 2 files changed, 25 insertions(+), 15 deletions(-) diff --git a/libs/horizon/src/renderer/taskExecutor/BrowserAsync.ts b/libs/horizon/src/renderer/taskExecutor/BrowserAsync.ts index 28e17a84..2aeead9f 100644 --- a/libs/horizon/src/renderer/taskExecutor/BrowserAsync.ts +++ b/libs/horizon/src/renderer/taskExecutor/BrowserAsync.ts @@ -19,11 +19,9 @@ let isMessageLoopRunning = false; let browserCallback = null; -const { port1, port2 } = new MessageChannel(); - -export function isOverTime() { - return false; -} +let port1 = null; +let port2 = null; +let isTestRuntime = false; // 1、设置deadline;2、回调TaskExecutor传过来的browserCallback const callRenderTasks = () => { @@ -41,21 +39,38 @@ const callRenderTasks = () => { browserCallback = null; } else { // 还有task,继续调用 - port2.postMessage(null); + asyncCall(); } } catch (error) { - port2.postMessage(null); + asyncCall(); throw error; } }; -port1.onmessage = callRenderTasks; +if (typeof MessageChannel === 'function') { + const mc = new MessageChannel(); + port1 = mc.port1; + port1.onmessage = callRenderTasks; + port2 = mc.port2; +} else { + // 测试环境没有 MessageChannel + isTestRuntime = true; +} + +function asyncCall() { + if (isTestRuntime) { + setTimeout(callRenderTasks, 0); + } else { + port2.postMessage(null); + } +} export function requestBrowserCallback(callback) { browserCallback = callback; if (!isMessageLoopRunning) { isMessageLoopRunning = true; - port2.postMessage(null); + asyncCall(); } } + diff --git a/libs/horizon/src/renderer/taskExecutor/TaskExecutor.ts b/libs/horizon/src/renderer/taskExecutor/TaskExecutor.ts index 8613cfca..ac5ce9b3 100644 --- a/libs/horizon/src/renderer/taskExecutor/TaskExecutor.ts +++ b/libs/horizon/src/renderer/taskExecutor/TaskExecutor.ts @@ -18,7 +18,7 @@ */ import { Node } from '../taskExecutor/TaskQueue'; -import { requestBrowserCallback, isOverTime } from './BrowserAsync'; +import { requestBrowserCallback } from './BrowserAsync'; import { add, shift, first, remove } from './TaskQueue'; @@ -44,11 +44,6 @@ function callTasks() { // 循环执行task while (task !== null) { - if (isOverTime()) { - // 超过了deadline - break; - } - const callback = task.callback; if (callback !== null) { task.callback = null; From 673a63c40daa3afa44b344fba59517e010299333 Mon Sep 17 00:00:00 2001 From: * <*> Date: Fri, 13 Jan 2023 10:38:17 +0800 Subject: [PATCH 4/4] Match-id-a6a5ea6fabf8fd82802778a00a09be2442e7a0b5 --- libs/horizon/src/renderer/taskExecutor/BrowserAsync.ts | 4 ++++ libs/horizon/src/renderer/taskExecutor/TaskExecutor.ts | 7 ++++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/libs/horizon/src/renderer/taskExecutor/BrowserAsync.ts b/libs/horizon/src/renderer/taskExecutor/BrowserAsync.ts index 2aeead9f..6da28613 100644 --- a/libs/horizon/src/renderer/taskExecutor/BrowserAsync.ts +++ b/libs/horizon/src/renderer/taskExecutor/BrowserAsync.ts @@ -23,6 +23,10 @@ let port1 = null; let port2 = null; let isTestRuntime = false; +export function isOverTime() { + return false; +} + // 1、设置deadline;2、回调TaskExecutor传过来的browserCallback const callRenderTasks = () => { if (browserCallback === null) { diff --git a/libs/horizon/src/renderer/taskExecutor/TaskExecutor.ts b/libs/horizon/src/renderer/taskExecutor/TaskExecutor.ts index ac5ce9b3..8613cfca 100644 --- a/libs/horizon/src/renderer/taskExecutor/TaskExecutor.ts +++ b/libs/horizon/src/renderer/taskExecutor/TaskExecutor.ts @@ -18,7 +18,7 @@ */ import { Node } from '../taskExecutor/TaskQueue'; -import { requestBrowserCallback } from './BrowserAsync'; +import { requestBrowserCallback, isOverTime } from './BrowserAsync'; import { add, shift, first, remove } from './TaskQueue'; @@ -44,6 +44,11 @@ function callTasks() { // 循环执行task while (task !== null) { + if (isOverTime()) { + // 超过了deadline + break; + } + const callback = task.callback; if (callback !== null) { task.callback = null;