Skip to content

[Feat Request]: Better observability/restorability #17

@ra1028

Description

@ra1028

Checklist

  • Reviewed the README and documentation.
  • Checked existing issues & PRs to ensure not duplicated.

Description

The current observability API done with AtomObserver has too few functionalities as it just can observe what atom is registered/released/updated and that's it.
It would be better to support more powerful functionalities like accessing arbitrary atom values through a Snapshot structure.
Also, currently Snapshot.restore() can only reset the current value to be the snapshotted value, but it should reset the whole store state to be the snapshotted one to prevent inconsistency.

Example Use Case

@State
var snapshots: [Snapshot]
@State
var position = 0

var body: some View {
    AtomRelay {
        ZStack {
            // ... content
            timeTravelSlider
        }
    }
    .observe { snapshot in
        Task {
            snapshots.append(snapshot)
        }
    }
}

var timeTravelSlider: some View {
    Slider(
        value: Binding(
            get: { Double(position) },
            set: {
                position = Int($0)
                snapshots[position].restore()
            }
        ),
        in: 0...Double(max(0, snapshots.endIndex - 1))
    )
}

Alternative Solution

N/A

Proposed Solution

  • Obsolete the current AtomObserver and related classes.
  • Handle atom events with an internal Observer class.
  • Capture the whole store state and restore it through Snapshot.
  • Add lookup<Node: Atom>(_ atom: Node) -> Node.Loader.Value? method to get value through a snapshot.

Motivation & Context

As described.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions