@@ -7,6 +7,7 @@ class Python {
77 #onRunSuccess;
88 #onRunError;
99 #cacheFile;
10+ #cacheFileUrl;
1011 name = 'Python' ;
1112 baseUrl = '' ;
1213 pyodide = null ;
@@ -15,12 +16,23 @@ class Python {
1516 $runBtn = null ;
1617
1718 async init ( $page , cacheFile , cacheFileUrl ) {
18- if ( window . toast ) {
19- window . toast ( 'Python is loading...' ) ;
20- }
19+
20+ this . #cacheFileUrl = cacheFileUrl ;
21+
2122 $page . id = 'acode-plugin-python' ;
23+ this . $refresh = tag ( 'span' , {
24+ className : 'icon refresh' ,
25+ attr : {
26+ action : 'refresh' ,
27+ } ,
28+ onclick : async ( ) => {
29+ await this . initWorker ( ) ;
30+ this . run ( ) ;
31+ } ,
32+ } ) ;
2233 this . $page = $page ;
2334 this . $page . settitle ( 'Python' ) ;
35+ this . $page . get ( 'header' ) . append ( this . $refresh ) ;
2436 this . #cacheFile = cacheFile ;
2537 const onhide = $page . onhide ;
2638 $page . onhide = ( ) => {
@@ -38,19 +50,6 @@ class Python {
3850 main . style . padding = '10px' ;
3951 main . style . overflow = 'auto' ;
4052 main . style . boxSizing = 'border-box' ;
41- this . #worker = new Worker ( this . baseUrl + 'worker.js' ) ;
42- console . log ( 'worker line' , this . #worker. line ) ;
43- this . #worker. postMessage ( {
44- action : 'init' ,
45- baseUrl : this . baseUrl ,
46- cacheFileUrl,
47- } ) ;
48- this . #worker. onmessage = this . #workerOnMessage. bind ( this ) ;
49-
50- await new Promise ( ( resolve , error ) => {
51- this . #onInitSuccess = resolve ;
52- this . #onInitError = error ;
53- } ) ;
5453
5554 this . $runBtn = tag ( 'span' , {
5655 className : 'icon play_arrow' ,
@@ -72,6 +71,26 @@ class Python {
7271 this . checkRunnable ( ) ;
7372 editorManager . on ( 'switch-file' , this . checkRunnable . bind ( this ) ) ;
7473 editorManager . on ( 'rename-file' , this . checkRunnable . bind ( this ) ) ;
74+
75+ await this . initWorker ( ) ;
76+ }
77+
78+ async initWorker ( ) {
79+ if ( this . #worker) this . #worker. terminate ( ) ;
80+ if ( window . toast ) {
81+ window . toast ( 'Python is loading...' ) ;
82+ }
83+ this . #worker = new Worker ( this . baseUrl + 'worker.js' ) ;
84+ this . #worker. postMessage ( {
85+ action : 'init' ,
86+ baseUrl : this . baseUrl ,
87+ cacheFileUrl : this . #cacheFileUrl,
88+ } ) ;
89+ this . #worker. onmessage = this . #workerOnMessage. bind ( this ) ;
90+ await new Promise ( ( resolve , error ) => {
91+ this . #onInitSuccess = resolve ;
92+ this . #onInitError = error ;
93+ } ) ;
7594 if ( window . toast ) {
7695 window . toast ( 'Python is loaded.' ) ;
7796 }
@@ -81,8 +100,11 @@ class Python {
81100 await this . #cacheFile. writeFile ( '' ) ;
82101 this . $page . get ( '.main' ) . innerHTML = '' ;
83102 this . #append( this . $input ) ;
84- this . $page . classList . remove ( 'hide' ) ;
85- this . $page . show ( ) ;
103+
104+ if ( ! this . $page . isConnected ) {
105+ this . $page . classList . remove ( 'hide' ) ;
106+ this . $page . show ( ) ;
107+ }
86108 setTimeout ( async ( ) => {
87109 const code = editorManager . editor . getValue ( ) ;
88110 this . #worker. postMessage ( {
@@ -106,7 +128,7 @@ class Python {
106128 this . $runBtn . onclick = null ;
107129 this . $runBtn . remove ( ) ;
108130 }
109- this . #worker. terminate ( ) ;
131+ if ( this . #worker ) this . #worker. terminate ( ) ;
110132 editorManager . off ( 'switch-file' , this . checkRunnable . bind ( this ) ) ;
111133 editorManager . off ( 'rename-file' , this . checkRunnable . bind ( this ) ) ;
112134 }
0 commit comments