Skip to content

Instantly share code, notes, and snippets.

@godrm
Last active December 10, 2021 06:28
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save godrm/02bc016ba7908e82a552fba85a0ad50d to your computer and use it in GitHub Desktop.
Save godrm/02bc016ba7908e82a552fba85a0ad50d to your computer and use it in GitHub Desktop.
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()
}
}
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