Permalink
Browse files

Stream app: added PersistedObject class that uses DOM storage as back…

…end.
  • Loading branch information...
1 parent 32b84d6 commit 18023306a267d1f6b90b48803c4a2b61e2d8608a @bard committed Apr 20, 2009
Showing with 84 additions and 26 deletions.
  1. +64 −0 modules/stream/lib/persisted_object.js
  2. +19 −26 modules/stream/stream.js
  3. +1 −0 modules/stream/stream.xhtml
@@ -0,0 +1,64 @@
+function PersistedObject(name, autosave) {
+ this._name = name;
+ this._data = $.evalJSON(PersistedObject._storage[name]);
+
+ function makeGetter(obj, n) {
+ obj.__defineGetter__(n, function() {
+ return obj._data[n];
+ });
+ }
+
+ function makeSetter(obj, n) {
+ obj.__defineSetter__(
+ n, (autosave ?
+ function(val) {
+ obj._data[n] = val;
+ this.save();
+ return val;
+ }
+ :
+ function(val) {
+ return obj._data[n] = val;
+ }));
+ }
+
+ for(var n in this._data) {
+ makeGetter(this, n);
+ makeSetter(this, n);
+ }
+}
+
+PersistedObject._storage = globalStorage[document.location.host];
+
+PersistedObject.create = function(name, defaults, autosave) {
+ if(name in this._storage) {
+ var obj = this._storage[name];
+ if('version' in defaults) {
+ throw new Error('Versioning not supported yet.');
+ } else {
+ throw new Error('Object "' + name + '" exists already.');
+ }
+ }
+
+ this._storage[name] = $.toJSON(defaults);
+ return new PersistedObject(name, autosave);
+};
+
+PersistedObject.get = function(name, autosave) {
+ if(PersistedObject.exists(name))
+ return new PersistedObject(name, autosave);
+ else
+ return null;
+};
+
+PersistedObject.exists = function(name) {
+ return (name in this._storage);
+};
+
+PersistedObject.prototype.save = function() {
+ return PersistedObject._storage[this._name] = $.toJSON(this._data);
+};
+
+PersistedObject.prototype.destroy = function() {
+ delete PersistedObject._storage[this._name];
+};
View
@@ -21,22 +21,7 @@ var applyFilters = compose(
filters.processImageBinds
);
-
-// Defaults
-var defaultConf = {
- // For internal use of the storage system
- _id: 'conf',
-
- // Every ten seconds, check how many events we're displaying and
- // remove older ones so that 100 events are left
- maxEvents: 100,
- cleanupInterval: 10000,
-
- muted: {},
-
- version: '0.10'
-};
-
+var conf;
XML.prettyPrinting = false;
XML.ignoreWhitespace = false;
@@ -47,13 +32,28 @@ XML.ignoreWhitespace = false;
// Initialize components that don't need the DOM right away
-storage.init({conf: defaultConf});
vcards = { __proto__: null };
rosters = new XMLList();
// Initialize DOM-dependent components when DOM is ready
$(document).ready(function() {
+ conf =
+ PersistedObject.get('conf', true) ||
+ PersistedObject.create({
+ // For internal use of the storage system
+ _id: 'conf',
+
+ // Every ten seconds, check how many events we're displaying and
+ // remove older ones so that 100 events are left
+ maxEvents: 100,
+ cleanupInterval: 10000,
+
+ muted: {},
+
+ version: 1
+ }, true);
+
// Initialize xmpp forwarding
xmpp.init();
@@ -114,13 +114,9 @@ $(document).ready(function() {
// Initialize periodic tasks
- // XXX race condition here, custom config isn't necessarily
- // available here.
-
- var conf = storage.load('conf');
window.setInterval(cleanup, conf.cleanupInterval);
- // simulate();
+// simulate();
// $(window).bind('custom/blur', function() {
// $('#stream > .event.reading-cue').removeClass('reading-cue');
@@ -224,8 +220,7 @@ translators.push({
if(stanza.status.text() == '')
return false;
if(stanza.@ns_x4m_in::direction == 'in' &&
- // XXX maybe should use names
- JID(stanza.@from).address in storage.load('conf').muted)
+ JID(stanza.@from).address in conf.muted)
return false;
if(stanza.@type == 'error')
return;
@@ -338,7 +333,6 @@ function prepareReply(recipient) {
function cleanup() {
var events = $('#stream').children();
- var conf = storage.load('conf');
if(events.length > conf.maxEvents)
events.slice(0, events.length-conf.maxEvents).remove();
}
@@ -351,7 +345,6 @@ function mutePresence(eventDescendant) {
.find('.origin')
.attr('href');
- var conf = storage.load('conf');
conf.muted[entity(originURI).address] = true;
$('#stream .event.presence').filter(function() {
@@ -24,6 +24,7 @@
<script type="application/x-javascript" src="lib/display_filters.js"/>
<script type="application/x-javascript" src="lib/utils_ui.js"/>
<script type="application/x-javascript" src="lib/utils_xmpp.js"/>
+ <script type="application/x-javascript" src="lib/persisted_object.js"/>
<script type="application/x-javascript" src="lib/storage.js"/>
<!-- local scripts: general -->

0 comments on commit 1802330

Please sign in to comment.