mirror of
https://github.com/storytold/artcraft.git
synced 2026-11-05 01:38:44 +00:00
Add generation engine and signals
This commit is contained in:
1 parent
99ac8faf33
commit
e7ed0693a3
2 files changed
+86
-4
No files matched your search
@@ -0,0 +1,45 @@
|
||||
import { invoke } from "@tauri-apps/api/core";
|
||||
import {
|
||||
cancelGeneration,
|
||||
finishGeneration,
|
||||
onStartGeneration,
|
||||
} from "~/signals/generation/generationSignals";
|
||||
|
||||
export class GenerationEngine {
|
||||
private isGenerating = false;
|
||||
|
||||
constructor() {
|
||||
this.attachEventListeners();
|
||||
}
|
||||
|
||||
private attachEventListeners() {
|
||||
onStartGeneration((prompt) => {
|
||||
this.invokeGeneration(prompt);
|
||||
});
|
||||
}
|
||||
|
||||
private async invokeGeneration(prompt: string) {
|
||||
if (this.isGenerating) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.isGenerating = true;
|
||||
console.log("Invoking generation for prompt:", prompt);
|
||||
|
||||
try {
|
||||
const base64BitmapResponse = await invoke("infer_image", {
|
||||
prompt: prompt,
|
||||
});
|
||||
|
||||
console.log("Generation finished with response:", base64BitmapResponse);
|
||||
|
||||
const base64string = base64BitmapResponse as string;
|
||||
finishGeneration(base64string);
|
||||
} catch (error) {
|
||||
console.error("Error during image processing:", error);
|
||||
cancelGeneration();
|
||||
} finally {
|
||||
this.isGenerating = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
import { signal } from "@preact/signals-react";
|
||||
import { effect, signal } from "@preact/signals-react";
|
||||
|
||||
export enum GenerationLoadingState {
|
||||
INIT,
|
||||
@@ -6,8 +6,45 @@ export enum GenerationLoadingState {
|
||||
GENERATED,
|
||||
}
|
||||
|
||||
const initValue = { loadingState: GenerationLoadingState.INIT, prompt: "" };
|
||||
|
||||
export const generationSignal = signal<{
|
||||
loadingState: GenerationLoadingState;
|
||||
prompt: string;
|
||||
loadingState: GenerationLoadingState;
|
||||
prompt: string;
|
||||
imageB64?: string;
|
||||
}>({ loadingState: GenerationLoadingState.INIT, prompt: "" });
|
||||
}>(initValue);
|
||||
|
||||
export const startGeneration = (prompt: string) => {
|
||||
generationSignal.value = {
|
||||
loadingState: GenerationLoadingState.GENERATING,
|
||||
prompt,
|
||||
imageB64: undefined,
|
||||
};
|
||||
};
|
||||
|
||||
export const onStartGeneration = (callback: (prompt: string) => void) => {
|
||||
effect(() => {
|
||||
// If value just changed to GENERATING, notify the listeners
|
||||
if (
|
||||
generationSignal.value.loadingState === GenerationLoadingState.GENERATING
|
||||
) {
|
||||
callback(generationSignal.value.prompt);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
export const finishGeneration = (imageB64: string) => {
|
||||
generationSignal.value = {
|
||||
loadingState: GenerationLoadingState.GENERATED,
|
||||
prompt: generationSignal.value.prompt,
|
||||
imageB64,
|
||||
};
|
||||
};
|
||||
|
||||
export const cancelGeneration = () => {
|
||||
if (
|
||||
generationSignal.value.loadingState === GenerationLoadingState.GENERATING
|
||||
) {
|
||||
generationSignal.value = initValue;
|
||||
}
|
||||
};
|
||||
Reference in new issue
Block a user