Teact: Some improvements

This commit is contained in:
Alexander Zinchuk 2025-03-07 15:16:48 +01:00
parent 90e56dcdd4
commit cbd0d47aa5
4 changed files with 37 additions and 20 deletions

View File

@ -3,7 +3,7 @@ import { DEBUG } from '../../config';
type Handler = (e: Event) => void;
type DelegationRegistry = Map<Element, Handler>;
const NON_BUBBLEABLE_EVENTS = new Set(['scroll', 'mouseenter', 'mouseleave', 'load']);
const NON_BUBBLEABLE_EVENTS = new Set(['scroll', 'mouseenter', 'mouseleave', 'load', 'error']);
const documentEventCounters: Record<string, number> = {};
const delegationRegistryByEventType: Record<string, DelegationRegistry> = {};

View File

@ -46,8 +46,11 @@ const FILTERED_ATTRIBUTES = new Set(['key', 'ref', 'teactFastList', 'teactOrderK
const HTML_ATTRIBUTES = new Set(['dir', 'role', 'form']);
const CONTROLLABLE_TAGS = ['INPUT', 'TEXTAREA', 'SELECT'];
const MAPPED_ATTRIBUTES: { [k: string]: string } = {
autoPlay: 'autoplay',
autoCapitalize: 'autocapitalize',
autoComplete: 'autocomplete',
autoCorrect: 'autocorrect',
autoPlay: 'autoplay',
spellCheck: 'spellcheck',
};
const INDEX_KEY_PREFIX = '__indexKey#';

View File

@ -451,11 +451,13 @@ export function renderComponent(componentInstance: ComponentInstance) {
incrementOverlayCounter(`${componentName} duration`, duration);
}
}
}, () => {
// eslint-disable-next-line no-console
console.error(`[Teact] Error while rendering component ${componentInstance.name}`, componentInstance);
}, {
rescue: () => {
// eslint-disable-next-line no-console
console.error(`[Teact] Error while rendering component ${componentInstance.name}`, componentInstance);
newRenderedValue = componentInstance.renderedValue;
newRenderedValue = componentInstance.renderedValue;
},
});
if (componentInstance.mountState === MountState.Mounted && newRenderedValue === componentInstance.renderedValue) {
@ -766,11 +768,15 @@ function scheduleEffect(
);
}
}
}, () => {
// eslint-disable-next-line no-console, max-len
console.error(`[Teact] Error in effect cleanup at cursor #${cursor} in ${componentInstance.name}`, componentInstance);
}, () => {
byCursor[cursor].cleanup = undefined;
}, {
rescue: () => {
// eslint-disable-next-line no-console, max-len
console.error(`[Teact] Error in effect cleanup at cursor #${cursor} in ${componentInstance.name}`,
componentInstance);
},
always: () => {
byCursor[cursor].cleanup = undefined;
},
});
cleanupsContainer.set(effectId, runEffectCleanup);
@ -800,9 +806,11 @@ function scheduleEffect(
console.warn(`[Teact] Slow effect at cursor #${cursor}: ${componentName}, ${Math.round(duration)} ms`);
}
}
}, () => {
// eslint-disable-next-line no-console
console.error(`[Teact] Error in effect at cursor #${cursor} in ${componentInstance.name}`, componentInstance);
}, {
rescue: () => {
// eslint-disable-next-line no-console
console.error(`[Teact] Error in effect at cursor #${cursor} in ${componentInstance.name}`, componentInstance);
},
});
effectsContainer.set(effectId, runEffect);

View File

@ -3,20 +3,26 @@ import { handleError } from './handleError';
const SAFE_EXEC_ENABLED = !DEBUG_MORE;
export default function safeExec<F extends AnyFunction>(
cb: F,
rescue?: (err: Error) => void,
always?: NoneToVoidFunction,
): ReturnType<F> | undefined {
type SafeExecOptions = {
rescue?: (err: Error) => void;
always?: NoneToVoidFunction;
shouldIgnoreError?: boolean;
};
export default function safeExec<T extends AnyFunction>(cb: T, options?: SafeExecOptions): ReturnType<T> | undefined {
if (!SAFE_EXEC_ENABLED) {
return cb();
}
const { rescue, always, shouldIgnoreError } = options ?? {};
try {
return cb();
} catch (err: any) {
rescue?.(err);
handleError(err);
if (!shouldIgnoreError) {
handleError(err);
}
return undefined;
} finally {
always?.();