From afabe5b7146abd22936b4d6ef8c7465635bcf7a6 Mon Sep 17 00:00:00 2001 From: Jeremy Ruston Date: Tue, 18 Feb 2025 17:30:11 +0000 Subject: [PATCH] More efficient checks if a tiddler is a plugin --- boot/boot.js | 16 ++++++++++------ core/modules/tiddler.js | 4 ---- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/boot/boot.js b/boot/boot.js index 499456862..b3b43a51d 100644 --- a/boot/boot.js +++ b/boot/boot.js @@ -1068,6 +1068,10 @@ $tw.Tiddler.prototype.hasField = function(field) { return $tw.utils.hop(this.fields,field); }; +$tw.Tiddler.prototype.isPlugin = function() { + return this.fields.type === "application/json" && this.hasField("plugin-type") && !this.hasField("draft.of"); +}; + /* Compare two tiddlers for equality tiddler: the tiddler to compare @@ -1407,7 +1411,7 @@ $tw.Wiki = function(options) { $tw.utils.each(titles || getTiddlerTitles(),function(title) { var tiddler = tiddlerStore[title]; if(tiddler) { - if(tiddler.fields.type === "application/json" && tiddler.hasField("plugin-type") && tiddler.fields.text) { + if(tiddler.isPlugin() && tiddler.fields.text) { var contents = $tw.utils.parseJSONSafe(tiddler.fields.text); pluginContents[tiddler.fields.title] = contents; results.modifiedPlugins.push(tiddler.fields.title); @@ -1445,7 +1449,7 @@ $tw.Wiki = function(options) { if(!tiddler) { tiddler = self.getTiddler(title); } - if(tiddler && tiddler.fields.type === "application/json" && tiddler.fields["plugin-type"] && (!pluginType || tiddler.fields["plugin-type"] === pluginType)) { + if(tiddler && tiddler.isPlugin() && (!pluginType || tiddler.fields["plugin-type"] === pluginType)) { var disablingTiddler = self.getTiddler("$:/config/Plugins/Disabled/" + title); if(title === "$:/core" || !disablingTiddler || (disablingTiddler.fields.text || "").trim() !== "yes") { self.unregisterPluginTiddlers(null,[title]); // Unregister the plugin if it's already registered @@ -1462,16 +1466,16 @@ $tw.Wiki = function(options) { } } function checkSubTiddler(parentPluginTitle,title) { - var tiddler = new $tw.Tiddler(pluginContents[parentPluginTitle].tiddlers[title]); - if(tiddler && tiddler.fields.type === "application/json" && tiddler.fields["plugin-type"] && (!pluginType || tiddler.fields["plugin-type"] === pluginType)) { - var disablingTiddler = self.getTiddler("$:/config/Plugins/Disabled/" + title); + var tiddlerFields = pluginContents[parentPluginTitle].tiddlers[title]; + if(tiddlerFields.type === "application/json" && tiddlerFields["plugin-type"] && !tiddlerFields["draft.of"] && (!pluginType || tiddlerFields["plugin-type"] === pluginType)) { + var tiddler = new $tw.Tiddler(tiddlerFields,{title: title}), + disablingTiddler = self.getTiddler("$:/config/Plugins/Disabled/" + title); if(title === "$:/core" || !disablingTiddler || (disablingTiddler.fields.text || "").trim() !== "yes") { self.unregisterPluginTiddlers(null,[title]); // Unregister the plugin if it's already registered pluginTiddlersInfo.push({tiddler: tiddler,parentPluginTitle: parentPluginTitle}); registeredTitles.push(tiddler.fields.title); } } - } }; diff --git a/core/modules/tiddler.js b/core/modules/tiddler.js index d8d67bf77..5028f813f 100644 --- a/core/modules/tiddler.js +++ b/core/modules/tiddler.js @@ -16,10 +16,6 @@ exports.hasTag = function(tag) { return this.fields.tags && this.fields.tags.indexOf(tag) !== -1; }; -exports.isPlugin = function() { - return this.fields.type === "application/json" && this.hasField("plugin-type"); -}; - exports.isDraft = function() { return this.hasField("draft.of"); };