Thunderbird or Firefox extension - nsISimpleEnumerator

Issue
Some of the Firefox or Mozilla application extension API s return  an enumerator object
of type interface nsISimpleEnumerator
that result objects can not be used in a global scope and can not do iteration through the 
list more than one time.
 
Cause:
nsISimpleEnumerator  interface has only two methods
hasMoreElements()
getNext()
 
- There is no way to reset this enumerator to the first position once it is iterated through once. 
Like in this example given in Mozilla developer example page,
 (https://developer.mozilla.org/en-US/docs/Using_nsISimpleEnumerator)
 
var enumerator = document.getElementById('aStringBundleID').strings;
var s = "";
while (enumerator.hasMoreElements()) {
  var property = enumerator.getNext().QueryInterface(Components.interfaces.nsIProperty);
  s += property.name + ' = ' + property.value + ';\n';
}

If you want to reuse the 'enumerator' object again, in another place another loop, this will through error that
enumerator.hasMoreElements() become false after the first loop is ended.
 
The API missing a 'RESET()' method which will save a lots of reuse issues of this feature. 

Fix:
 The quick fix would be , move the objects into a global array the first time you traverse through the enumerator list.

var Global {}; 
var Global.enumArray=new Array();


var enumerator = document.getElementById('aStringBundleID').strings;
var s = "";
var y=0; 
 while (enumerator.hasMoreElements()) {
  var property = enumerator.getNext().QueryInterface(Components.interfaces.nsIProperty);
  s += property.name + ' = ' + property.value + ';\n';
Global.enumArray[i]= property;
y++;
 }

Then you can use this array with for loop as many times as you needed.  
It saves your memory store as you can use these array all through the plugin.
You can even try using Jquery or other javascript frameworks that provide deep clone copy.  Or you can write your own, which is almost similar like above.

Comments