11import type { Ref } from 'vue'
2- import { ref } from 'vue'
32import { listen , UnlistenFn } from '@tauri-apps/api/event'
43import { CodeOutputEvent } from '../types/app.ts'
54
@@ -14,6 +13,11 @@ interface EventManagerOptions
1413 lastExecutionTime : Ref < number >
1514 currentLanguage : Ref < string >
1615 toast : any
16+ handleRealtimeOutput : ( currentLanguage : string , data : any ) => void
17+ handleExecutionComplete : ( currentLanguage : string , data : any ) => void
18+ handleExecutionStopped : ( currentLanguage : string , data : any ) => void
19+ handleExecutionTimeout : ( currentLanguage : string , data : any ) => void
20+ handleExecutionError : ( currentLanguage : string , data : any ) => void
1721}
1822
1923export function useEventManager ( options : EventManagerOptions )
@@ -22,12 +26,12 @@ export function useEventManager(options: EventManagerOptions)
2226 showAbout,
2327 showSettings,
2428 showUpdate,
25- output,
26- isRunning,
27- isSuccess,
28- lastExecutionTime,
2929 currentLanguage,
30- toast
30+ handleRealtimeOutput,
31+ handleExecutionComplete,
32+ handleExecutionStopped,
33+ handleExecutionTimeout,
34+ handleExecutionError
3135 } = options
3236
3337 // 事件监听器引用
@@ -41,40 +45,11 @@ export function useEventManager(options: EventManagerOptions)
4145 let unlistenExecutionTimeoutFn : UnlistenFn | null = null
4246 let unlistenExecutionErrorFn : UnlistenFn | null = null
4347
44- // 实时输出相关
45- const realTimeOutput = ref ( '' )
46- const realTimeStderr = ref ( '' )
47-
4848 // 处理实时输出
49- const handleRealtimeOutput = ( event : any ) => {
49+ const handleRealtimeOutputWrapper = ( event : any ) => {
5050 const data : CodeOutputEvent = event . payload
5151 console . log ( '实时输出:' , data )
52-
53- // 只处理当前语言的输出
54- if ( data . language !== currentLanguage . value ) {
55- return
56- }
57-
58- if ( data . type === 'stdout' ) {
59- realTimeOutput . value += data . content + '\n'
60- }
61- else if ( data . type === 'stderr' ) {
62- realTimeStderr . value += data . content + '\n'
63- }
64-
65- // 合并输出显示
66- let combinedOutput = ''
67- if ( realTimeOutput . value ) {
68- combinedOutput += realTimeOutput . value
69- }
70- if ( realTimeStderr . value ) {
71- if ( combinedOutput ) {
72- combinedOutput += '\n'
73- }
74- combinedOutput += realTimeStderr . value
75- }
76-
77- output . value = combinedOutput
52+ handleRealtimeOutput ( currentLanguage . value , data )
7853 }
7954
8055 // 处理执行状态事件
@@ -85,44 +60,26 @@ export function useEventManager(options: EventManagerOptions)
8560 }
8661 }
8762
88- const handleExecutionComplete = ( event : any ) => {
63+ const handleExecutionCompleteWrapper = ( event : any ) => {
8964 const data = event . payload
90- if ( data . language === currentLanguage . value ) {
91- isRunning . value = false
92- isSuccess . value = data . success
93- if ( data . execution_time ) {
94- lastExecutionTime . value = data . execution_time
95- }
96- console . log ( '代码执行完成' )
97- }
65+ console . log ( '代码执行完成' )
66+ handleExecutionComplete ( currentLanguage . value , data )
9867 }
9968
100- const handleExecutionStopped = ( event : any ) => {
69+ const handleExecutionStoppedWrapper = ( event : any ) => {
10170 const data = event . payload
102- if ( data . language === currentLanguage . value ) {
103- isRunning . value = false
104- output . value += '\n\n🛑 代码执行已被用户停止'
105- toast . warning ( '代码执行已停止' )
106- console . log ( '代码执行已停止' )
107- }
71+ console . log ( '代码执行已停止' )
72+ handleExecutionStopped ( currentLanguage . value , data )
10873 }
10974
110- const handleExecutionTimeout = ( event : any ) => {
75+ const handleExecutionTimeoutWrapper = ( event : any ) => {
11176 const data = event . payload
112- if ( data . language === currentLanguage . value ) {
113- isRunning . value = false
114- output . value += '\n\n⚠️ 代码执行超时(30秒)'
115- toast . error ( '代码执行超时' )
116- }
77+ handleExecutionTimeout ( currentLanguage . value , data )
11778 }
11879
119- const handleExecutionError = ( event : any ) => {
80+ const handleExecutionErrorWrapper = ( event : any ) => {
12081 const data = event . payload
121- if ( data . language === currentLanguage . value ) {
122- isRunning . value = false
123- output . value += `\n\n❌ 执行错误: ${ data . error } `
124- toast . error ( '代码执行出错' )
125- }
82+ handleExecutionError ( currentLanguage . value , data )
12683 }
12784
12885 const initializeEventListeners = async ( ) => {
@@ -140,14 +97,14 @@ export function useEventManager(options: EventManagerOptions)
14097 } )
14198
14299 // 监听实时输出事件
143- unlistenOutputFn = await listen ( 'code-output' , handleRealtimeOutput )
100+ unlistenOutputFn = await listen ( 'code-output' , handleRealtimeOutputWrapper )
144101
145102 // 监听执行状态事件
146103 unlistenExecutionStartFn = await listen ( 'code-execution-start' , handleExecutionStart )
147- unlistenExecutionCompleteFn = await listen ( 'code-execution-complete' , handleExecutionComplete )
148- unlistenExecutionStoppedFn = await listen ( 'code-execution-stopped' , handleExecutionStopped )
149- unlistenExecutionTimeoutFn = await listen ( 'code-execution-timeout' , handleExecutionTimeout )
150- unlistenExecutionErrorFn = await listen ( 'code-execution-error' , handleExecutionError )
104+ unlistenExecutionCompleteFn = await listen ( 'code-execution-complete' , handleExecutionCompleteWrapper )
105+ unlistenExecutionStoppedFn = await listen ( 'code-execution-stopped' , handleExecutionStoppedWrapper )
106+ unlistenExecutionTimeoutFn = await listen ( 'code-execution-timeout' , handleExecutionTimeoutWrapper )
107+ unlistenExecutionErrorFn = await listen ( 'code-execution-error' , handleExecutionErrorWrapper )
151108 }
152109
153110 const cleanupEventListeners = ( ) => {
0 commit comments