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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
import { OneOrMore, UnknownProps, Lookup, Falsy } from '@react-spring/types'
import {
  is,
  raf,
  each,
  noop,
  flush,
  toArray,
  eachProp,
  flushCalls,
  addFluidObserver,
  FluidObserver,
} from '@react-spring/shared'

import { getDefaultProp } from './helpers'
import { FrameValue } from './FrameValue'
import type { SpringRef } from './SpringRef'
import { SpringValue, createLoopUpdate, createUpdate } from './SpringValue'
import { getCancelledResult, getCombinedResult } from './AnimationResult'
import { runAsync, RunAsyncState, stopAsync } from './runAsync'
import { scheduleProps } from './scheduleProps'
import {
  AnimationResult,
  AsyncResult,
  ControllerFlushFn,
  ControllerUpdate,
  OnChange,
  OnRest,
  OnStart,
  SpringChain,
  SpringToFn,
  SpringValues,
} from './types'

/** Events batched by the `Controller` class */
const BATCHED_EVENTS = ['onStart', 'onChange', 'onRest'] as const

let nextId = 1

/** Queue of pending updates for a `Controller` instance. */
export interface ControllerQueue<State extends Lookup = Lookup>
  extends Array<
    ControllerUpdate<State, any> & {
      /** The keys affected by this update. When null, all keys are affected. */
      keys: string[] | null
    }
  > {}

export class Controller<State extends Lookup = Lookup> {
  readonly id = nextId++

  /** The animated values */
  springs: SpringValues<State> = {} as any

  /** The queue of props passed to the `update` method. */
  queue: ControllerQueue<State> = []

  /**
   * The injected ref. When defined, render-based updates are pushed
   * onto the `queue` instead of being auto-started.
   */
  ref?: SpringRef<State>

  /** Custom handler for flushing update queues */
  protected _flush?: ControllerFlushFn<this>

  /** These props are used by all future spring values */
  protected _initialProps?: Lookup

  /** The counter for tracking `scheduleProps` calls */
  protected _lastAsyncId = 0

  /** The values currently being animated */
  protected _active = new Set<FrameValue>()

  /** The values that changed recently */
  protected _changed = new Set<FrameValue>()

  /** Equals false when `onStart` listeners can be called */
  protected _started = false

  private _item?: any

  /** State used by the `runAsync` function */
  protected _state: RunAsyncState<this> = {
    paused: false,
    pauseQueue: new Set(),
    resumeQueue: new Set(),
    timeouts: new Set(),
  }

  /** The event queues that are flushed once per frame maximum */
  protected _events = {
    onStart: new Map<
      OnStart<SpringValue<State>, Controller<State>, any>,
      AnimationResult
    >(),
    onChange: new Map<
      OnChange<SpringValue<State>, Controller<State>, any>,
      AnimationResult
    >(),
    onRest: new Map<
      OnRest<SpringValue<State>, Controller<State>, any>,
      AnimationResult
    >(),
  }

  constructor(
    props?: ControllerUpdate<State> | null,
    flush?: ControllerFlushFn<any>
  ) {
    this._onFrame = this._onFrame.bind(this)
    if (flush) {
      this._flush = flush
    }
    if (props) {
      this.start({ default: true, ...props })
    }
  }

  /**
   * Equals `true` when no spring values are in the frameloop, and
   * no async animation is currently active.
   */
  get idle() {
    return (
      !this._state.asyncTo &&
      Object.values(this.springs as Lookup<SpringValue>).every(spring => {
        return spring.idle && !spring.isDelayed && !spring.isPaused
      })
    )
  }

  get item() {
    return this._item
  }

  set item(item) {
    this._item = item
  }

  /** Get the current values of our springs */
  get(): State & UnknownProps {
    const values: any = {}
    this.each((spring, key) => (values[key] = spring.get()))
    return values
  }

  /** Set the current values without animating. */
  set(values: Partial<State>) {
    for (const key in values) {
      const value = values[key]
      if (!is.und(value)) {
        this.springs[key].set(value)
      }
    }
  }

  /** Push an update onto the queue of each value. */
  update(props: ControllerUpdate<State> | Falsy) {
    if (props) {
      this.queue.push(createUpdate(props))
    }
    return this
  }

  /**
   * Start the queued animations for every spring, and resolve the returned
   * promise once all queued animations have finished or been cancelled.
   *
   * When you pass a queue (instead of nothing), that queue is used instead of
   * the queued animations added with the `update` method, which are left alone.
   */
  start(props?: OneOrMore<ControllerUpdate<State>> | null): AsyncResult<this> {
    let { queue } = this as any
    if (props) {
      queue = toArray<any>(props).map(createUpdate)
    } else {
      this.queue = []
    }

    if (this._flush) {
      return this._flush(this, queue)
    }

    prepareKeys(this, queue)
    return flushUpdateQueue(this, queue)
  }

