Skip to content

Commit e18af4b

Browse files
authored
fix(types): avoid emitting THREE.XRFrame (#3196)
1 parent 4a6f8ff commit e18af4b

File tree

3 files changed

+23
-20
lines changed

3 files changed

+23
-20
lines changed

packages/fiber/src/core/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import {
1717
privateKeys,
1818
} from './store'
1919
import { createRenderer, extend, prepare, Root } from './renderer'
20-
import { createLoop, addEffect, addAfterEffect, addTail, flushGlobalEffects } from './loop'
20+
import { createLoop, addEffect, addAfterEffect, addTail, flushGlobalEffects, Invalidate, Advance } from './loop'
2121
import { getEventPriority, EventManager, ComputeFunction } from './events'
2222
import {
2323
is,
@@ -38,7 +38,7 @@ import type { Properties } from '../three-types'
3838
type Canvas = HTMLCanvasElement | OffscreenCanvas
3939

4040
const roots = new Map<Canvas, Root>()
41-
const { invalidate, advance } = createLoop(roots)
41+
const { invalidate, advance }: { invalidate: Invalidate; advance: Advance } = createLoop(roots)
4242
const { reconciler, applyProps } = createRenderer(roots, getEventPriority)
4343
const shallowLoose = { objects: 'shallow', strict: false } as EquConfig
4444

packages/fiber/src/core/loop.ts

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,24 @@ function render(timestamp: number, state: RootState, frame?: _XRFrame) {
7878
return state.frameloop === 'always' ? 1 : state.internal.frames
7979
}
8080

81-
export function createLoop<TCanvas>(roots: Map<TCanvas, Root>) {
81+
export type Invalidate = (state?: RootState, frames?: number) => void
82+
export type Advance = (timestamp: number, runGlobalEffects?: boolean, state?: RootState, frame?: _XRFrame) => void
83+
84+
interface Loop {
85+
loop: (timestamp: number) => void
86+
/**
87+
* Invalidates the view, requesting a frame to be rendered. Will globally invalidate unless passed a root's state.
88+
* @see https://docs.pmnd.rs/react-three-fiber/api/additional-exports#invalidate
89+
*/
90+
invalidate: Invalidate
91+
/**
92+
* Advances the frameloop and runs render effects, useful for when manually rendering via `frameloop="never"`.
93+
* @see https://docs.pmnd.rs/react-three-fiber/api/additional-exports#advance
94+
*/
95+
advance: Advance
96+
}
97+
98+
export function createLoop<TCanvas>(roots: Map<TCanvas, Root>): Loop {
8299
let running = false
83100
let repeat: number
84101
let frame: number
@@ -138,17 +155,5 @@ export function createLoop<TCanvas>(roots: Map<TCanvas, Root>) {
138155
if (runGlobalEffects) flushGlobalEffects('after', timestamp)
139156
}
140157

141-
return {
142-
loop,
143-
/**
144-
* Invalidates the view, requesting a frame to be rendered. Will globally invalidate unless passed a root's state.
145-
* @see https://docs.pmnd.rs/react-three-fiber/api/additional-exports#invalidate
146-
*/
147-
invalidate,
148-
/**
149-
* Advances the frameloop and runs render effects, useful for when manually rendering via `frameloop="never"`.
150-
* @see https://docs.pmnd.rs/react-three-fiber/api/additional-exports#advance
151-
*/
152-
advance,
153-
}
158+
return { loop, invalidate, advance }
154159
}

packages/fiber/src/core/store.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import * as React from 'react'
33
import create, { GetState, SetState, StoreApi, UseBoundStore } from 'zustand'
44
import { DomEvent, EventManager, PointerCaptureTarget, ThreeEvent } from './events'
55
import { _XRFrame, calculateDpr, Camera, isOrthographicCamera, updateCamera } from './utils'
6+
import { Advance, Invalidate } from './loop'
67

78
// Keys that shouldn't be copied between R3F stores
89
export const privateKeys = [
@@ -164,10 +165,7 @@ export type RootState = {
164165

165166
const context = React.createContext<UseBoundStore<RootState>>(null!)
166167

167-
const createStore = (
168-
invalidate: (state?: RootState, frames?: number) => void,
169-
advance: (timestamp: number, runGlobalEffects?: boolean, state?: RootState, frame?: _XRFrame) => void,
170-
): UseBoundStore<RootState> => {
168+
const createStore = (invalidate: Invalidate, advance: Advance): UseBoundStore<RootState> => {
171169
const rootState = create<RootState>((set, get) => {
172170
const position = new THREE.Vector3()
173171
const defaultTarget = new THREE.Vector3()

0 commit comments

Comments
 (0)