vinum-team / Vinum

A modern reactive state management library for correctness and speed.
MIT License
16 stars 1 forks source link

A unified injector through overloading #14

Closed sinlerdev closed 1 year ago

sinlerdev commented 1 year ago

Currently, there are two injectors (and possibly 3 if #12 is implemented) that serve connecting a dependent to a dependency, useState and useKeyState. The difference though is that the first is used for self-containing objects, while the latter is for Groups.

While they are certainly doing their job well, they can overcomplicate codebases, and generally speaking, each of them share some logic that can be unified.

The solution in my mind is to introduce a unified, and overloaded injector that can work with Groups and the rest of self-containing objects- like so:

Calc(function(use)
    return use(x) + use(y, "key")
end)

I'm not entirely sure whether this is a good solution, or even that there is an issue with the current stuff we have- so I am waiting for some feedback.

sinlerdev commented 1 year ago
return function(self)
    local selfGraph = self._graph
    return function(dependency, keyName: string?)

        -- if the dependency is a group and the keyName argument is provided
        if dependency.kind == "group" and keyName then
            local keyGraph = dependency._graphs[keyName]

            keyGraph._dependentSet[self._graph] = true
            selfGraph._dependencySet[keyGraph] = true

            return dependency:getKey(keyName)
        else -- this means that our dependency is a self-contained
            local stateGraph = dependency._graph
            stateGraph._dependentSet[selfGraph] = true
            selfGraph._dependencySet[stateGraph] = true

            return dependency:get()
        end
    end
end

This is a simple prototype of this feature- play with it as much as you want.