Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
Zach Eriksen committed Jan 11, 2021
1 parent de9c91d commit 463040e
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 7 deletions.
41 changes: 39 additions & 2 deletions Sources/Chain/Chain.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,40 @@
struct Chain {
var text = "Hello, World!"
import Foundation

public typealias ChainAction = () -> Void

public indirect enum Chain {
case end
case complete(ChainAction?)
case link(ChainAction, Chain)
case background(ChainAction, Chain)
case multi([Chain])
}

public extension Chain {
func run() {
switch self {
case .end:
print("[\(Date())] Chain: End")
case .complete(let completion):
print("[\(Date())] Chain: Complete")
completion?()
case .link(let action,
let next):
print("[\(Date())] Chain: Link")
action()
next.run()
case .background(let action,
let next):
print("[\(Date())] Chain: Background")
DispatchQueue.global().async {
action()
DispatchQueue.main.async {
next.run()
}
}
case .multi(let links):
print("[\(Date())] Chain: Multi")
links.forEach { $0.run() }
}
}
}
46 changes: 41 additions & 5 deletions Tests/ChainTests/ChainTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,48 @@ import XCTest

final class ChainTests: XCTestCase {
func testExample() {
// This is an example of a functional test case.
// Use XCTAssert and related functions to verify your tests produce the correct
// results.
XCTAssertEqual(Chain().text, "Hello, World!")
var isLooping = true

var text = ""


Chain.link(
{ print(0) },
.link(
{ print(1) },
.background(
{ print(2)
sleep(3)
},
.link(
{ print(3) },
.complete {
text = "Hello, World?"
}
)
)
)
)
.run()

Chain.background(
{
sleep(5)
},
.link(
{
XCTAssertEqual(text, "Hello, World!")
},
.complete {
isLooping = false
}
)
)
.run()

while isLooping { }
}

static var allTests = [
("testExample", testExample),
]
Expand Down

0 comments on commit 463040e

Please sign in to comment.