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AAC,IAAI,CAAC,CAAC;QACf,CAAC,CAAC,CAAC;IACJ,CAAC;CACD;AAED,eAAe,WAAW,CAAC","sourcesContent":["import { Handle } from './interfaces';\nimport { createCompositeHandle } from './lang';\nimport Promise from '@dojo/shim/Promise';\n\n/**\n * No operation function to replace own once instance is destoryed\n */\nfunction noop(): Promise<boolean> {\n\treturn Promise.resolve(false);\n}\n\n/**\n * No op function used to replace own, once instance has been destoryed\n */\nfunction destroyed(): never {\n\tthrow new Error('Call made to destroyed method');\n}\n\nexport class Destroyable {\n\t/**\n\t * register handles for the instance\n\t */\n\tprivate handles: Handle[];\n\n\t/**\n\t * @constructor\n\t */\n\tconstructor() {\n\t\tthis.handles = [];\n\t}\n\n\t/**\n\t * Register handles for the instance that will be destroyed when `this.destroy` is called\n\t *\n\t * @param {Handle} handle The handle to add for the instance\n\t * @returns {Handle} a handle for the handle, removes the handle for the instance and calls destroy\n\t */\n\town(handles: Handle | Handle[]): Handle {\n\t\tconst handle = Array.isArray(handles) ? createCompositeHandle(...handles) : handles;\n\t\tconst { handles: _handles } = this;\n\t\t_handles.push(handle);\n\t\treturn {\n\t\t\tdestroy() {\n\t\t\t\t_handles.splice(_handles.indexOf(handle));\n\t\t\t\thandle.destroy();\n\t\t\t}\n\t\t};\n\t}\n\n\t/**\n\t * Destrpys all handers registered for the instance\n\t *\n\t * @returns {Promise<any} a promise that resolves once all handles have been destroyed\n\t */\n\tdestroy(): Promise<any> {\n\t\treturn new Promise((resolve) => {\n\t\t\tthis.handles.forEach((handle) => {\n\t\t\t\thandle && handle.destroy && handle.destroy();\n\t\t\t});\n\t\t\tthis.destroy = noop;\n\t\t\tthis.own = destroyed;\n\t\t\tresolve(true);\n\t\t});\n\t}\n}\n\nexport default Destroyable;\n"]}