Combine reaction with async callbacks
#4590
Unanswered
Lonli-Lokli
asked this question in
Q&A
Replies: 1 comment
-
|
@Lonli-Lokli your that said, import { reaction, flow, isFlowCancellationError } from 'mobx'
class BotAI {
pendingFlow = null
constructor() {
this.disposeReaction = reaction(
() => ({
isBot: this.gameClient.currentPlayer.isBot,
subPhase: this.gameClient.state.subPhase,
turnCount: this.gameClient.state.turnCount,
activeTossIn: this.gameClient.state.activeTossIn,
}),
({ isBot, subPhase, activeTossIn }) => {
this.pendingFlow?.cancel()
if (subPhase === 'toss_queue_active' && activeTossIn) {
this.pendingFlow = this.handleTossInFlow()
} else if (isBot) {
this.pendingFlow = this.executeBotTurnFlow()
}
}
)
}
executeBotTurnFlow = flow(function* () {
try {
const move = yield calculateMove()
this.state.lastMove = move // no runInAction needed
} catch (e) {
if (!isFlowCancellationError(e)) {
console.error('[BotAI] Error:', e)
}
}
})
}why
ref: flow docs | reactions docs |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Is tthere better, mobx-way to rewrite it without it? I cannot use await in callbacks
Beta Was this translation helpful? Give feedback.
All reactions