Skip to content

Commit 1d57819

Browse files
gaearonsebmarkbage
authored andcommitted
Simplify ReactNoop fix
1 parent d232806 commit 1d57819

File tree

1 file changed

+97
-118
lines changed

1 file changed

+97
-118
lines changed

src/renderers/noop/ReactNoopEntry.js

Lines changed: 97 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import type {Fiber} from 'ReactFiber';
2121
import type {UpdateQueue} from 'ReactFiberUpdateQueue';
2222

23+
var ReactFeatureFlags = require('ReactFeatureFlags');
2324
var ReactFiberInstrumentation = require('ReactFiberInstrumentation');
2425
var ReactFiberReconciler = require('ReactFiberReconciler');
2526
var ReactInstanceMap = require('ReactInstanceMap');
@@ -178,105 +179,94 @@ var SharedHostConfig = {
178179
},
179180
};
180181

181-
var MutationHostConfig = {
182-
commitMount(instance: Instance, type: string, newProps: Props): void {
183-
// Noop
184-
},
185-
186-
commitUpdate(
187-
instance: Instance,
188-
updatePayload: Object,
189-
type: string,
190-
oldProps: Props,
191-
newProps: Props,
192-
): void {
193-
instance.prop = newProps.prop;
194-
},
195-
196-
commitTextUpdate(
197-
textInstance: TextInstance,
198-
oldText: string,
199-
newText: string,
200-
): void {
201-
textInstance.text = newText;
202-
},
203-
204-
appendChild: appendChild,
205-
appendChildToContainer: appendChild,
206-
insertBefore: insertBefore,
207-
insertInContainerBefore: insertBefore,
208-
removeChild: removeChild,
209-
removeChildFromContainer: removeChild,
210-
211-
resetTextContent(instance: Instance): void {},
212-
};
213-
214-
var PersistenceHostConfig = {
215-
cloneInstance(
216-
instance: Instance,
217-
updatePayload: null | Object,
218-
type: string,
219-
oldProps: Props,
220-
newProps: Props,
221-
internalInstanceHandle: Object,
222-
keepChildren: boolean,
223-
recyclableInstance: null | Instance,
224-
): Instance {
225-
const clone = {
226-
id: instance.id,
227-
type: type,
228-
children: keepChildren ? instance.children : [],
229-
prop: newProps.prop,
230-
};
231-
Object.defineProperty(clone, 'id', {
232-
value: clone.id,
233-
enumerable: false,
234-
});
235-
return clone;
236-
},
237-
238-
cloneContainer(
239-
container: Container,
240-
recyclableContainer: Container,
241-
): Container {
242-
return {rootID: container.rootID, children: []};
243-
},
244-
245-
appendInititalChildToContainer(
246-
parentInstance: Container,
247-
child: Instance | TextInstance,
248-
) {
249-
parentInstance.children.push(child);
250-
},
251-
252-
finalizeContainerChildren(container: Container): void {},
253-
254-
replaceContainer(oldContainer: Container, newContainer: Container): void {
255-
rootContainers.set(oldContainer.rootID, newContainer);
256-
},
257-
};
258-
259-
// They are created lazily because only one can be created per test file.
260-
var renderer = null;
261-
var persistentRenderer = null;
262-
function getRenderer() {
263-
return (
264-
renderer ||
265-
(renderer = ReactFiberReconciler({
182+
var NoopRenderer = ReactFiberReconciler({
183+
...SharedHostConfig,
184+
mutation: {
185+
commitMount(instance: Instance, type: string, newProps: Props): void {
186+
// Noop
187+
},
188+
189+
commitUpdate(
190+
instance: Instance,
191+
updatePayload: Object,
192+
type: string,
193+
oldProps: Props,
194+
newProps: Props,
195+
): void {
196+
instance.prop = newProps.prop;
197+
},
198+
199+
commitTextUpdate(
200+
textInstance: TextInstance,
201+
oldText: string,
202+
newText: string,
203+
): void {
204+
textInstance.text = newText;
205+
},
206+
207+
appendChild: appendChild,
208+
appendChildToContainer: appendChild,
209+
insertBefore: insertBefore,
210+
insertInContainerBefore: insertBefore,
211+
removeChild: removeChild,
212+
removeChildFromContainer: removeChild,
213+
214+
resetTextContent(instance: Instance): void {},
215+
},
216+
});
217+
218+
var PersistentNoopRenderer = ReactFeatureFlags.enablePersistentReconciler
219+
? ReactFiberReconciler({
266220
...SharedHostConfig,
267-
mutation: MutationHostConfig,
268-
}))
269-
);
270-
}
271-
function getPersistentRenderer() {
272-
return (
273-
persistentRenderer ||
274-
(persistentRenderer = ReactFiberReconciler({
275-
...SharedHostConfig,
276-
persistence: PersistenceHostConfig,
277-
}))
278-
);
279-
}
221+
persistence: {
222+
cloneInstance(
223+
instance: Instance,
224+
updatePayload: null | Object,
225+
type: string,
226+
oldProps: Props,
227+
newProps: Props,
228+
internalInstanceHandle: Object,
229+
keepChildren: boolean,
230+
recyclableInstance: null | Instance,
231+
): Instance {
232+
const clone = {
233+
id: instance.id,
234+
type: type,
235+
children: keepChildren ? instance.children : [],
236+
prop: newProps.prop,
237+
};
238+
Object.defineProperty(clone, 'id', {
239+
value: clone.id,
240+
enumerable: false,
241+
});
242+
return clone;
243+
},
244+
245+
cloneContainer(
246+
container: Container,
247+
recyclableContainer: Container,
248+
): Container {
249+
return {rootID: container.rootID, children: []};
250+
},
251+
252+
appendInititalChildToContainer(
253+
parentInstance: Container,
254+
child: Instance | TextInstance,
255+
) {
256+
parentInstance.children.push(child);
257+
},
258+
259+
finalizeContainerChildren(container: Container): void {},
260+
261+
replaceContainer(
262+
oldContainer: Container,
263+
newContainer: Container,
264+
): void {
265+
rootContainers.set(oldContainer.rootID, newContainer);
266+
},
267+
},
268+
})
269+
: null;
280270

