{"version":3,"file":"delay.mjs","sources":["../../../src/utils/delay.ts"],"sourcesContent":["import { cancelFrame, frame } from \"../frameloop\"\nimport { time } from \"../frameloop/sync-time\"\nimport type { FrameData } from \"../frameloop/types\"\nimport { secondsToMilliseconds } from \"motion-utils\"\nimport { addToScope, bindToScope } from \"../scope/active\"\n\nexport type DelayedFunction = (overshoot: number) => void\n\n/**\n * Timeout defined in ms\n */\nexport function delay(callback: DelayedFunction, timeout: number) {\n    const start = time.now()\n    callback = bindToScope(callback)\n\n    const checkElapsed = ({ timestamp }: FrameData) => {\n        const elapsed = timestamp - start\n\n        if (elapsed >= timeout) {\n            cancelFrame(checkElapsed)\n            callback(elapsed - timeout)\n        }\n    }\n\n    frame.setup(checkElapsed, true)\n\n    return addToScope(() => cancelFrame(checkElapsed))\n}\n\nexport function delayInSeconds(callback: DelayedFunction, timeout: number) {\n    return delay(callback, secondsToMilliseconds(timeout))\n}\n"],"names":[],"mappings":";;;;;AAQA;;AAEG;AACG,SAAU,KAAK,CAAC,QAAyB,EAAE,OAAe,EAAA;AAC5D,IAAA,MAAM,KAAK,GAAG,IAAI,CAAC,GAAG,EAAE;AACxB,IAAA,QAAQ,GAAG,WAAW,CAAC,QAAQ,CAAC;AAEhC,IAAA,MAAM,YAAY,GAAG,CAAC,EAAE,SAAS,EAAa,KAAI;AAC9C,QAAA,MAAM,OAAO,GAAG,SAAS,GAAG,KAAK;AAEjC,QAAA,IAAI,OAAO,IAAI,OAAO,EAAE;YACpB,WAAW,CAAC,YAAY,CAAC;AACzB,YAAA,QAAQ,CAAC,OAAO,GAAG,OAAO,CAAC;QAC/B;AACJ,IAAA,CAAC;AAED,IAAA,KAAK,CAAC,KAAK,CAAC,YAAY,EAAE,IAAI,CAAC;IAE/B,OAAO,UAAU,CAAC,MAAM,WAAW,CAAC,YAAY,CAAC,CAAC;AACtD;AAEM,SAAU,cAAc,CAAC,QAAyB,EAAE,OAAe,EAAA;IACrE,OAAO,KAAK,CAAC,QAAQ,EAAE,qBAAqB,CAAC,OAAO,CAAC,CAAC;AAC1D;;;;"}