'use strict';

var syncTime = require('../../frameloop/sync-time.js');
var frame = require('../../frameloop/frame.js');

const frameloopDriver = (update) => {
    const passTimestamp = ({ timestamp }) => update(timestamp);
    return {
        start: (keepAlive = true) => frame.frame.update(passTimestamp, keepAlive),
        stop: () => frame.cancelFrame(passTimestamp),
        /**
         * If we're processing this frame we can use the
         * framelocked timestamp to keep things in sync.
         */
        now: () => (frame.frameData.isProcessing ? frame.frameData.timestamp : syncTime.time.now()),
    };
};

exports.frameloopDriver = frameloopDriver;
