Began migrating action engine to async
To suspending actions to await user input
This commit is contained in:
28
app/imports/api/engine/actions/getUserInput.js
Normal file
28
app/imports/api/engine/actions/getUserInput.js
Normal 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')
|
||||
},
|
||||
});
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user