Browse Source

re-factoring, added limit on story cache size.

git-svn-id: file:///tmp/snv/trunk@23 12951d8a-c33d-4b7c-b961-822215c816
Abhinav Sarkar 12 years ago
parent
commit
2fccc97094
2 changed files with 56 additions and 42 deletions
  1. 5
    0
      chrome/content/TODO.txt
  2. 51
    42
      chrome/content/diggsidebar.js

+ 5
- 0
chrome/content/TODO.txt View File

@@ -5,3 +5,8 @@ caching, notifier-observer -- done
5 5
 new UI, icons -- done
6 6
 toolbar button -- done
7 7
 put license --done
8
+
9
+modify background colors to indicate new and read stories
10
+implement search
11
+new interface to navigate containers, topics and categories
12
+make configuration screen

+ 51
- 42
chrome/content/diggsidebar.js View File

@@ -22,24 +22,26 @@
22 22
 var DiggSidebar = {
23 23
     homepageURL: "http://code.google.com/p/digg-sidebar/",
24 24
     prefs: Application.extensions.get("diggsidebar@abhinavsarkar.net").prefs,
25
+    observerService: Components.classes["@mozilla.org/observer-service;1"]
26
+        .getService(Components.interfaces.nsIObserverService),
25 27
     storyListRefreshEventTopic: "extension-diggsidebar-abhinavsarkar-net-storylist-refresh",
28
+    updateInterval: 1000,
29
+    updateIntervalDecay: 0,
30
+    shownStoriesCount: 30,//make configurable
31
+    cachedStoriesCount: 1000,//make configurable
32
+    categories: ["All", "Popular", "Upcoming", "Hot", "Top"],
33
+
26 34
     endpoint: {
27 35
         container: null,
28 36
         topic: null,
29 37
         category: "all"
30 38
     },
31
-    updateInterval: 1000,
32
-    updateIntervalDecay: 0,
33 39
     lastUpdateAt: new Date().getTime(),
34
-    shownStoriesCount: 30,
35
-    categories: ["All", "Popular", "Upcoming", "Hot", "Top"],
36 40
     timerId: null,
37 41
     storyIds: [],
38 42
     refreshing: false,
39 43
     stories: [],
40 44
     expandedStory: null,
41
-    observerService: Components.classes["@mozilla.org/observer-service;1"]
42
-        .getService(Components.interfaces.nsIObserverService),
43 45
 
44 46
     $ce: function (tagName) {
45 47
         return document.createElement(tagName);
@@ -84,21 +86,19 @@ var DiggSidebar = {
84 86
             var cmenupopup = DiggSidebar.$ce("menupopup");
85 87
             cmenupopup.setAttribute("id", container.short_name + "Popup");
86 88
 
87
-            DiggSidebar.categories.forEach(
88
-                function (category) {
89
-                    var cmenuitem = DiggSidebar.$ce("menuitem");
90
-                    with (cmenuitem) {
91
-                        setAttribute("label", category);
92
-                        setAttribute("value",
93
-                            "/container/" + container.short_name +
94
-                            "/" + category.toLowerCase());
95
-                        setAttribute("accesskey", category.charAt(0));
96
-                        setAttribute("oncommand",
97
-                            "DiggSidebar.setEndPoint(this.value)");
98
-                    }
99
-                    cmenupopup.appendChild(cmenuitem);
89
+            DiggSidebar.categories.forEach(function (category) {
90
+                var cmenuitem = DiggSidebar.$ce("menuitem");
91
+                with (cmenuitem) {
92
+                    setAttribute("label", category);
93
+                    setAttribute("value",
94
+                        "/container/" + container.short_name +
95
+                        "/" + category.toLowerCase());
96
+                    setAttribute("accesskey", category.charAt(0));
97
+                    setAttribute("oncommand",
98
+                        "DiggSidebar.setEndPoint(this.value)");
100 99
                 }
101
-            );
100
+                cmenupopup.appendChild(cmenuitem);
101
+            });
102 102
             cmenupopup.appendChild(DiggSidebar.$ce("menuseparator"));
103 103
             var topics = container.topics;
104 104
             topics.forEach(function (topic) {
@@ -159,14 +159,14 @@ var DiggSidebar = {
159 159
             DiggSidebar.Utils.removeAllChildren(DiggSidebar.UI.storyListBox);
160 160
         }
161 161
 
162
+        var now = new Date();
163
+
162 164
         filteredStories.forEach(function (story, index) {
163 165
             if (index < DiggSidebar.shownStoriesCount) {
164
-                var now = new Date();
165
-                if (story.promote_date != null) {
166
-                    var then = new Date(story.promote_date * 1000);
167
-                } else {
168
-                    var then = new Date(story.submit_date * 1000);
169
-                }
166
+                var then = (story.promote_date != null) ?
167
+                    new Date(story.promote_date * 1000) :
168
+                    new Date(story.submit_date * 1000);
169
+
170 170
                 var diff = Math.max(now - then, 0);
171 171
 
172 172
                 var hr = Math.floor(diff / (1000 * 3600));
@@ -209,7 +209,10 @@ var DiggSidebar = {
209 209
                 newStoryIds.push(story.id);
210 210
             }
211 211
         });
212
-        DiggSidebar.storyIds = DiggSidebar.storyIds.concat(newStoryIds);
212
+        DiggSidebar.storyIds = newStoryIds.concat(DiggSidebar.storyIds);
213
+        if (DiggSidebar.storyIds.length > DiggSidebar.cachedStoriesCount) {
214
+            DiggSidebar.storyIds.length = DiggSidebar.cachedStoriesCount;
215
+        }
213 216
     },
214 217
 
215 218
     showDescription: function (storyId) {
@@ -232,22 +235,30 @@ var DiggSidebar = {
232 235
             var ep = DiggSidebar.prefs.get("endpoint").value || "";
233 236
 
234 237
             DiggSidebar.indicateActivity();
235
-            DiggSidebar.fetchData("http://services.digg.com/stories" +
236
-                ep.replace(/\/all/g, "") + "?count=30" + "&type=json" +
238
+            DiggSidebar.fetchData(
239
+                "http://services.digg.com/stories" + ep.replace(/\/all/g, "") +
240
+                "?count=30" + "&type=json" +
237 241
                 "&appkey=" + encodeURIComponent(DiggSidebar.homepageURL),
238 242
 
239 243
                 function (e) {
240 244
                     var stories = DiggSidebar.Utils.decodeJson(e.target.responseText).stories;
241
-                    stories.forEach(function (story) { story.id = parseInt(story.id); });
242
-                    
243
-                    var newStories = stories.filter(function (story) {
244
-                        return DiggSidebar.storyIds.indexOf(story.id) == -1;
245
+                    stories.forEach(function (story) {
246
+                        story.id = parseInt(story.id);
245 247
                     });
248
+
249
+                    var newStories = [
250
+                        story for each (story in stories)
251
+                        if (DiggSidebar.storyIds.indexOf(story.id) == -1)];
252
+
246 253
                     newStories.forEach(function (story) {
247 254
                         story.category = DiggSidebar.endpoint.category;
248 255
                         story.read = false;
249 256
                     });
250 257
                     DiggSidebar.stories = newStories.concat(DiggSidebar.stories);
258
+                    if (DiggSidebar.stories.length > DiggSidebar.cachedStoriesCount) {
259
+                        DiggSidebar.stories.length = DiggSidebar.cachedStoriesCount;
260
+                    }
261
+
251 262
                     DiggSidebar.observerService.notifyObservers(
252 263
                         null, DiggSidebar.storyListRefreshEventTopic, null);
253 264
 
@@ -268,7 +279,8 @@ var DiggSidebar = {
268 279
 
269 280
     createMenu: function () {
270 281
         DiggSidebar.indicateActivity();
271
-        DiggSidebar.fetchData("http://services.digg.com/containers" + "?type=json" +
282
+        DiggSidebar.fetchData(
283
+            "http://services.digg.com/containers" + "?type=json" +
272 284
             "&appkey=" + encodeURIComponent(DiggSidebar.homepageURL),
273 285
             DiggSidebar.populateMenu, DiggSidebar.errorHandler);
274 286
     },
@@ -295,12 +307,9 @@ var DiggSidebar = {
295 307
             sum += weights[i] * diff;
296 308
         }
297 309
         var newUpdateInterval = Math.round(
298
-            sum * 1000 / weights
299
-                .splice(0, Math.min(4, stories.length - 1))
300
-                .reduce(function (a, b) {
301
-                    return a + b;
302
-                })
303
-        );
310
+            sum * 1000 /
311
+            weights.slice(0, Math.min(4, stories.length - 1))
312
+                .reduce(function (a, b) a + b));
304 313
 
305 314
         if (newUpdateInterval > 0) {
306 315
             var previousUpdateInterval = DiggSidebar.updateInterval;
@@ -316,8 +325,8 @@ var DiggSidebar = {
316 325
     },
317 326
 
318 327
     calculateTimeout: function () {
319
-        return Math.round(
320
-            DiggSidebar.updateInterval * (Math.pow(1.5, DiggSidebar.updateIntervalDecay)));
328
+        return Math.round(DiggSidebar.updateInterval *
329
+            Math.pow(1.5, DiggSidebar.updateIntervalDecay));
321 330
     },
322 331
 
323 332
     setEndPoint: function (ep) {

Loading…
Cancel
Save