Observation

0.0.2

🔭 Observable variable project using E.num and Chain
0xLet/Observation

What's New

0.0.2

2021-03-17T00:00:07Z

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")
    }
}

Observation

What is this

This is a work in progress project to experiment with Chain and E.num in useful ways.

Dependencies

public enum Variable: Hashable {
    case void
    case bool(Bool)
    case int(Int)
    case float(Float)
    case double(Double)
    case string(String)
    case set(Set<Variable>)
    case array([Variable])
    case dictionary([Variable: Variable])
}
public enum Function {
    case void(() -> ())
    case `in`((Variable) -> ())
    case out(() -> Variable)
    case `inout`((Variable) -> Variable)
}
public indirect enum Chain {
    case end
    case complete(E.Function?)
    case link(E.Function, Chain)
    case background(E.Function, Chain)
    case multi([Chain])
}

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

Description

  • Swift Tools 5.3.0
View More Packages from this Author

Dependencies

Last updated: Fri Oct 18 2024 08:14:16 GMT-0900 (Hawaii-Aleutian Daylight Time)