Skip to content

Commit

Permalink
refactor: add setReady
Browse files Browse the repository at this point in the history
  • Loading branch information
andretchen0 committed Jun 22, 2024
1 parent e1df8a9 commit 543fbfa
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/composables/useTresContextProvider/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,9 +212,11 @@ export function useTresContextProvider({

const { on: onTresReady, cancel: cancelTresReady } = useTresReady(ctx)!

ctx.loop.setReady(false)
ctx.loop.start()
onTresReady(() => {
emit('ready', ctx)
ctx.loop.start()
ctx.loop.setReady(true)
})

onUnmounted(() => {
Expand Down
7 changes: 7 additions & 0 deletions src/core/loop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export interface RendererLoop {
isActive: Ref<boolean>
isRenderPaused: Ref<boolean>
setContext: (newContext: Record<string, any>) => void
setReady: (isReady: boolean) => void
}

export function createRenderLoop(): RendererLoop {
Expand All @@ -52,6 +53,7 @@ export function createRenderLoop(): RendererLoop {
const subscribersBefore = createPriorityEventHook<LoopCallbackWithCtx>()
const subscriberRender = createPriorityEventHook<LoopCallbackWithCtx>()
const subscribersAfter = createPriorityEventHook<LoopCallbackWithCtx>()
let isReady = true

// Context to be passed to callbacks
let context: Record<string, any> = {}
Expand Down Expand Up @@ -110,6 +112,10 @@ export function createRenderLoop(): RendererLoop {
}

function loop() {
if (!isReady) {
animationFrameId = requestAnimationFrame(loop)
return
}
const delta = clock.getDelta()
const elapsed = clock.getElapsedTime()
const snapshotCtx = {
Expand Down Expand Up @@ -157,5 +163,6 @@ export function createRenderLoop(): RendererLoop {
isRenderPaused,
isActive,
setContext,
setReady: (b: boolean) => isReady = b,
}
}

0 comments on commit 543fbfa

Please sign in to comment.