@ginden/blinkstick-v2
Version:
Improved Blickstick API for Node.js
44 lines • 1.84 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.limitDuration = limitDuration;
const tsafe_1 = require("tsafe");
const complex_frame_1 = require("../frame/complex-frame");
const simple_frame_1 = require("../frame/simple-frame");
const wait_frame_1 = require("../frame/wait-frame");
/**
* @summary Sets maximum duration for an animation.
* The last frame can be shortened to fit the maximum duration.
* Frames beyond the maximum duration are dropped
* @category Animation
*/
function limitDuration(animation, maximumDuration) {
(0, tsafe_1.assert)(maximumDuration > 0, 'maximumDuration must be greater than 0');
return {
[Symbol.asyncIterator]: async function* () {
let timeTook = 0;
for await (const frame of animation) {
if (timeTook >= maximumDuration) {
return;
}
const { duration } = frame;
if (timeTook + duration > maximumDuration) {
// If the frame is longer than the remaining time, we need to truncate it.
if (frame instanceof simple_frame_1.SimpleFrame) {
yield new simple_frame_1.SimpleFrame(frame.rgb, maximumDuration - timeTook);
}
else if (frame instanceof complex_frame_1.ComplexFrame) {
yield new complex_frame_1.ComplexFrame(frame.colors, maximumDuration - timeTook);
}
else {
yield new wait_frame_1.WaitFrame(maximumDuration - timeTook);
}
}
else {
yield frame;
}
timeTook += duration;
}
},
};
}
//# sourceMappingURL=limit-duration.js.map