Navigation Menu

Skip to content
This repository has been archived by the owner on Nov 18, 2021. It is now read-only.

Commit

Permalink
Stream app: added PersistedObject class that uses DOM storage as back…
Browse files Browse the repository at this point in the history
…end.
  • Loading branch information
bard committed Apr 20, 2009
1 parent 32b84d6 commit 1802330
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 26 deletions.
64 changes: 64 additions & 0 deletions modules/stream/lib/persisted_object.js
@@ -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];
};
45 changes: 19 additions & 26 deletions modules/stream/stream.js
Expand Up @@ -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;
Expand All @@ -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();
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
Expand All @@ -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() {
Expand Down
1 change: 1 addition & 0 deletions modules/stream/stream.xhtml
Expand Up @@ -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 -->
Expand Down

0 comments on commit 1802330

Please sign in to comment.