Skip to content

Commit

Permalink
fix: removing resetState and going for more granular approach of disp…
Browse files Browse the repository at this point in the history
…osal
  • Loading branch information
alvarosabu committed Mar 27, 2023
1 parent dbbaee7 commit 6f1e38b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 23 deletions.
24 changes: 13 additions & 11 deletions src/components/TresCanvas.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { defineComponent, h, onUnmounted, ref, watch } from 'vue'
import { App, defineComponent, h, onUnmounted, ref, watch } from 'vue'
import * as THREE from 'three'
import { ShadowMapType, TextureEncoding, ToneMapping } from 'three'
import { createTres } from '/@/core/renderer'
import { useLogger } from '/@/composables'
import { useCamera, useRenderer, useRenderLoop, useRaycaster, useTres } from '/@/composables'
import { extend } from '/@/core/catalogue'
import { RendererPresetsType } from '/@/composables/useRenderer/const'
import { TresObject } from '../types'
import { extend } from '../core/catalogue'
import { RendererPresetsType } from '../composables/useRenderer/const'

export interface TresCanvasProps {
shadows?: boolean
Expand Down Expand Up @@ -53,12 +53,12 @@ export const TresCanvas = defineComponent<TresCanvasProps>({
const container = ref<HTMLElement>()
const canvas = ref<HTMLCanvasElement>()
const scene = new THREE.Scene()
const { setState, resetState } = useTres()
const { setState } = useTres()

setState('scene', scene)

onUnmounted(() => {
resetState()
setState('renderer', null)
})

function initRenderer() {
Expand All @@ -82,7 +82,7 @@ export const TresCanvas = defineComponent<TresCanvasProps>({

watch(canvas, initRenderer)

let app
let app: App

function mountApp() {
app = createTres(slots)
Expand All @@ -96,12 +96,14 @@ export const TresCanvas = defineComponent<TresCanvasProps>({
scene,
})

function dispose() {
scene.children = []
app.unmount()
mountApp()
}

if (import.meta.hot) {
import.meta.hot.on('vite:afterUpdate', () => {
scene.children = []
app.unmount()
mountApp()
})
import.meta.hot.on('vite:afterUpdate',dispose)
}

return () => {
Expand Down
12 changes: 0 additions & 12 deletions src/composables/useTres/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,22 +129,10 @@ export function useTres() {
state[key] = value
}

/**
* Reset a state
*
*/
function resetState() {
setState('scene', null)
setState('renderer', null)
setState('camera', null)
setState('cameras', [])
}

return {
state,
...toRefs(state),
getState,
setState,
resetState
}
}

0 comments on commit 6f1e38b

Please sign in to comment.