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