-
Notifications
You must be signed in to change notification settings - Fork 534
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tree: Allow array from map and map from array (#22036)
## Description Allow constructing ArrayNodes from Maps and MapNodes from arrays when unambiguous. Also removes some no longer needed shallow copies in the map and array constructors . See changeset for details.
- Loading branch information
1 parent
06681c3
commit 25e74f9
Showing
7 changed files
with
150 additions
and
32 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
--- | ||
"@fluidframework/tree": minor | ||
"fluid-framework": minor | ||
--- | ||
|
||
Allow constructing ArrayNodes from Maps and MapNodes from arrays when unambiguous. | ||
|
||
Since the types for ArrayNodes and MapNodes indicate they can be constructed from iterables, | ||
it should work, even if those iterables are themselves arrays or maps. | ||
To avoid this being a breaking change, a priority system was introduced. | ||
ArrayNodes will only be implicitly constructable from JavaScript Map objects in contexts where no MapNodes are allowed. | ||
Similarly MapNodes will only be implicitly constructable from JavaScript Array objects in contexts where no ArrayNodes are allowed. | ||
|
||
In practice, the main case in which this is likely to matter is when implicitly constructing a map node. If you provide an array of key value pairs, this now works instead of erroring, as long as no ArrayNode is valid at that location in the tree. | ||
|
||
```typescript | ||
class MyMapNode extends schemaFactory.map("x", schemaFactory.number) {} | ||
class Root extends schemaFactory.object("root", { data: MyMapNode }) {} | ||
// This now works (before it compiled, but error at runtime): | ||
const fromArray = new Root({ data: [["x", 5]] }); | ||
``` | ||
|
||
Prior versions used to have to do: | ||
```typescript | ||
new Root({ data: new MyMapNode([["x", 5]]) }); | ||
``` | ||
or: | ||
```typescript | ||
new Root({ data: new Map([["x", 5]]) }); | ||
``` | ||
Both of these options still work: strictly more cases are allowed with this change. |
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
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