Skip to content

danielinoa/Ripple

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Ripple

A reactivity runtime for Swift that automatically tracks state, and re-runs dependents when state changes.

Ripple is designed to run on the main actor.

import Ripple

@Atom var count = 0
@Derived var title = "Count: \(count)"

let render = Effect {     // → keep the Effect alive
    label.text = title    // → auto-updates when count changes
}

count += 1                // → title recomputes → effect runs

Outline

Features

  • Ergonomic API: @Atom var count = 0, @Derived var title = "Count: \(count)".
  • Fine-grained tracking: only recompute what actually depends on what changed.
  • Memoized computed values: @Derived caches and invalidates on upstream mutations.
  • Side effects: Effect { … } runs once up-front and whenever its inputs change.
  • Test isolation: withIsolatedRuntime { … } gives each test a fresh graph.
  • No macros, no Combine: simple value semantics and identity-based graphs.

Installation

Swift Package Manager

Add the package in Xcode (File → Add Packages…) or declare it in Package.swift:

.package(url: "https://github.com/danielinoa/Ripple.git", branch: "main")

Concepts

@Atom: The fundamental “state” type.

@Atom var name = "Ripple"
name = "Ripple 2"           // notifies dependents if value actually changed
print($name)                // `$` exposes the AtomObject
  • Reads link the current subscriber.
  • Writes call Runtime.didMutate only if the value changes (Equatable).

@Derived: A cached, read-only value that is derived from other state.

@Atom var x = 1
@Atom var y = 2
@Derived var sum = x + y     // cached until x or y mutates

If T : Equatable, Ripple skips propagation when the recomputed value equals the cached one.

Effect: An observer that fires when its enclosed dependencies change.

@Atom var count = 0
var bag: [Effect] = []

bag.append(Effect {
    label.text = "Count: \(count)"
})

Runs once on creation and after every relevant mutation.

Convenience forms

The property‑wrapper syntax is most succinct, but Ripple also exposes low‑level factory helpers that return the underlying nodes. This is useful when you need explicit type annotations or want to store the nodes in collections.

Purpose Property‑wrapper Factory function
Mutable state @Atom var count = 0 let count = atom(0)
Cached value @Derived var total = price * qty let total: Derivation<Int> = derive { price * qty }

Both forms interoperate:

@Atom var price = 10
let qty = atom(2)

@Derived var total = price * qty.value // wrapper + node
let tax = derive { total + 1 } // node based on wrapper

print(tax.value) // 21

Choose whichever style reads best in a given context; under the hood they all participate in the same dependency graph.

Testing & isolation

@Test
func example() {
    withIsolatedRuntime {
        @Atom var a = 1
        @Derived var doubled = a * 2
        #expect(doubled == 2)
    }
}

withIsolatedRuntime (sync & async overloads) swaps Runtime.current with a fresh graph for the duration of the task tree.

Behavior details

Item Behaviour
Atom writes Notify dependents only when newValue != oldValue.
Derived cache Recompute on first read or after any dependency mutates.
Derived equality If T : Equatable, skip propagation when value is unchanged.
Effect lifetime Runs while at least one strong reference exists; unsubscribes on deinit.

Examples

UIKit binding

final class CounterVC: UIViewController {
    private var label: UILabel = .init()
    private var bag: [Effect] = []

    @Atom
    private var count = 0
    
    @Derived
    private var title = "Count: \(count)"

    override func viewDidLoad() {
        super.viewDidLoad()
        bag.append(Effect { self.label.text = self.title })
    }

    private func plus() { count += 1 }
}

Chained derived values

@Atom var a = 1
@Derived var d1 = a * 2          // 2
@Derived var d2 = d1 + 3         // 5
@Derived var d3 = d2 * 4         // 20
let e = Effect { print(d3) }     // prints 20, then 36 when a = 3

Parallel-safe tests

@Test
func isolatedGraphs() async {
  let r1: Int = await withIsolatedRuntime {
    @Atom var x = 1 
    @Atom var y = 2
    @Derived var s = x + y
    return s
  }
  let r2: Int = await withIsolatedRuntime {
    @Atom var x = 10
    @Atom var y = 20
    @Derived var s = x + y
    return s
  }
  #expect(r1 == 3 && r2 == 30)
}

Roadmap

  • Scheduler: coalesce burst mutations into a single run loop pass.

License

MIT License

Copyright (c) 2025 Daniel Inoa

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES, OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

About

A tiny dependency-tracked reactivity runtime for Swift

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages