Component makeShallowCopyWithNewId() { final Component component = makeShallowCopy(); component.mId = sIdGenerator.incrementAndGet(); return component; }
/** * If this component instance had its layout created on a different thread, we need to create a * copy to create its layout on this thread, otherwise we'll end up accessing the internal data * structures of the same instance on different threads. This can happen when the component is * passed as a prop and the same instance can be used in layout calculations on main and * background threads. https://github.com/facebook/litho/issues/360 */ Component getThreadSafeInstance() { // Needed for tests, mocks can run into this. if (mLayoutVersionGenerator == null) { return this; } final boolean shouldCreateNewInstance = mLayoutVersionGenerator.getAndSet(true); return shouldCreateNewInstance ? makeShallowCopy() : this; }
void updateStateInternal(boolean isAsync, String attribution) { final Component root; final @Nullable TreeProps rootTreeProps; synchronized (this) { if (mRoot == null) { return; } if (mIsMeasuring) { // If the layout calculation was already scheduled to happen synchronously let's just go // with a sync layout calculation. if (mScheduleLayoutAfterMeasure == SCHEDULE_LAYOUT_SYNC) { return; } mScheduleLayoutAfterMeasure = isAsync ? SCHEDULE_LAYOUT_ASYNC : SCHEDULE_LAYOUT_SYNC; return; } root = mRoot.makeShallowCopy(); rootTreeProps = TreeProps.copy(mRootTreeProps); } setRootAndSizeSpecInternal( root, SIZE_UNINITIALIZED, SIZE_UNINITIALIZED, isAsync, null /*output */, CalculateLayoutSource.UPDATE_STATE, attribution, rootTreeProps); }
@Override public void run() { // Wait for component to have a cached layout, then make copy. try { lockWaitMeasure.await(5, TimeUnit.SECONDS); } catch (InterruptedException e) { throw new IllegalStateException(); } resultCopyComponent[0] = component.makeShallowCopy(); cachedLayouts[1] = resultCopyComponent[0].getCachedLayout(); testFinished.countDown(); } });
@Override public void run() { // Wait for component to have a cached layout, then make copy. try { lockWaitMeasure.await(5, TimeUnit.SECONDS); } catch (InterruptedException e) { throw new IllegalStateException(); } resultCopyComponent[0] = component.makeShallowCopy(); resultCopyComponent[0].measure(mContext, 100, 100, new Size()); cachedLayouts[1] = resultCopyComponent[0].getCachedLayout(); testFinished.countDown(); } });
@Override Component makeShallowCopyWithNewId() { shallowCopy = (TestComponent) super.makeShallowCopy(); shallowCopy.mId = sIdGenerator.getAndIncrement(); if (getScopedContext().isNestedTreeResolutionExperimentEnabled()) { shallowCopy.setGlobalKey(getGlobalKey()); } return shallowCopy; }
@Test public void testShallowCopyCachedLayoutSameThread() { Component component = TestDrawableComponent.create(mContext).build(); component.measure(mContext, 100, 100, new Size()); assertThat(component.getCachedLayout()).isNotNull(); assertThat(component.getCachedLayout()).isNotNull(); Component copyComponent = component.makeShallowCopy(); assertThat(copyComponent.getCachedLayout()).isNotNull(); }
doReturn(componentSpy).when(componentSpy).makeShallowCopy(); heightSpec); verify(componentSpy, times(1)).makeShallowCopy();
doReturn(componentSpy).when(componentSpy).makeShallowCopy(); heightSpec); verify(componentSpy, times(1)).makeShallowCopy();
doReturn(sizeDependentComponentSpy1).when(sizeDependentComponentSpy1).makeShallowCopy(); mContext, rootContainer2, widthSpecContainer, heightSpec, prevLayoutState); verify(sizeDependentComponentSpy1).makeShallowCopy();
component = mRoot.makeShallowCopy(); treeProps = TreeProps.copy(mRootTreeProps); mForceLayout = false; layoutScheduleType = mScheduleLayoutAfterMeasure; mScheduleLayoutAfterMeasure = SCHEDULE_NONE; root = mRoot.makeShallowCopy(); rootTreeProps = TreeProps.copy(mRootTreeProps);
doReturn(sizeDependentComponentSpy).when(sizeDependentComponentSpy).makeShallowCopy(); heightSpec); verify(sizeDependentComponentSpy, times(1)).makeShallowCopy();
doReturn(sizeDependentComponentSpy).when(sizeDependentComponentSpy).makeShallowCopy(); heightSpec); verify(sizeDependentComponentSpy, times(1)).makeShallowCopy();
mPendingLayoutWidthSpec = widthSpec; mPendingLayoutHeightSpec = heightSpec; root = mRoot.makeShallowCopy();
final Component copy = root.makeShallowCopy();