Skip to content

Commit

Permalink
Allow "input()" to be called multiple times per block
Browse files Browse the repository at this point in the history
  • Loading branch information
bitspittle committed Dec 26, 2021
1 parent be2270b commit 1c9f2fe
Showing 1 changed file with 17 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,17 @@ private class InputState {
private val UpdateInputJobKey = KonsoleBlock.RunScope.Lifecycle.createKey<Job>()
private val OnlyCalledOncePerRenderKey = KonsoleBlock.Render.Lifecycle.createKey<Unit>()

/**
* Clear all input related data when we're done using it
*
* This can be useful if we, for example, allow calling "input()" serially multiple times in the same block.
*/
private fun ConcurrentScopedData.finishInput() {
remove(OnlyCalledOncePerRenderKey)
remove(InputState.Key)
remove(UpdateInputJobKey)
}

/**
* If necessary, instantiate data that the [input] method expects to exist.
*
Expand All @@ -157,12 +168,16 @@ private fun ConcurrentScopedData.prepareInput(scope: RenderScope) {
}

prepareKeyFlow(konsoleBlock.app.terminal)
if (tryPut(InputState.Key) { InputState() }) {
var stopTimer = false
if (tryPut(InputState.Key, { InputState() }, { stopTimer = true })) {
val state = get(InputState.Key)!!
addTimer(KonsoleAnim.ONE_FRAME_60FPS, repeat = true) {
if (state.elapse(elapsed)) {
konsoleBlock.requestRerender()
}
if (stopTimer) {
repeat = false
}
}
konsoleBlock.onFinishing {
if (state.blinkOn) {
Expand Down Expand Up @@ -221,6 +236,7 @@ private fun ConcurrentScopedData.prepareInput(scope: RenderScope) {
}
if (!rejected) {
get(SystemInputEnteredCallbackKey) { this.invoke() }
finishInput()
}
}
else ->
Expand Down

0 comments on commit 1c9f2fe

Please sign in to comment.