Skip to content

Commit

Permalink
refactor(playground): 無駄なDynamic Importを削減
Browse files Browse the repository at this point in the history
Tree-shakingされなかったため
  • Loading branch information
kakkokari-gtyih committed Dec 5, 2024
1 parent 435e1ac commit 7e42779
Showing 1 changed file with 4 additions and 19 deletions.
23 changes: 4 additions & 19 deletions .vitepress/components/CodeBlockPlayground.vue
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@

<script setup lang="ts">
import { ref } from 'vue';
import { utils, errors } from '@syuilo/aiscript';
import type { Interpreter, Parser } from '@syuilo/aiscript';
import { Parser, Interpreter, utils, errors } from '@syuilo/aiscript';
const props = defineProps<{
code: string;
Expand All @@ -36,9 +35,6 @@ const logs = ref<{
}[]>([]);
const loading = ref(false);
let ParserClass: typeof Parser | null = null;
let InterpreterClass: typeof Interpreter | null = null;
let parser: Parser | null = null;
let interpreter: Interpreter | null = null;
Expand All @@ -55,27 +51,16 @@ async function run() {
type: 'info',
}];
if (!ParserClass || !InterpreterClass) {
const [
{ Parser, Interpreter },
] = await Promise.all([
import('@syuilo/aiscript'),
new Promise((resolve) => setTimeout(resolve, 250)), // あまりにも高速に切り替わると実行できてるのかわかりにくいので、最低250msはロード画面を挟む
]);
ParserClass = Parser;
InterpreterClass = Interpreter;
} else {
await new Promise((resolve) => setTimeout(resolve, 250)); // あまりにも高速に切り替わると実行できてるのかわかりにくいので、最低250msはロード画面を挟む
}
await new Promise((resolve) => setTimeout(resolve, 250)); // あまりにも高速に切り替わると実行できてるのかわかりにくいので、最低250msはロード画面を挟む
if (!parser) {
parser = new ParserClass();
parser = new Parser();
}
if (interpreter) {
interpreter.abort();
}
interpreter = new InterpreterClass({}, {
interpreter = new Interpreter({}, {
out: (value) => {
logs.value.push({
text: value.type === 'num' ? value.value.toString() : value.type === 'str' ? `"${value.value}"` : JSON.stringify(utils.valToJs(value), null, 2) ?? '',
Expand Down

0 comments on commit 7e42779

Please sign in to comment.