<commit_before>
/*
 * vim: ts=4:sw=4:expandtab
 */
;(function() {
    'use strict';
    window.Whisper = window.Whisper || {};

    function destroyExpiredMessages() {
        // Load messages that have expired and destroy them
        var expired = new Whisper.MessageCollection();
        expired.on('add', function(message) {
            console.log('message', message.get('sent_at'), 'expired');
            message.getConversation().trigger('expired', message);

            // We delete after the trigger to allow the conversation time to process
            //   the expiration before the message is removed from the database.
            message.destroy();
        });
        expired.on('reset', checkExpiringMessages);

        expired.fetchExpired();
    }

    var timeout;
    function checkExpiringMessages() {
        // Look up the next expiring message and set a timer to destroy it
        var expiring = new Whisper.MessageCollection();
        expiring.once('add', function(next) {
            var expires_at = next.get('expires_at');
            console.log('next message expires', new Date(expires_at).toISOString());

            var wait = expires_at - Date.now();
            if (wait < 0) { wait = 0; }

            clearTimeout(timeout);
            timeout = setTimeout(destroyExpiredMessages, wait);
        });
        expiring.fetchNextExpiring();
    }

    Whisper.ExpiringMessagesListener = {
        init: function(events) {
            checkExpiringMessages();
            events.on('timetravel', checkExpiringMessages);
        },
        update: checkExpiringMessages
    };

    var TimerOption = Backbone.Model.extend({
      getName: function() {
        return i18n([
          'timerOption', this.get('time'), this.get('unit'),
        ].join('_')) || moment.duration(this.get('time'), this.get('unit')).humanize();
      },
      getAbbreviated: function() {
        return i18n([
          'timerOption', this.get('time'), this.get('unit'), 'abbreviated'<commit_msg>Throttle expiring messages data query and deletion<commit_after>
/*
 * vim: ts=4:sw=4:expandtab
 */
;(function() {
    'use strict';
    window.Whisper = window.Whisper || {};

    function destroyExpiredMessages() {
        // Load messages that have expired and destroy them
        var expired = new Whisper.MessageCollection();
        expired.on('add', function(message) {
            console.log('message', message.get('sent_at'), 'expired');
            message.getConversation().trigger('expired', message);

            // We delete after the trigger to allow the conversation time to process
            //   the expiration before the message is removed from the database.
            message.destroy();
        });
        expired.on('reset', throttledCheckExpiringMessages);

        expired.fetchExpired();
    }

    var timeout;
    function checkExpiringMessages() {
        // Look up the next expiring message and set a timer to destroy it
        var expiring = new Whisper.MessageCollection();
        expiring.once('add', function(next) {
            var expires_at = next.get('expires_at');
            console.log('next message expires', new Date(expires_at).toISOString());

            var wait = expires_at - Date.now();
            if (wait < 0) { wait = 0; }

            clearTimeout(timeout);
            timeout = setTimeout(destroyExpiredMessages, wait);
        });
        expiring.fetchNextExpiring();
    }
    var throttledCheckExpiringMessages = _.throttle(checkExpiringMessages, 1000);

    Whisper.ExpiringMessagesListener = {
        init: function(events) {
            checkExpiringMessages();
            events.on('timetravel', throttledCheckExpiringMessages);
        },
        update: throttledCheckExpiringMessages
    };

    var TimerOption = Backbone.Model.extend({
      getName: function() {
        return i18n([
          'timerOption', this.get('time'), this.get('unit'),
        ].join('_')) || moment.duration(this.get('time'), this.get('unit')).humanize();
      },
      getAbbreviated: function() {
        return i18n([
          'timerOption', this.get('time'), this.get('unit'), 'abbreviated'
{'fraction_non_alphanumeric': 0.1276391554702495, 'fraction_numerical': 0.0047984644913627635, 'mean_word_length': 2.4712739383846793, 'pattern_counts': {'":': 0, '<': 5, '<?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': '81879', 'n_tokens_mistral': 1159, 'n_tokens_neox': 1108, 'n_words': 267}