2016-09-26 09:28:47 +00:00
|
|
|
|
2015-01-23 13:09:30 +00:00
|
|
|
/**
|
|
|
|
* The main base class. Manages the signal system.
|
|
|
|
*
|
|
|
|
* @param signals Map with all connected signal handlers
|
2022-05-26 06:08:31 +00:00
|
|
|
*/
|
2022-05-28 15:49:46 +00:00
|
|
|
module.exports = new Class({
|
2015-01-23 13:09:30 +00:00
|
|
|
Tag: 'vn-object'
|
|
|
|
,Properties: {}
|
2015-07-28 19:14:26 +00:00
|
|
|
|
2015-11-09 08:14:33 +00:00
|
|
|
,_refCount: 1
|
|
|
|
,_signalData: null
|
2015-01-23 13:09:30 +00:00
|
|
|
|
2022-05-28 15:49:46 +00:00
|
|
|
,initialize: function(props) {
|
|
|
|
this.setProperties(props);
|
2015-11-17 10:34:33 +00:00
|
|
|
}
|
|
|
|
|
2022-05-28 15:49:46 +00:00
|
|
|
,setProperties: function(props) {
|
2015-01-23 13:09:30 +00:00
|
|
|
for (var prop in props)
|
|
|
|
this[prop] = props[prop];
|
|
|
|
}
|
|
|
|
|
2022-05-28 15:49:46 +00:00
|
|
|
,ref: function() {
|
2015-11-09 08:14:33 +00:00
|
|
|
this._refCount++;
|
2015-07-28 19:14:26 +00:00
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2022-05-28 15:49:46 +00:00
|
|
|
,unref: function() {
|
2015-11-09 08:14:33 +00:00
|
|
|
this._refCount--;
|
2015-07-28 19:14:26 +00:00
|
|
|
|
2015-11-09 08:14:33 +00:00
|
|
|
if (this._refCount === 0)
|
2022-05-28 15:49:46 +00:00
|
|
|
this._destroy();
|
2015-07-28 19:14:26 +00:00
|
|
|
}
|
|
|
|
|
2022-05-28 15:49:46 +00:00
|
|
|
,loadXml: function(builder, node) {}
|
|
|
|
,appendChild: function(child) {}
|
2015-11-09 08:14:33 +00:00
|
|
|
|
2022-05-28 15:49:46 +00:00
|
|
|
,_signalInit: function() {
|
2015-11-09 08:14:33 +00:00
|
|
|
if (!this._signalData)
|
|
|
|
this._signalData = {
|
|
|
|
signals: {},
|
|
|
|
links: {}
|
|
|
|
};
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Conects a signal with a function.
|
|
|
|
*
|
|
|
|
* @param {String} id The signal identifier
|
|
|
|
* @param {Function} callback The callback
|
|
|
|
* @param {Object} instance The instance
|
2022-05-26 06:08:31 +00:00
|
|
|
*/
|
2022-05-28 15:49:46 +00:00
|
|
|
,on: function(id, callback, instance) {
|
|
|
|
if (!(callback instanceof Function)) {
|
|
|
|
console.warn('Vn.Object: Invalid callback for signal \'%s\'', id);
|
2015-07-17 14:34:42 +00:00
|
|
|
return;
|
2015-11-09 08:14:33 +00:00
|
|
|
}
|
|
|
|
|
2022-05-28 15:49:46 +00:00
|
|
|
this._signalInit();
|
2015-11-09 08:14:33 +00:00
|
|
|
var callbacks = this._signalData.signals[id];
|
|
|
|
|
|
|
|
if (!callbacks)
|
|
|
|
callbacks = this._signalData.signals[id] = [];
|
2015-01-23 13:09:30 +00:00
|
|
|
|
2022-05-28 15:49:46 +00:00
|
|
|
callbacks.push({
|
2015-01-23 13:09:30 +00:00
|
|
|
blocked: false
|
|
|
|
,callback: callback
|
|
|
|
,instance: instance
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Locks/Unlocks a signal emission to the specified object.
|
|
|
|
*
|
|
|
|
* @param {String} id The signal identifier
|
|
|
|
* @param {Function} callback The callback
|
|
|
|
* @param {Boolean} block %true for lock the signal, %false for unlock
|
2022-05-26 06:08:31 +00:00
|
|
|
*/
|
2022-05-28 15:49:46 +00:00
|
|
|
,blockSignal: function(id, callback, block, instance) {
|
2015-11-09 08:14:33 +00:00
|
|
|
if (!this._signalData)
|
|
|
|
return;
|
|
|
|
|
|
|
|
var callbacks = this._signalData.signals[id];
|
2015-01-23 13:09:30 +00:00
|
|
|
|
|
|
|
if (!callbacks)
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (var i = 0; i < callbacks.length; i++)
|
2015-07-28 19:14:26 +00:00
|
|
|
if (callbacks[i].callback == callback
|
|
|
|
&& callbacks[i].instance == instance)
|
2015-01-23 13:09:30 +00:00
|
|
|
callbacks[i].blocked = block;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Emits a signal in the current object.
|
|
|
|
*
|
|
|
|
* @param {String} id The signal identifier
|
2022-05-26 06:08:31 +00:00
|
|
|
*/
|
2022-05-28 15:49:46 +00:00
|
|
|
,signalEmit: function(id) {
|
2015-11-09 08:14:33 +00:00
|
|
|
if (!this._signalData)
|
|
|
|
return;
|
|
|
|
|
|
|
|
var callbacks = this._signalData.signals[id];
|
2015-01-23 13:09:30 +00:00
|
|
|
|
|
|
|
if (!callbacks)
|
|
|
|
return;
|
|
|
|
|
|
|
|
var callbackArgs = [];
|
2022-05-28 15:49:46 +00:00
|
|
|
callbackArgs.push(this);
|
2015-01-23 13:09:30 +00:00
|
|
|
|
|
|
|
for (var i = 1; i < arguments.length; i++)
|
2022-05-28 15:49:46 +00:00
|
|
|
callbackArgs.push(arguments[i]);
|
2015-01-23 13:09:30 +00:00
|
|
|
|
|
|
|
for (var i = 0; i < callbacks.length; i++)
|
|
|
|
if (!callbacks[i].blocked)
|
2022-05-28 15:49:46 +00:00
|
|
|
callbacks[i].callback.apply(callbacks[i].instance, callbackArgs);
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Disconnects a signal from current object.
|
|
|
|
*
|
|
|
|
* @param {String} id The signal identifier
|
|
|
|
* @param {Function} callback The connected callback
|
|
|
|
* @param {Object} instance The instance
|
2022-05-26 06:08:31 +00:00
|
|
|
*/
|
2022-05-28 15:49:46 +00:00
|
|
|
,disconnect: function(id, callback, instance) {
|
2015-11-09 08:14:33 +00:00
|
|
|
if (!this._signalData)
|
|
|
|
return;
|
|
|
|
|
|
|
|
var callbacks = this._signalData.signals[id];
|
2015-01-23 13:09:30 +00:00
|
|
|
|
|
|
|
if (!callbacks)
|
|
|
|
return;
|
2015-07-28 19:14:26 +00:00
|
|
|
|
2015-01-23 13:09:30 +00:00
|
|
|
for (var i = 0; i < callbacks.length; i++)
|
|
|
|
if (callbacks[i].callback == callback
|
|
|
|
&& callbacks[i].instance == instance)
|
2022-05-28 15:49:46 +00:00
|
|
|
callbacks.splice(i--, 1);
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
2015-07-28 19:14:26 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Disconnects all signals for the given instance.
|
|
|
|
*
|
|
|
|
* @param {Object} instance The instance
|
2022-05-26 06:08:31 +00:00
|
|
|
*/
|
2022-05-28 15:49:46 +00:00
|
|
|
,disconnectByInstance: function(instance) {
|
2015-11-09 08:14:33 +00:00
|
|
|
if (!this._signalData)
|
|
|
|
return;
|
|
|
|
|
|
|
|
var signals = this._signalData.signals;
|
|
|
|
|
2022-05-28 15:49:46 +00:00
|
|
|
for (var signalId in signals) {
|
2015-11-09 08:14:33 +00:00
|
|
|
var callbacks = signals[signalId];
|
2015-07-28 19:14:26 +00:00
|
|
|
|
|
|
|
for (var i = 0; i < callbacks.length; i++)
|
|
|
|
if (callbacks[i].instance == instance)
|
2022-05-28 15:49:46 +00:00
|
|
|
callbacks.splice(i--, 1);
|
2015-07-28 19:14:26 +00:00
|
|
|
}
|
|
|
|
}
|
2015-01-23 13:09:30 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Destroys the object, this method should only be called before losing
|
|
|
|
* the last reference to the object.
|
2022-05-26 06:08:31 +00:00
|
|
|
*/
|
2022-05-28 15:49:46 +00:00
|
|
|
,_destroy: function() {
|
2015-11-09 08:14:33 +00:00
|
|
|
if (!this._signalData)
|
|
|
|
return;
|
|
|
|
|
|
|
|
var links = this._signalData.links;
|
2015-07-28 19:14:26 +00:00
|
|
|
|
|
|
|
for (var key in links)
|
2022-05-28 15:49:46 +00:00
|
|
|
links[key].disconnectByInstance(this);
|
2015-07-28 19:14:26 +00:00
|
|
|
|
2015-11-09 08:14:33 +00:00
|
|
|
this._signalData = null;
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
|
|
|
|
2022-05-28 15:49:46 +00:00
|
|
|
,link: function(prop, handlers) {
|
|
|
|
this._signalInit();
|
2015-11-09 08:14:33 +00:00
|
|
|
var links = this._signalData.links;
|
|
|
|
|
2022-05-28 15:49:46 +00:00
|
|
|
for (var key in prop) {
|
2015-01-23 13:09:30 +00:00
|
|
|
var newObject = prop[key];
|
|
|
|
var oldObject = this[key];
|
|
|
|
|
2022-05-28 15:49:46 +00:00
|
|
|
if (oldObject) {
|
|
|
|
oldObject.disconnectByInstance(this);
|
|
|
|
oldObject.unref();
|
2015-07-28 19:14:26 +00:00
|
|
|
}
|
2015-01-23 13:09:30 +00:00
|
|
|
|
|
|
|
this[key] = newObject;
|
|
|
|
|
2022-05-28 15:49:46 +00:00
|
|
|
if (newObject) {
|
|
|
|
links[key] = newObject.ref();
|
2015-07-28 19:14:26 +00:00
|
|
|
|
|
|
|
for (var signal in handlers)
|
2022-05-28 15:49:46 +00:00
|
|
|
newObject.on(signal, handlers[signal], this);
|
|
|
|
} else if (oldObject)
|
2015-11-09 08:14:33 +00:00
|
|
|
delete links[key];
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2015-10-14 11:51:43 +00:00
|
|
|
|