Fusion icon indicating copy to clipboard operation
Fusion copied to clipboard

Add safety checks for applying properties

Open dphfox opened this issue 3 years ago • 0 comments

Closes #108 and related to #34.

This adds a few safety checks to applyInstanceProps to help stop developers footgunning themselves with hard-to-debug undefined behaviour. Specifically:

  • Duplicate property key binding will throw an error. For any given key, there should be one, and only one, source of truth. Imperative-style property modification is non-idiomatic and explicitly disallowed:
local instance = New "Folder" {
    Name = "Bob"
}

Hydrate(instance) {
    Name = "Suze" --> Can't assign to 'Name' twice.
}
local instance = New "Folder" {
    [Children] = Computed(function() ... end)
}

Hydrate(instance) {
    [Children] = Computed(function() ... end) --> Can't assign to 'Children' twice.
}
  • Ambiguous parents/children will throw an error. Ambiguity is introduced when an instance with Parent defined is passed to [Children], or when one instance is passed into two [Children] definitions:
New "Folder" {
    [Children] = New "Folder" {
        Parent = workspace --> The parent of 'Folder' is ambiguous - only one Parent or [Children] is allowed at a time.
    }
}
local childA = Value(New "Part" {})
local childB = Value(nil)

New "Folder" {
    [Children] = childA
}
New "Folder" {
    [Children] = childB
}

childB:set(childA:get()) --> The parent of 'Part' is ambiguous - only one Parent or [Children] is allowed at a time.

dphfox avatar Jan 30 '22 06:01 dphfox