Began migrating action engine to async

To suspending actions to await user input
This commit is contained in:
ThaumRystra
2023-11-13 00:24:51 +02:00
parent 800ef3328c
commit 5a2df36e8b
17 changed files with 222 additions and 71 deletions

View File

@@ -0,0 +1,28 @@
import ActiveActions from '/imports/api/creature/actions/ActiveActions';
export default async function getUserInput(questions, actionContext) {
const activeActionId = actionContext.activeActionId;
// Set the questions on the active action
ActiveActions.update(activeActionId, {
$set: { questions },
$unset: { answers: 1 },
});
// Wait for answers
return new Promise((resolve, reject) => {
const observerHandle = ActiveActions.find({
_id: activeActionId
}).observeChanges({
changed(id, fields) {
// Only watch for answers
if (!fields.answers) return;
// Stop watching
observerHandle.stop();
// Give answers
resolve(fields.answers);
},
removed() {
reject('Active action was deleted')
},
});
});
}