{"version":3,"file":"GroupAnimation.mjs","sources":["../../../src/animation/GroupAnimation.ts"],"sourcesContent":["import { AnimationPlaybackControls, TimelineWithFallback } from \"./types\"\n\ntype PropNames =\n    | \"time\"\n    | \"speed\"\n    | \"duration\"\n    | \"attachTimeline\"\n    | \"startTime\"\n    | \"state\"\n\nexport type AcceptedAnimations = AnimationPlaybackControls\n\nexport type GroupedAnimations = AcceptedAnimations[]\n\nexport class GroupAnimation implements AnimationPlaybackControls {\n    animations: GroupedAnimations\n\n    constructor(animations: Array<AcceptedAnimations | undefined>) {\n        this.animations = animations.filter(Boolean) as GroupedAnimations\n    }\n\n    get finished() {\n        return Promise.all(\n            this.animations.map((animation) => animation.finished)\n        )\n    }\n\n    /**\n     * TODO: Filter out cancelled or stopped animations before returning\n     */\n    private getAll(propName: PropNames) {\n        return this.animations[0][propName] as any\n    }\n\n    attachTimeline(timeline: TimelineWithFallback) {\n        const subscriptions = this.animations.map((animation) =>\n            animation.attachTimeline(timeline)\n        )\n\n        return () => {\n            subscriptions.forEach((cancel, i) => {\n                cancel && cancel()\n                this.animations[i].stop()\n            })\n        }\n    }\n\n    get time() {\n        return this.getAll(\"time\")\n    }\n\n    /**\n     * Setters and methods name the property directly, rather than through\n     * a shared helper, so each call site stays monomorphic. Sequences\n     * call these on every animation, many times a second.\n     */\n    set time(time: number) {\n        for (const animation of this.animations) animation.time = time\n    }\n\n    get speed() {\n        return this.getAll(\"speed\")\n    }\n\n    set speed(speed: number) {\n        for (const animation of this.animations) animation.speed = speed\n    }\n\n    get state() {\n        return this.getAll(\"state\")\n    }\n\n    get startTime() {\n        return this.getAll(\"startTime\")\n    }\n\n    get duration() {\n        return getMax(this.animations, \"duration\")\n    }\n\n    get iterationDuration() {\n        return getMax(this.animations, \"iterationDuration\")\n    }\n\n    play() {\n        for (const animation of this.animations) animation.play()\n    }\n\n    pause() {\n        for (const animation of this.animations) animation.pause()\n    }\n\n    // Bound to accommodate common `return animation.stop` pattern\n    stop = () => {\n        for (const animation of this.animations) animation.stop()\n    }\n\n    cancel() {\n        for (const animation of this.animations) animation.cancel()\n    }\n\n    complete() {\n        for (const animation of this.animations) animation.complete()\n    }\n}\n\nfunction getMax(\n    animations: GroupedAnimations,\n    propName: \"iterationDuration\" | \"duration\"\n): number {\n    let max = 0\n\n    for (let i = 0; i < animations.length; i++) {\n        const value = animations[i][propName]\n        if (value !== null && value > max) {\n            max = value\n        }\n    }\n    return max\n}\n"],"names":[],"mappings":"MAca,cAAc,CAAA;AAGvB,IAAA,WAAA,CAAY,UAAiD,EAAA;;QA4E7D,IAAA,CAAA,IAAI,GAAG,MAAK;AACR,YAAA,KAAK,MAAM,SAAS,IAAI,IAAI,CAAC,UAAU;gBAAE,SAAS,CAAC,IAAI,EAAE;AAC7D,QAAA,CAAC;QA7EG,IAAI,CAAC,UAAU,GAAG,UAAU,CAAC,MAAM,CAAC,OAAO,CAAsB;IACrE;AAEA,IAAA,IAAI,QAAQ,GAAA;QACR,OAAO,OAAO,CAAC,GAAG,CACd,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC,SAAS,KAAK,SAAS,CAAC,QAAQ,CAAC,CACzD;IACL;AAEA;;AAEG;AACK,IAAA,MAAM,CAAC,QAAmB,EAAA;QAC9B,OAAO,IAAI,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAQ;IAC9C;AAEA,IAAA,cAAc,CAAC,QAA8B,EAAA;QACzC,MAAM,aAAa,GAAG,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC,SAAS,KAChD,SAAS,CAAC,cAAc,CAAC,QAAQ,CAAC,CACrC;AAED,QAAA,OAAO,MAAK;YACR,aAAa,CAAC,OAAO,CAAC,CAAC,MAAM,EAAE,CAAC,KAAI;gBAChC,MAAM,IAAI,MAAM,EAAE;gBAClB,IAAI,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,IAAI,EAAE;AAC7B,YAAA,CAAC,CAAC;AACN,QAAA,CAAC;IACL;AAEA,IA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