Skip to content

Releases: 0xLet/Observation

0.0.2

17 Mar 00:00
9e04c70
Compare
Choose a tag to compare

Modifiable callChain

public class ObservedValue<T> {
    private(set) public var value: T?
    
    public var callChain: Chain = .end
    public var didChangeHandler: Chain?
    
    public init(value: T? = nil) {
        self.value = value
        self.callChain = .complete(
            .out { [weak self] in
                self?.didChangeHandler?.run(name: "ObservedValue<\(T.self)>.didChangeHandler") ?? .void
            }
        )
    }

    @discardableResult
    public func update(value: T) -> Variable {
        self.value = value
        
        return callChain.run(name: "ObservedValue<\(T.self)>.callChain")
    }
}

ObservedValue<T>

02 Mar 03:57
3f3e0e9
Compare
Choose a tag to compare

Example Code

let observedValue: ObservedValue<Int> = ObservedValue()

observedValue.didChangeHandler = .complete(
    .void {
        sleep(1)
        print("Done!")
        XCTAssertNotNil(observedValue.value)
    }
)

observedValue.update(value: 5)
observedValue.update(value: 15)
observedValue.update(value: 25)

Property Wrapper

@Observed var index = 4

_index.didChangeHandler = .link(
    .void {
        viewModel.update(value: values[index])
    },
    .complete(
        .void {
            updateUI()
        }
    )
)

guard values.count < index && index >= 0 else {
    return
}

index += 1