Skip to content
This repository has been archived by the owner on Dec 13, 2023. It is now read-only.

Commit

Permalink
Change refs in one pass, making switching refs atomic (#98)
Browse files Browse the repository at this point in the history
  • Loading branch information
LPGhatguy committed May 22, 2018
1 parent f8edb9f commit 01f327b
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* Added `defaultProps` value on stateful components to define values for props that aren't specified ([#79](https://github.com/Roblox/roact/pull/79))
* Added `getElementTraceback` ([#81](https://github.com/Roblox/roact/issues/81), [#93](https://github.com/Roblox/roact/pull/93))
* Added `createRef` ([#70](https://github.com/Roblox/roact/issues/70), [#92](https://github.com/Roblox/roact/pull/92))
* Ref switching now occurs in one pass, which should fix edge cases where the result of a ref is `nil`, especially in property changed events ([#98](https://github.com/Roblox/roact/pull/98))

## 1.0.0 Prerelease 2 (March 22, 2018)
* Removed `is*Element` methods, this is unlikely to affect anyone ([#50](https://github.com/Roblox/roact/pull/50))
Expand Down
13 changes: 5 additions & 8 deletions lib/Reconciler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -331,11 +331,13 @@ function Reconciler._reconcileInternal(instanceHandle, newElement)

local oldRef = oldElement.props[Core.Ref]
local newRef = newElement.props[Core.Ref]
local refChanged = (oldRef ~= newRef)

-- Cancel the old ref before we make changes. Apply the new one after.
if refChanged and oldRef then
-- Change the ref in one pass before applying any changes.
-- Roact doesn't provide any guarantees with regards to the sequencing
-- between refs and other changes in the commit phase.
if newRef ~= oldRef then
applyRef(oldRef, nil)
applyRef(newRef, instanceHandle._rbx)
end

-- Update properties and children of the Roblox object.
Expand All @@ -344,11 +346,6 @@ function Reconciler._reconcileInternal(instanceHandle, newElement)

instanceHandle._element = newElement

-- Apply the new ref if there was a ref change.
if refChanged and newRef then
applyRef(newRef, instanceHandle._rbx)
end

return instanceHandle
elseif isFunctionalElement(newElement) then
instanceHandle._element = newElement
Expand Down

0 comments on commit 01f327b

Please sign in to comment.