Skip to content

Commit

Permalink
preload project runner
Browse files Browse the repository at this point in the history
  • Loading branch information
nighca committed Nov 5, 2024
1 parent 2200594 commit 7ab70d2
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 14 deletions.
8 changes: 6 additions & 2 deletions spx-gui/src/components/editor/preview/EditorPreview.vue
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,19 @@
</template>

<script lang="ts" setup>
import { ref } from 'vue'
import { onMounted, ref } from 'vue'
import { useEditorCtx } from '@/components/editor/EditorContextProvider.vue'
import { UICard, UICardHeader, UIButton, UIFullScreenModal } from '@/components/ui'
import StageViewer from './stage-viewer/StageViewer.vue'
import RunnerContainer from '@/components/project/runner/RunnerContainer.vue'
import RunnerContainer, { preload as preloadRunner } from '@/components/project/runner/RunnerContainer.vue'
let show = ref(false)
const editorCtx = useEditorCtx()
onMounted(() => {
preloadRunner()
})
</script>

<style scoped lang="scss">
Expand Down
41 changes: 35 additions & 6 deletions spx-gui/src/components/project/runner/IframeDisplay.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,41 @@
<template>
<iframe ref="iframe" class="iframe" frameborder="0" src="about:blank" />
</template>

<script lang="ts">
import { ref, watch } from 'vue'
import rawRunnerHtml from '@/assets/ispx/runner.html?raw'
import wasmExecUrl from '@/assets/wasm_exec.js?url'
import wasmUrl from '@/assets/ispx/main.wasm?url'
// Patch for type declaration of fetch priority
// TODO: upgrade typescript & related development tools, then remove the patch
declare global {
interface HTMLLinkElement {
/**
* An optional string representing a hint given to the browser on how it should prioritize fetching of a preload relative to other resources of the same type.
* [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/HTMLLinkElement/fetchPriority)
*/
fetchPriority?: 'low' | 'high' | 'auto'
}
}
function addPreloadLink(type: 'fetch' | 'script', url: string) {
const link = document.createElement('link')
link.rel = 'preload'
link.as = type
link.href = url
link.fetchPriority = 'low'
document.head.appendChild(link)
}
// preload resources (for example, wasm files) to accelerate the loading
export function preload() {
addPreloadLink('script', wasmExecUrl)
addPreloadLink('fetch', wasmUrl)
}
</script>

<script setup lang="ts">
const emit = defineEmits<{
console: [type: 'log' | 'warn', args: unknown[]]
Expand All @@ -12,12 +47,6 @@ interface IframeWindow extends Window {
console: typeof console
}
import { ref } from 'vue'
import rawRunnerHtml from '@/assets/ispx/runner.html?raw'
import wasmExecUrl from '@/assets/wasm_exec.js?url'
import wasmUrl from '@/assets/ispx/main.wasm?url'
import { watch } from 'vue'
const props = defineProps<{ zipData: ArrayBuffer | Uint8Array }>()
const iframe = ref<HTMLIFrameElement>()
Expand Down
6 changes: 5 additions & 1 deletion spx-gui/src/components/project/runner/ProjectRunner.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@
</div>
</template>

<script lang="ts">
import IframeDisplay, { preload } from './IframeDisplay.vue'
export { preload }
</script>

<script lang="ts" setup>
import { onUnmounted, ref } from 'vue'
import { registerPlayer } from '@/utils/player-registry'
import { useFileUrl } from '@/utils/file'
import { Project } from '@/models/project'
import { UIImg, UILoading } from '@/components/ui'
import IframeDisplay from './IframeDisplay.vue'
const props = defineProps<{ project: Project }>()
Expand Down
7 changes: 6 additions & 1 deletion spx-gui/src/components/project/runner/RunnerContainer.vue
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,16 @@
</div>
</div>
</template>

<script lang="ts">
import ProjectRunner, { preload } from './ProjectRunner.vue'
export { preload }
</script>

<script setup lang="ts">
import { onMounted, ref, type CSSProperties, watch, nextTick } from 'vue'
import dayjs from 'dayjs'
import type { Project } from '@/models/project'
import ProjectRunner from './ProjectRunner.vue'
import { usePublishProject } from '@/components/project'
import { UIButton, UIIcon, UIModalClose } from '@/components/ui'
import { useMessageHandle } from '@/utils/exception'
Expand Down
8 changes: 6 additions & 2 deletions spx-gui/src/pages/community/project.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script setup lang="ts">
import { computed, ref, watch } from 'vue'
import { computed, onMounted, ref, watch } from 'vue'
import { useRouter } from 'vue-router'
import { useMessageHandle } from '@/utils/exception'
import { useQuery } from '@/utils/query'
Expand Down Expand Up @@ -29,7 +29,7 @@ import {
import CenteredWrapper from '@/components/community/CenteredWrapper.vue'
import ProjectsSection from '@/components/community/ProjectsSection.vue'
import ProjectItem from '@/components/project/ProjectItem.vue'
import ProjectRunner from '@/components/project/runner/ProjectRunner.vue'
import ProjectRunner, { preload as preloadRunner } from '@/components/project/runner/ProjectRunner.vue'
import RemixedFrom from '@/components/community/project/RemixedFrom.vue'
import OwnerInfo from '@/components/community/project/OwnerInfo.vue'
import { useCreateProject, useRemoveProject, useShareProject, useUnpublishProject } from '@/components/project'
Expand Down Expand Up @@ -267,6 +267,10 @@ const remixesRet = useQuery(
},
{ en: 'Failed to load projects', zh: '加载失败' }
)
onMounted(() => {
preloadRunner()
})
</script>

<template>
Expand Down
8 changes: 6 additions & 2 deletions spx-gui/src/widgets/spx-runner/SpxRunner.ce.vue
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
</div>
</template>
<script setup lang="ts">
import ProjectRunner from '@/components/project/runner/ProjectRunner.vue'
import { ref, watch } from 'vue'
import ProjectRunner, { preload as preloadRunner } from '@/components/project/runner/ProjectRunner.vue'
import { onMounted, ref, watch } from 'vue'
import { Project, fullName } from '@/models/project'
import { shallowRef } from 'vue'
const props = defineProps<{ owner?: string; name?: string }>()
Expand Down Expand Up @@ -71,6 +71,10 @@ const onStop = () => {
run.value = false
runner.value.stop()
}

onMounted(() => {
preloadRunner()
})
</script>
<style lang="scss">
.spx-runner-widget {
Expand Down

0 comments on commit 7ab70d2

Please sign in to comment.