Title: [93061] trunk/Tools
Revision
93061
Author
[email protected]
Date
2011-08-15 14:31:36 -0700 (Mon, 15 Aug 2011)

Log Message

garden-o-matic frontend needs a generic way to track updates.
https://bugs.webkit.org/show_bug.cgi?id=66245

Reviewed by Adam Barth.

* BuildSlaveSupport/build.webkit.org-config/public_html/TestFailures/base.js: Added UpdateTracker.
* BuildSlaveSupport/build.webkit.org-config/public_html/TestFailures/base_unittests.js: Added tests.

Modified Paths

Diff

Modified: trunk/Tools/BuildSlaveSupport/build.webkit.org-config/public_html/TestFailures/base.js (93060 => 93061)


--- trunk/Tools/BuildSlaveSupport/build.webkit.org-config/public_html/TestFailures/base.js	2011-08-15 21:28:13 UTC (rev 93060)
+++ trunk/Tools/BuildSlaveSupport/build.webkit.org-config/public_html/TestFailures/base.js	2011-08-15 21:31:36 UTC (rev 93061)
@@ -226,6 +226,65 @@
     });
 };
 
+/*
+    Maintains a dictionary of items, tracking their updates and removing items that haven't been updated.
+    An "update" is a call to the "update" method.
+    To remove stale items, call the "remove" method. It will remove all
+    items that have not been been updated since the last call of "remove".
+*/
+base.UpdateTracker = function()
+{
+    this._items = {};
+    this._updated = {};
+}
+
+base.UpdateTracker.prototype = {
+    /*
+        Update an {key}/{item} pair. You can make the dictionary act as a set and
+        skip the {item}, in which case the {key} is also the {item}.
+    */
+    update: function(key, object)
+    {
+        object = object || key;
+        this._items[key] = object;
+        this._updated[key] = 1;
+    },
+    exists: function(key)
+    {
+        return !!this.get(key);
+    },
+    get: function(key)
+    {
+        return this._items[key];
+    },
+    /*
+        Callback parameters are:
+        - item
+        - key
+        - updated, which is true if the item was updated after last purge() call.
+    */
+    forEach: function(callback, thisObject)
+    {
+        if (!callback)
+            return;
+
+        Object.keys(this._items).forEach(function(key) {
+            var item = this._items[key];
+            callback.call(thisObject || item, item, key, !!this._updated[key]);
+        }, this);
+    },
+    purge: function(removeCallback, thisObject) {
+        removeCallback = removeCallback || function() {};
+        this.forEach(function(item, key, updated) {
+            if (updated)
+                return;
+            removeCallback.call(thisObject || item, item);
+            delete this._items[key];
+        }, this);
+        this._updated = {};
+    }
+}
+
 // Based on http://src.chromium.org/viewvc/chrome/trunk/src/chrome/browser/resources/shared/js/cr/ui.js
 base.extends = function(base, prototype)
 {

Modified: trunk/Tools/BuildSlaveSupport/build.webkit.org-config/public_html/TestFailures/base_unittests.js (93060 => 93061)


--- trunk/Tools/BuildSlaveSupport/build.webkit.org-config/public_html/TestFailures/base_unittests.js	2011-08-15 21:28:13 UTC (rev 93060)
+++ trunk/Tools/BuildSlaveSupport/build.webkit.org-config/public_html/TestFailures/base_unittests.js	2011-08-15 21:31:36 UTC (rev 93061)
@@ -221,6 +221,83 @@
     });
 });
 
+test("UpdateTracker", 20, function() {
+    var dict;
+
+    function dumpKeys()
+    {
+        var updates = []
+        dict.forEach(function(item, key, updated) {
+            updates.push(key);
+        });
+        return updates;
+    }
+
+    function dumpUpdatedKeys()
+    {
+        var updates = []
+        dict.forEach(function(item, key, updated) {
+            updated && updates.push(key);
+        });
+        return updates;
+    }
+
+
+    dict = new base.UpdateTracker();
+    dict.update("5", {});
+    deepEqual(dumpUpdatedKeys(), ["5"]);
+    dict.update("6", {});
+    dict.update("7", {});
+    deepEqual(dumpUpdatedKeys(), ["5", "6", "7"]);
+    deepEqual(dict.get("6"), {});
+    ok(dict.exists("7"));
+    dict.purge();
+    deepEqual(dumpUpdatedKeys(), []);
+    deepEqual(dumpKeys(), ["5", "6", "7"]);
+    dict.update("5", {});
+    deepEqual(dumpUpdatedKeys(), ["5"]);
+    dict.update("4", {});
+    deepEqual(dumpUpdatedKeys(), ["4", "5"]);
+    deepEqual(dumpKeys(), ["4", "5", "6", "7"]);
+    dict.purge();
+    deepEqual(dumpKeys(), ["4", "5"]);
+    deepEqual(dumpUpdatedKeys(), []);
+    dict.purge();
+    deepEqual(dumpKeys(), []);
+
+    var removeCount = 0;
+    dict.update("one");
+    deepEqual(dumpUpdatedKeys(), ["one"]);
+    dict.update("two");
+    deepEqual(dumpUpdatedKeys(), ["one", "two"]);
+    dict.update("three");
+    dict.purge();
+    deepEqual(dumpKeys(), ["one", "two", "three"]);
+    dict.update("two");
+    dict.purge(function() {
+        removeCount++;
+    });
+    deepEqual(dumpKeys(), ["two"]);
+    equal(removeCount, 2);
+    dict.update("four");
+    var removeCounter = { count: 0 };
+    dict.purge(function() {
+        this.count++;
+    }, removeCounter);
+    equal(removeCounter.count, 1);
+    dict.purge(function() {
+        equal(String(this), "four");
+    });
+
+    dict = new base.UpdateTracker();
+    dict.update("one");
+    var thisObject = {}
+    dict.forEach(function(item) {
+        equal(this, thisObject);
+    }, thisObject);
+
+});
+
 test("extends", 17, function() {
 
     var LikeDiv = base.extends("div", {

Modified: trunk/Tools/ChangeLog (93060 => 93061)


--- trunk/Tools/ChangeLog	2011-08-15 21:28:13 UTC (rev 93060)
+++ trunk/Tools/ChangeLog	2011-08-15 21:31:36 UTC (rev 93061)
@@ -1,3 +1,13 @@
+2011-08-15  Dimitri Glazkov  <[email protected]>
+
+        garden-o-matic frontend needs a generic way to track updates.
+        https://bugs.webkit.org/show_bug.cgi?id=66245
+
+        Reviewed by Adam Barth.
+
+        * BuildSlaveSupport/build.webkit.org-config/public_html/TestFailures/base.js: Added UpdateTracker.
+        * BuildSlaveSupport/build.webkit.org-config/public_html/TestFailures/base_unittests.js: Added tests.
+
 2011-08-15  Adam Roben  <[email protected]>
 
         Teach TestWebKitAPI/gtest how to print _javascript_ failures nicely
_______________________________________________
webkit-changes mailing list
[email protected]
http://lists.webkit.org/mailman/listinfo.cgi/webkit-changes

Reply via email to