refactor: move engine's start() to separate file

This commit is contained in:
Settel 2022-08-01 17:56:51 +02:00
parent d9ebd90c12
commit 0b406595c7
2 changed files with 23 additions and 14 deletions

View File

@ -0,0 +1,9 @@
export default function (): void {
if (this.isActive.value && !this.shouldStop.value) {
console.warn('attempt to start already running engine!')
return
}
this.isActive.value = true
this.shouldStop.value = false
console.log('start engine')
}

View File

@ -1,4 +1,11 @@
import { useState } from '#app' import { useState } from '#app'
import { Ref } from 'vue'
import start from '@/composables/engine/start'
interface EngineContext {
isActive: Ref<boolean>
shouldStop: Ref<boolean>
}
export interface useEngine { export interface useEngine {
start(): void start(): void
@ -6,23 +13,16 @@ export interface useEngine {
} }
export default (): useEngine => { export default (): useEngine => {
const isActive = useState('engine__is-active', () => false) const context: EngineContext = {
const shouldStop = useState('engine__should-stop', () => false) isActive: useState('engine__is-active', () => false),
shouldStop: useState('engine__should-stop', () => false),
}
return { return {
start: (): void => { start: () => start.apply(context),
if (isActive.value && !shouldStop.value) {
console.warn('attempt to start already running engine!')
return
}
isActive.value = true
shouldStop.value = false
console.log('start engine')
},
stop: (): void => { stop: (): void => {
if (isActive.value) { if (context.isActive.value) {
shouldStop.value = true context.shouldStop.value = true
console.log('shut down engine') console.log('shut down engine')
} }
}, },