diff --git a/android/src/main/java/com/facebook/flipper/plugins/sections/ChangesetDebug.java b/android/src/main/java/com/facebook/flipper/plugins/sections/ChangesetDebug.java new file mode 100644 index 000000000..ed4f1792c --- /dev/null +++ b/android/src/main/java/com/facebook/flipper/plugins/sections/ChangesetDebug.java @@ -0,0 +1,77 @@ +/* + * Copyright (c) Facebook, Inc. and its affiliates. + * + * This source code is licensed under the MIT license found in the LICENSE + * file in the root directory of this source tree. + */ +package com.facebook.flipper.plugins.sections; + +import com.facebook.flipper.core.FlipperArray; +import com.facebook.flipper.core.FlipperObject; +import com.facebook.litho.sections.ChangesInfo; +import com.facebook.litho.sections.ChangesetDebugConfiguration; +import com.facebook.litho.sections.ChangesetDebugConfiguration.ChangesetDebugListener; +import com.facebook.litho.sections.Section; +import com.facebook.litho.sections.SectionsLogEventUtils; +import com.facebook.litho.sections.SectionsLogEventUtils.ApplyNewChangeSet; +import java.util.concurrent.atomic.AtomicInteger; + +public class ChangesetDebug implements ChangesetDebugListener { + + private static ChangesetListener sSectionsFlipperPlugin; + private static ChangesetDebug sInstance; + private static AtomicInteger sChangesetIdGenerator = new AtomicInteger(); + + public interface ChangesetListener { + void onChangesetApplied( + String name, + boolean isAsync, + String surfaceId, + String id, + FlipperArray tree, + FlipperObject changesetData); + } + + public static void setListener(ChangesetListener listener) { + if (sInstance == null) { + sInstance = new ChangesetDebug(listener); + ChangesetDebugConfiguration.setListener(sInstance); + } + } + + private ChangesetDebug(ChangesetListener listener) { + sSectionsFlipperPlugin = listener; + } + + @Override + public void onChangesetApplied( + Section rootSection, + Section oldSection, + ChangesInfo changesInfo, + String surfaceId, + @ApplyNewChangeSet int attribution, + String extra) { + final FlipperArray.Builder tree = new FlipperArray.Builder(); + final FlipperObject.Builder changesetData = new FlipperObject.Builder(); + + final String sourceName = SectionsLogEventUtils.applyNewChangeSetSourceToString(attribution); + + sSectionsFlipperPlugin.onChangesetApplied( + sourceName + " " + extra, + isEventAsync(attribution), + surfaceId, + sChangesetIdGenerator.incrementAndGet() + "-" + surfaceId, + tree.build(), + changesetData.build()); + } + + private static boolean isEventAsync(@ApplyNewChangeSet int source) { + switch (source) { + case ApplyNewChangeSet.SET_ROOT_ASYNC: + case ApplyNewChangeSet.UPDATE_STATE_ASYNC: + return true; + default: + return false; + } + } +} diff --git a/android/src/main/java/com/facebook/flipper/plugins/sections/SectionsFlipperPlugin.java b/android/src/main/java/com/facebook/flipper/plugins/sections/SectionsFlipperPlugin.java index 17c01666b..130814759 100644 --- a/android/src/main/java/com/facebook/flipper/plugins/sections/SectionsFlipperPlugin.java +++ b/android/src/main/java/com/facebook/flipper/plugins/sections/SectionsFlipperPlugin.java @@ -6,10 +6,13 @@ */ package com.facebook.flipper.plugins.sections; +import com.facebook.flipper.core.FlipperArray; import com.facebook.flipper.core.FlipperConnection; +import com.facebook.flipper.core.FlipperObject; import com.facebook.flipper.core.FlipperPlugin; +import com.facebook.flipper.plugins.sections.ChangesetDebug.ChangesetListener; -public class SectionsFlipperPlugin implements FlipperPlugin { +public class SectionsFlipperPlugin implements FlipperPlugin, ChangesetListener { private FlipperConnection mConnection; @@ -21,6 +24,7 @@ public class SectionsFlipperPlugin implements FlipperPlugin { @Override public void onConnect(FlipperConnection connection) throws Exception { mConnection = connection; + ChangesetDebug.setListener(this); } @Override @@ -30,4 +34,70 @@ public class SectionsFlipperPlugin implements FlipperPlugin { public boolean runInBackground() { return false; } + + /** + * @param name Name of event + * @param isAsync Whether the event was sync or async + * @param surfaceId SectionTree tag + * @param id Changeset generation unique id + * @param tree Representation of the SectionTree hierarchy + * @param changesetData Changeset information + */ + @Override + public void onChangesetApplied( + String name, + boolean isAsync, + String surfaceId, + String id, + FlipperArray tree, + FlipperObject changesetData) { + if (mConnection == null) { + return; + } + mConnection.send( + "addEvent", + new FlipperObject.Builder() + .put("id", id) + .put("update_mode", isAsync ? 0 : 1) + .put("reason", name) + .put("surface_key", surfaceId) + .put("tree_generation_timestamp", 10000) // TODO + .put("stack_trace", new FlipperArray.Builder().build()) + .put("payload", new FlipperObject.Builder().build()) + .build()); + + mConnection.send( + "updateTreeGenerationHierarchyGeneration", + new FlipperObject.Builder() + .put("id", id) + .put("hierarchy_generation_timestamp", 10000) // TODO + .put("hierarchy_generation_duration", 0) // TODO + .put("tree", tree) + .put("reason", name) + .build()); + + // Not sure both CHANGESET_GENERATED and CHANGESET_APPLIED need to sent here, need + // to investigate a bit more. + mConnection.send( + "updateTreeGenerationChangesetGeneration", + new FlipperObject.Builder() + .put("type", "CHANGESET_GENERATED") + .put("identifier", id) + .put("tree_generation_id", "" + id) + .put("timestamp", 10000) // TODO + .put("duration", 0) // TODO + .put("changeset", changesetData) + .build()); + + mConnection.send( + "updateTreeGenerationChangesetApplication", + new FlipperObject.Builder() + .put("type", "CHANGESET_APPLIED") + .put("identifier", id) + .put("tree_generation_id", id) + .put("timestamp", 10000) // TODO + .put("duration", 0) // TODO + .put("changeset", changesetData) + .build()); + } }