-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.vue
30 lines (29 loc) · 893 Bytes
/
app.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<template>
<div>
<v-btn :loading="busy" @click="callWorker">Call Worker</v-btn>
<div v-for="(val, index) in countingMsg" :key="index">{{ val }}</div>
</div>
</template>
<script setup lang="ts">
// import worker script according to the doc of Vite
// @ts-ignore
import MyWorker from '@/assets/workerVite?worker'
const numToSum = ref(1000000000)
const countingMsg = ref<string[]>([])
const busy = ref(false)
async function callWorker() {
busy.value = true
const result = await new Promise<number>((resolve) => {
const worker = new MyWorker()
worker.addEventListener('message', (e: any) => {
if (e.data) {
resolve(e.data)
worker.terminate()
}
}, false);
worker.postMessage(numToSum.value);
})
countingMsg.value.push(`The sum of 1 to ${numToSum.value} is ${result}.`)
busy.value = false
}
</script>