Match-id-34cb287f0510c3c16a0ee5a4a7c59da3301ad491

This commit is contained in:
* 2023-01-16 20:00:54 +08:00
commit ef3ffc5bce
4 changed files with 30 additions and 9 deletions

View File

@ -77,3 +77,5 @@ export type Source = {
fileName: string;
lineNumber: number;
};
export type Callback = () => void;

View File

@ -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<Update> | null;
export enum UpdateState {

View File

@ -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<P, S, C> {
this.context = context;
}
setState(state: S) {
setState(state: S, callback?: Callback) {
if (isDev) {
console.error('Cant not call `this.setState` in the constructor of class component, it will do nothing');
}

View File

@ -19,7 +19,9 @@
let isMessageLoopRunning = false;
let browserCallback = null;
const { port1, port2 } = new MessageChannel();
let port1 = null;
let port2 = null;
let isTestRuntime = false;
export function isOverTime() {
return false;
@ -41,21 +43,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();
}
}