1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-07-07 12:34:22 +00:00

Move the cache from the Tiddler object to the WikiStore

It's not really a concern of the Tiddler object
This commit is contained in:
Jeremy Ruston 2012-01-17 13:31:06 +00:00
parent b898afe3e5
commit bd17a389cb
2 changed files with 41 additions and 31 deletions

View File

@ -21,7 +21,6 @@ var utils = require("./Utils.js"),
ArgParser = require("./ArgParser.js").ArgParser; ArgParser = require("./ArgParser.js").ArgParser;
var Tiddler = function(/* tiddler,fields */) { var Tiddler = function(/* tiddler,fields */) {
this.cache = {}; // Expose the cache object
var fields = {}, // Keep the fields private, later we'll expose getters for them var fields = {}, // Keep the fields private, later we'll expose getters for them
tags, // Keep the tags separately because they're the only Array field tags, // Keep the tags separately because they're the only Array field
f,t,c,arg,src; f,t,c,arg,src;

View File

@ -23,15 +23,15 @@ Available options are:
*/ */
var WikiStore = function WikiStore(options) { var WikiStore = function WikiStore(options) {
options = options || {}; options = options || {};
this.tiddlers = {}; this.tiddlers = {}; // Hashmap of tiddlers by title
this.parsers = {}; this.parsers = {}; // Hashmap of parsers by accepted MIME type
this.macros = {}; this.macros = {}; // Hashmap of macros by macro name
this.tiddlerSerializers = {}; this.caches = {}; // Hashmap of cache objects by tiddler title, each is a hashmap of named caches
this.tiddlerDeserializers = {}; this.tiddlerSerializers = {}; // Hashmap of serializers by target MIME type
this.tiddlerDeserializers = {}; // Hashmap of deserializers by accepted MIME type
this.eventListeners = []; // Array of {filter:,listener:} this.eventListeners = []; // Array of {filter:,listener:}
this.eventsTriggered = false; this.eventsTriggered = false;
this.changedTiddlers = {}; // Hashmap of {title: "created|modified|deleted"} this.changedTiddlers = {}; // Hashmap of {title: "created|modified|deleted"}
this.sandbox = options.sandbox;
this.shadows = options.shadowStore !== undefined ? options.shadowStore : new WikiStore({ this.shadows = options.shadowStore !== undefined ? options.shadowStore : new WikiStore({
shadowStore: null shadowStore: null
}); });
@ -264,6 +264,21 @@ WikiStore.prototype.adjustClassesForLink = function(classes,target) {
return newClasses; return newClasses;
}; };
// Return the named cache object for a tiddler. If the cache doesn't exist then the initializer function is invoked to create it
WikiStore.prototype.getCacheForTiddler = function(title,cacheName,initializer) {
var caches = this.caches[title];
if(caches && caches[cacheName]) {
return caches[cacheName];
} else {
if(!caches) {
caches = {};
this.caches[title] = caches;
}
caches[cacheName] = initializer();
return caches[cacheName];
}
};
WikiStore.prototype.parseText = function(type,text) { WikiStore.prototype.parseText = function(type,text) {
var parser = this.parsers[type]; var parser = this.parsers[type];
if(!parser) { if(!parser) {
@ -277,16 +292,12 @@ WikiStore.prototype.parseText = function(type,text) {
}; };
WikiStore.prototype.parseTiddler = function(title) { WikiStore.prototype.parseTiddler = function(title) {
var tiddler = this.getTiddler(title); var me = this,
if(tiddler) { tiddler = this.getTiddler(title),
// Check the cache parseTree = this.getCacheForTiddler(title,"parseTree",function() {
if(!tiddler.cache.parseTree) { return me.parseText(tiddler.type,tiddler.text);
tiddler.cache.parseTree = this.parseText(tiddler.type,tiddler.text); });
} return parseTree;
return tiddler.cache.parseTree;
} else {
return null;
}
}; };
/* /*
@ -303,16 +314,16 @@ Compiles a JavaScript function that renders a tiddler in a particular MIME type
*/ */
WikiStore.prototype.compileTiddler = function(title,type) { WikiStore.prototype.compileTiddler = function(title,type) {
/*jslint evil: true */ /*jslint evil: true */
var tiddler = this.getTiddler(title); var tiddler = this.getTiddler(title),
renderers = this.getCacheForTiddler(title,"renderers",function() {
return {};
});
if(tiddler) { if(tiddler) {
if(!tiddler.cache.renderers) { if(!renderers[type]) {
tiddler.cache.renderers = {};
}
if(!tiddler.cache.renderers[type]) {
var tree = this.parseTiddler(title); var tree = this.parseTiddler(title);
tiddler.cache.renderers[type] = eval(tree.compile(type)); renderers[type] = eval(tree.compile(type));
} }
return tiddler.cache.renderers[type]; return renderers[type];
} else { } else {
return null; return null;
} }
@ -334,19 +345,19 @@ store.renderTiddler("text/html",templateTitle,tiddlerTitle)
*/ */
WikiStore.prototype.renderTiddler = function(targetType,title,asTitle) { WikiStore.prototype.renderTiddler = function(targetType,title,asTitle) {
var tiddler = this.getTiddler(title), var tiddler = this.getTiddler(title),
fn = this.compileTiddler(title,targetType); fn = this.compileTiddler(title,targetType),
renditions = this.getCacheForTiddler(title,"renditions",function() {
return {};
});
if(tiddler) { if(tiddler) {
if(asTitle) { if(asTitle) {
var asTiddler = this.getTiddler(asTitle); var asTiddler = this.getTiddler(asTitle);
return fn(asTiddler,this,utils); return fn(asTiddler,this,utils);
} else { } else {
if(!tiddler.cache.renditions) { if(!renditions[targetType]) {
tiddler.cache.renditions = {}; renditions[targetType] = fn(tiddler,this,utils);
} }
if(!tiddler.cache.renditions[targetType]) { return renditions[targetType];
tiddler.cache.renditions[targetType] = fn(tiddler,this,utils);
}
return tiddler.cache.renditions[targetType];
} }
} }
return null; return null;