Skip to content

Commit f8cfd4b

Browse files
committed
Added support for blocking window destruction (#72)
1 parent 177c719 commit f8cfd4b

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

__tests__/window.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,4 +307,23 @@ describe('Window', () => {
307307
expect(childWindow.parent).toBe(null);
308308
childWindow.destroy();
309309
});
310+
311+
test('Should block destruction', () => {
312+
const blockWindow = new Window(core, {
313+
ondestroy: () => false
314+
});
315+
316+
const noblockWindow = new Window(core, {
317+
ondestroy: () => true
318+
});
319+
320+
const defaultWindow = new Window(core);
321+
322+
blockWindow.destroy();
323+
noblockWindow.destroy();
324+
defaultWindow.destroy();
325+
expect(blockWindow.destroyed).toBe(false);
326+
expect(noblockWindow.destroyed).toBe(true);
327+
expect(defaultWindow.destroyed).toBe(true);
328+
});
310329
});

src/window.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ export default class Window extends EventEmitter {
155155
* @param {string} [options.icon] Window Icon
156156
* @param {Window} [options.parent] The parent Window reference
157157
* @param {string|Function} [options.template] The Window HTML template (or function with signature (el, win) for programatic construction)
158+
* @param {Function} [options.ondestroy] A callback function when window destructs to interrupt the procedure
158159
* @param {WindowPosition|string} [options.position] Window position
159160
* @param {WindowDimension} [options.dimension] Window dimension
160161
* @param {WindowAttributes} [options.attributes] Apply Window attributes
@@ -166,6 +167,7 @@ export default class Window extends EventEmitter {
166167
title: null,
167168
parent: null,
168169
template: null,
170+
ondestroy: null,
169171
attributes: {},
170172
position: {},
171173
dimension: {},
@@ -285,6 +287,12 @@ export default class Window extends EventEmitter {
285287
*/
286288
this._template = options.template;
287289

290+
/**
291+
* Custom destructor callback
292+
* @type {Function}
293+
*/
294+
this._ondestroy = options.ondestroy || (() => true);
295+
288296
windows.push(this);
289297
}
290298

@@ -295,6 +303,11 @@ export default class Window extends EventEmitter {
295303
if (this.destroyed) {
296304
return;
297305
}
306+
307+
if (typeof this._ondestroy === 'function' && this._ondestroy() === false) {
308+
return;
309+
}
310+
298311
this.destroyed = true;
299312

300313
logger.debug('Window::destroy()');

0 commit comments

Comments
 (0)