re-factoring, added limit on story cache size.
git-svn-id: file:///tmp/snv/trunk@23 12951d8a-c33d-4b7c-b961-822215c816e1
This commit is contained in:
parent
8aff3915e8
commit
2fccc97094
@ -5,3 +5,8 @@ caching, notifier-observer -- done
|
||||
new UI, icons -- done
|
||||
toolbar button -- done
|
||||
put license --done
|
||||
|
||||
modify background colors to indicate new and read stories
|
||||
implement search
|
||||
new interface to navigate containers, topics and categories
|
||||
make configuration screen
|
||||
|
@ -22,24 +22,26 @@
|
||||
var DiggSidebar = {
|
||||
homepageURL: "http://code.google.com/p/digg-sidebar/",
|
||||
prefs: Application.extensions.get("diggsidebar@abhinavsarkar.net").prefs,
|
||||
observerService: Components.classes["@mozilla.org/observer-service;1"]
|
||||
.getService(Components.interfaces.nsIObserverService),
|
||||
storyListRefreshEventTopic: "extension-diggsidebar-abhinavsarkar-net-storylist-refresh",
|
||||
updateInterval: 1000,
|
||||
updateIntervalDecay: 0,
|
||||
shownStoriesCount: 30,//make configurable
|
||||
cachedStoriesCount: 1000,//make configurable
|
||||
categories: ["All", "Popular", "Upcoming", "Hot", "Top"],
|
||||
|
||||
endpoint: {
|
||||
container: null,
|
||||
topic: null,
|
||||
category: "all"
|
||||
},
|
||||
updateInterval: 1000,
|
||||
updateIntervalDecay: 0,
|
||||
lastUpdateAt: new Date().getTime(),
|
||||
shownStoriesCount: 30,
|
||||
categories: ["All", "Popular", "Upcoming", "Hot", "Top"],
|
||||
timerId: null,
|
||||
storyIds: [],
|
||||
refreshing: false,
|
||||
stories: [],
|
||||
expandedStory: null,
|
||||
observerService: Components.classes["@mozilla.org/observer-service;1"]
|
||||
.getService(Components.interfaces.nsIObserverService),
|
||||
|
||||
$ce: function (tagName) {
|
||||
return document.createElement(tagName);
|
||||
@ -84,21 +86,19 @@ var DiggSidebar = {
|
||||
var cmenupopup = DiggSidebar.$ce("menupopup");
|
||||
cmenupopup.setAttribute("id", container.short_name + "Popup");
|
||||
|
||||
DiggSidebar.categories.forEach(
|
||||
function (category) {
|
||||
var cmenuitem = DiggSidebar.$ce("menuitem");
|
||||
with (cmenuitem) {
|
||||
setAttribute("label", category);
|
||||
setAttribute("value",
|
||||
"/container/" + container.short_name +
|
||||
"/" + category.toLowerCase());
|
||||
setAttribute("accesskey", category.charAt(0));
|
||||
setAttribute("oncommand",
|
||||
"DiggSidebar.setEndPoint(this.value)");
|
||||
}
|
||||
cmenupopup.appendChild(cmenuitem);
|
||||
DiggSidebar.categories.forEach(function (category) {
|
||||
var cmenuitem = DiggSidebar.$ce("menuitem");
|
||||
with (cmenuitem) {
|
||||
setAttribute("label", category);
|
||||
setAttribute("value",
|
||||
"/container/" + container.short_name +
|
||||
"/" + category.toLowerCase());
|
||||
setAttribute("accesskey", category.charAt(0));
|
||||
setAttribute("oncommand",
|
||||
"DiggSidebar.setEndPoint(this.value)");
|
||||
}
|
||||
);
|
||||
cmenupopup.appendChild(cmenuitem);
|
||||
});
|
||||
cmenupopup.appendChild(DiggSidebar.$ce("menuseparator"));
|
||||
var topics = container.topics;
|
||||
topics.forEach(function (topic) {
|
||||
@ -159,14 +159,14 @@ var DiggSidebar = {
|
||||
DiggSidebar.Utils.removeAllChildren(DiggSidebar.UI.storyListBox);
|
||||
}
|
||||
|
||||
var now = new Date();
|
||||
|
||||
filteredStories.forEach(function (story, index) {
|
||||
if (index < DiggSidebar.shownStoriesCount) {
|
||||
var now = new Date();
|
||||
if (story.promote_date != null) {
|
||||
var then = new Date(story.promote_date * 1000);
|
||||
} else {
|
||||
var then = new Date(story.submit_date * 1000);
|
||||
}
|
||||
var then = (story.promote_date != null) ?
|
||||
new Date(story.promote_date * 1000) :
|
||||
new Date(story.submit_date * 1000);
|
||||
|
||||
var diff = Math.max(now - then, 0);
|
||||
|
||||
var hr = Math.floor(diff / (1000 * 3600));
|
||||
@ -209,7 +209,10 @@ var DiggSidebar = {
|
||||
newStoryIds.push(story.id);
|
||||
}
|
||||
});
|
||||
DiggSidebar.storyIds = DiggSidebar.storyIds.concat(newStoryIds);
|
||||
DiggSidebar.storyIds = newStoryIds.concat(DiggSidebar.storyIds);
|
||||
if (DiggSidebar.storyIds.length > DiggSidebar.cachedStoriesCount) {
|
||||
DiggSidebar.storyIds.length = DiggSidebar.cachedStoriesCount;
|
||||
}
|
||||
},
|
||||
|
||||
showDescription: function (storyId) {
|
||||
@ -232,22 +235,30 @@ var DiggSidebar = {
|
||||
var ep = DiggSidebar.prefs.get("endpoint").value || "";
|
||||
|
||||
DiggSidebar.indicateActivity();
|
||||
DiggSidebar.fetchData("http://services.digg.com/stories" +
|
||||
ep.replace(/\/all/g, "") + "?count=30" + "&type=json" +
|
||||
DiggSidebar.fetchData(
|
||||
"http://services.digg.com/stories" + ep.replace(/\/all/g, "") +
|
||||
"?count=30" + "&type=json" +
|
||||
"&appkey=" + encodeURIComponent(DiggSidebar.homepageURL),
|
||||
|
||||
function (e) {
|
||||
var stories = DiggSidebar.Utils.decodeJson(e.target.responseText).stories;
|
||||
stories.forEach(function (story) { story.id = parseInt(story.id); });
|
||||
|
||||
var newStories = stories.filter(function (story) {
|
||||
return DiggSidebar.storyIds.indexOf(story.id) == -1;
|
||||
stories.forEach(function (story) {
|
||||
story.id = parseInt(story.id);
|
||||
});
|
||||
|
||||
var newStories = [
|
||||
story for each (story in stories)
|
||||
if (DiggSidebar.storyIds.indexOf(story.id) == -1)];
|
||||
|
||||
newStories.forEach(function (story) {
|
||||
story.category = DiggSidebar.endpoint.category;
|
||||
story.read = false;
|
||||
});
|
||||
DiggSidebar.stories = newStories.concat(DiggSidebar.stories);
|
||||
if (DiggSidebar.stories.length > DiggSidebar.cachedStoriesCount) {
|
||||
DiggSidebar.stories.length = DiggSidebar.cachedStoriesCount;
|
||||
}
|
||||
|
||||
DiggSidebar.observerService.notifyObservers(
|
||||
null, DiggSidebar.storyListRefreshEventTopic, null);
|
||||
|
||||
@ -268,7 +279,8 @@ var DiggSidebar = {
|
||||
|
||||
createMenu: function () {
|
||||
DiggSidebar.indicateActivity();
|
||||
DiggSidebar.fetchData("http://services.digg.com/containers" + "?type=json" +
|
||||
DiggSidebar.fetchData(
|
||||
"http://services.digg.com/containers" + "?type=json" +
|
||||
"&appkey=" + encodeURIComponent(DiggSidebar.homepageURL),
|
||||
DiggSidebar.populateMenu, DiggSidebar.errorHandler);
|
||||
},
|
||||
@ -295,12 +307,9 @@ var DiggSidebar = {
|
||||
sum += weights[i] * diff;
|
||||
}
|
||||
var newUpdateInterval = Math.round(
|
||||
sum * 1000 / weights
|
||||
.splice(0, Math.min(4, stories.length - 1))
|
||||
.reduce(function (a, b) {
|
||||
return a + b;
|
||||
})
|
||||
);
|
||||
sum * 1000 /
|
||||
weights.slice(0, Math.min(4, stories.length - 1))
|
||||
.reduce(function (a, b) a + b));
|
||||
|
||||
if (newUpdateInterval > 0) {
|
||||
var previousUpdateInterval = DiggSidebar.updateInterval;
|
||||
@ -316,8 +325,8 @@ var DiggSidebar = {
|
||||
},
|
||||
|
||||
calculateTimeout: function () {
|
||||
return Math.round(
|
||||
DiggSidebar.updateInterval * (Math.pow(1.5, DiggSidebar.updateIntervalDecay)));
|
||||
return Math.round(DiggSidebar.updateInterval *
|
||||
Math.pow(1.5, DiggSidebar.updateIntervalDecay));
|
||||
},
|
||||
|
||||
setEndPoint: function (ep) {
|
||||
|
Loading…
Reference in New Issue
Block a user