From b1a7007cc5194d769610abf9de796eb152f161b1 Mon Sep 17 00:00:00 2001 From: Dan Abramov Date: Tue, 23 Apr 2019 15:42:51 +0100 Subject: [PATCH] Keep track of root insertion order in the renderer Normally, Fibers have key or index which we'll use to match things up between reloads. However, roots don't have such a concept. We'll use their insertion order as an approximation. If it's consistent, we'll be able to restore the selection. --- src/backend/renderer.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/backend/renderer.js b/src/backend/renderer.js index caf165dd8e..7275386270 100644 --- a/src/backend/renderer.js +++ b/src/backend/renderer.js @@ -539,6 +539,11 @@ export function attach( // When a mount or update is in progress, this value tracks the root that is being operated on. let currentRootID: number = -1; + // Track the order in which roots were added. + // We will use it to disambiguate roots when restoring selection between reloads. + let nextRootIndex = 0; + const rootInsertionOrder: Map = new Map(); + function getFiberID(primaryFiber: Fiber): number { if (!fiberToIDMap.has(primaryFiber)) { const id = getUID(); @@ -690,6 +695,7 @@ export function attach( const hasOwnerMetadata = fiber.hasOwnProperty('_debugOwner'); if (isRoot) { + rootInsertionOrder.set(id, nextRootIndex++); pushOperation(TREE_OPERATION_ADD); pushOperation(id); pushOperation(ElementTypeRoot); @@ -777,6 +783,7 @@ export function attach( } const id = getFiberID(primaryFiber); if (isRoot) { + rootInsertionOrder.delete(id); // Removing a root needs to happen at the end // so we don't batch it with other unmounts. pushOperation(TREE_OPERATION_REMOVE);