Visitar URL original
perf(ios): rebuild label text once per native update batch by triniwiz · Pull Request #11510 · NativeScript/NativeScript · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions packages/core/ui/text-base/index.ios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,29 @@ export class TextBase extends TextBaseCommon {
}
}

private _deferNativeText = false;
private _pendingNativeTextReset: boolean | undefined;

public onResumeNativeUpdates(): void {
// Each text setter rebuilds the native text; the initial apply needs it once.
this._deferNativeText = true;
try {
super.onResumeNativeUpdates();
} finally {
this._deferNativeText = false;
}
const reset = this._pendingNativeTextReset;
if (reset !== undefined) {
this._pendingNativeTextReset = undefined;
this._setNativeText(reset);
}
}

_setNativeText(reset = false): void {
if (this._deferNativeText) {
this._pendingNativeTextReset = reset;
return;
}
this._animationWrap(() => {
if (reset) {
const nativeView = this.nativeTextViewProtected;
Expand Down
31 changes: 31 additions & 0 deletions packages/core/ui/text-base/native-text-batch.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { Label } from '../label';

describe('iOS native text rebuild', () => {
function pendingLabel() {
const label: any = new Label();
label.text = 'hello';
label.style.letterSpacing = 1;
label.style.textDecoration = 'underline';
const rebuild = vi.spyOn(label, '_animationWrap').mockImplementation(() => {});
return { label, rebuild };
}

it('rebuilds the text once when several text setters apply together', () => {
const { label, rebuild } = pendingLabel();

label.onResumeNativeUpdates();

expect(rebuild).toHaveBeenCalledTimes(1);
});

it('rebuilds right away outside a native update batch', () => {
const { label, rebuild } = pendingLabel();
label.onResumeNativeUpdates();
rebuild.mockClear();

label._setNativeText();
label._setNativeText();

expect(rebuild).toHaveBeenCalledTimes(2);
});
});
Loading