Last active
December 10, 2021 06:28
-
-
Save godrm/02bc016ba7908e82a552fba85a0ad50d to your computer and use it in GitHub Desktop.
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
class ReadValue { | |
private var thread : Thread? = nil | |
init(with handler: @escaping (String) -> ()) { | |
thread = Thread(block: { | |
while(true) { | |
let value = readLine() ?? "" | |
handler(value) | |
} | |
}) | |
thread?.start() | |
} | |
func stop() { | |
thread?.cancel() | |
} | |
} |
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
var asyncRead = { value in | |
print(value) | |
} | |
print("$", terminator: "") | |
var read = ReadValue(with: asyncRead) | |
RunLoop.current.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment