1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2025-01-22 23:16:53 +00:00

Improve performance of tag handling with some caching

This commit is contained in:
Jermolene 2013-11-13 23:33:32 +00:00
parent 874bd7b2b8
commit 45c51481de

View File

@ -8,14 +8,12 @@ Extension methods for the $tw.Wiki object
Adds the following properties to the wiki object: Adds the following properties to the wiki object:
* `eventListeners` is a hashmap by type of arrays of listener functions * `eventListeners` is a hashmap by type of arrays of listener functions
* `changedTiddlers` is a hashmap describing changes to named tiddlers since wiki change events were * `changedTiddlers` is a hashmap describing changes to named tiddlers since wiki change events were last dispatched. Each entry is a hashmap containing two fields:
last dispatched. Each entry is a hashmap containing two fields:
modified: true/false modified: true/false
deleted: true/false deleted: true/false
* `changeCount` is a hashmap by tiddler title containing a numerical index that starts at zero and is * `changeCount` is a hashmap by tiddler title containing a numerical index that starts at zero and is incremented each time a tiddler is created changed or deleted
incremented each time a tiddler is created changed or deleted * `caches` is a hashmap by tiddler title containing a further hashmap of named cache objects. Caches are automatically cleared when a tiddler is modified or deleted
* `caches` is a hashmap by tiddler title containing a further hashmap of named cache objects. Caches * `globalCache` is a hashmap by cache name of cache objects that are cleared whenever any tiddler change occurs
are automatically cleared when a tiddler is modified or deleted
\*/ \*/
(function(){ (function(){
@ -163,6 +161,7 @@ exports.getChangeCount = function(title) {
exports.deleteTiddler = function(title) { exports.deleteTiddler = function(title) {
delete this.tiddlers[title]; delete this.tiddlers[title];
this.clearCache(title); this.clearCache(title);
this.clearGlobalCache();
this.enqueueTiddlerEvent(title,true); this.enqueueTiddlerEvent(title,true);
}; };
@ -207,6 +206,7 @@ exports.addTiddler = function(tiddler) {
// Save the tiddler // Save the tiddler
this.tiddlers[title] = tiddler; this.tiddlers[title] = tiddler;
this.clearCache(title); this.clearCache(title);
this.clearGlobalCache();
this.enqueueTiddlerEvent(title); this.enqueueTiddlerEvent(title);
}; };
@ -425,15 +425,36 @@ exports.getShadowTitles = function() {
Retrieves a list of the tiddler titles that are tagged with a given tag Retrieves a list of the tiddler titles that are tagged with a given tag
*/ */
exports.getTiddlersWithTag = function(tag) { exports.getTiddlersWithTag = function(tag) {
// Get the list associated with the tag var self = this;
var titles = []; return this.getGlobalCache("taglist-" + tag,function() {
for(var title in this.tiddlers) { var tagmap = self.getTagMap();
var tiddler = this.tiddlers[title]; return self.sortByList(tagmap[tag],tag);
if($tw.utils.isArray(tiddler.fields.tags) && tiddler.fields.tags.indexOf(tag) !== -1) { });
titles.push(title); };
/*
Get a hashmap by tag of arrays of tiddler titles
*/
exports.getTagMap = function() {
var self = this;
return this.getGlobalCache("tagmap",function() {
var tags = {};
// Collect up all the tags
for(var title in self.tiddlers) {
var tiddler = self.tiddlers[title];
if(tiddler.fields.tags) {
for(var index=0; index<tiddler.fields.tags.length; index++) {
var tag = tiddler.fields.tags[index];
if(tags[tag]) {
tags[tag].push(title)
} else {
tags[tag] = [title];
}
}
}
} }
} return tags;
return this.sortByList(titles,tag); });
}; };
/* /*
@ -456,7 +477,9 @@ Sorts an array of tiddler titles according to an ordered list
*/ */
exports.sortByList = function(array,listTitle) { exports.sortByList = function(array,listTitle) {
var list = this.getTiddlerList(listTitle); var list = this.getTiddlerList(listTitle);
if(list) { if(!array || array.length === 0) {
return [];
} else if(list) {
var titles = [], t, title; var titles = [], t, title;
// First place any entries that are present in the list // First place any entries that are present in the list
for(t=0; t<list.length; t++) { for(t=0; t<list.length; t++) {
@ -557,6 +580,21 @@ exports.getTiddlerList = function(title) {
return []; return [];
}; };
// Return a named global cache object. Global cache objects are cleared whenever a tiddler change occurs
exports.getGlobalCache = function(cacheName,initializer) {
this.globalCache = this.globalCache || {};
if($tw.utils.hop(this.globalCache,cacheName)) {
return this.globalCache[cacheName];
} else {
this.globalCache[cacheName] = initializer();
return this.globalCache[cacheName];
}
};
exports.clearGlobalCache = function() {
this.globalCache = {};
}
// Return the named cache object for a tiddler. If the cache doesn't exist then the initializer function is invoked to create it // Return the named cache object for a tiddler. If the cache doesn't exist then the initializer function is invoked to create it
exports.getCacheForTiddler = function(title,cacheName,initializer) { exports.getCacheForTiddler = function(title,cacheName,initializer) {