@urso/core 0.7.46 → 0.7.47
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
|
@@ -11,56 +11,107 @@ class ModulesObserverController {
|
|
|
11
11
|
this._getUid = this._getUid.bind(this);
|
|
12
12
|
}
|
|
13
13
|
|
|
14
|
+
/**
|
|
15
|
+
* fire event
|
|
16
|
+
* @param {String} eventName
|
|
17
|
+
* @param {Mixed} params
|
|
18
|
+
* @param {Number} delay
|
|
19
|
+
*/
|
|
14
20
|
fire(eventName, params, delay) {
|
|
15
|
-
if (!eventName)
|
|
16
|
-
|
|
21
|
+
if (!eventName) {
|
|
22
|
+
Urso.logger.error('ModulesObserverController fire error:', eventName);
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
17
25
|
|
|
18
|
-
if (delay)
|
|
19
|
-
|
|
26
|
+
if (delay) {
|
|
27
|
+
setTimeout((() => { this.fire(eventName, params); }).bind(this), delay);
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
20
30
|
|
|
21
31
|
this._fireLocal(eventName, params);
|
|
22
|
-
this._fireLocal(eventName + this.
|
|
32
|
+
this._fireLocal(eventName + this._getLocalSuffix(), params);
|
|
23
33
|
};
|
|
24
34
|
|
|
35
|
+
/**
|
|
36
|
+
* add listener
|
|
37
|
+
* @param {String} eventName
|
|
38
|
+
* @param {Function} callback
|
|
39
|
+
* @param {Boolean} global
|
|
40
|
+
*/
|
|
25
41
|
add(eventName, callback, global) {
|
|
26
|
-
if (!eventName || !callback)
|
|
27
|
-
|
|
42
|
+
if (!eventName || !callback) {
|
|
43
|
+
Urso.logger.error('ModulesObserverController add error:', eventName, callback);
|
|
44
|
+
return
|
|
45
|
+
}
|
|
28
46
|
|
|
29
47
|
if (global)
|
|
30
48
|
this._addLocal(eventName, callback);
|
|
31
49
|
else
|
|
32
|
-
this._addLocal(eventName + this.
|
|
50
|
+
this._addLocal(eventName + this._getLocalSuffix(), callback);
|
|
33
51
|
}
|
|
34
52
|
|
|
53
|
+
/**
|
|
54
|
+
* remove listener
|
|
55
|
+
* @param {String} eventName
|
|
56
|
+
* @param {Function} callback
|
|
57
|
+
* @param {Boolean} global
|
|
58
|
+
*/
|
|
35
59
|
remove(eventName, callback, global) {
|
|
36
60
|
if (global)
|
|
37
61
|
this._removeLocal(eventName, callback);
|
|
38
62
|
else
|
|
39
|
-
this._removeLocal(eventName + this.
|
|
63
|
+
this._removeLocal(eventName + this._getLocalSuffix(), callback);
|
|
40
64
|
}
|
|
41
65
|
|
|
66
|
+
/**
|
|
67
|
+
* set local events prefix
|
|
68
|
+
* @param {String} p
|
|
69
|
+
*/
|
|
42
70
|
setPrefix(p) {
|
|
43
71
|
this._prefix = p;
|
|
44
72
|
};
|
|
45
73
|
|
|
46
|
-
|
|
74
|
+
/**
|
|
75
|
+
* get callback uid
|
|
76
|
+
* @param {Function} callback
|
|
77
|
+
* @returns {String}
|
|
78
|
+
*/
|
|
79
|
+
_getUid(callback) {
|
|
80
|
+
if (callback._ouid) //observer callback uid alredy exists
|
|
81
|
+
return callback._ouid;
|
|
82
|
+
|
|
47
83
|
this._counter++;
|
|
48
84
|
return 'observer_' + this._counter;
|
|
49
85
|
}
|
|
50
86
|
|
|
87
|
+
/**
|
|
88
|
+
* add local listener
|
|
89
|
+
* @param {String} name
|
|
90
|
+
* @param {Function} callback
|
|
91
|
+
*/
|
|
51
92
|
_addLocal(name, callback) {
|
|
52
93
|
if (!this._observers[name])
|
|
53
94
|
this._observers[name] = {};
|
|
54
95
|
|
|
55
|
-
const uid = this._getUid();
|
|
56
|
-
|
|
96
|
+
const uid = this._getUid(callback);
|
|
97
|
+
|
|
98
|
+
if (!callback._ouid) {
|
|
99
|
+
callback._ouid = uid;
|
|
100
|
+
}
|
|
57
101
|
|
|
58
102
|
this._observers[name][uid] = callback;
|
|
59
103
|
}
|
|
60
104
|
|
|
105
|
+
/**
|
|
106
|
+
* remove local listener
|
|
107
|
+
* @param {String} name
|
|
108
|
+
* @param {Function} callback
|
|
109
|
+
*/
|
|
61
110
|
_removeLocal(name, callback) {
|
|
62
|
-
if (!this._observers[name] || !callback._ouid)
|
|
63
|
-
|
|
111
|
+
if (!this._observers[name] || !callback._ouid) {
|
|
112
|
+
Urso.logger.error('ModulesObserverController remove error, no observer with', name, callback);
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
64
115
|
|
|
65
116
|
const uid = callback._ouid;
|
|
66
117
|
delete this._observers[name][uid];
|
|
@@ -69,6 +120,12 @@ class ModulesObserverController {
|
|
|
69
120
|
delete this._observers[name];
|
|
70
121
|
}
|
|
71
122
|
|
|
123
|
+
/**
|
|
124
|
+
* fire local event
|
|
125
|
+
* @param {String} name
|
|
126
|
+
* @param {Mixed} params
|
|
127
|
+
* @returns {Boolean}
|
|
128
|
+
*/
|
|
72
129
|
_fireLocal(name, params) {
|
|
73
130
|
if (!this._observers[name])
|
|
74
131
|
return false;
|
|
@@ -79,20 +136,31 @@ class ModulesObserverController {
|
|
|
79
136
|
return true;
|
|
80
137
|
}
|
|
81
138
|
|
|
139
|
+
/**
|
|
140
|
+
* clear all local listeners (can be used on scene switch)
|
|
141
|
+
*/
|
|
82
142
|
clearAllLocal() {
|
|
83
143
|
for (let name in this._observers) {
|
|
84
|
-
if (name.endsWith(this.
|
|
144
|
+
if (name.endsWith(this._getLocalSuffix()))
|
|
85
145
|
delete this._observers[name];
|
|
86
146
|
}
|
|
87
147
|
}
|
|
88
148
|
|
|
149
|
+
/**
|
|
150
|
+
* clear all listeners
|
|
151
|
+
* @returns {Boolean}
|
|
152
|
+
*/
|
|
89
153
|
clear() {
|
|
90
154
|
this._observers = {};
|
|
91
155
|
|
|
92
156
|
return true;
|
|
93
157
|
}
|
|
94
158
|
|
|
95
|
-
|
|
159
|
+
/**
|
|
160
|
+
* get local events suffix
|
|
161
|
+
* @returns {String}
|
|
162
|
+
*/
|
|
163
|
+
_getLocalSuffix() {
|
|
96
164
|
return this._prefixDelimiter + this._prefix;
|
|
97
165
|
};
|
|
98
166
|
};
|