Skip to content

DEV: cleanup diff streaming #1370

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 13 additions & 16 deletions assets/javascripts/discourse/components/modal/diff-modal.gjs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import DiffStreamer from "../../lib/diff-streamer";
import SmoothStreamer from "../../lib/smooth-streamer";
import AiIndicatorWave from "../ai-indicator-wave";

const CHANNEL = "/discourse-ai/ai-helper/stream_composer_suggestion";

export default class ModalDiffModal extends Component {
@service currentUser;
@service messageBus;
Expand Down Expand Up @@ -49,12 +51,9 @@ export default class ModalDiffModal extends Component {
}

get isStreaming() {
// diffStreamer stops "streaming" when it is finished with a chunk
return (
this.diffStreamer.isStreaming ||
!this.diffStreamer.isDone ||
this.smoothStreamer.isStreaming
);
// diffStreamer stops Streaming when it is finished with a chunk, looking at isDone is safe
// it starts off not done
return !this.diffStreamer.isDone || this.smoothStreamer.isStreaming;
}

get primaryBtnLabel() {
Expand All @@ -69,25 +68,23 @@ export default class ModalDiffModal extends Component {

@bind
subscribe() {
const channel = "/discourse-ai/ai-helper/stream_composer_suggestion";
this.messageBus.subscribe(channel, this.updateResult);
this.messageBus.subscribe(CHANNEL, this.updateResult);
}

@bind
unsubscribe() {
const channel = "/discourse-ai/ai-helper/stream_composer_suggestion";
this.messageBus.unsubscribe(channel, this.updateResult);
cleanup() {
// stop all callbacks so it does not end up streaming pointlessly
this.smoothStreamer.resetStreaming();
this.diffStreamer.reset();
this.messageBus.unsubscribe(CHANNEL, this.updateResult);
}

@action
async updateResult(result) {
updateResult(result) {
this.loading = false;

if (result.done) {
this.finalResult = result.result;
}

if (result.done) {
this.loading = false;
}

Expand Down Expand Up @@ -154,7 +151,7 @@ export default class ModalDiffModal extends Component {
<:body>
<div
{{didInsert this.subscribe}}
{{willDestroy this.unsubscribe}}
{{willDestroy this.cleanup}}
class="text-preview"
>
{{#if this.loading}}
Expand Down
1 change: 1 addition & 0 deletions assets/javascripts/discourse/lib/diff-streamer.gjs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ export default class DiffStreamer {
this.currentWordIndex = 0;
this.currentCharIndex = 0;
this.isStreaming = false;
this.isDone = false;
if (this.typingTimer) {
cancel(this.typingTimer);
this.typingTimer = null;
Expand Down
Loading