Each time the anonymous function is invoked with a parameter, it encapsulates everything within itself. If another call to the subscribed function occurs before the first anonymous function's callback from the database or ajax call, it will not affect the previous anonymous function. The current anonymous function creates a new closure for each call, ensuring that data for each closure remains isolated and unaffected by other closures.
this.subscribe.call(this, e.horizontaladded, function(a, fn) {
if (!a.extra.owner.id) { return; };
(function(a) {
dataBase.insert(
dbPart(['horizontal?a=', a.extra.owner.instanceName, '&id=', a.extra.owner.id].join(''),
a.target),
dbCB(success, error)
);
function success(data, status) {
if (status === 'error') { return; };
console.log('Horizontal Added');
a.target.id = data.id;
a.target.HTML().addClass('alum_' + data.id);
a.target.finish.id = data.finishID;
a.target.size.id = data.sizeID;
a.target.siteLine.id = data.sitelineID;
}(a));
}, true);
In essence, every invocation of the e.horizontaladded I am subscribed to triggers a new anonymous function which closes up with its own set of personal data. Garbage collection takes care of cleaning up these closures individually.
Therefore, if 30 calls are made to the function I am subscribed to, resulting in 30 closures being created, each closure will clean itself up once the database success callback is triggered.