Skip to content

Commit

Permalink
createHiddenTextInstance -> cloneHiddenTextInstance
Browse files Browse the repository at this point in the history
This sidesteps the problem where createHiddenTextInstance needs access
to the host context.
  • Loading branch information
acdlite committed Mar 5, 2019
1 parent f333ed8 commit 37fd3fb
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 129 deletions.
13 changes: 10 additions & 3 deletions packages/react-native-renderer/src/ReactFabricHostConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -418,10 +418,17 @@ export function cloneUnhiddenInstance(
};
}

export function createHiddenTextInstance(
export function cloneHiddenTextInstance(
instance: Instance,
text: string,
internalInstanceHandle: Object,
): TextInstance {
throw new Error('Not yet implemented.');
}

export function cloneUnhiddenTextInstance(
instance: Instance,
text: string,
rootContainerInstance: Container,
hostContext: HostContext,
internalInstanceHandle: Object,
): TextInstance {
throw new Error('Not yet implemented.');
Expand Down
46 changes: 34 additions & 12 deletions packages/react-noop-renderer/src/createReactNoop.js
Original file line number Diff line number Diff line change
Expand Up @@ -510,32 +510,54 @@ function createReactNoop(reconciler: Function, useMutation: boolean) {
true,
null,
);
clone.hidden = props.hidden;
clone.hidden = props.hidden === true;
return clone;
},

createHiddenTextInstance(
cloneHiddenTextInstance(
instance: TextInstance,
text: string,
rootContainerInstance: Container,
hostContext: Object,
internalInstanceHandle: Object,
): TextInstance {
const inst = {
text: text,
const clone = {
text: instance.text,
id: instanceCounter++,
hidden: true,
context: hostContext,
context: instance.context,
};
// Hide from unit tests
Object.defineProperty(inst, 'id', {
value: inst.id,
Object.defineProperty(clone, 'id', {
value: clone.id,
enumerable: false,
});
Object.defineProperty(inst, 'context', {
value: inst.context,
Object.defineProperty(clone, 'context', {
value: clone.context,
enumerable: false,
});
return inst;
return clone;
},

cloneUnhiddenTextInstance(
instance: TextInstance,
text: string,
internalInstanceHandle: Object,
): TextInstance {
const clone = {
text: instance.text,
id: instanceCounter++,
hidden: false,
context: instance.context,
};
// Hide from unit tests
Object.defineProperty(clone, 'id', {
value: clone.id,
enumerable: false,
});
Object.defineProperty(clone, 'context', {
value: clone.context,
enumerable: false,
});
return clone;
},
};

Expand Down
129 changes: 17 additions & 112 deletions packages/react-reconciler/src/ReactFiberCompleteWork.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import type {
Props,
Container,
ChildSet,
HostContext,
} from './ReactFiberHostConfig';

import {
Expand Down Expand Up @@ -51,10 +50,8 @@ import {
import invariant from 'shared/invariant';

import {
getRootHostContext,
createInstance,
createTextInstance,
createHiddenTextInstance,
appendInitialChild,
finalizeInitialChildren,
prepareUpdate,
Expand All @@ -63,6 +60,8 @@ import {
cloneInstance,
cloneHiddenInstance,
cloneUnhiddenInstance,
cloneHiddenTextInstance,
cloneUnhiddenTextInstance,
createContainerChildSet,
appendChildToContainerChildSet,
finalizeContainerChildren,
Expand Down Expand Up @@ -107,8 +106,6 @@ if (supportsMutation) {
appendAllChildren = function(
parent: Instance,
workInProgress: Fiber,
rootContainerInstance: Container,
childHostContext: HostContext,
needsVisibilityToggle: boolean,
isHidden: boolean,
) {
Expand Down Expand Up @@ -141,11 +138,7 @@ if (supportsMutation) {
}
};

updateHostContainer = function(
workInProgress: Fiber,
rootContainerInstance: Container,
childHostContext: HostContext,
) {
updateHostContainer = function(workInProgress: Fiber) {
// Noop
};
updateHostComponent = function(
Expand All @@ -154,7 +147,6 @@ if (supportsMutation) {
type: Type,
newProps: Props,
rootContainerInstance: Container,
childHostContext: HostContext,
) {
// If we have an alternate, that means this is an update and we need to
// schedule a side-effect to do the updates.
Expand Down Expand Up @@ -207,8 +199,6 @@ if (supportsMutation) {
appendAllChildren = function(
parent: Instance,
workInProgress: Fiber,
rootContainerInstance: Container,
childHostContext: HostContext,
needsVisibilityToggle: boolean,
isHidden: boolean,
) {
Expand Down Expand Up @@ -239,19 +229,9 @@ if (supportsMutation) {
if (needsVisibilityToggle) {
const text = node.memoizedProps;
if (isHidden) {
instance = createHiddenTextInstance(
text,
rootContainerInstance,
childHostContext,
workInProgress,
);
instance = cloneHiddenTextInstance(instance, text, node);
} else {
instance = createTextInstance(
text,
rootContainerInstance,
childHostContext,
workInProgress,
);
instance = cloneUnhiddenTextInstance(instance, text, node);
}
node.stateNode = instance;
}
Expand All @@ -267,27 +247,13 @@ if (supportsMutation) {
if (newIsHidden) {
const primaryChildParent = node.child;
if (primaryChildParent !== null) {
appendAllChildren(
parent,
primaryChildParent,
rootContainerInstance,
childHostContext,
true,
newIsHidden,
);
appendAllChildren(parent, primaryChildParent, true, newIsHidden);
node = primaryChildParent.sibling;
continue;
}
} else {
const primaryChildParent = node;
appendAllChildren(
parent,
primaryChildParent,
rootContainerInstance,
childHostContext,
true,
newIsHidden,
);
appendAllChildren(parent, primaryChildParent, true, newIsHidden);
// eslint-disable-next-line no-labels
break branches;
}
Expand Down Expand Up @@ -323,8 +289,6 @@ if (supportsMutation) {
const appendAllChildrenToContainer = function(
containerChildSet: ChildSet,
workInProgress: Fiber,
rootContainerInstance: Container,
rootHostContext: HostContext,
needsVisibilityToggle: boolean,
isHidden: boolean,
) {
Expand Down Expand Up @@ -355,19 +319,9 @@ if (supportsMutation) {
if (needsVisibilityToggle) {
const text = node.memoizedProps;
if (isHidden) {
instance = createHiddenTextInstance(
text,
rootContainerInstance,
rootHostContext,
workInProgress,
);
instance = cloneHiddenTextInstance(instance, text, node);
} else {
instance = createTextInstance(
text,
rootContainerInstance,
rootHostContext,
workInProgress,
);
instance = cloneUnhiddenTextInstance(instance, text, node);
}
node.stateNode = instance;
}
Expand All @@ -386,8 +340,6 @@ if (supportsMutation) {
appendAllChildrenToContainer(
containerChildSet,
primaryChildParent,
rootContainerInstance,
rootHostContext,
true,
newIsHidden,
);
Expand All @@ -399,8 +351,6 @@ if (supportsMutation) {
appendAllChildrenToContainer(
containerChildSet,
primaryChildParent,
rootContainerInstance,
rootHostContext,
true,
newIsHidden,
);
Expand Down Expand Up @@ -434,11 +384,7 @@ if (supportsMutation) {
node = node.sibling;
}
};
updateHostContainer = function(
workInProgress: Fiber,
rootContainerInstance: Container,
rootHostContext: HostContext,
) {
updateHostContainer = function(workInProgress: Fiber) {
const portalOrRoot: {
containerInfo: Container,
pendingChildren: ChildSet,
Expand All @@ -451,14 +397,7 @@ if (supportsMutation) {
const container = portalOrRoot.containerInfo;
let newChildSet = createContainerChildSet(container);
// If children might have changed, we have to add them all to the set.
appendAllChildrenToContainer(
newChildSet,
workInProgress,
rootContainerInstance,
rootHostContext,
false,
false,
);
appendAllChildrenToContainer(newChildSet, workInProgress, false, false);
portalOrRoot.pendingChildren = newChildSet;
// Schedule an update on the container to swap out the container.
markUpdate(workInProgress);
Expand All @@ -471,7 +410,6 @@ if (supportsMutation) {
type: Type,
newProps: Props,
rootContainerInstance: Container,
childHostContext: HostContext,
) {
const currentInstance = current.stateNode;
const oldProps = current.memoizedProps;
Expand Down Expand Up @@ -532,14 +470,7 @@ if (supportsMutation) {
markUpdate(workInProgress);
} else {
// If children might have changed, we have to add them all to the set.
appendAllChildren(
newInstance,
workInProgress,
rootContainerInstance,
childHostContext,
false,
false,
);
appendAllChildren(newInstance, workInProgress, false, false);
}
};
updateHostText = function(
Expand All @@ -565,11 +496,7 @@ if (supportsMutation) {
};
} else {
// No host operations
updateHostContainer = function(
workInProgress: Fiber,
rootContainerInstance: Container,
hostContext: HostContext,
) {
updateHostContainer = function(workInProgress: Fiber) {
// Noop
};
updateHostComponent = function(
Expand All @@ -578,7 +505,6 @@ if (supportsMutation) {
type: Type,
newProps: Props,
rootContainerInstance: Container,
childHostContext: HostContext,
) {
// Noop
};
Expand Down Expand Up @@ -630,19 +556,12 @@ function completeWork(
// TODO: Delete this when we delete isMounted and findDOMNode.
workInProgress.effectTag &= ~Placement;
}
const rootContainerInstance = fiberRoot.containerInfo;
const rootHostContext = getRootHostContext(rootContainerInstance);
updateHostContainer(
workInProgress,
rootContainerInstance,
rootHostContext,
);
updateHostContainer(workInProgress);
break;
}
case HostComponent: {
const rootContainerInstance = getRootHostContainer();
const childHostContext = getHostContext();
popHostContext(workInProgress);
const rootContainerInstance = getRootHostContainer();
const type = workInProgress.type;
if (current !== null && workInProgress.stateNode != null) {
updateHostComponent(
Expand All @@ -651,7 +570,6 @@ function completeWork(
type,
newProps,
rootContainerInstance,
childHostContext,
);

if (current.ref !== workInProgress.ref) {
Expand Down Expand Up @@ -697,14 +615,7 @@ function completeWork(
workInProgress,
);

appendAllChildren(
instance,
workInProgress,
rootContainerInstance,
childHostContext,
false,
false,
);
appendAllChildren(instance, workInProgress, false, false);

// Certain renderers require commit-time effects for initial mount.
// (eg DOM renderer supports auto-focus for certain elements).
Expand Down Expand Up @@ -818,14 +729,8 @@ function completeWork(
case Profiler:
break;
case HostPortal:
const rootContainerInstance = getRootHostContainer();
const childHostContext = getHostContext();
popHostContainer(workInProgress);
updateHostContainer(
workInProgress,
rootContainerInstance,
childHostContext,
);
updateHostContainer(workInProgress);
break;
case ContextProvider:
// Pop provider fiber
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ export const finalizeContainerChildren =
export const replaceContainerChildren = $$$hostConfig.replaceContainerChildren;
export const cloneHiddenInstance = $$$hostConfig.cloneHiddenInstance;
export const cloneUnhiddenInstance = $$$hostConfig.cloneUnhiddenInstance;
export const createHiddenTextInstance = $$$hostConfig.createHiddenTextInstance;

// -------------------
// Hydration
Expand Down
Loading

0 comments on commit 37fd3fb

Please sign in to comment.