2010-06-22 05:38:42 -04:00
|
|
|
/*
|
|
|
|
Shared Namespace Library for JavaScript Code Modules
|
|
|
|
|
|
|
|
Usage:
|
|
|
|
Components.utils.import('resource://my-modules/namespace.jsm');
|
|
|
|
var namespace = getNamespaceFor('mylibrary');
|
2010-06-22 12:50:55 -04:00
|
|
|
if (!namespace.func1) {
|
|
|
|
namespace.func1 = function() { ... };
|
|
|
|
namespace.func2 = function() { ... };
|
|
|
|
}
|
2010-06-22 05:38:42 -04:00
|
|
|
var EXPORTED_SYMBOLS = ['func1', 'func2'];
|
|
|
|
var func1 = namespace.func1;
|
|
|
|
var func2 = namespace.func2;
|
|
|
|
|
2010-08-26 11:40:13 -04:00
|
|
|
license: The MIT License, Copyright (c) 2010 SHIMODA "Piro" Hiroshi
|
2010-06-22 05:38:42 -04:00
|
|
|
http://www.cozmixng.org/repos/piro/fx3-compatibility-lib/trunk/license.txt
|
|
|
|
original:
|
|
|
|
http://www.cozmixng.org/repos/piro/fx3-compatibility-lib/trunk/namespace.js
|
|
|
|
*/
|
|
|
|
|
|
|
|
var EXPORTED_SYMBOLS = ['getNamespaceFor'];
|
|
|
|
|
|
|
|
const Cc = Components.classes;
|
|
|
|
const Ci = Components.interfaces;
|
|
|
|
|
2010-07-06 22:11:57 -04:00
|
|
|
const currentRevision = 2;
|
2010-06-22 05:38:42 -04:00
|
|
|
|
2010-07-06 22:11:57 -04:00
|
|
|
const QUERY = 'namespace.jsm[piro.sakura.ne.jp]:GetExistingNamespace';
|
|
|
|
const STORAGE_PROP = 'namespaces-storage';
|
2010-06-22 05:38:42 -04:00
|
|
|
|
2010-07-06 22:11:57 -04:00
|
|
|
const ObserverService = Cc['@mozilla.org/observer-service;1']
|
|
|
|
.getService(Ci.nsIObserverService);
|
2010-06-22 05:38:42 -04:00
|
|
|
|
2010-07-06 22:11:57 -04:00
|
|
|
var bag = Cc['@mozilla.org/hash-property-bag;1']
|
|
|
|
.createInstance(Ci.nsIPropertyBag);
|
|
|
|
ObserverService.notifyObservers(bag, QUERY, null);
|
2010-06-22 05:52:18 -04:00
|
|
|
|
2010-07-06 22:11:57 -04:00
|
|
|
var storage;
|
|
|
|
try {
|
|
|
|
storage = bag.getProperty(STORAGE_PROP);
|
|
|
|
}
|
|
|
|
catch(e) {
|
|
|
|
// This is the first provider of namespaces, so I create a new storage.
|
|
|
|
storage = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
// Export the storage to other instances when they are loaded.
|
|
|
|
ObserverService.addObserver(
|
|
|
|
{
|
|
|
|
observe : function(aSubject, aTopic, aData)
|
|
|
|
{
|
|
|
|
if (aTopic != QUERY) return;
|
|
|
|
aSubject.QueryInterface(Ci.nsIWritablePropertyBag)
|
|
|
|
.setProperty(STORAGE_PROP, storage);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
QUERY,
|
|
|
|
false
|
|
|
|
);
|
2010-06-22 05:38:42 -04:00
|
|
|
|
|
|
|
function getNamespaceFor(aName)
|
|
|
|
{
|
|
|
|
if (!aName)
|
|
|
|
throw new Error('you must specify the name of the namespace!');
|
|
|
|
|
2010-07-06 22:11:57 -04:00
|
|
|
if (!(aName in storage))
|
|
|
|
storage[aName] = {};
|
2010-06-22 05:38:42 -04:00
|
|
|
|
2010-07-06 22:11:57 -04:00
|
|
|
return storage[aName];
|
2010-06-22 05:38:42 -04:00
|
|
|
}
|