-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Zach Eriksen
committed
Jan 11, 2021
1 parent
de9c91d
commit 463040e
Showing
2 changed files
with
80 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() } | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters