I'm working with a MobX State Tree (MST) store to manage the history of image generation processes. My goal is to check the status of ongoing generations at regular intervals when there are generations with a status of "Running." However, I'm encountering an issue where I receive the following error:
[mobx-state-tree] a mst flow must always have a parent context
This error occurs when I attempt to use setTimeout to call a flow action (checkRunningGenerations) after fetching the status of running generations.
My Questions: What is causing the "a mst flow must always have a parent context" error when using setTimeout to call a flow action? How can I properly implement a periodic check for running generations without encountering this error? Any insights or suggestions on how to resolve this issue would be greatly appreciated!
Here’s a simplified version of my GenerationHistory model:
export const GenerationHistory = types
.model({
generations: types.optional(types.array(Generation), []),
hasRunningGenerations: types.boolean,
runningGenerationsIds: types.optional(types.array(types.number), []),
isLoadingGenerations: types.boolean,
isCheckingGenerations: types.optional(types.boolean, false),
})
.actions((self) => {
const checkRunningGenerations = flow(function* () {
try {
const updatedGenerations = yield Promise.all(
self.runningGenerationsIds.map((id) => fetchGenerationById(id))
);
updatedGenerations.forEach((updatedGeneration) => {
if (updatedGeneration.status === "Completed") {
const index = self.generations.findIndex(
(gen) => gen.id === updatedGeneration.id
);
if (index !== -1) {
self.generations[index] = cast(updatedGeneration);
}
}
});
} catch (error) {
console.error("Error fetching running generations:", error);
}
if (self.runningGenerationsIds.length > 0) {
setTimeout(() => {
checkRunningGenerations().catch((error) =>
console.error("Error in checkRunningGenerations:", error)
);
}, 5000);
}
});
return {
// ... other actions ...
getGenerationsList: flow(function* getGenerationsList() {
self.isLoadingGenerations = true;
try {
const response = yield fetchAllGenerations(self.page);
self.generations = cast(response.generates);
self.runningGenerationsIds = self.generations
.filter((generation) => generation.status === "Running")
.map((generation) => generation.id);
self.hasRunningGenerations = self.runningGenerationsIds.length > 0;
if (self.hasRunningGenerations) {
checkRunningGenerations();
}
} catch (error) {
// Handle error
}
self.isLoadingGenerations = false;
})
}});