This repository has been archived by the owner on Dec 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 143
createRef API #92
Merged
Merged
createRef API #92
Changes from 15 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
4e773ef
add createRef function
AmaranthineCodices 82b2330
handle correctly in reconciler
AmaranthineCodices 72b907c
basic test case for createRef
AmaranthineCodices 35a9df7
test cases for references
AmaranthineCodices d002373
export createRef
AmaranthineCodices e2f452f
clean up ref-related API docs
AmaranthineCodices b624801
add a tostring metamethod to refs for debugging purposes
AmaranthineCodices 9ce1d84
Revert "clean up ref-related API docs"
AmaranthineCodices 3cda6f7
refactor applyRef to eliminate some nil checks
AmaranthineCodices 1f86eb1
re-add API docs
AmaranthineCodices 878ecc5
descriptive comments!
AmaranthineCodices 598af40
fix applyRef style
AmaranthineCodices 6f770ab
expand tostring test case
AmaranthineCodices 8c6cb36
don't quote strings
AmaranthineCodices 4614af4
fix tests, oops!
AmaranthineCodices 3378d4a
fix missing ref!
AmaranthineCodices 336db31
fix changing refs
AmaranthineCodices 9de73c7
test cases for changing refs
AmaranthineCodices b139cc3
add to CHANGELOG
AmaranthineCodices File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,39 @@ | ||
return function() | ||
local Core = require(script.Parent.Core) | ||
local Reconciler = require(script.Parent.Reconciler) | ||
local createRef = require(script.Parent.createRef) | ||
|
||
it("should mount booleans as nil", function() | ||
local booleanReified = Reconciler.mount(false) | ||
expect(booleanReified).to.never.be.ok() | ||
end) | ||
|
||
it("should handle object references properly", function() | ||
local objectRef = createRef() | ||
local element = Core.createElement("StringValue", { | ||
[Core.Ref] = objectRef, | ||
}) | ||
|
||
local handle = Reconciler.mount(element) | ||
expect(objectRef.current).to.be.ok() | ||
Reconciler.unmount(handle) | ||
expect(objectRef.current).to.never.be.ok() | ||
end) | ||
|
||
it("should handle function references properly", function() | ||
local currentRbx | ||
|
||
local function ref(rbx) | ||
currentRbx = rbx | ||
end | ||
|
||
local element = Core.createElement("StringValue", { | ||
[Core.Ref] = ref, | ||
}) | ||
|
||
local handle = Reconciler.mount(element) | ||
expect(currentRbx).to.be.ok() | ||
Reconciler.unmount(handle) | ||
expect(currentRbx).to.never.be.ok() | ||
end) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we add a test that makes sure that refs can be unattached and attached through |
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
--[[ | ||
Provides an API for acquiring a reference to a reified object. This | ||
API is designed to mimic React 16.3's createRef API. | ||
|
||
See: | ||
* https://reactjs.org/docs/refs-and-the-dom.html | ||
* https://reactjs.org/blog/2018/03/29/react-v-16-3.html#createref-api | ||
]] | ||
|
||
local refMetatable = { | ||
__tostring = function(self) | ||
return ("RoactReference(%s)"):format(tostring(self.current)) | ||
end, | ||
} | ||
|
||
return function() | ||
return setmetatable({ | ||
current = nil, | ||
}, refMetatable) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
return function() | ||
local createRef = require(script.Parent.createRef) | ||
|
||
it("should create refs", function() | ||
expect(createRef()).to.be.ok() | ||
end) | ||
|
||
it("should support tostring on refs", function() | ||
local ref = createRef() | ||
expect(tostring(ref)).to.equal("RoactReference(nil)") | ||
|
||
ref.current = "foo" | ||
expect(tostring(ref)).to.equal("RoactReference(foo)") | ||
end) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we're going to test There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's fair, done! |
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need to update the branch on line 349 that applies
newRef
as well! It's callingnewRef
as a function.