Initial Commit
This commit is contained in:
parent
21c8cf5b5c
commit
33c1b15a88
|
@ -0,0 +1,25 @@
|
||||||
|
{
|
||||||
|
"object": {
|
||||||
|
"pins": [
|
||||||
|
{
|
||||||
|
"package": "Chain",
|
||||||
|
"repositoryURL": "https://github.com/0xLeif/Chain",
|
||||||
|
"state": {
|
||||||
|
"branch": null,
|
||||||
|
"revision": "a431a470148372639c9a35dce4c04b30fd7adc60",
|
||||||
|
"version": "0.2.0"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"package": "E",
|
||||||
|
"repositoryURL": "https://github.com/0xLeif/E.num",
|
||||||
|
"state": {
|
||||||
|
"branch": null,
|
||||||
|
"revision": "a4b2b0cf453edf2e2b3853cdcdd7c8596cadbf43",
|
||||||
|
"version": "0.3.0"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"version": 1
|
||||||
|
}
|
|
@ -14,13 +14,16 @@ let package = Package(
|
||||||
dependencies: [
|
dependencies: [
|
||||||
// Dependencies declare other packages that this package depends on.
|
// Dependencies declare other packages that this package depends on.
|
||||||
// .package(url: /* package url */, from: "1.0.0"),
|
// .package(url: /* package url */, from: "1.0.0"),
|
||||||
|
.package(url: "https://github.com/0xLeif/Chain", from: "0.2.0")
|
||||||
],
|
],
|
||||||
targets: [
|
targets: [
|
||||||
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
|
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
|
||||||
// Targets can depend on other targets in this package, and on products in packages this package depends on.
|
// Targets can depend on other targets in this package, and on products in packages this package depends on.
|
||||||
.target(
|
.target(
|
||||||
name: "Observation",
|
name: "Observation",
|
||||||
dependencies: []),
|
dependencies: [
|
||||||
|
"Chain"
|
||||||
|
]),
|
||||||
.testTarget(
|
.testTarget(
|
||||||
name: "ObservationTests",
|
name: "ObservationTests",
|
||||||
dependencies: ["Observation"]),
|
dependencies: ["Observation"]),
|
||||||
|
|
19
README.md
19
README.md
|
@ -1,3 +1,20 @@
|
||||||
# Observation
|
# Observation
|
||||||
|
|
||||||
A description of this package.
|
|
||||||
|
## Example Code
|
||||||
|
|
||||||
|
```swift
|
||||||
|
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)
|
||||||
|
```
|
||||||
|
|
|
@ -1,3 +0,0 @@
|
||||||
struct Observation {
|
|
||||||
var text = "Hello, World!"
|
|
||||||
}
|
|
|
@ -0,0 +1,36 @@
|
||||||
|
import Chain
|
||||||
|
|
||||||
|
@propertyWrapper public struct Observed<T> {
|
||||||
|
private var observedValue = ObservedValue<T>()
|
||||||
|
|
||||||
|
public var didChangeHandler: Chain? {
|
||||||
|
didSet {
|
||||||
|
observedValue.didChangeHandler = didChangeHandler
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public var wrappedValue: T {
|
||||||
|
didSet {
|
||||||
|
observedValue.update(value: wrappedValue)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public init(wrappedValue: T) {
|
||||||
|
self.wrappedValue = wrappedValue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class ObservedValue<T> {
|
||||||
|
private(set) var value: T?
|
||||||
|
|
||||||
|
public var didChangeHandler: Chain?
|
||||||
|
|
||||||
|
public init(value: T? = nil) {
|
||||||
|
self.value = value
|
||||||
|
}
|
||||||
|
|
||||||
|
public func update(value: T) {
|
||||||
|
self.value = value
|
||||||
|
|
||||||
|
_ = didChangeHandler?.run(name: "")
|
||||||
|
}
|
||||||
|
}
|
|
@ -2,13 +2,27 @@ import XCTest
|
||||||
@testable import Observation
|
@testable import Observation
|
||||||
|
|
||||||
final class ObservationTests: XCTestCase {
|
final class ObservationTests: XCTestCase {
|
||||||
|
@Observed var ov = 4
|
||||||
|
|
||||||
func testExample() {
|
func testExample() {
|
||||||
// This is an example of a functional test case.
|
|
||||||
// Use XCTAssert and related functions to verify your tests produce the correct
|
let observedValue: ObservedValue<Int> = ObservedValue()
|
||||||
// results.
|
|
||||||
XCTAssertEqual(Observation().text, "Hello, World!")
|
observedValue.didChangeHandler = .complete(
|
||||||
}
|
.void {
|
||||||
|
sleep(1)
|
||||||
|
print("Done!")
|
||||||
|
XCTAssertNotNil(observedValue.value)
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
observedValue.update(value: 5)
|
||||||
|
observedValue.update(value: 15)
|
||||||
|
observedValue.update(value: 25)
|
||||||
|
|
||||||
|
XCTAssertEqual(observedValue.value, 25)
|
||||||
|
}
|
||||||
|
|
||||||
static var allTests = [
|
static var allTests = [
|
||||||
("testExample", testExample),
|
("testExample", testExample),
|
||||||
]
|
]
|
||||||
|
|
Loading…
Reference in New Issue