dphfox / Fusion

A modern reactive UI library, built specifically for Roblox and Luau.
https://elttob.uk/Fusion/
MIT License
530 stars 91 forks source link

Derive scopes with additional methods #326

Closed dphfox closed 2 months ago

dphfox commented 2 months ago

deriveScope and innerScope should accept extra arguments to add additional methods to the newly created scope.

This is useful because many components already implement what is effectively an inner scope already, but have to use scoped for the task because the other functions do not allow for a changed set of methods.

local function TodoEntry(
    outerScope: Fusion.Scope<{}>,
    props: {
        ...
    }
): Fusion.Child
    local scope = scoped(Fusion, {
        Draggable = Draggable
    })
    table.insert(outerScope, scope)
    -- ... continues ...

If implemented, the above could could be reduced to a single call:

local function TodoEntry(
    outerScope: Fusion.Scope<typeof(Fusion)>,
    props: {
        ...
    }
): Fusion.Child
    local scope = outerScope:innerScope {
        Draggable = Draggable
    }
    -- ... continues ...

This notably means that it's impossible to forget to add the inner scope to the outer scope, reducing the surface area for bugs.

dphfox commented 2 months ago

One gotcha here is merge conflict handling. It's entirely possible that the outer scope defines some method with the same name as one which the inner scope would like to have. scoped throws an error on merge conflicts, because it's likely programmer error there. However, in this case, it would be forming an implicit contract with the owner of the outer scope that they don't use methods of that name. This breaks encapsulation, so I would suggest that merge conflict errors should only occur for methods that are passed in simultaneously, not between an inner scope's method definitions and an outer scope's method definitions.

dphfox commented 2 months ago

Implemented - just need to write docs.