Skip to content

Commit 3862a03

Browse files
committed
wip: round 3
1 parent 6fa673c commit 3862a03

File tree

1 file changed

+18
-16
lines changed

1 file changed

+18
-16
lines changed

src/animated/Controller.ts

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -41,28 +41,30 @@ class Controller<DS extends object = any> {
4141

4242
getValues = () => this.interpolations
4343

44-
/** update(props)
45-
* This function filters input props and creates an array of tasks which are executed in .start()
46-
* Each task is allowed to carry a delay, which means it can execute asnychroneously */
47-
update(args: UpdateProps<DS>) {
44+
/**
45+
* Update the controller by merging the given props into an array of tasks.
46+
* Individual tasks may be async and/or delayed.
47+
*/
48+
update(props: UpdateProps<DS>) {
4849
// Extract delay and the to-prop from props
49-
const { delay = 0, to, ...props } = interpolateTo(args) as any
50+
const { delay = 0, to, ...restProps } = interpolateTo(props) as any
5051

5152
// If config is either a function or an array, queue it up as is
5253
if (is.arr(to) || is.fun(to)) {
53-
this.queue.push({ ...props, delay, to })
54+
this.queue.push({ ...restProps, delay, to })
5455
}
5556
// Otherwise go through each key since it could be delayed individually
5657
else if (to) {
5758
let ops: any[] = []
5859
Object.entries(to).forEach(([k, v]) => {
59-
// Fetch delay and create an entry, consisting of the to-props, the delay, and basic props
60-
const entry = { to: { [k]: v }, delay: callProp(delay, k), ...props }
61-
const previous = ops[entry.delay] && ops[entry.delay].to
62-
ops[entry.delay] = {
63-
...ops[entry.delay],
64-
...entry,
65-
to: { ...previous, ...entry.to },
60+
// Merge entries with the same delay
61+
const dt = callProp(delay, k)
62+
const previous = ops[dt]
63+
ops[dt] = {
64+
...previous,
65+
...restProps,
66+
delay: dt,
67+
to: { ...previous.to, [k]: v },
6668
}
6769
})
6870
ops.forEach(op => this.queue.push(op))
@@ -72,7 +74,7 @@ class Controller<DS extends object = any> {
7274
this.queue.sort((a, b) => a.delay - b.delay)
7375

7476
// Diff the reduced props immediately (they'll contain the from-prop and some config)
75-
if (hasKeys(props)) this._diff(props)
77+
if (hasKeys(restProps)) this._diff(restProps)
7678

7779
return this
7880
}
@@ -105,12 +107,12 @@ class Controller<DS extends object = any> {
105107
this.onEndQueue.length = 0
106108

107109
// Never assume that the last update always finishes last, since that's
108-
// not true when 2+ async animations have indeterminate durations.
110+
// not true when 2+ async updates have indeterminate durations.
109111
let remaining = queue.length
110112
const didEnd =
111113
onEnd &&
112114
((finished?: boolean) => {
113-
if (--remaining < 0 || guid !== this.guid) return
115+
if (--remaining > 0 || guid !== this.guid) return
114116
onEnd(finished)
115117
})
116118

0 commit comments

Comments
 (0)