Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[v9] refactor(types): export public types #2559

Merged
merged 5 commits into from
Oct 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 5 additions & 7 deletions packages/fiber/src/core/events.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as THREE from 'three'
import { ContinuousEventPriority, DiscreteEventPriority, DefaultEventPriority } from 'react-reconciler/constants'
import { getRootState } from './utils'
import { Camera, getRootState } from './utils'
import type { Instance } from './renderer'
import type { RootState, RootStore } from './store'

Expand All @@ -9,7 +9,7 @@ export interface Intersection extends THREE.Intersection {
eventObject: THREE.Object3D
}

export interface IntersectionEvent<TSourceEvent> extends Intersection {
export interface ThreeEvent<TEvent> extends Intersection {
/** The event source (the object which registered the handler) */
eventObject: THREE.Object3D
/** An array of intersections */
Expand All @@ -27,16 +27,14 @@ export interface IntersectionEvent<TSourceEvent> extends Intersection {
/** stopPropagation will stop underlying handlers from firing */
stopPropagation: () => void
/** The original host event */
nativeEvent: TSourceEvent
nativeEvent: TEvent
/** If the event was stopped by calling stopPropagation */
stopped: boolean
}

export type Camera = THREE.OrthographicCamera | THREE.PerspectiveCamera
export type ThreeEvent<TEvent> = IntersectionEvent<TEvent>
export type DomEvent = PointerEvent | MouseEvent | WheelEvent

export type Events = {
export interface Events {
onClick: EventListener
onContextMenu: EventListener
onDoubleClick: EventListener
Expand All @@ -49,7 +47,7 @@ export type Events = {
onLostPointerCapture: EventListener
}

export type EventHandlers = {
export interface EventHandlers {
onClick?: (event: ThreeEvent<MouseEvent>) => void
onContextMenu?: (event: ThreeEvent<MouseEvent>) => void
onDoubleClick?: (event: ThreeEvent<MouseEvent>) => void
Expand Down
4 changes: 2 additions & 2 deletions packages/fiber/src/core/hooks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import * as THREE from 'three'
import * as React from 'react'
import { GLTF } from 'three/examples/jsm/loaders/GLTFLoader'
import { suspend, preload, clear } from 'suspend-react'
import { context, RootState, RenderCallback, StageTypes } from './store'
import { context, RootState, RenderCallback, UpdateCallback, StageTypes } from './store'
import { buildGraph, ObjectMap, is, useMutableCallback, useIsomorphicLayoutEffect } from './utils'
import { Stage, Stages, UpdateCallback } from './stages'
import { Stages } from './stages'
import { LoadingManager } from 'three'
import { Instance } from './renderer'

Expand Down
42 changes: 42 additions & 0 deletions packages/fiber/src/core/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -590,3 +590,45 @@ export {
act,
roots as _roots,
}

export type {
AttachFnType,
AttachType,
ConstructorRepresentation,
Catalogue,
Args,
InstanceProps,
Instance,
} from './renderer'
export type {
Subscription,
Dpr,
Size,
Viewport,
RenderCallback,
UpdateCallback,
LegacyAlways,
FrameloopMode,
FrameloopRender,
FrameloopLegacy,
Frameloop,
Performance,
Renderer,
XRManager,
RootState,
RootStore,
} from './store'
export type {
Intersection,
ThreeEvent,
DomEvent,
Events,
EventHandlers,
FilterFunction,
ComputeFunction,
EventManager,
createEvents,
} from './events'
export type { ObjectMap, Camera } from './utils'
export type { GlobalRenderCallback, GlobalEffectType, flushGlobalEffects } from './loop'
export { Stage, FixedStage, Stages } from './stages'
2 changes: 1 addition & 1 deletion packages/fiber/src/core/loop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as THREE from 'three'
import { Root } from './renderer'
import { RootState, Subscription } from './store'

type GlobalRenderCallback = (timeStamp: number) => void
export type GlobalRenderCallback = (timestamp: number) => void
type SubItem = { callback: GlobalRenderCallback }

function createSubs(callback: GlobalRenderCallback, subs: Set<SubItem>): () => void {
Comment on lines 4 to 8
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be backported to v8 in a patch release. We didn't actually export anything in #2481.

Expand Down
17 changes: 5 additions & 12 deletions packages/fiber/src/core/stages.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
import { MutableRefObject } from 'react'
import { RootState, RootStore } from './store'
import { StoreApi, UseBoundStore } from 'zustand'
import { RootState, RootStore, Subscription } from './store'

export interface UpdateCallback {
(state: RootState, delta: number, frame?: XRFrame): void
}

export type UpdateCallbackRef = MutableRefObject<UpdateCallback>
export type UpdateSubscription = { ref: UpdateCallbackRef; store: RootStore }

export type FixedStageOptions = { fixedStep?: number; maxSubsteps?: number }
export type FixedStageProps = { fixedStep: number; maxSubsteps: number; accumulator: number; alpha: number }
// TODO: Remove deprecated fields in `Subscription`
export type UpdateSubscription = Omit<Subscription, 'priority'>

/**
* Class representing a stage that updates every frame.
Expand Down Expand Up @@ -46,7 +39,7 @@ export class Stage {
* @param store - The store to be used with the callback execution.
* @returns A function to remove the subscription.
*/
add(ref: UpdateCallbackRef, store: RootStore) {
add(ref: UpdateSubscription['ref'], store: RootStore) {
this.subscribers.push({ ref, store })

return () => {
Expand Down
39 changes: 24 additions & 15 deletions packages/fiber/src/core/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,20 @@ export const privateKeys = [

export type PrivateKeys = typeof privateKeys[number]

export interface Intersection extends THREE.Intersection {
eventObject: THREE.Object3D
}

export type Subscription = {
export interface Subscription {
ref: React.MutableRefObject<RenderCallback>
priority: number
store: RootStore
}

export type Dpr = number | [min: number, max: number]
export type Size = { width: number; height: number; top: number; left: number }
export type Viewport = Size & {
export interface Size {
width: number
height: number
top: number
left: number
}
export interface Viewport extends Size {
/** The initial pixel ratio */
initialDpr: number
/** Current pixel ratio */
Expand All @@ -47,14 +48,15 @@ export type Viewport = Size & {
}

export type RenderCallback = (state: RootState, delta: number, frame?: XRFrame) => void
export type UpdateCallback = RenderCallback

type LegacyAlways = 'always'
export type LegacyAlways = 'always'
export type FrameloopMode = LegacyAlways | 'auto' | 'demand' | 'never'
type FrameloopRender = 'auto' | 'manual'
export type FrameloopRender = 'auto' | 'manual'
export type FrameloopLegacy = 'always' | 'demand' | 'never'
export type Frameloop = FrameloopLegacy | { mode?: FrameloopMode; render?: FrameloopRender; maxDelta?: number }

export type Performance = {
export interface Performance {
/** Current performance normal, between min and max */
current: number
/** How low the performance can go, between 0 and max */
Expand All @@ -67,12 +69,14 @@ export type Performance = {
regress: () => void
}

export type Renderer = { render: (scene: THREE.Scene, camera: THREE.Camera) => any }
export interface Renderer {
render: (scene: THREE.Scene, camera: THREE.Camera) => any
}
export const isRenderer = (def: any) => !!def?.render

export type StageTypes = Stage | FixedStage

export type InternalState = {
export interface InternalState {
interaction: THREE.Object3D[]
hovered: Map<string, ThreeEvent<DomEvent>>
subscribers: Subscription[]
Expand All @@ -92,7 +96,12 @@ export type InternalState = {
subscribe: (callback: React.MutableRefObject<RenderCallback>, priority: number, store: RootStore) => () => void
}

export type RootState = {
export interface XRManager {
connect: () => void
disconnect: () => void
}

export interface RootState {
/** Set current state */
set: StoreApi<RootState>['setState']
/** Get current state */
Expand All @@ -110,7 +119,7 @@ export type RootState = {
/** Event layer interface, contains the event handler and the node they're connected to */
events: EventManager<any>
/** XR interface */
xr: { connect: () => void; disconnect: () => void }
xr: XRManager
/** Currently used controls */
controls: THREE.EventDispatcher | null
/** Normalized event coordinates */
Expand Down Expand Up @@ -204,7 +213,7 @@ const createStore = (
camera: null as unknown as Camera,
raycaster: null as unknown as THREE.Raycaster,
events: { priority: 1, enabled: true, connected: false },
xr: null as unknown as { connect: () => void; disconnect: () => void },
xr: null as unknown as XRManager,

invalidate: (frames = 1) => invalidate(get(), frames),
advance: (timestamp: number, runGlobalEffects?: boolean) => advance(timestamp, runGlobalEffects, get()),
Expand Down
26 changes: 1 addition & 25 deletions packages/fiber/src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,28 +1,4 @@
export * from './core'
export * from './three-types'
export type {
AttachFnType,
AttachType,
ConstructorRepresentation,
Catalogue,
Args,
InstanceProps,
Instance,
} from './core/renderer'
export type {
Intersection,
Subscription,
Dpr,
Size,
Viewport,
RenderCallback,
Performance,
RootState,
RootStore,
} from './core/store'
export type { ThreeEvent, Events, EventManager, ComputeFunction } from './core/events'
export type { ObjectMap, Camera } from './core/utils'
export * from './web/Canvas'
export { createEvents } from './core/events'
export { createPointerEvents as events } from './web/events'
export * from './core'
export { Stage, FixedStage, Stages } from './core/stages'
24 changes: 1 addition & 23 deletions packages/fiber/src/native.tsx
Original file line number Diff line number Diff line change
@@ -1,26 +1,4 @@
export * from './three-types'
export type {
AttachFnType,
AttachType,
ConstructorRepresentation,
Catalogue,
Args,
InstanceProps,
Instance,
} from './core/renderer'
export type {
Intersection,
Subscription,
Dpr,
Size,
Viewport,
RenderCallback,
Performance,
RootState,
RootStore,
} from './core/store'
export type { ThreeEvent, Events, EventManager, ComputeFunction } from './core/events'
export type { ObjectMap, Camera } from './core/utils'
export * from './core'
export * from './native/Canvas'
export { createTouchEvents as events } from './native/events'
export * from './core'