2018-03-18 00:45:52 +00:00
|
|
|
<template>
|
|
|
|
<section class="container">
|
|
|
|
<div>
|
2018-11-24 18:49:19 +00:00
|
|
|
<AppLogo />
|
2018-03-18 00:45:52 +00:00
|
|
|
<h1 class="title">
|
|
|
|
web-worker
|
|
|
|
</h1>
|
|
|
|
<h2 class="subtitle">
|
|
|
|
Nuxt.js project
|
|
|
|
</h2>
|
|
|
|
<p>{{ notification }}</p>
|
|
|
|
<ul class="list">
|
|
|
|
<li>Number of Web Workers: {{ workers.length }}</li>
|
|
|
|
<li>Number of long Running Workers: {{ longRunningWorkers.length }}</li>
|
|
|
|
<li>Number of unused Workers: {{ workers.filter(w => !w.inUse).length }}</li>
|
|
|
|
</ul>
|
|
|
|
<div class="links">
|
|
|
|
<a
|
|
|
|
:class="needWorkerSetup ? 'hidden' : 'visible'"
|
2018-07-12 12:03:50 +00:00
|
|
|
class="button button--green"
|
2018-08-31 20:34:12 +00:00
|
|
|
@click="test"
|
2018-11-24 18:49:19 +00:00
|
|
|
>
|
|
|
|
Test Worker
|
|
|
|
</a>
|
2018-03-18 00:45:52 +00:00
|
|
|
<a
|
|
|
|
:class="needWorkerSetup ? 'hidden' : 'visible'"
|
2018-07-12 12:03:50 +00:00
|
|
|
class="button button--green"
|
2018-08-31 20:34:12 +00:00
|
|
|
@click="long(4000)"
|
2018-11-24 18:49:19 +00:00
|
|
|
>
|
|
|
|
Execute long running Worker
|
|
|
|
</a>
|
2018-03-18 00:45:52 +00:00
|
|
|
<a
|
|
|
|
:class="needWorkerSetup || !longRunningWorkers.length ? 'hidden' : 'visible'"
|
2018-07-12 12:03:50 +00:00
|
|
|
class="button button--green"
|
2018-08-31 20:34:12 +00:00
|
|
|
@click="freeWorker"
|
2018-11-24 18:49:19 +00:00
|
|
|
>
|
|
|
|
Free long running Worker
|
|
|
|
</a>
|
2018-03-18 00:45:52 +00:00
|
|
|
<a
|
2018-07-12 12:03:50 +00:00
|
|
|
class="button button--grey"
|
2018-08-31 20:34:12 +00:00
|
|
|
@click="removeWorker"
|
2018-11-24 18:49:19 +00:00
|
|
|
>
|
|
|
|
Remove Web Worker
|
|
|
|
</a>
|
2018-03-18 00:45:52 +00:00
|
|
|
<a
|
2018-07-12 12:03:50 +00:00
|
|
|
class="button button--grey"
|
2018-08-31 20:34:12 +00:00
|
|
|
@click="createWorkers"
|
2018-11-24 18:49:19 +00:00
|
|
|
>
|
|
|
|
Create more Workers
|
|
|
|
</a>
|
2018-03-18 00:45:52 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</section>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
|
|
|
import AppLogo from '~/components/AppLogo.vue'
|
|
|
|
|
|
|
|
export default {
|
|
|
|
components: {
|
|
|
|
AppLogo
|
|
|
|
},
|
|
|
|
data () {
|
|
|
|
return {
|
|
|
|
notification: '',
|
|
|
|
workers: [],
|
|
|
|
workerIndex: 0,
|
|
|
|
longRunningWorkers: [],
|
|
|
|
longIndex: 0
|
|
|
|
}
|
|
|
|
},
|
2018-07-12 12:03:50 +00:00
|
|
|
computed: {
|
|
|
|
needWorkerSetup () {
|
|
|
|
return this.workers.length === 0 && this.longRunningWorkers.length === 0
|
|
|
|
}
|
|
|
|
},
|
2018-03-18 00:45:52 +00:00
|
|
|
watch: {
|
|
|
|
workers (workers) {
|
|
|
|
if (workers.length === 0) this.notification = 'Zero free Web Workers - click "Create more Workers"'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
test () {
|
|
|
|
const worker = this.workers[this.workerIndex++ % this.workers.length]
|
|
|
|
|
2018-06-26 10:33:51 +00:00
|
|
|
if (worker) {
|
|
|
|
worker.onmessage = (event) => {
|
|
|
|
this.notification = event.data.hello
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-18 00:45:52 +00:00
|
|
|
if (worker) worker.postMessage({ hello: 'world' })
|
|
|
|
else this.notification = 'No more test workers available'
|
|
|
|
},
|
2018-09-29 20:05:20 +00:00
|
|
|
long (milliseconds) {
|
2018-03-18 00:45:52 +00:00
|
|
|
let worker = this.workers.shift()
|
|
|
|
|
|
|
|
if (worker) {
|
|
|
|
worker.onmessage = (event) => {
|
2018-06-26 10:33:51 +00:00
|
|
|
this.notification = `expensive made ${event.data} loops`
|
|
|
|
worker.onmessage = null
|
|
|
|
this.workers.push(...this.longRunningWorkers.splice(this.longRunningWorkers.indexOf(worker), 1))
|
2018-03-18 00:45:52 +00:00
|
|
|
}
|
|
|
|
this.longRunningWorkers.push(worker)
|
|
|
|
} else {
|
|
|
|
worker = this.longRunningWorkers[ this.longIndex++ % this.longRunningWorkers.length]
|
|
|
|
}
|
|
|
|
|
2018-09-29 20:05:20 +00:00
|
|
|
worker.postMessage({ action: 'expensive', time: milliseconds })
|
2018-03-18 00:45:52 +00:00
|
|
|
},
|
|
|
|
freeWorker () {
|
2018-06-26 10:33:51 +00:00
|
|
|
// we can't really free a worker, we can only terminate it and create a new
|
2018-03-18 00:45:52 +00:00
|
|
|
const worker = this.longRunningWorkers.pop()
|
|
|
|
worker.onmessage = null
|
2018-06-26 10:33:51 +00:00
|
|
|
worker.terminate()
|
|
|
|
this.workers.push(this.$worker.createWorker())
|
2018-03-18 00:45:52 +00:00
|
|
|
this.notification = 'Worker freed'
|
|
|
|
},
|
|
|
|
removeWorker () {
|
|
|
|
const worker = this.workers.pop() || this.longRunningWorkers.pop()
|
|
|
|
|
|
|
|
if (!worker) return
|
|
|
|
|
|
|
|
if (this.longRunningWorkers.indexOf(worker) > -1) this.longRunningWorkers.splice(this.longRunningWorkers.indexOf(worker), 1)
|
|
|
|
|
|
|
|
worker.onmessage = null
|
|
|
|
worker.terminate()
|
|
|
|
},
|
|
|
|
createWorkers () {
|
2018-09-19 09:00:23 +00:00
|
|
|
if (process.client) {
|
2018-03-18 00:45:52 +00:00
|
|
|
for(let i = 0, len = navigator.hardwareConcurrency || 1; i < len; i++) {
|
|
|
|
this.workers.push(this.$worker.createWorker())
|
|
|
|
}
|
|
|
|
|
|
|
|
this.notification = 'Go nuts!'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<style>
|
|
|
|
.hidden {
|
|
|
|
visibility: hidden;
|
|
|
|
}
|
|
|
|
|
|
|
|
.visible {
|
|
|
|
visibility: visible;
|
|
|
|
}
|
|
|
|
|
|
|
|
.container {
|
|
|
|
min-height: 100vh;
|
|
|
|
display: flex;
|
|
|
|
justify-content: center;
|
|
|
|
align-items: center;
|
|
|
|
text-align: center;
|
|
|
|
}
|
|
|
|
|
|
|
|
.title {
|
|
|
|
font-family: "Quicksand", "Source Sans Pro", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif; /* 1 */
|
|
|
|
display: block;
|
|
|
|
font-weight: 300;
|
|
|
|
font-size: 100px;
|
|
|
|
color: #35495e;
|
|
|
|
letter-spacing: 1px;
|
|
|
|
}
|
|
|
|
|
|
|
|
.subtitle {
|
|
|
|
font-weight: 300;
|
|
|
|
font-size: 42px;
|
|
|
|
color: #526488;
|
|
|
|
word-spacing: 5px;
|
|
|
|
padding-bottom: 15px;
|
|
|
|
}
|
|
|
|
|
|
|
|
.links {
|
|
|
|
padding-top: 15px;
|
|
|
|
}
|
|
|
|
|
|
|
|
.list {
|
|
|
|
text-align: left;
|
|
|
|
color: #526488;
|
|
|
|
list-style: none;
|
|
|
|
}
|
|
|
|
</style>
|