{"version":3,"file":"active.mjs","sources":["../../../src/scope/active.ts"],"sourcesContent":["import type { AnimationOptions } from \"../animation/types\"\n\n/**\n * The scope that `animate()`, `scroll()`, `inView()`, gestures and\n * `resize()` register their cleanups with while it runs. Set by\n * `scopeMotion()`.\n */\nexport interface ActiveScope {\n    root?: Element\n    transition?: AnimationOptions\n    add(cleanup: VoidFunction): VoidFunction\n    bind<F extends (...args: any[]) => any>(callback: F): F\n    track(element: Element): void\n}\n\nexport const activeScope: { current?: ActiveScope } = {}\n\n/**\n * Register `cleanup` with the active scope, if there is one, so stopping\n * the scope also runs it.\n */\nexport function addToScope<T extends VoidFunction>(cleanup: T): T {\n    activeScope.current?.add(cleanup)\n    return cleanup\n}\n\n/**\n * Collect the animation a builder like `animateLayout()` or\n * `animateView()` resolves with. If the scope stops first, the animation\n * is stopped as soon as it starts.\n */\nexport function addBuilderToScope(builder: {\n    then(resolve: (animation?: any) => void, reject?: () => void): unknown\n}) {\n    const scope = activeScope.current\n    if (!scope) return\n\n    let isStopped = false\n    let stop: VoidFunction | undefined\n    const remove = scope.add(() => {\n        isStopped = true\n        stop?.()\n    })\n\n    builder.then((animation?: { stop(): void; finished: Promise<any> }) => {\n        if (!animation) return\n        stop = () => animation.stop()\n        isStopped ? stop() : animation.finished.then(remove)\n    }, remove)\n}\n\n/**\n * Bind `callback` to the active scope, so anything it starts later (for\n * instance an animation in an `inView()` callback) is collected too.\n */\nexport const bindToScope = <F extends (...args: any[]) => any>(callback: F) =>\n    activeScope.current ? activeScope.current.bind(callback) : callback\n"],"names":[],"mappings":"AAeO,MAAM,WAAW,GAA8B;AAEtD;;;AAGG;AACG,SAAU,UAAU,CAAyB,OAAU,EAAA;AACzD,IAAA,WAAW,CAAC,OAAO,EAAE,GAAG,CAAC,OAAO,CAAC;AACjC,IAAA,OAAO,OAAO;AAClB;AAEA;;;;AAIG;AACG,SAAU,iBAAiB,CAAC,OAEjC,EAAA;AACG,IAAA,MAAM,KAAK,GAAG,WAAW,CAAC,OAAO;AACjC,IAAA,IAAI,CAAC,KAAK;QAAE;IAEZ,IAAI,SAAS,GAAG,KAAK;AACrB,IAAA,IAAI,IAA8B;AAClC,IAAA,MAAM,MAAM,GAAG,KAAK,CAAC,GAAG,CAAC,MAAK;QAC1B,SAAS,GAAG,IAAI;QAChB,IAAI,IAAI;AACZ,IAAA,CAAC,CAAC;AAEF,IAAA,OAAO,CAAC,IAAI,CAAC,CAAC,SAAoD,KAAI;AAClE,QAAA,IAAI,CAAC,SAAS;YAAE;QAChB,IAAI,GAAG,MAAM,SAAS,CAAC,IAAI,EAAE;AAC7B,QAAA,SAAS,GAAG,IAAI,EAAE,GAAG,SAAS,CAAC,QAAQ,CAAC,IAAI,CAAC,MAAM,CAAC;IACxD,CAAC,EAAE,MAAM,CAAC;AACd;AAEA;;;AAGG;AACI,MAAM,WAAW,GAAG,CAAoC,QAAW,KACtE,WAAW,CAAC,OAAO,GAAG,WAAW,CAAC,OAAO,CAAC,IAAI,CAAC,QAAQ,CAAC,GAAG;;;;"}