-
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.
Merge pull request #2 from 0xLeif/develop
0.2.0 ChainOutput
- Loading branch information
Showing
3 changed files
with
134 additions
and
27 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
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,40 +1,74 @@ | ||
import Foundation | ||
|
||
public typealias ChainAction = () -> Void | ||
import E | ||
|
||
public indirect enum Chain { | ||
case end | ||
case complete(ChainAction?) | ||
case link(ChainAction, Chain) | ||
case background(ChainAction, Chain) | ||
case complete(E.Function?) | ||
case link(E.Function, Chain) | ||
case background(E.Function, Chain) | ||
case multi([Chain]) | ||
} | ||
|
||
public extension Chain { | ||
func run() { | ||
func run( | ||
name: String? = nil, | ||
input: Variable? = nil, | ||
shouldFlattenOutput: Bool = false | ||
) -> Variable { | ||
var logInfo: String { | ||
"[\(Date())] Chain\(name.map { " (\($0)) "} ?? ""):" | ||
} | ||
var output: Variable = .array([]) | ||
|
||
switch self { | ||
case .end: | ||
print("[\(Date())] Chain: End") | ||
print("\(logInfo) End") | ||
|
||
output = output.update { | ||
.array($0 + [Variable.void]) | ||
} | ||
case .complete(let completion): | ||
print("[\(Date())] Chain: Complete") | ||
completion?() | ||
print("\(logInfo) Complete") | ||
|
||
output = output.update { | ||
.array($0 + [completion?.run(input) ?? Variable.void]) | ||
} | ||
case .link(let action, | ||
let next): | ||
print("[\(Date())] Chain: Link") | ||
action() | ||
next.run() | ||
print("\(logInfo) Link") | ||
|
||
let actionOutput: Variable = action.run(input) ?? Variable.void | ||
|
||
output = output.update { | ||
.array($0 + [actionOutput] + [next.run(name: name, input: actionOutput)]) | ||
} | ||
case .background(let action, | ||
let next): | ||
print("[\(Date())] Chain: Background") | ||
print("\(logInfo) Background") | ||
DispatchQueue.global().async { | ||
action() | ||
let actionOutput: Variable = action.run(input) ?? Variable.void | ||
|
||
output = output.update { | ||
.array($0 + [actionOutput]) | ||
} | ||
DispatchQueue.main.async { | ||
next.run() | ||
output = output.update { | ||
.array($0 + [next.run(name: name, input: actionOutput)]) | ||
} | ||
} | ||
} | ||
case .multi(let links): | ||
print("[\(Date())] Chain: Multi") | ||
links.forEach { $0.run() } | ||
print("\(logInfo) Multi") | ||
output = output.update { | ||
.array($0 + links.map { $0.run(name: name) }) | ||
} | ||
} | ||
|
||
// Flatten Output | ||
if shouldFlattenOutput { | ||
return output.flatten | ||
} | ||
|
||
return output | ||
} | ||
} |
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