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'
2
10
import * as G from 'shared/globals'
3
11
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'
7
14
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'
9
19
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
12
24
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 >
14
33
15
34
/** The props that are cached by `Controller` objects */
16
35
interface CachedProps < State extends Indexable > extends RunAsyncState < State > {
17
36
onFrame ?: OnFrame < State >
18
37
}
19
38
20
39
/** An update that hasn't been applied yet */
21
- type PendingProps < State extends Indexable > = SpringProps < State > & {
40
+ type PendingProps < State extends Indexable > = ControllerProps < State > & {
22
41
keys : string [ ]
23
42
}
24
43
@@ -37,7 +56,7 @@ export class Controller<State extends Indexable = UnknownProps> {
37
56
springs : Indexable < SpringValue > = { }
38
57
39
58
/** The values that changed in the last animation frame */
40
- frame : LatestValues < State > = { }
59
+ frame : UnknownPartial < State > = { }
41
60
42
61
/** The current props for the controller only */
43
62
props : CachedProps < State > = { }
@@ -48,7 +67,7 @@ export class Controller<State extends Indexable = UnknownProps> {
48
67
/** The queue of pending props */
49
68
queue : PendingProps < State > [ ] = [ ]
50
69
51
- constructor ( props ?: SpringProps < State > ) {
70
+ constructor ( props ?: ControllerProps < State > ) {
52
71
this . _onChange = this . _onChange . bind ( this )
53
72
this . _onFrame = this . _onFrame . bind ( this )
54
73
if ( props ) {
@@ -72,7 +91,7 @@ export class Controller<State extends Indexable = UnknownProps> {
72
91
}
73
92
74
93
/** Push an update onto the queue of each value. */
75
- update ( propsArg : SpringProps < State > | Falsy ) {
94
+ update ( propsArg : ControllerProps < State > | Falsy ) {
76
95
if ( ! propsArg ) return this
77
96
78
97
// This returns a new object every time.
@@ -189,7 +208,7 @@ export class Controller<State extends Indexable = UnknownProps> {
189
208
}
190
209
191
210
/** @internal Attached as an observer to every spring */
192
- protected _onChange ( value : any , spring : SpringValue ) {
211
+ protected _onChange ( value : any , spring : AnimationValue ) {
193
212
if ( this . props . onFrame ) {
194
213
this . frame [ spring . key as keyof State ] = value
195
214
G . frameLoop . onFrame ( this . _onFrame )
@@ -206,16 +225,16 @@ export class Controller<State extends Indexable = UnknownProps> {
206
225
}
207
226
208
227
/** 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 ) =>
212
231
each ( obj , ( value , key ) => {
213
232
if ( ! is . und ( value ) ) {
214
233
keys . add ( key )
215
234
}
216
235
} )
217
236
218
- const { from, to } = props as any
237
+ const { from, to } = props
219
238
if ( is . obj ( to ) ) extract ( to )
220
239
if ( from ) extract ( from )
221
240
0 commit comments