Teact: Fix missing update

This commit is contained in:
Alexander Zinchuk 2021-05-11 23:18:02 +03:00
parent 227fd355ef
commit f26f30ccb9

View File

@ -80,7 +80,7 @@ interface ComponentInstance {
}[]; }[];
}; };
}; };
scheduleNextState?: () => void; prepareForFrame?: () => void;
forceUpdate?: () => void; forceUpdate?: () => void;
onUpdate?: () => void; onUpdate?: () => void;
} }
@ -372,8 +372,9 @@ function unmountComponent(componentInstance: ComponentInstance) {
} }
// We force cleaning as many objects as possible. Not sure this is needed at all. // We force cleaning as many objects as possible. Not sure this is needed at all.
/* eslint-disable no-null/no-null */
function helpGc(componentInstance: ComponentInstance) { function helpGc(componentInstance: ComponentInstance) {
/* eslint-disable no-null/no-null */
componentInstance.hooks.effects.byCursor.forEach((hook) => { componentInstance.hooks.effects.byCursor.forEach((hook) => {
hook.cleanup = null as any; hook.cleanup = null as any;
hook.effect = null as any; hook.effect = null as any;
@ -396,11 +397,11 @@ function helpGc(componentInstance: ComponentInstance) {
componentInstance.props = null as any; componentInstance.props = null as any;
componentInstance.forceUpdate = null as any; componentInstance.forceUpdate = null as any;
componentInstance.onUpdate = null as any; componentInstance.onUpdate = null as any;
/* eslint-enable no-null/no-null */
} }
/* eslint-enable no-null/no-null */ function prepareComponentForFrame(componentInstance: ComponentInstance) {
function applyNextState(componentInstance: ComponentInstance) {
if (!componentInstance.isMounted) { if (!componentInstance.isMounted) {
return; return;
} }
@ -408,6 +409,9 @@ function applyNextState(componentInstance: ComponentInstance) {
componentInstance.hooks.state.byCursor.forEach((hook) => { componentInstance.hooks.state.byCursor.forEach((hook) => {
hook.value = hook.nextValue; hook.value = hook.nextValue;
}); });
componentInstance.prepareForFrame = throttleWithPrimaryRaf(() => prepareComponentForFrame(componentInstance));
componentInstance.forceUpdate = throttleWithRaf(() => forceUpdateComponent(componentInstance));
} }
function forceUpdateComponent(componentInstance: ComponentInstance) { function forceUpdateComponent(componentInstance: ComponentInstance) {
@ -416,6 +420,7 @@ function forceUpdateComponent(componentInstance: ComponentInstance) {
} }
const currentElement = componentInstance.$element; const currentElement = componentInstance.$element;
renderComponent(componentInstance); renderComponent(componentInstance);
if (componentInstance.$element !== currentElement) { if (componentInstance.$element !== currentElement) {
@ -452,11 +457,18 @@ export function useState<T>(initial?: T): [T, StateHookSetter<T>] {
? (newValue as (current: T) => T)(byCursor[cursor].value) ? (newValue as (current: T) => T)(byCursor[cursor].value)
: newValue; : newValue;
if (!componentInstance.scheduleNextState || !componentInstance.forceUpdate) { if (!componentInstance.prepareForFrame || !componentInstance.forceUpdate) {
componentInstance.scheduleNextState = throttleWithPrimaryRaf(() => applyNextState(componentInstance)); componentInstance.prepareForFrame = throttleWithPrimaryRaf(
componentInstance.forceUpdate = throttleWithRaf(() => forceUpdateComponent(componentInstance)); () => prepareComponentForFrame(componentInstance),
);
componentInstance.forceUpdate = throttleWithRaf(
() => forceUpdateComponent(componentInstance),
);
} }
componentInstance.prepareForFrame();
componentInstance.forceUpdate();
if (DEBUG_MORE) { if (DEBUG_MORE) {
// eslint-disable-next-line no-console // eslint-disable-next-line no-console
console.log( console.log(
@ -470,9 +482,6 @@ export function useState<T>(initial?: T): [T, StateHookSetter<T>] {
byCursor[cursor].nextValue, byCursor[cursor].nextValue,
); );
} }
componentInstance.scheduleNextState();
componentInstance.forceUpdate();
} }
})(renderingInstance), })(renderingInstance),
}; };