var EE = require('./event_stream_emitter');
var inherits = require('inherits');
var uuid = require('uuid');

function Session(dataOrId) {
    if (!(this instanceof Session)) {
        return new Session(dataOrId);
    }
    switch (typeof dataOrId) {
        case 'object':
            this.setData(dataOrId);
            break;
        default:
            this.id = dataOrId || uuid.v4();
            this.results = [];
    }
}

inherits(Session, EE);

Session.prototype.setData = function(data) {
    if (this.id !== undefined && data.id !== this.id) throw new Error('session data id mismatch');
    if (this.id === undefined) this.id = data.id;
    this.results = data.results;
    this.emit('change');
};

Session.prototype.addResult = function(result) {
    this.results.push(result);
    this.emit('resultAdd', result);
    this.emit('change');
};

Session.prototype.currentLevelResults = function() {
    var rs = this.results, n = rs.length;
    if (!n) return [];
    var level = rs[n-1].level;
    var i=n-1;
    while (i>0 && rs[i-1].level === level) i--;
    return rs.slice(i);
};

module.exports = Session;

@@ -100,16 +100,63 @@
 'uuid');
+%0Avar CachedObject = require('./cached_object');
 %0A%0Afuncti
@@ -260,16 +260,45 @@
 ;%0A    %7D%0A
+    CachedObject.call(this);%0A
     swit
@@ -852,32 +852,55 @@
 s.push(result);%0A
+    this.clearCache();%0A
     this.emit('r

use CachedObject