|
|
import { each, useIsomorphicLayoutEffect } from '@react-spring/shared' |
|
|
import { SpringRef } from '../SpringRef' |
|
|
import { callProp } from '../helpers' |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function useChain( |
|
|
refs: ReadonlyArray<SpringRef>, |
|
|
timeSteps?: number[], |
|
|
timeFrame = 1000 |
|
|
) { |
|
|
useIsomorphicLayoutEffect(() => { |
|
|
if (timeSteps) { |
|
|
let prevDelay = 0 |
|
|
each(refs, (ref, i) => { |
|
|
const controllers = ref.current |
|
|
if (controllers.length) { |
|
|
let delay = timeFrame * timeSteps[i] |
|
|
|
|
|
|
|
|
if (isNaN(delay)) delay = prevDelay |
|
|
else prevDelay = delay |
|
|
|
|
|
each(controllers, ctrl => { |
|
|
each(ctrl.queue, props => { |
|
|
|
|
|
const memoizedDelayProp = props.delay |
|
|
props.delay = key => delay + callProp(memoizedDelayProp || 0, key) |
|
|
}) |
|
|
}) |
|
|
|
|
|
ref.start() |
|
|
} |
|
|
}) |
|
|
} else { |
|
|
let p: Promise<any> = Promise.resolve() |
|
|
each(refs, ref => { |
|
|
const controllers = ref.current |
|
|
if (controllers.length) { |
|
|
|
|
|
const queues = controllers.map(ctrl => { |
|
|
const q = ctrl.queue |
|
|
ctrl.queue = [] |
|
|
return q |
|
|
}) |
|
|
|
|
|
|
|
|
p = p.then(() => { |
|
|
each(controllers, (ctrl, i) => |
|
|
each(queues[i] || [], update => ctrl.queue.push(update)) |
|
|
) |
|
|
return Promise.all(ref.start()) |
|
|
}) |
|
|
} |
|
|
}) |
|
|
} |
|
|
}) |
|
|
} |
|
|
|