File: namespace.jsm

package info (click to toggle)
tree-style-tab 0.14.2012050301-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 1,624 kB
  • sloc: xml: 36; makefile: 2
file content (70 lines) | stat: -rwxr-xr-x 1,875 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/*
 Shared Namespace Library for JavaScript Code Modules

 Usage:
   Components.utils.import('resource://my-modules/namespace.jsm');
   var namespace = getNamespaceFor('mylibrary');
   if (!namespace.func1) {
     namespace.func1 = function() { ... };
     namespace.func2 = function() { ... };
   }
   var EXPORTED_SYMBOLS = ['func1', 'func2'];
   var func1 = namespace.func1;
   var func2 = namespace.func2;

 license: The MIT License, Copyright (c) 2010 SHIMODA "Piro" Hiroshi
   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;

const currentRevision = 2;

const QUERY = 'namespace.jsm[piro.sakura.ne.jp]:GetExistingNamespace';
const STORAGE_PROP = 'namespaces-storage';

const ObserverService = Cc['@mozilla.org/observer-service;1']
						.getService(Ci.nsIObserverService);

var bag = Cc['@mozilla.org/hash-property-bag;1']
			.createInstance(Ci.nsIPropertyBag);
ObserverService.notifyObservers(bag, QUERY, null);

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
);

function getNamespaceFor(aName)
{
	if (!aName)
		throw new Error('you must specify the name of the namespace!');

	if (!(aName in storage))
		storage[aName] = {};

	return storage[aName];
}