Skip to content

Commit

Permalink
types: get everything to work (almost)
Browse files Browse the repository at this point in the history
So much work T_T

Idk if this affects #613 or not.
  • Loading branch information
aleclarson committed Sep 27, 2019
1 parent c74d4c0 commit 5d583de
Show file tree
Hide file tree
Showing 22 changed files with 624 additions and 509 deletions.
1 change: 1 addition & 0 deletions packages/animated/src/AnimatedObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { isAnimationValue } from './AnimationValue'

type Source = Indexable | null

/** An object containing `Animated` nodes */
export class AnimatedObject extends Animated {
protected source!: Source
constructor(source: Source = null) {
Expand Down
53 changes: 36 additions & 17 deletions packages/core/src/Controller.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,43 @@
import { is, each, OneOrMore, toArray, UnknownProps } from 'shared'
import {
is,
each,
OneOrMore,
toArray,
UnknownProps,
UnknownPartial,
AnyKey,
} from 'shared'
import * as G from 'shared/globals'

import { SpringValue } from './SpringValue'
import { interpolateTo } from './helpers'
import { runAsync, scheduleProps, RunAsyncState, AsyncResult } from './runAsync'
import { SpringProps, FluidProps } from './types/spring'
import { AnimationEvents } from './types/animated'
import { Indexable, Falsy } from './types/common'
import { SpringProps } from './types/spring'
import { runAsync, scheduleProps, RunAsyncState, AsyncResult } from './runAsync'
import { interpolateTo } from './helpers'
import { SpringValue } from './SpringValue'
import { AnimationValue } from '@react-spring/animated'

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

type OnFrame<State extends Indexable> = (frame: LatestValues<State>) => void
export type ControllerProps<State extends Indexable = UnknownProps> = {
/**
* Called on every frame when animations are active
*/
onFrame?: OnFrame<State>
} & SpringProps<State> &
UnknownPartial<FluidProps<State>> &
AnimationEvents<unknown>

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

/** An update that hasn't been applied yet */
type PendingProps<State extends Indexable> = SpringProps<State> & {
type PendingProps<State extends Indexable> = ControllerProps<State> & {
keys: string[]
}

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

/** The values that changed in the last animation frame */
frame: LatestValues<State> = {}
frame: UnknownPartial<State> = {}

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

constructor(props?: SpringProps<State>) {
constructor(props?: ControllerProps<State>) {
this._onChange = this._onChange.bind(this)
this._onFrame = this._onFrame.bind(this)
if (props) {
Expand All @@ -72,7 +91,7 @@ export class Controller<State extends Indexable = UnknownProps> {
}

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

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

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

/** Determine which keys should receive an update */
function extractKeys(props: SpringProps, springs: Indexable<SpringValue>) {
const keys = new Set<string>()
const extract = (obj: object) =>
function extractKeys(props: ControllerProps, springs: Indexable<SpringValue>) {
const keys = new Set<AnyKey>()
const extract = (obj: Indexable) =>
each(obj, (value, key) => {
if (!is.und(value)) {
keys.add(key)
}
})

const { from, to } = props as any
const { from, to } = props
if (is.obj(to)) extract(to)
if (from) extract(from)

Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/FrameLoop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ export class FrameLoop {

invariant(
!Number.isNaN(position),
`Found NaN value while advancing "${spring.key}" animation`
`Found NaN value while advancing "${spring.key as any}" animation`
)

if (position !== node.lastPosition) {
Expand Down
Loading

0 comments on commit 5d583de

Please sign in to comment.