Sha256: 58cc76abefe6d13d6c9e705fe44b0a4d241645c59a509d765678a784c65954cf

Contents?: true

Size: 1.32 KB

Versions: 3

Compression:

Stored size: 1.32 KB

Contents

(function() {
    "use strict";
    window.Suricate = window.Suricate || {};

    Suricate.WidgetUpdater = function(widget, interval) {
        this.widget        = widget;
        this.interval      = interval || 1000;
        this.nextUpdateAt  = undefined;
        this.updating      = false;
    };

    /*
     * Public
     */

    Suricate.WidgetUpdater.prototype.update = function(now, callback) {
        if(this.isUpdatedNeeded(now)) {
            var self      = this;
            this.updating = true;
            this.fetchData(function(data) {
                self.nextUpdateAt = new Date(now.getTime() + self.interval);
                self.updating     = false;
                callback(data);
            });
        }
    };

    /*
     * Private
     */

    Suricate.WidgetUpdater.prototype.areDataOutdated = function(now) {
        var needsUpdate = true;
        if(this.nextUpdateAt) {
            needsUpdate = (this.nextUpdateAt < now);
        }
        return needsUpdate;
    };

    Suricate.WidgetUpdater.prototype.isUpdatedNeeded = function(now) {
        return !this.updating && this.areDataOutdated(now);
    };

    Suricate.WidgetUpdater.prototype.fetchData = function(callback) {
        var api = this.widget.getApplication().createAPI();
        api.getWidgetData(this.widget.getID(), callback);
    };
}());

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
suricate-0.0.4 lib/suricate/generator/assets/javascript/widget-updater.js
suricate-0.0.3 lib/suricate/generator/assets/javascript/widget-updater.js
suricate-0.0.2 lib/suricate/generator/assets/javascript/widget-updater.js