vinum-team / Vinum

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

Adding a first-class Async Primitive. #43

Closed xiyler closed 5 months ago

xiyler commented 6 months ago

It could be worthwhile to add first class support for high level management of async operations. It's important to note that Vinum technically allows for writing custom async primitives using RNode's cancelableTasks. These CTasks get "cancel()ed" before every state recomputation, so it's a reliable place for placing something like async stuff.

This feature is deferred to Vinum 0.5

xiyler commented 5 months ago

A simple API for this feature is to follow create an Operator named Async, which in practice just adds a new cTask to a ReactiveObject's cTasks field.

For example:

scope:Derived(function(self)
    Async(self, function()
        task.wait(2)
        Write(self, 20)
    end)

    return 0
end)

While a simple API like this doesn't cover as much as other async-focused libraries (Promises, Futures, etc) do, it satisfices our main goal, which is running operations that are async safely.

Additionally, as much as performance is concerned, we could pool coroutines so the cost of creating coroutines is minimized.

xiyler commented 5 months ago

On a side note, this requires Write to be able to actually overwrite data of ReactiveObjects with a _computer.

xiyler commented 5 months ago

Added on master