Skip to content

Commit 4075eb7

Browse files
astreetfacebook-github-bot
authored andcommitted
Allow ComponentsLogger implementations to return null for event types they don't care about
Summary: Quality of life improvement for when people implement loggers that don't care about certain events. Reviewed By: gmoeck Differential Revision: D15915909 fbshipit-source-id: ce500e3a75f5412aea9a1ea7808c2635cd919a85
1 parent 867119b commit 4075eb7

File tree

4 files changed

+56
-2
lines changed

4 files changed

+56
-2
lines changed

litho-core/src/main/java/com/facebook/litho/ComponentsLogger.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,11 @@ enum LogLevel {
3333
FATAL
3434
}
3535

36-
/** Create a new performance event with the given event id and start counting the time. */
36+
/**
37+
* Create a new performance event with the given event id and start counting the time. If the
38+
* logger doesn't care about this event id, it may return null.
39+
*/
40+
@Nullable
3741
PerfEvent newPerformanceEvent(ComponentContext c, @FrameworkLogEvents.LogEventId int eventId);
3842

3943
/** Write a {@link PerfEvent} to storage. This also marks the end of the event. */

litho-core/src/main/java/com/facebook/litho/LogTreePopulator.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,11 @@ private LogTreePopulator() {}
3737
@Nullable
3838
@CheckReturnValue
3939
public static PerfEvent populatePerfEventFromLogger(
40-
ComponentContext c, ComponentsLogger logger, PerfEvent perfEvent) {
40+
ComponentContext c, ComponentsLogger logger, @Nullable PerfEvent perfEvent) {
41+
if (perfEvent == null) {
42+
return null;
43+
}
44+
4145
final String logTag = c.getLogTag();
4246
if (logTag == null) {
4347
logger.cancelPerfEvent(perfEvent);

litho-it/src/test/java/com/facebook/litho/LayoutStateCalculateTest.java

+32
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
import android.graphics.Rect;
6060
import android.util.SparseArray;
6161
import android.view.accessibility.AccessibilityManager;
62+
import androidx.annotation.Nullable;
6263
import com.facebook.litho.config.ComponentsConfiguration;
6364
import com.facebook.litho.testing.TestComponent;
6465
import com.facebook.litho.testing.TestDrawableComponent;
@@ -67,6 +68,7 @@
6768
import com.facebook.litho.testing.TestSizeDependentComponent;
6869
import com.facebook.litho.testing.TestViewComponent;
6970
import com.facebook.litho.testing.Whitebox;
71+
import com.facebook.litho.testing.logging.TestComponentsLogger;
7072
import com.facebook.litho.testing.testrunner.ComponentsTestRunner;
7173
import com.facebook.litho.testing.util.InlineLayoutSpec;
7274
import com.facebook.litho.widget.Text;
@@ -2765,6 +2767,36 @@ protected Component onCreateLayout(final ComponentContext c) {
27652767
ComponentsConfiguration.createPhantomLayoutOutputsForTransitions = false;
27662768
}
27672769

2770+
@Test
2771+
public void testComponentsLoggerCanReturnNullPerfEventsDuringLayout() {
2772+
final Component component =
2773+
new InlineLayoutSpec() {
2774+
@Override
2775+
protected Component onCreateLayout(final ComponentContext c) {
2776+
return create(c).child(TestDrawableComponent.create(c)).wrapInView().build();
2777+
}
2778+
};
2779+
2780+
final ComponentsLogger logger =
2781+
new TestComponentsLogger() {
2782+
@Override
2783+
public @Nullable PerfEvent newPerformanceEvent(ComponentContext c, int eventId) {
2784+
return null;
2785+
}
2786+
};
2787+
2788+
final LayoutState layoutState =
2789+
LayoutState.calculate(
2790+
new ComponentContext(application, "test", logger),
2791+
component,
2792+
-1,
2793+
makeSizeSpec(100, EXACTLY),
2794+
makeSizeSpec(100, EXACTLY),
2795+
LayoutState.CalculateLayoutSource.TEST);
2796+
2797+
assertThat(layoutState.getMountableOutputCount()).isEqualTo(2);
2798+
}
2799+
27682800
private void enableAccessibility() {
27692801
final ShadowAccessibilityManager manager = Shadows.shadowOf(
27702802
(AccessibilityManager)

litho-it/src/test/java/com/facebook/litho/LogTreePopulatorTest.java

+14
Original file line numberDiff line numberDiff line change
@@ -150,5 +150,19 @@ public Map<String, String> getExtraAnnotations(TreeProps treeProps) {
150150
assertThat(res).isEqualTo("my_key:1337:other_key:value:");
151151
}
152152

153+
@Test
154+
public void testSkipNullPerfEvent() {
155+
final ComponentsLogger logger =
156+
new TestComponentsLogger() {
157+
@Nullable
158+
@Override
159+
public Map<String, String> getExtraAnnotations(TreeProps treeProps) {
160+
return null;
161+
}
162+
};
163+
164+
assertThat(LogTreePopulator.populatePerfEventFromLogger(mContext, logger, null)).isNull();
165+
}
166+
153167
private static class MyKey {}
154168
}

0 commit comments

Comments
 (0)