Skip to content
This repository was archived by the owner on Jul 22, 2025. It is now read-only.

Commit 2b961c4

Browse files
committed
DEV: cleanup diff streaming
This simplifies some of the internal logic and ensures it is a bit more robust
1 parent e264572 commit 2b961c4

File tree

2 files changed

+14
-16
lines changed

2 files changed

+14
-16
lines changed

assets/javascripts/discourse/components/modal/diff-modal.gjs

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import DiffStreamer from "../../lib/diff-streamer";
1818
import SmoothStreamer from "../../lib/smooth-streamer";
1919
import AiIndicatorWave from "../ai-indicator-wave";
2020

21+
const CHANNEL = "/discourse-ai/ai-helper/stream_composer_suggestion";
22+
2123
export default class ModalDiffModal extends Component {
2224
@service currentUser;
2325
@service messageBus;
@@ -49,12 +51,9 @@ export default class ModalDiffModal extends Component {
4951
}
5052

5153
get isStreaming() {
52-
// diffStreamer stops "streaming" when it is finished with a chunk
53-
return (
54-
this.diffStreamer.isStreaming ||
55-
!this.diffStreamer.isDone ||
56-
this.smoothStreamer.isStreaming
57-
);
54+
// diffStreamer stops Streaming when it is finished with a chunk, looking at isDone is safe
55+
// it starts off not done
56+
return !this.diffStreamer.isDone || this.smoothStreamer.isStreaming;
5857
}
5958

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

7069
@bind
7170
subscribe() {
72-
const channel = "/discourse-ai/ai-helper/stream_composer_suggestion";
73-
this.messageBus.subscribe(channel, this.updateResult);
71+
this.messageBus.subscribe(CHANNEL, this.updateResult);
7472
}
7573

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

8282
@action
83-
async updateResult(result) {
83+
updateResult(result) {
8484
this.loading = false;
8585

8686
if (result.done) {
8787
this.finalResult = result.result;
88-
}
89-
90-
if (result.done) {
9188
this.loading = false;
9289
}
9390

@@ -154,7 +151,7 @@ export default class ModalDiffModal extends Component {
154151
<:body>
155152
<div
156153
{{didInsert this.subscribe}}
157-
{{willDestroy this.unsubscribe}}
154+
{{willDestroy this.cleanup}}
158155
class="text-preview"
159156
>
160157
{{#if this.loading}}

assets/javascripts/discourse/lib/diff-streamer.gjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ export default class DiffStreamer {
100100
this.currentWordIndex = 0;
101101
this.currentCharIndex = 0;
102102
this.isStreaming = false;
103+
this.isDone = false;
103104
if (this.typingTimer) {
104105
cancel(this.typingTimer);
105106
this.typingTimer = null;

0 commit comments

Comments
 (0)