bzr branch
http://gegoxaren.bato24.eu/bzr/%2Bjunk/gjs_values
2
by Gustav Hartvigsson
saving |
1 |
/* Copyright 2014 Gustav Hartvigsson <gustav.hartvigsson<at>gmail.com>
|
2 |
*
|
|
3 |
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
4 |
* of this software and associated documentation files (the "Software"), to deal
|
|
5 |
* in the Software without restriction, including without limitation the rights
|
|
6 |
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
7 |
* copies of the Software, and to permit persons to whom the Software is
|
|
8 |
* furnished to do so, subject to the following conditions:
|
|
9 |
*
|
|
10 |
* The above copyright notice and this permission notice shall be included in
|
|
11 |
* all copies or substantial portions of the Software.
|
|
12 |
*
|
|
13 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
14 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
15 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
16 |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
17 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
18 |
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
19 |
* DEALINGS IN THE SOFTWARE.
|
|
20 |
*/
|
|
1
by Gustav Hartvigsson
initial messy code |
21 |
|
22 |
const GObject = imports.gi.GObject; |
|
23 |
const GLib = imports.gi.GLib; |
|
24 |
const Lang = imports.lang; |
|
25 |
||
2
by Gustav Hartvigsson
saving |
26 |
const ValueTypes = { |
27 |
OBJECT: typeof({}), |
|
28 |
NUMBER: typeof(1), |
|
29 |
BOOLEAN: typeof(true), |
|
30 |
STRING: typeof("abc"), |
|
31 |
UNDEFINED: undefined |
|
32 |
}
|
|
1
by Gustav Hartvigsson
initial messy code |
33 |
|
2
by Gustav Hartvigsson
saving |
34 |
/** @class Values
|
35 |
* A special class that can hold value and can do multicast listening events.
|
|
36 |
*
|
|
37 |
* The reasoning behind this class is that sometimes we want to store values
|
|
38 |
* in an easier way then the one that is in GObject.
|
|
39 |
*
|
|
40 |
* Also, this class provides broadcast like capabilities for others components
|
|
41 |
* to listen to. This is something that can not be done in the GObject
|
|
42 |
* signal system, where only one callback can be used for each signal.
|
|
43 |
*
|
|
44 |
* please note that this may be very slow.
|
|
45 |
*/
|
|
1
by Gustav Hartvigsson
initial messy code |
46 |
const Values = new Lang.Class ({ |
47 |
Name: 'Values', |
|
48 |
|
|
49 |
/** |
|
2
by Gustav Hartvigsson
saving |
50 |
* Name to keep track of what object we are using.
|
51 |
*/
|
|
52 |
domain: "non", |
|
53 |
|
|
54 |
|
|
55 |
/** @structure _value_defs |
|
1
by Gustav Hartvigsson
initial messy code |
56 |
* holds the definitions of values with their name, type, and optionally
|
57 |
* user defined data that may be needed when dealing with getting and
|
|
58 |
* setting of the values:
|
|
59 |
* <code>
|
|
4
by Gustav Hartvigsson
* got the signaling working... I think. |
60 |
{"value1": {type: "string",
|
61 |
setter: function1,
|
|
62 |
getter: function2,
|
|
63 |
user_data: {any}},
|
|
64 |
"value2": {type: "boolean",
|
|
65 |
setter: function3,
|
|
66 |
getter: function4,
|
|
67 |
user_data: {any}}
|
|
1
by Gustav Hartvigsson
initial messy code |
68 |
}
|
69 |
</code>
|
|
70 |
*/
|
|
71 |
_value_defs: {}, |
|
72 |
|
|
2
by Gustav Hartvigsson
saving |
73 |
/** @structure |
74 |
* Holds the holds of the tells that listeners can listen to.
|
|
75 |
*/
|
|
76 |
_tellers: [], |
|
1
by Gustav Hartvigsson
initial messy code |
77 |
|
2
by Gustav Hartvigsson
saving |
78 |
/** @structure _listeners |
79 |
* Holds a list of listeners in a multi map:
|
|
80 |
* <code>
|
|
81 |
{ "tell1": [callback0, callback1, callback2], "tell2": [callback4]}
|
|
82 |
</code>
|
|
1
by Gustav Hartvigsson
initial messy code |
83 |
*/
|
2
by Gustav Hartvigsson
saving |
84 |
_listeners: {}, |
1
by Gustav Hartvigsson
initial messy code |
85 |
|
86 |
_init: function (params) { |
|
87 |
|
|
88 |
|
|
89 |
if (params != undefined) { |
|
90 |
if (params["domain"] != undefined) { |
|
91 |
this.domain = params["domain"]; |
|
92 |
} else { |
|
2
by Gustav Hartvigsson
saving |
93 |
print ("It is recommended that a Values object has a domain."); |
1
by Gustav Hartvigsson
initial messy code |
94 |
} |
95 |
|
|
96 |
if (params["tellers"] != undefined) { |
|
97 |
this._tellers = params["tellers"] |
|
98 |
} |
|
4
by Gustav Hartvigsson
* got the signaling working... I think. |
99 |
|
100 |
if (params["value_defs"] != undefined) { |
|
101 |
this._value_defs = params["value_defs"]; |
|
102 |
} |
|
1
by Gustav Hartvigsson
initial messy code |
103 |
} |
104 |
}, |
|
105 |
|
|
106 |
/** @method |
|
2
by Gustav Hartvigsson
saving |
107 |
* adds a setter to a value.
|
108 |
*
|
|
109 |
* @param name The name of the value to add a setter to.
|
|
110 |
* @param func the function that is used for setting of data.
|
|
111 |
*
|
|
112 |
* @return true on success
|
|
113 |
* @return false on fail
|
|
114 |
*/
|
|
115 |
add_setter: function (name, func) { |
|
116 |
if (name == undefined || |
|
117 |
func == undefined || |
|
118 |
this._value_defs[name] == undefined || |
|
119 |
!(func instanceof Function)) { |
|
120 |
return false; |
|
121 |
} |
|
4
by Gustav Hartvigsson
* got the signaling working... I think. |
122 |
this._value_defs[name]["setting"] = func; |
3
by Gustav Hartvigsson
saving |
123 |
|
4
by Gustav Hartvigsson
* got the signaling working... I think. |
124 |
return true; |
2
by Gustav Hartvigsson
saving |
125 |
}, |
126 |
|
|
127 |
/** @method add_value_def |
|
128 |
* adds a value defined to the value definitions list.
|
|
129 |
*
|
|
130 |
* @param name the name of the value to add
|
|
131 |
* @param type the type of the value (string)
|
|
132 |
* @param user_data any extra data that may be used with the value's setting
|
|
133 |
* and getting.
|
|
134 |
*/
|
|
135 |
add_value_def: function (name, type, user_data) { |
|
4
by Gustav Hartvigsson
* got the signaling working... I think. |
136 |
if (name == undefined || |
137 |
func == undefined || |
|
138 |
this._value_defs[name] == undefined || |
|
139 |
!(func instanceof Function)) { |
|
140 |
return false; |
|
141 |
} |
|
2
by Gustav Hartvigsson
saving |
142 |
|
4
by Gustav Hartvigsson
* got the signaling working... I think. |
143 |
return true; |
144 |
}, |
|
2
by Gustav Hartvigsson
saving |
145 |
|
146 |
/** @method |
|
1
by Gustav Hartvigsson
initial messy code |
147 |
* Method to get the value stored.
|
148 |
*/
|
|
149 |
get_value: function (name) { |
|
4
by Gustav Hartvigsson
* got the signaling working... I think. |
150 |
if (name == undefined || |
151 |
value == undefined) { |
|
152 |
return undefined; |
|
153 |
} |
|
154 |
if (this._value_defs[name == undefined]) { |
|
155 |
return undefined; |
|
156 |
} |
|
1
by Gustav Hartvigsson
initial messy code |
157 |
|
4
by Gustav Hartvigsson
* got the signaling working... I think. |
158 |
return this._value_defs[name]["getter"](); |
1
by Gustav Hartvigsson
initial messy code |
159 |
}, |
160 |
|
|
2
by Gustav Hartvigsson
saving |
161 |
/** @method set_value |
1
by Gustav Hartvigsson
initial messy code |
162 |
* Sets a value.
|
163 |
*/
|
|
164 |
set_value: function (name, value) { |
|
2
by Gustav Hartvigsson
saving |
165 |
if (name == undefined || |
4
by Gustav Hartvigsson
* got the signaling working... I think. |
166 |
value == undefined) { |
167 |
return false; |
|
168 |
} |
|
169 |
if (this._value_defs[name == undefined]) { |
|
170 |
return false; |
|
171 |
} |
|
172 |
this._value_defs[name]["setter"] (value, this._value_defs[name]["user_data"]); |
|
173 |
return true; |
|
174 |
}, |
|
2
by Gustav Hartvigsson
saving |
175 |
|
176 |
/** @method tell |
|
177 |
* Sends a "tell" to the listeners.
|
|
178 |
* @param name the name of the tell
|
|
179 |
* @param user_data user data to send to the listeners
|
|
180 |
*/
|
|
181 |
tell: function (name, user_data) { |
|
182 |
if (this._listeners[name] == undefined || this._tellers.indexOf (name) < 0 ) { |
|
4
by Gustav Hartvigsson
* got the signaling working... I think. |
183 |
return false; |
2
by Gustav Hartvigsson
saving |
184 |
} |
4
by Gustav Hartvigsson
* got the signaling working... I think. |
185 |
for (let i = 0; i < this._listeners[name].length; i++) { |
2
by Gustav Hartvigsson
saving |
186 |
this._listeners[name][i](this, user_data); |
187 |
} |
|
4
by Gustav Hartvigsson
* got the signaling working... I think. |
188 |
return true; |
2
by Gustav Hartvigsson
saving |
189 |
}, |
190 |
|
|
191 |
/** @method listen |
|
192 |
* add a listener function to the object.
|
|
193 |
*/
|
|
194 |
listen: function (name, func) { |
|
195 |
if (name == undefined || |
|
196 |
func == undefined || |
|
197 |
this._tellers.indexOf (name) < 0 || |
|
198 |
!(func instanceof Function)) { |
|
199 |
return false; |
|
200 |
} |
|
4
by Gustav Hartvigsson
* got the signaling working... I think. |
201 |
if (this._listeners[name] == undefined) { |
202 |
this._listeners[name] = []; |
|
203 |
} |
|
2
by Gustav Hartvigsson
saving |
204 |
this._listeners[name].push (func); |
4
by Gustav Hartvigsson
* got the signaling working... I think. |
205 |
return true; |
2
by Gustav Hartvigsson
saving |
206 |
} |
1
by Gustav Hartvigsson
initial messy code |
207 |
});
|