https://github.com/0xlet/observation
🔠Observable variable project using E.num and Chain
https://github.com/0xlet/observation
chain swift
Last synced: about 2 months ago
JSON representation
🔠Observable variable project using E.num and Chain
- Host: GitHub
- URL: https://github.com/0xlet/observation
- Owner: 0xLet
- License: mit
- Created: 2021-03-01T22:42:31.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2021-03-17T00:00:07.000Z (about 4 years ago)
- Last Synced: 2025-03-08T06:04:30.151Z (3 months ago)
- Topics: chain, swift
- Language: Swift
- Homepage: https://github.com/0xLeif/E.num
- Size: 9.77 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Observation
## What is this
This is a work in progress project to experiment with [Chain](https://github.com/0xLeif/Chain) and [E.num](https://github.com/0xLeif/E.num) in useful ways.
## Dependencies
### [E.num](https://github.com/0xLeif/E.num)
#### [Variable](https://github.com/0xLeif/E.num/blob/main/Sources/E/Variable.swift)
```swift
public enum Variable: Hashable {
case void
case bool(Bool)
case int(Int)
case float(Float)
case double(Double)
case string(String)
case set(Set)
case array([Variable])
case dictionary([Variable: Variable])
}
```#### [Function](https://github.com/0xLeif/E.num/blob/main/Sources/E/Function.swift)
```swift
public enum Function {
case void(() -> ())
case `in`((Variable) -> ())
case out(() -> Variable)
case `inout`((Variable) -> Variable)
}
```### [Chain](https://github.com/0xLeif/Chain)
```swift
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
```swift
let observedValue: ObservedValue = 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
```swift
@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
```