<commit_before>var async = require("async"),
    fs = require("fs"),
    _ = require("lodash");

module.exports = function setupSync(model, api, options)
{
    if (model.linvoSync) return;
    model.linvoSync = true;

    var options = options || {};

    var status = function(s) {
        if (options.log) console.log("LinvoDB Sync: "+s);
    };

    var dirty = false;
    var triggerSync = function(cb)
    { 
        dirty = true;
        q.push({}, cb);
    };
    model.on("updated", function(items, quiet) { if (!quiet) triggerSync() });
    model.on("inserted", function(items, quiet) { if (!quiet) triggerSync() });
    model.static("triggerSync", triggerSync);

    var mtimes = {};
    model.on("reset", function() { mtimes = {} });
    model.on("refresh", function() { triggerSync() });
    model.on("construct", function(x) { if (x._id && x._mtime) mtimes[x._id] = x._mtime });
    model.on("save", function(x) { if (x._id && x._mtime) mtimes[x._id] = x._mtime });

    /* We need to run only one task at a time */
    var q = async.queue(function(opts, cb)
    {
        if (! api.user) return cb();
        if (! dirty) return cb();

        var baseQuery = { collection: options.remoteCollection || model.modelName };
        var remote = {}, push = [], pull = [];

        var uid = api.user._id, checkUid = function() { return (api.user && api.user._id) == uid };

        async.auto({
            ensure_indexes: function(callback) { // Meaningless lookup to Ensure the DB has been indexed<commit_msg>Ability to debounce .triggerSync<commit_after>var async = require("async"),
    fs = require("fs"),
    _ = require("lodash");

module.exports = function setupSync(model, api, options)
{
    if (model.linvoSync) return;
    model.linvoSync = true;

    var options = options || {};

    var status = function(s) {
        if (options.log) console.log("LinvoDB Sync: "+s);
    };

    var dirty = false;
    var triggerSync = _.debounce(function(cb)
    { 
        dirty = true;
        q.push({}, cb);
    }, options.debounce || 500);
    model.on("updated", function(items, quiet) { if (!quiet) triggerSync() });
    model.on("inserted", function(items, quiet) { if (!quiet) triggerSync() });
    model.static("triggerSync", triggerSync);

    var mtimes = {};
    model.on("reset", function() { mtimes = {} });
    model.on("refresh", function() { triggerSync() });
    model.on("construct", function(x) { if (x._id && x._mtime) mtimes[x._id] = x._mtime });
    model.on("save", function(x) { if (x._id && x._mtime) mtimes[x._id] = x._mtime });

    /* We need to run only one task at a time */
    var q = async.queue(function(opts, cb)
    {
        if (! api.user) return cb();
        if (! dirty) return cb();

        var baseQuery = { collection: options.remoteCollection || model.modelName };
        var remote = {}, push = [], pull = [];

        var uid = api.user._id, checkUid = function() { return (api.user && api.user._id) == uid };

        async.auto({
            ensure_indexes: function(callback) { // Meaningless lookup to Ensure the DB has been indexed
{'fraction_non_alphanumeric': 0.17301484828921884, 'fraction_numerical': 0.0009683666881859264, 'mean_word_length': 2.797794117647059, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5482631', 'n_tokens_mistral': 988, 'n_tokens_neox': 956, 'n_words': 254}