Skip to content
This repository was archived by the owner on Jun 26, 2020. It is now read-only.

Commit ad1da26

Browse files
author
Piotr Jasiun
authored
Merge pull request #135 from ckeditor/t/134
Fix: Editor#destroy waits for the initialization. Closes #134.
2 parents 734166a + b86c0fd commit ad1da26

File tree

4 files changed

+59
-31
lines changed

4 files changed

+59
-31
lines changed

src/editor/editor.js

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -239,17 +239,26 @@ export default class Editor {
239239
/**
240240
* Destroys the editor instance, releasing all resources used by it.
241241
*
242+
* **Note** The editor can not be destroyed during the initialization phase so
243+
* this methods waits for the editor initialization before destroying.
244+
*
242245
* @fires destroy
243246
* @returns {Promise} A promise that resolves once the editor instance is fully destroyed.
244247
*/
245248
destroy() {
246-
this.fire( 'destroy' );
247-
248-
this.stopListening();
249+
let readyPromise = Promise.resolve();
249250

250-
this.commands.destroy();
251+
if ( this.state == 'initializing' ) {
252+
readyPromise = new Promise( resolve => this.once( 'ready', resolve ) );
253+
}
251254

252-
return this.plugins.destroy()
255+
return readyPromise
256+
.then( () => {
257+
this.fire( 'destroy' );
258+
this.stopListening();
259+
this.commands.destroy();
260+
} )
261+
.then( () => this.plugins.destroy() )
253262
.then( () => {
254263
this.model.destroy();
255264
this.data.destroy();

tests/_utils-tests/modeltesteditor.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,14 @@ describe( 'ModelTestEditor', () => {
2929

3030
it( 'should disable editing pipeline', () => {
3131
const spy = sinon.spy( EditingController.prototype, 'destroy' );
32-
const editor = new ModelTestEditor( { foo: 1 } );
3332

34-
sinon.assert.calledOnce( spy );
33+
return ModelTestEditor.create( { foo: 1 } ).then( editor => {
34+
sinon.assert.calledOnce( spy );
35+
36+
spy.restore();
3537

36-
spy.restore();
37-
return editor.destroy();
38+
return editor.destroy();
39+
} );
3840
} );
3941

4042
it( 'creates main root element', () => {

tests/editor/editor.js

Lines changed: 38 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -197,10 +197,10 @@ describe( 'Editor', () => {
197197
} );
198198

199199
it( 'is `destroyed` after editor destroy', () => {
200-
const editor = new Editor();
201-
202-
return editor.destroy().then( () => {
203-
expect( editor.state ).to.equal( 'destroyed' );
200+
return Editor.create().then( editor => {
201+
return editor.destroy().then( () => {
202+
expect( editor.state ).to.equal( 'destroyed' );
203+
} );
204204
} );
205205
} );
206206

@@ -283,33 +283,49 @@ describe( 'Editor', () => {
283283

284284
describe( 'destroy()', () => {
285285
it( 'should fire "destroy"', () => {
286-
const editor = new Editor();
287-
const spy = sinon.spy();
286+
return Editor.create().then( editor => {
287+
const spy = sinon.spy();
288288

289-
editor.on( 'destroy', spy );
289+
editor.on( 'destroy', spy );
290290

291-
return editor.destroy().then( () => {
292-
expect( spy.calledOnce ).to.be.true;
291+
return editor.destroy().then( () => {
292+
expect( spy.calledOnce ).to.be.true;
293+
} );
293294
} );
294295
} );
295296

296297
it( 'should destroy all components it initialized', () => {
298+
return Editor.create().then( editor => {
299+
const dataDestroySpy = sinon.spy( editor.data, 'destroy' );
300+
const modelDestroySpy = sinon.spy( editor.model, 'destroy' );
301+
const editingDestroySpy = sinon.spy( editor.editing, 'destroy' );
302+
const pluginsDestroySpy = sinon.spy( editor.plugins, 'destroy' );
303+
const keystrokesDestroySpy = sinon.spy( editor.keystrokes, 'destroy' );
304+
305+
return editor.destroy()
306+
.then( () => {
307+
sinon.assert.calledOnce( dataDestroySpy );
308+
sinon.assert.calledOnce( modelDestroySpy );
309+
sinon.assert.calledOnce( editingDestroySpy );
310+
sinon.assert.calledOnce( pluginsDestroySpy );
311+
sinon.assert.calledOnce( keystrokesDestroySpy );
312+
} );
313+
} );
314+
} );
315+
316+
it( 'should wait for the full init before destroying', done => {
317+
const spy = sinon.spy();
297318
const editor = new Editor();
298319

299-
const dataDestroySpy = sinon.spy( editor.data, 'destroy' );
300-
const modelDestroySpy = sinon.spy( editor.model, 'destroy' );
301-
const editingDestroySpy = sinon.spy( editor.editing, 'destroy' );
302-
const pluginsDestroySpy = sinon.spy( editor.plugins, 'destroy' );
303-
const keystrokesDestroySpy = sinon.spy( editor.keystrokes, 'destroy' );
320+
editor.on( 'destroy', () => {
321+
done();
322+
} );
304323

305-
return editor.destroy()
306-
.then( () => {
307-
sinon.assert.calledOnce( dataDestroySpy );
308-
sinon.assert.calledOnce( modelDestroySpy );
309-
sinon.assert.calledOnce( editingDestroySpy );
310-
sinon.assert.calledOnce( pluginsDestroySpy );
311-
sinon.assert.calledOnce( keystrokesDestroySpy );
312-
} );
324+
editor.destroy();
325+
326+
sinon.assert.notCalled( spy );
327+
328+
editor.fire( 'ready' );
313329
} );
314330
} );
315331

tests/editor/utils/attachtoform.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ describe( 'attachToForm()', () => {
3434
editor.data.processor = new HtmlDataProcessor();
3535
editor.model.document.createRoot();
3636
editor.model.schema.extend( '$text', { allowIn: '$root' } );
37+
editor.fire( 'ready' );
3738

3839
editor.data.set( 'foo bar' );
3940
} );

0 commit comments

Comments
 (0)