Skip to content

Commit

Permalink
feat: split loading phases
Browse files Browse the repository at this point in the history
  • Loading branch information
ouuan committed Apr 3, 2022
1 parent 616b27c commit 55c5a02
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
13 changes: 10 additions & 3 deletions src/components/MainPage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
</template>
</n-page-header>
<n-space
v-if="correctRoot"
v-if="loadPhase === true"
vertical
>
<n-card>
Expand Down Expand Up @@ -116,7 +116,7 @@
style="margin-top: 20vh;"
>
<n-space justify="space-around">
<n-text>Loading...</n-text>
<n-text>{{ loadPhase }}</n-text>
</n-space>
<n-space justify="space-around">
<n-spin size="large" />
Expand Down Expand Up @@ -176,7 +176,7 @@ import {
updatePuzzle,
} from '../store/useLocalStorage';
import { standardizeCode } from '../store/useRootTreeOption';
import parse from '../parse';
import { initParser, parse } from '../parse';
import {
enableAutoOutboundTracking,
trackEvent,
Expand All @@ -199,16 +199,23 @@ const editor = ref();

const dialog = useDialog();

const loadPhase = ref<true | string>('Loading page...');

onMounted(async () => {
trackPageview();
enableAutoOutboundTracking();
loadPhase.value = 'Loading puzzle...';
await updatePuzzle(dialog);
if (targetCode.value === '') return;
if (guesses.value.length) {
code.value = guesses.value[guesses.value.length - 1];
}
loadPhase.value = 'Loading TreeSitter...';
await initParser;
loadPhase.value = 'Constructing AST...';
const tree = await parse(targetCode.value);
correctRoot.value = tree.rootNode;
loadPhase.value = true;
});

const lengthLimit = computed(() => targetCode.value.length * 2);
Expand Down
2 changes: 1 addition & 1 deletion src/components/SyntaxTree.vue
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import { SyntaxNode } from 'web-tree-sitter';
import SyntaxTreeNode from './SyntaxTreeNode.vue';
import parse from '../parse';
import { parse } from '../parse';
import {
CorrectStatus,
MarkRange,
Expand Down
4 changes: 2 additions & 2 deletions src/parse.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import Parser from 'web-tree-sitter';

// load parser only once across all SyntaxTree instances
const initParser = (async () => {
export const initParser = (async () => {
await Parser.init();
const parser = new Parser();
const Lang = await Parser.Language.load('/tree-sitter-cpp.wasm');
parser.setLanguage(Lang);
return parser;
})();

export default async function parse(code: string) {
export async function parse(code: string) {
const parser = await initParser;
return parser.parse(code);
}

0 comments on commit 55c5a02

Please sign in to comment.