Debian Codegraph @

dijit/Destroyable.js

raw symbols SCIP
1 define([
2 "dojo/_base/array", // array.forEach array.map
3 "dojo/aspect",
4 "dojo/_base/declare"
5 ], function(array, aspect, declare){
6
7 // module:
8 // dijit/Destroyable
9
10 return declare("dijit.Destroyable", null, {
11 // summary:
12 // Mixin to track handles and release them when instance is destroyed.
13 // description:
14 // Call this.own(...) on list of handles (returned from dojo/aspect, dojo/on,
15 // dojo/Stateful::watch, or any class (including widgets) with a destroyRecursive() or destroy() method.
16 // Then call destroy() later to destroy this instance and release the resources.
17
18 destroy: function(/*Boolean*/ preserveDom){
19 // summary:
20 // Destroy this class, releasing any resources registered via own().
21 this._destroyed = true;
22 },
23
24 own: function(){
25 // summary:
26 // Track specified handles and remove/destroy them when this instance is destroyed, unless they were
27 // already removed/destroyed manually.
28 // tags:
29 // protected
30 // returns:
31 // The array of specified handles, so you can do for example:
32 // | var handle = this.own(on(...))[0];
33
34 var cleanupMethods = [
35 "destroyRecursive",
36 "destroy",
37 "remove"
38 ];
39
40 array.forEach(arguments, function(handle){
41 // When this.destroy() is called, destroy handle. Since I'm using aspect.before(),
42 // the handle will be destroyed before a subclass's destroy() method starts running, before it calls
43 // this.inherited() or even if it doesn't call this.inherited() at all. If that's an issue, make an
44 // onDestroy() method and connect to that instead.
45 var destroyMethodName;
46 var odh = aspect.before(this, "destroy", function (preserveDom){
47 handle[destroyMethodName](preserveDom);
48 });
49
50 // Callback for when handle is manually destroyed.
51 var hdhs = [];
52 function onManualDestroy(){
53 odh.remove();
54 array.forEach(hdhs, function(hdh){
55 hdh.remove();
56 });
57 }
58
59 // Setup listeners for manual destroy of handle.
60 // Also computes destroyMethodName, used in listener above.
61 if(handle.then){
62 // Special path for Promises. Detect when Promise is resolved, rejected, or
63 // canceled (nb: cancelling a Promise causes it to be rejected).
64 destroyMethodName = "cancel";
65 handle.then(onManualDestroy, onManualDestroy);
66 }else{
67 // Path for other handles. Just use AOP to detect when handle is manually destroyed.
68 array.forEach(cleanupMethods, function(cleanupMethod){
69 if(typeof handle[cleanupMethod] === "function"){
70 if(!destroyMethodName){
71 // Use first matching method name in above listener (prefer destroyRecursive() to destroy())
72 destroyMethodName = cleanupMethod;
73 }
74 hdhs.push(aspect.after(handle, cleanupMethod, onManualDestroy, true));
75 }
76 });
77 }
78 }, this);
79
80 return arguments; // handle
81 }
82 });
83 });