From fc6dd83f2f770d3e66326350d1fdbd7348dc19fc Mon Sep 17 00:00:00 2001 From: Jermolene Date: Wed, 13 Nov 2013 21:25:45 +0000 Subject: [PATCH] Use prototypal inheritance to speed up the inheritance of widget variables Now we reuse the prototype mechanism to let javascript do the work of searching up the parent chain. --- core/modules/widgets/widget.js | 39 ++++++++++++++++------------------ 1 file changed, 18 insertions(+), 21 deletions(-) diff --git a/core/modules/widgets/widget.js b/core/modules/widgets/widget.js index aca142834..3f2db2a97 100755 --- a/core/modules/widgets/widget.js +++ b/core/modules/widgets/widget.js @@ -38,8 +38,10 @@ Widget.prototype.initialise = function(parseTreeNode,options) { // Save widget info this.parseTreeNode = parseTreeNode; this.wiki = options.wiki; - this.variables = options.variables || {}; this.parentWidget = options.parentWidget; + this.variablesConstructor = function() {}; + this.variablesConstructor.prototype = this.parentWidget ? this.parentWidget.variables : {}; + this.variables = new this.variablesConstructor(); this.document = options.document; this.attributes = {}; this.children = []; @@ -67,6 +69,16 @@ Widget.prototype.execute = function() { this.makeChildWidgets(); }; +/* +Set the value of a context variable +name: name of the variable +value: value of the variable +params: array of {name:, default:} for each parameter +*/ +Widget.prototype.setVariable = function(name,value,params) { + this.variables[name] = {value: value, params: params}; +}; + /* Get the prevailing value of a context variable name: name of variable @@ -78,19 +90,14 @@ defaultValue: default value if the variable is not defined Widget.prototype.getVariable = function(name,options) { options = options || {}; var actualParams = options.params || []; - // Search up the widget tree for the variable name - var node = this; - while(node && !$tw.utils.hop(node.variables,name)) { - node = node.parentWidget; - } - // If we get to the root then look for a macro module - if(!node) { + // If the variable doesn't exist then look for a macro module + if(!(name in this.variables)) { return this.evaluateMacroModule(name,actualParams,options.defaultValue); } - // Get the value - var value = node.variables[name].value || ""; + var variable = this.variables[name], + value = variable.value || ""; // Substitute any parameters specified in the definition - value = this.substituteVariableParameters(value,node.variables[name].params,actualParams); + value = this.substituteVariableParameters(value,variable.params,actualParams); value = this.substituteVariableReferences(value); return value; }; @@ -166,16 +173,6 @@ Widget.prototype.evaluateMacroModule = function(name,actualParams,defaultValue) } }; -/* -Set the value of a context variable -name: name of the variable -value: value of the variable -params: array of {name:, default:} for each parameter -*/ -Widget.prototype.setVariable = function(name,value,params) { - this.variables[name] = {value: value, params: params}; -}; - /* Check whether a given context variable value exists in the parent chain */