Cleanup.js
1.11 KB
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
41
42
43
44
(function(factory){
if(typeof define != "undefined"){
define(["../dcl", "./Destroyable"], factory);
}else if(typeof module != "undefined"){
module.exports = factory(require("../dcl"), require("./Destroyable"));
}else{
dclMixinsCleanup = factory(dcl, dclMixinsDestroyable);
}
})(function(dcl, Destroyable){
"use strict";
return dcl(Destroyable, {
declaredClass: "dcl/mixins/Cleanup",
constructor: function(){
this.__cleanupStack = [];
},
pushCleanup: function(resource, cleanup){
var f = cleanup ? function(){ cleanup(resource); } : function(){ resource.destroy(); };
this.__cleanupStack.push(f);
return f;
},
popCleanup: function(dontRun){
if(dontRun){
return this.__cleanupStack.pop();
}
this.__cleanupStack.pop()();
},
removeCleanup: function(f){
for(var i = this.__cleanupStack.length - 1; i >= 0; --i){
if(this.__cleanupStack[i] === f){
this.__cleanupStack.splice(i, 1);
return true;
}
}
},
cleanup: function(){
while(this.__cleanupStack.length){
this.__cleanupStack.pop()();
}
},
destroy: function(){
this.cleanup();
}
});
});