Skip to content

Commit d32dc87

Browse files
committed
types: get everything to work (almost)
So much work T_T Idk if this affects #613 or not.
1 parent 661cea0 commit d32dc87

22 files changed

+625
-509
lines changed

packages/animated/src/AnimatedObject.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { isAnimationValue } from './AnimationValue'
55

66
type Source = Indexable | null
77

8+
/** An object containing `Animated` nodes */
89
export class AnimatedObject extends Animated {
910
protected source!: Source
1011
constructor(source: Source = null) {

packages/core/src/Controller.ts

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,43 @@
1-
import { is, each, OneOrMore, toArray, UnknownProps } from 'shared'
1+
import {
2+
is,
3+
each,
4+
OneOrMore,
5+
toArray,
6+
UnknownProps,
7+
UnknownPartial,
8+
AnyKey,
9+
} from 'shared'
210
import * as G from 'shared/globals'
311

4-
import { SpringValue } from './SpringValue'
5-
import { interpolateTo } from './helpers'
6-
import { runAsync, scheduleProps, RunAsyncState, AsyncResult } from './runAsync'
12+
import { SpringProps, FluidProps } from './types/spring'
13+
import { AnimationEvents } from './types/animated'
714
import { Indexable, Falsy } from './types/common'
8-
import { SpringProps } from './types/spring'
15+
import { runAsync, scheduleProps, RunAsyncState, AsyncResult } from './runAsync'
16+
import { interpolateTo } from './helpers'
17+
import { SpringValue } from './SpringValue'
18+
import { AnimationValue } from '@react-spring/animated'
919

10-
/** The latest values of a `Controller` object */
11-
type LatestValues<State extends Indexable> = Partial<State>
20+
/** A callback that receives the changed values for each frame. */
21+
export type OnFrame<State extends Indexable> = (
22+
frame: UnknownPartial<State>
23+
) => void
1224

13-
type OnFrame<State extends Indexable> = (frame: LatestValues<State>) => void
25+
export type ControllerProps<State extends Indexable = UnknownProps> = {
26+
/**
27+
* Called on every frame when animations are active
28+
*/
29+
onFrame?: OnFrame<State>
30+
} & SpringProps<State> &
31+
UnknownPartial<FluidProps<State>> &
32+
AnimationEvents<unknown>
1433

1534
/** The props that are cached by `Controller` objects */
1635
interface CachedProps<State extends Indexable> extends RunAsyncState<State> {
1736
onFrame?: OnFrame<State>
1837
}
1938

2039
/** An update that hasn't been applied yet */
21-
type PendingProps<State extends Indexable> = SpringProps<State> & {
40+
type PendingProps<State extends Indexable> = ControllerProps<State> & {
2241
keys: string[]
2342
}
2443

@@ -37,7 +56,7 @@ export class Controller<State extends Indexable = UnknownProps> {
3756
springs: Indexable<SpringValue> = {}
3857

3958
/** The values that changed in the last animation frame */
40-
frame: LatestValues<State> = {}
59+
frame: UnknownPartial<State> = {}
4160

4261
/** The current props for the controller only */
4362
props: CachedProps<State> = {}
@@ -48,7 +67,7 @@ export class Controller<State extends Indexable = UnknownProps> {
4867
/** The queue of pending props */
4968
queue: PendingProps<State>[] = []
5069

51-
constructor(props?: SpringProps<State>) {
70+
constructor(props?: ControllerProps<State>) {
5271
this._onChange = this._onChange.bind(this)
5372
this._onFrame = this._onFrame.bind(this)
5473
if (props) {
@@ -72,7 +91,7 @@ export class Controller<State extends Indexable = UnknownProps> {
7291
}
7392

7493
/** Push an update onto the queue of each value. */
75-
update(propsArg: SpringProps<State> | Falsy) {
94+
update(propsArg: ControllerProps<State> | Falsy) {
7695
if (!propsArg) return this
7796

7897
// This returns a new object every time.
@@ -189,7 +208,7 @@ export class Controller<State extends Indexable = UnknownProps> {
189208
}
190209

191210
/** @internal Attached as an observer to every spring */
192-
protected _onChange(value: any, spring: SpringValue) {
211+
protected _onChange(value: any, spring: AnimationValue) {
193212
if (this.props.onFrame) {
194213
this.frame[spring.key as keyof State] = value
195214
G.frameLoop.onFrame(this._onFrame)
@@ -206,16 +225,16 @@ export class Controller<State extends Indexable = UnknownProps> {
206225
}
207226

208227
/** Determine which keys should receive an update */
209-
function extractKeys(props: SpringProps, springs: Indexable<SpringValue>) {
210-
const keys = new Set<string>()
211-
const extract = (obj: object) =>
228+
function extractKeys(props: ControllerProps, springs: Indexable<SpringValue>) {
229+
const keys = new Set<AnyKey>()
230+
const extract = (obj: Indexable) =>
212231
each(obj, (value, key) => {
213232
if (!is.und(value)) {
214233
keys.add(key)
215234
}
216235
})
217236

218-
const { from, to } = props as any
237+
const { from, to } = props
219238
if (is.obj(to)) extract(to)
220239
if (from) extract(from)
221240

packages/core/src/FrameLoop.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ export class FrameLoop {
289289

290290
invariant(
291291
!Number.isNaN(position),
292-
`Found NaN value while advancing "${spring.key}" animation`
292+
`Found NaN value while advancing "${spring.key as any}" animation`
293293
)
294294

295295
if (position !== node.lastPosition) {

0 commit comments

Comments
 (0)