/
joose.singleton.js
40 lines (36 loc) · 1.07 KB
/
joose.singleton.js
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
//require('./diet-joose');
/**
* Joose.Singleton
* Role for singleton classes.
* Gives a getInstance class method to classes using this role.
* The getInstance method will create a method on first invocation and return the same instance
* upon every consecutive invocation.
*/
Role("Joose.Singleton", {
before: {
initialize: function () {
if(Joose._.singletonlock && this.meta['class'].__instance) {
throw new Error("The class "+this.meta.className()+" is a singleton. Please use the class method getInstance().")
}
}
},
methods: {
singletonInitialize: function () {
}
},
classMethods: {
getInstance: function () {
var registry = (Joose.Singleton.registry || (Joose.Singleton.registry = {}))
var name = this.meta.getName();
if (registry[name]) {
return registry[name];
}
Joose._.singletonlock = false;
registry[name] = this.meta.instantiate();
Joose._.singletonlock = true;
registry[name].singletonInitialize.apply(registry[name], arguments)
return registry[name];
}
}
});
Joose._.singletonlock = true;