281271
var rootContainers = new Map();
282272
var roots = new Map();
@@ -333,7 +323,6 @@ var ReactNoop = {
333323
rootID: string,
334324
callback: ?Function,
335325
) {
336-
const NoopRenderer = getRenderer();
337326
let root = roots.get(rootID);
338327
if (!root) {
339328
const container = {rootID: rootID, children: []};
@@ -349,7 +338,11 @@ var ReactNoop = {
349338
rootID: string,
350339
callback: ?Function,
351340
) {
352-
const PersistentNoopRenderer = getPersistentRenderer();
341+
if (PersistentNoopRenderer === null) {
342+
throw new Error(
343+
'Enable ReactFeatureFlags.enablePersistentReconciler to use it in tests.',
344+
);
345+
}
353346
let root = persistentRoots.get(rootID);
354347
if (!root) {
355348
const container = {rootID: rootID, children: []};
@@ -361,7 +354,6 @@ var ReactNoop = {
361354
},
362355

363356
unmountRootWithID(rootID: string) {
364-
const NoopRenderer = getRenderer();
365357
const root = roots.get(rootID);
366358
if (root) {
367359
NoopRenderer.updateContainer(null, root, null, () => {
@@ -374,7 +366,6 @@ var ReactNoop = {
374366
findInstance(
375367
componentOrElement: Element | ?React$Component<any, any>,
376368
): null | Instance | TextInstance {
377-
const NoopRenderer = getRenderer();
378369
if (componentOrElement == null) {
379370
return null;
380371
}
@@ -451,25 +442,13 @@ var ReactNoop = {
451442
return !!scheduledCallback;
452443
},
453444

454-
batchedUpdates(...args: Array<any>) {
455-
const NoopRenderer = getRenderer();
456-
return NoopRenderer.batchedUpdates(...args);
457-
},
445+
batchedUpdates: NoopRenderer.batchedUpdates,
458446

459-
deferredUpdates(...args: Array<any>) {
460-
const NoopRenderer = getRenderer();
461-
return NoopRenderer.deferredUpdates(...args);
462-
},
447+
deferredUpdates: NoopRenderer.deferredUpdates,
463448

464-
unbatchedUpdates(...args: Array<any>) {
465-
const NoopRenderer = getRenderer();
466-
return NoopRenderer.unbatchedUpdates(...args);
467-
},
449+
unbatchedUpdates: NoopRenderer.unbatchedUpdates,
468450

469-
flushSync(...args: Array<any>) {
470-
const NoopRenderer = getRenderer();
471-
return NoopRenderer.flushSync(...args);
472-
},
451+
flushSync: NoopRenderer.flushSync,
473452

474453
// Logs the current state of the tree.
475454
dumpTree(rootID: string = DEFAULT_ROOT_ID) {

0 commit comments

Comments
 (0)