vinum-team / Vinum

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

Add a method for generic table processing #23

Open sinlerdev opened 1 year ago

sinlerdev commented 1 year ago

While it's not the goal to have something strictly similar to ForX objects, it's generally a good idea to offer some kind of of table processing.

This issue's status is the same as #22.

sinlerdev commented 1 year ago

I think a very good approach to this issue is to provide a utility function that takes in an input state object (assuming its value is a table), and then computes a table of state objects.

An API for doing that might look like this:

local libraryArray  = Hold({
    "Vinum", "Atticus", "Streamable"
}, ...)

-- In this example, both `key` and `value` are state objectified beforehand.
-- as such, the computer function can efficiently decide whether it needs to care 
-- about the key/value
local libraryDictonary = InTable(libraryArray, function(use, key, value)
    return "Library", use(value)
end)

for key, stateObj in libraryDictonary do -- Remember: InTable returns a table of state objects!
    print(key, stateObj:get() -- This prints "Library Vinum/Atticus/Streamable".
end

Plus, cases where a state object needs to recompute its key (either the key changed, or removed entirely), we could store a reference to the key, and when any operations concerning keys/indexes are needed to be done, we could do it without making it vulnerable to bugs.

I also think that InTable should error when two objects try to use the same key, but thats an implementation detail.

xiyler commented 6 months ago

Deferred to 0.5

xiyler commented 5 months ago

An alternative design that I am leaning towards more is something like this:

local projects = scope:Source({Vinum = true, reactifiy = false, codify = false})
-- NOTE: InTable should be able to also work with BulkCreate's resultant tables.
local libraries = scope:InTable(projects, function(key, value)
   if not value then
      return nil
   end
   return key(), true
end)

for libraryName in Read(libraries) do
   print(libraryName)
end

-- prints:
-- "Vinum"
xiyler commented 5 months ago

Deferred to Vinum 0.6.