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,IAAI,CAAC,MAAM,CAAC,IAAI,EAAE,CAAC;IAC5B,CAAC;CAyIF;AACD,eAAe,aAAa,CAAC;AAE7B,iCAAoC,GAAW;IAC7C,MAAM,CAAC;QACL,SAAS,CAAC,KAAQ;YAChB,MAAM,CAAC;gBACL,CAAC,GAAG,CAAC,EAAE,KAAK;aACb,CAAC;QACJ,CAAC;KACF,CAAC;AACJ,CAAC\"}","code":"/** @license\r\n *  Copyright 2016 - present The Material Motion Authors. All Rights Reserved.\r\n *\r\n *  Licensed under the Apache License, Version 2.0 (the \"License\"); you may not\r\n *  use this file except in compliance with the License. You may obtain a copy\r\n *  of the License at\r\n *\r\n *      http://www.apache.org/licenses/LICENSE-2.0\r\n *\r\n *  Unless required by applicable law or agreed to in writing, software\r\n *  distributed under the License is distributed on an \"AS IS\" BASIS, WITHOUT\r\n *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the\r\n *  License for the specific language governing permissions and limitations\r\n *  under the License.\r\n */\r\nimport { Spring as WobbleSpring, } from 'wobble';\r\nimport { createProperty, } from '../observables/createProperty';\r\nimport { MotionObservable, } from '../observables/MotionObservable';\r\nimport { State, } from '../enums';\r\nexport const DEFAULT_STIFFNESS = 342;\r\nexport const DEFAULT_DAMPING = 30;\r\nexport const DEFAULT_THRESHOLD = .001;\r\n// Springs are a core primitive in Material Motion, yet, the implementations we\r\n// have are all from 3rd party libraries.  Thus, the common\r\n// getters/setters/properties live here, and each implementation can extend it\r\n// to implement its own `value$`.\r\nexport class NumericSpring {\r\n    constructor() {\r\n        this.destination$ = createProperty({\r\n            initialValue: 0,\r\n        });\r\n        this.initialValue$ = createProperty({\r\n            initialValue: 0,\r\n        });\r\n        this.initialVelocity$ = createProperty({\r\n            initialValue: 0,\r\n        });\r\n        this.stiffness$ = createProperty({\r\n            initialValue: DEFAULT_STIFFNESS,\r\n        });\r\n        this.damping$ = createProperty({\r\n            initialValue: DEFAULT_DAMPING,\r\n        });\r\n        this.threshold$ = createProperty({\r\n            initialValue: DEFAULT_THRESHOLD,\r\n        });\r\n        this.enabled$ = createProperty({\r\n            initialValue: true,\r\n        });\r\n        this.state$ = createProperty({\r\n            initialValue: State.AT_REST,\r\n        });\r\n        this.value$ = new MotionObservable((observer) => {\r\n            const spring = new WobbleSpring();\r\n            const updateSpringConfig = spring.updateConfig.bind(spring);\r\n            let initialized = false;\r\n            let initialValueChangedWhileDisabled = false;\r\n            let initialVelocityChangedWhileDisabled = false;\r\n            this.state$.write(State.AT_REST);\r\n            spring.onStart(() => {\r\n                this.state$.write(State.ACTIVE);\r\n            });\r\n            spring.onUpdate(() => {\r\n                observer.next(spring.currentValue);\r\n            });\r\n            spring.onStop(() => {\r\n                this.state$.write(State.AT_REST);\r\n            });\r\n            // During initialization, the configuration values will be set in the\r\n            // order they are subscribed to; hence,\r\n            const subscriptions = [\r\n                // properties that configure the spring\r\n                this.stiffness$._map(transformToValueWithKey('stiffness')).subscribe(updateSpringConfig),\r\n                this.damping$._map(transformToValueWithKey('damping')).subscribe(updateSpringConfig),\r\n                this.threshold$._map(transformToValueWithKey('restDisplacementThreshold')).subscribe(updateSpringConfig),\r\n                // properties that initialize the spring\r\n                this.initialVelocity$.subscribe((initialVelocity) => {\r\n                    if (this.enabled$.read()) {\r\n                        updateSpringConfig({ initialVelocity });\r\n                    }\r\n                    else {\r\n                        initialVelocityChangedWhileDisabled = true;\r\n                    }\r\n                }),\r\n                this.initialValue$.subscribe((initialValue) => {\r\n                    // We need to figure out what the right interplay is between\r\n                    // `initialValue` and `value$`.  It's probably more convenient for\r\n                    // an author if changes to `initialValue` are passed through to\r\n                    // `value$`. This means you can do something like\r\n                    // `spring.value$.subscribe(location$)` and trust `location$` is\r\n                    // always up-to-date.  However, it's unclear how that would affect\r\n                    // `state$`.  Should `state$` go at_rest -> active -> at_rest every\r\n                    // time something touches `initialValue`?  Should `value$` be able\r\n                    // to emit `initialValue$` without touching `value$`?  Should we\r\n                    // require authors to worry about connecting the two?\r\n                    //\r\n                    // Punting on this for now and forcing authors to deal with it.\r\n                    if (this.enabled$.read()) {\r\n                        updateSpringConfig({ fromValue: initialValue });\r\n                        observer.next(initialValue);\r\n                    }\r\n                    else {\r\n                        initialValueChangedWhileDisabled = true;\r\n                    }\r\n                }),\r\n                // properties that can start/stop the spring\r\n                this.enabled$.subscribe((enabled) => {\r\n                    if (initialized) {\r\n                        if (enabled) {\r\n                            // For simple cases, the spring can manage its own state;\r\n                            // however, we provide property APIs (like initialValue$) for\r\n                            // situations where it should be managed externally.\r\n                            //\r\n                            // The ability to manage this state externally shouldn't require\r\n                            // every author to do so; hence, we use dirty-checking to only\r\n                            // initialize the spring's values if the author has used these\r\n                            // features.\r\n                            //\r\n                            // There's probably a more elegant way to do this (queueing up\r\n                            // changes as they come through and then applying the most\r\n                            // recent one when enabled is set), but this is the simplest\r\n                            // quick solution.\r\n                            const springConfig = {\r\n                                toValue: this.destination$.read(),\r\n                            };\r\n                            if (initialValueChangedWhileDisabled) {\r\n                                const initialValue = this.initialValue$.read();\r\n                                springConfig.fromValue = initialValue;\r\n                                observer.next(initialValue);\r\n                            }\r\n                            if (initialVelocityChangedWhileDisabled) {\r\n                                springConfig.initialVelocity = this.initialVelocity$.read();\r\n                            }\r\n                            updateSpringConfig(springConfig);\r\n                            spring.start();\r\n                            initialValueChangedWhileDisabled = false;\r\n                            initialVelocityChangedWhileDisabled = false;\r\n                        }\r\n                        else {\r\n                            spring.stop();\r\n                        }\r\n                    }\r\n                }),\r\n                this.destination$.subscribe((destination) => {\r\n                    if (this.enabled$.read()) {\r\n                        updateSpringConfig({ toValue: destination });\r\n                        spring.start();\r\n                    }\r\n                    ;\r\n                }),\r\n            ];\r\n            initialized = true;\r\n            return function disconnect() {\r\n                subscriptions.forEach(subscription => subscription.unsubscribe());\r\n            };\r\n        })._remember();\r\n    }\r\n    get destination() {\r\n        return this.destination$.read();\r\n    }\r\n    set destination(value) {\r\n        this.destination$.write(value);\r\n    }\r\n    get initialValue() {\r\n        return this.initialValue$.read();\r\n    }\r\n    set initialValue(value) {\r\n        this.initialValue$.write(value);\r\n    }\r\n    get initialVelocity() {\r\n        return this.initialVelocity$.read();\r\n    }\r\n    set initialVelocity(value) {\r\n        this.initialVelocity$.write(value);\r\n    }\r\n    get stiffness() {\r\n        return this.stiffness$.read();\r\n    }\r\n    set stiffness(value) {\r\n        this.stiffness$.write(value);\r\n    }\r\n    get damping() {\r\n        return this.damping$.read();\r\n    }\r\n    set damping(value) {\r\n        this.damping$.write(value);\r\n    }\r\n    get threshold() {\r\n        return this.threshold$.read();\r\n    }\r\n    set threshold(value) {\r\n        this.threshold$.write(value);\r\n    }\r\n    get enabled() {\r\n        return this.enabled$.read();\r\n    }\r\n    set enabled(value) {\r\n        this.enabled$.write(value);\r\n    }\r\n    get state() {\r\n        return this.state$.read();\r\n    }\r\n}\r\nexport default NumericSpring;\r\nfunction transformToValueWithKey(key) {\r\n    return {\r\n        transform(value) {\r\n            return {\r\n                [key]: value,\r\n            };\r\n        },\r\n    };\r\n}\r\n//# sourceMappingURL=NumericSpring.js.map","dts":{"name":"/Users/brentons/Projects/material-motion-js/packages/core/interactions/NumericSpring.d.ts","text":"import { MotionProperty } from '../observables/MotionProperty';\r\nimport { State } from '../enums';\r\nimport { ObservableWithMotionOperators, Spring } from '../types';\r\nexport declare const DEFAULT_STIFFNESS: number;\r\nexport declare const DEFAULT_DAMPING: number;\r\nexport declare const DEFAULT_THRESHOLD: number;\r\nexport declare class NumericSpring implements Spring<number> {\r\n    readonly destination$: MotionProperty<number>;\r\n    destination: number;\r\n    readonly initialValue$: MotionProperty<number>;\r\n    initialValue: number;\r\n    readonly initialVelocity$: MotionProperty<number>;\r\n    initialVelocity: number;\r\n    readonly stiffness$: MotionProperty<number>;\r\n    stiffness: number;\r\n    readonly damping$: MotionProperty<number>;\r\n    damping: number;\r\n    readonly threshold$: MotionProperty<number>;\r\n    threshold: number;\r\n    readonly enabled$: MotionProperty<boolean>;\r\n    enabled: boolean;\r\n    readonly state$: MotionProperty<State>;\r\n    readonly state: State;\r\n    value$: ObservableWithMotionOperators<number>;\r\n}\r\nexport default NumericSpring;\r\n"}}