  /** Stop all animations. */
  stop(): this
  /** Stop animations for the given keys. */
  stop(keys: OneOrMore<string>): this
  /** Cancel all animations. */
  stop(cancel: boolean): this
  /** Cancel animations for the given keys. */
  stop(cancel: boolean, keys: OneOrMore<string>): this
  /** Stop some or all animations. */
  stop(keys?: OneOrMore<string>): this
  /** Cancel some or all animations. */
  stop(cancel: boolean, keys?: OneOrMore<string>): this
  /** @internal */
  stop(arg?: boolean | OneOrMore<string>, keys?: OneOrMore<string>) {
    if (arg !== !!arg) {
      keys = arg as OneOrMore<string>
    }
    if (keys) {
      const springs = this.springs as Lookup<SpringValue>
      each(toArray(keys) as string[], key => springs[key].stop(!!arg))
    } else {
      stopAsync(this._state, this._lastAsyncId)
      this.each(spring => spring.stop(!!arg))
    }
    return this
  }

  /** Freeze the active animation in time */
  pause(keys?: OneOrMore<string>) {
    if (is.und(keys)) {
      this.start({ pause: true })
    } else {
      const springs = this.springs as Lookup<SpringValue>
      each(toArray(keys) as string[], key => springs[key].pause())
    }
    return this
  }

  /** Resume the animation if paused. */
  resume(keys?: OneOrMore<string>) {
    if (is.und(keys)) {
      this.start({ pause: false })
    } else {
      const springs = this.springs as Lookup<SpringValue>
      each(toArray(keys) as string[], key => springs[key].resume())
    }
    return this
  }

  /** Call a function once per spring value */
  each(iterator: (spring: SpringValue, key: string) => void) {
    eachProp(this.springs, iterator as any)
  }

  /** @internal Called at the end of every animation frame */
  protected _onFrame() {
    const { onStart, onChange, onRest } = this._events

    const active = this._active.size > 0
    const changed = this._changed.size > 0

    if ((active && !this._started) || (changed && !this._started)) {
      this._started = true
      flush(onStart, ([onStart, result]) => {
        result.value = this.get()
        onStart(result, this, this._item)
      })
    }

    const idle = !active && this._started
    const values = changed || (idle && onRest.size) ? this.get() : null

    if (changed && onChange.size) {
      flush(onChange, ([onChange, result]) => {
        result.value = values
        onChange(result, this, this._item)
      })
    }

    // The "onRest" queue is only flushed when all springs are idle.
    if (idle) {
      this._started = false
      flush(onRest, ([onRest, result]) => {
        result.value = values
        onRest(result, this, this._item)
      })
    }
  }

  /** @internal */
  eventObserved(event: FrameValue.Event) {
    if (event.type == 'change') {
      this._changed.add(event.parent)
      if (!event.idle) {
        this._active.add(event.parent)
      }
    } else if (event.type == 'idle') {
      this._active.delete(event.parent)
    }
    // The `onFrame` handler runs when a parent is changed or idle.
    else return
    raf.onFrame(this._onFrame)
  }
}

/**
 * Warning: Props might be mutated.
 */
export function flushUpdateQueue(
  ctrl: Controller<any>,
  queue: ControllerQueue
) {
  return Promise.all(queue.map(props => flushUpdate(ctrl, props))).then(
    results => getCombinedResult(ctrl, results)
  )
}

/**
 * Warning: Props might be mutated.
 *
 * Process a single set of props using the given controller.
 *
 * The returned promise resolves to `true` once the update is
 * applied and any animations it starts are finished without being
 * stopped or cancelled.
 */
