Skip to content

Commit 3e35a2c

Browse files
ilgonmiczoobestik
authored andcommitted
Load skiko with every window in iframe
1 parent 1b64353 commit 3e35a2c

File tree

2 files changed

+32
-35
lines changed

2 files changed

+32
-35
lines changed

src/executable-code/executable-fragment.js

Lines changed: 5 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import directives from 'monkberry-directives';
66
import 'monkberry-events';
77
import ExecutableCodeTemplate from './executable-fragment.monk';
88
import WebDemoApi from '../webdemo-api';
9-
import { executeJs } from '../js-executor/execute-es-module';
109

1110
import {
1211
isJavaRelated,
@@ -314,13 +313,14 @@ export default class ExecutableFragment extends ExecutableCodeTemplate {
314313
}
315314

316315
onConsoleCloseButtonEnter() {
317-
const { jsLibs, onCloseConsole, targetPlatform } = this.state;
316+
const { jsLibs, onCloseConsole, targetPlatform, compilerVersion } = this.state;
318317
// creates a new iframe and removes the old one, thereby stops execution of any running script
319318
if (isJsRelated(targetPlatform) || isWasmRelated(targetPlatform))
320319
this.jsExecutor.reloadIframeScripts(
321320
jsLibs,
322321
this.getNodeForMountIframe(),
323322
targetPlatform,
323+
compilerVersion,
324324
);
325325
this.update({ output: '', openConsole: false, exception: null });
326326
if (onCloseConsole) onCloseConsole();
@@ -409,35 +409,12 @@ export default class ExecutableFragment extends ExecutableCodeTemplate {
409409
jsLibs,
410410
this.getNodeForMountIframe(),
411411
targetPlatform,
412+
compilerVersion,
412413
);
413414
const additionalRequests = [];
414415
if (targetPlatform === TargetPlatforms.COMPOSE_WASM) {
415-
if (!this.jsExecutor.skikoImports) {
416-
const skikoImport = fetch(API_URLS.SKIKO_MJS(compilerVersion), {
417-
method: 'GET',
418-
headers: {
419-
'Content-Type': 'text/javascript',
420-
},
421-
})
422-
.then((script) => script.text())
423-
.then((script) =>
424-
script.replace(
425-
'new URL("skiko.wasm",import.meta.url).href',
426-
`'${API_URLS.SKIKO_WASM(compilerVersion)}'`,
427-
),
428-
)
429-
.then(async (skikoCode) => {
430-
return await executeJs(
431-
this.jsExecutor.iframe.contentWindow,
432-
skikoCode,
433-
);
434-
})
435-
.then((skikoImports) => {
436-
this.jsExecutor.skikoImports = skikoImports;
437-
this.jsExecutor.iframe.contentWindow.skikoImports = skikoImports;
438-
});
439-
440-
additionalRequests.push(skikoImport);
416+
if (!this.jsExecutor.skikoImport) {
417+
additionalRequests.push(this.jsExecutor.skikoImport);
441418
}
442419
}
443420

src/js-executor/index.js

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ import { API_URLS } from '../config';
33
import { showJsException } from '../view/output-view';
44
import { processingHtmlBrackets } from '../utils';
55
import { isWasmRelated, TargetPlatforms } from '../utils/platforms';
6-
import { executeWasmCode, executeWasmCodeWithSkiko } from './execute-es-module';
6+
import { executeJs, executeWasmCode, executeWasmCodeWithSkiko } from './execute-es-module';
7+
import { fetch } from "whatwg-fetch";
78

89
const INIT_SCRIPT =
910
'if(kotlin.BufferedOutput!==undefined){kotlin.out = new kotlin.BufferedOutput()}' +
@@ -25,7 +26,7 @@ const normalizeJsVersion = (version) => {
2526
export default class JsExecutor {
2627
constructor(kotlinVersion) {
2728
this.kotlinVersion = kotlinVersion;
28-
this.skikoImports = undefined;
29+
this.skikoImport = undefined;
2930
}
3031

3132
async executeJsCode(
@@ -138,7 +139,7 @@ export default class JsExecutor {
138139
return new Promise((resolve) => setTimeout(resolve, ms));
139140
}
140141

141-
reloadIframeScripts(jsLibs, node, targetPlatform) {
142+
reloadIframeScripts(jsLibs, node, targetPlatform, compilerVersion) {
142143
if (this.iframe !== undefined) {
143144
node.removeChild(this.iframe);
144145
}
@@ -170,10 +171,29 @@ export default class JsExecutor {
170171
}
171172
}
172173
if (targetPlatform === TargetPlatforms.COMPOSE_WASM) {
173-
if (this.skikoImports) {
174-
this.iframe.contentWindow.skikoImports = this.skikoImports;
175-
}
176-
this.iframe.height = '1000';
174+
this.skikoImport = fetch(API_URLS.SKIKO_MJS(compilerVersion), {
175+
method: 'GET',
176+
headers: {
177+
'Content-Type': 'text/javascript',
178+
}
179+
})
180+
.then(script => script.text())
181+
.then(script => script.replace(
182+
"new URL(\"skiko.wasm\",import.meta.url).href",
183+
`'${API_URLS.SKIKO_WASM(compilerVersion)}'`
184+
))
185+
.then(async skikoCode => {
186+
return await executeJs(
187+
this.iframe.contentWindow,
188+
skikoCode,
189+
);
190+
}
191+
)
192+
.then(skikoImports => {
193+
this.iframe.contentWindow.skikoImports = skikoImports;
194+
});
195+
196+
this.iframe.height = "1000"
177197
iframeDoc.write(`<canvas height="1000" id="ComposeTarget"></canvas>`);
178198
}
179199
iframeDoc.write('<body style="margin: 0; overflow: hidden;"></body>');

0 commit comments

Comments
 (0)