export async function flushUpdate(
  ctrl: Controller<any>,
  props: ControllerQueue[number],
  isLoop?: boolean
): AsyncResult {
  const { keys, to, from, loop, onRest, onResolve } = props
  const defaults = is.obj(props.default) && props.default

  // Looping must be handled in this function, or else the values
  // would end up looping out-of-sync in many common cases.
  if (loop) {
    props.loop = false
  }

  // Treat false like null, which gets ignored.
  if (to === false) props.to = null
  if (from === false) props.from = null

  const asyncTo = is.arr(to) || is.fun(to) ? to : undefined
  if (asyncTo) {
    props.to = undefined
    props.onRest = undefined
    if (defaults) {
      defaults.onRest = undefined
    }
  }
  // For certain events, use batching to prevent multiple calls per frame.
  // However, batching is avoided when the `to` prop is async, because any
  // event props are used as default props instead.
  else {
    each(BATCHED_EVENTS, key => {
      const handler: any = props[key]
      if (is.fun(handler)) {
        const queue = ctrl['_events'][key]
        props[key] = (({ finished, cancelled }: AnimationResult) => {
          const result = queue.get(handler)
          if (result) {
            if (!finished) result.finished = false
            if (cancelled) result.cancelled = true
          } else {
            // The "value" is set before the "handler" is called.
            queue.set(handler, {
              value: null,
              finished: finished || false,
              cancelled: cancelled || false,
            })
          }
        }) as any

        // Avoid using a batched `handler` as a default prop.
        if (defaults) {
          defaults[key] = props[key] as any
        }
      }
    })
  }

  const state = ctrl['_state']

  // Pause/resume the `asyncTo` when `props.pause` is true/false.
  if (props.pause === !state.paused) {
    state.paused = props.pause
    flushCalls(props.pause ? state.pauseQueue : state.resumeQueue)
  }
  // When a controller is paused, its values are also paused.
  else if (state.paused) {
    props.pause = true
  }

  const promises: AsyncResult[] = (keys || Object.keys(ctrl.springs)).map(key =>
    ctrl.springs[key]!.start(props as any)
  )

  const cancel =
    props.cancel === true || getDefaultProp(props, 'cancel') === true

  if (asyncTo || (cancel && state.asyncId)) {
    promises.push(
      scheduleProps(++ctrl['_lastAsyncId'], {
        props,
        state,
        actions: {
          pause: noop,
          resume: noop,
          start(props, resolve) {
            if (cancel) {
              stopAsync(state, ctrl['_lastAsyncId'])
              resolve(getCancelledResult(ctrl))
            } else {
              props.onRest = onRest
              resolve(
                runAsync(
                  asyncTo as SpringChain | SpringToFn,
                  props,
                  state,
                  ctrl
                )
              )
            }
          },
        },
      })
    )
  }

  // Pause after updating each spring, so they can be resumed separately
  // and so their default `pause` and `cancel` props are updated.
  if (state.paused) {
    // Ensure `this` must be resumed before the returned promise
    // is resolved and before starting the next `loop` repetition.
    await new Promise<void>(resume => {
      state.resumeQueue.add(resume)
    })
  }

  const result = getCombinedResult<any>(ctrl, await Promise.all(promises))
  if (loop && result.finished && !(isLoop && result.noop)) {
    const nextProps = createLoopUpdate(props, loop, to)
    if (nextProps) {
      prepareKeys(ctrl, [nextProps])
      return flushUpdate(ctrl, nextProps, true)
    }
  }
  if (onResolve) {
    raf.batchedUpdates(() => onResolve(result, ctrl, ctrl.item))
  }
  return result
}

/**
 * From an array of updates, get the map of `SpringValue` objects
 * by their keys. Springs are created when any update wants to
 * animate a new key.
 *
 * Springs created by `getSprings` are neither cached nor observed
 * until they're given to `setSprings`.
 */
export function getSprings<State extends Lookup>(
  ctrl: Controller<Lookup<any>>,
  props?: OneOrMore<ControllerUpdate<State>>
) {
  const springs = { ...ctrl.springs }
  if (props) {
    each(toArray(props), (props: any) => {
      if (is.und(props.keys)) {
        props = createUpdate(props)
      }
      if (!is.obj(props.to)) {
        // Avoid passing array/function to each spring.
        props = { ...props, to: undefined }
      }
      prepareSprings(springs as any, props, key => {
        return createSpring(key)
      })
    })
  }
  setSprings(ctrl, springs)
  return springs
}

/**
 * Tell a controller to manage the given `SpringValue` objects
 * whose key is not already in use.
 */
export function setSprings(
  ctrl: Controller<Lookup<any>>,
  springs: SpringValues<UnknownProps>
) {
  eachProp(springs, (spring, key) => {
    if (!ctrl.springs[key]) {
      ctrl.springs[key] = spring
      addFluidObserver(spring, ctrl)
    }
  })
}

function createSpring(key: string, observer?: FluidObserver<FrameValue.Event>) {
  const spring = new SpringValue()
  spring.key = key
  if (observer) {
    addFluidObserver(spring, observer)
  }
  return spring
}

/**
 * Ensure spring objects exist for each defined key.
 *
 * Using the `props`, the `Animated` node of each `SpringValue` may
 * be created or updated.
 */
function prepareSprings(
  springs: SpringValues,
  props: ControllerQueue[number],
  create: (key: string) => SpringValue
) {
  if (props.keys) {
    each(props.keys, key => {
      const spring = springs[key] || (springs[key] = create(key))
      spring['_prepareNode'](props)
    })
  }
}

/**
 * Ensure spring objects exist for each defined key, and attach the
 * `ctrl` to them for observation.
 *
 * The queue is expected to contain `createUpdate` results.
 */
function prepareKeys(ctrl: Controller<any>, queue: ControllerQueue[number][]) {
  each(queue, props => {
    prepareSprings(ctrl.springs, props, key => {
      return createSpring(key, ctrl)
    })
  })
}