This repository has been archived by the owner on Jun 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Merged
Changes from 13 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
d0fe7f0
Implemented new version of the Autosave mechanism.
ma2ciek 1bbb661
Improved manual tests.
ma2ciek 1700f71
Introduced public Autosave#state.
ma2ciek 8634e66
Removed custom throttle function.
ma2ciek d268277
Introduced `config.autosave.waitingTime`.
ma2ciek 0b8ff77
Fixed tests with usage of `lodash.debounce`.
ma2ciek 766f6d5
Improved unit tests.
ma2ciek 29d3e76
Code style.
ma2ciek 5174852
Improved unit tests.
ma2ciek b07f3e0
Fixed bug and added test for it.
ma2ciek b6800cd
Fixed incorrect test, CC 100%.
ma2ciek c8a3c17
Cleaned up the API and docs.
ma2ciek 3ae597c
Added missing test.
ma2ciek aa35629
Improved comment.
ma2ciek fe7f4eb
Improved scenarios in the manual tests.
ma2ciek 5e66b2a
Modified default waitingTime to 1000ms.
ma2ciek File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,7 +10,9 @@ | |
import Plugin from '@ckeditor/ckeditor5-core/src/plugin'; | ||
import PendingActions from '@ckeditor/ckeditor5-core/src/pendingactions'; | ||
import DomEmitterMixin from '@ckeditor/ckeditor5-utils/src/dom/emittermixin'; | ||
import throttle from './throttle'; | ||
import ObservableMixin from '@ckeditor/ckeditor5-utils/src/observablemixin'; | ||
import mix from '@ckeditor/ckeditor5-utils/src/mix'; | ||
import { debounce } from 'lodash-es'; | ||
|
||
/* globals window */ | ||
|
||
|
@@ -62,6 +64,12 @@ export default class Autosave extends Plugin { | |
constructor( editor ) { | ||
super( editor ); | ||
|
||
const config = editor.config.get( 'autosave' ) || {}; | ||
|
||
// A minimum amount of time that needs to pass after the last action. | ||
// After that time the provided save callbacks are being called. | ||
const waitingTime = config.waitingTime || 2000; | ||
|
||
/** | ||
* The adapter is an object with a `save()` method. That method will be called whenever | ||
* the data changes. It might be called some time after the change, | ||
|
@@ -71,17 +79,31 @@ export default class Autosave extends Plugin { | |
*/ | ||
|
||
/** | ||
* Throttled save method. | ||
* The state of this plugin. | ||
* | ||
* The plugin can be in the following states: | ||
* | ||
* * synchronized - when all changes are saved | ||
* * waiting - when the plugin is waiting for other changes before calling `adapter#save()` and `config.autosave.save()` | ||
* * saving - when the provided save method is called and the plugin waits for the response | ||
* | ||
* @member {'synchronized'|'waiting'|'saving'} #state | ||
*/ | ||
this.set( 'state', 'synchronized' ); | ||
|
||
/** | ||
* Debounced save method. The `save` method is called the specified `waitingTime` after the `debouncedSave` is called, | ||
* unless new action happens in the meantime. | ||
* | ||
* @protected | ||
* @private | ||
* @type {Function} | ||
*/ | ||
this._throttledSave = throttle( this._save.bind( this ), 1000 ); | ||
this._debouncedSave = debounce( this._save.bind( this ), waitingTime ); | ||
|
||
/** | ||
* Last document version. | ||
* | ||
* @protected | ||
* @private | ||
* @type {Number} | ||
*/ | ||
this._lastDocumentVersion = editor.model.document.version; | ||
|
@@ -95,12 +117,12 @@ export default class Autosave extends Plugin { | |
this._domEmitter = Object.create( DomEmitterMixin ); | ||
|
||
/** | ||
* Save action counter monitors number of actions. | ||
* The config of this plugins. | ||
* | ||
* @private | ||
* @type {Number} | ||
* @type {Object} | ||
*/ | ||
this._saveActionCounter = 0; | ||
this._config = config; | ||
|
||
/** | ||
* An action that will be added to pending action manager for actions happening in that plugin. | ||
|
@@ -109,14 +131,6 @@ export default class Autosave extends Plugin { | |
* @member {Object} #_action | ||
*/ | ||
|
||
/** | ||
* Plugins' config. | ||
* | ||
* @private | ||
* @type {Object} | ||
*/ | ||
this._config = editor.config.get( 'autosave' ) || {}; | ||
|
||
/** | ||
* Editor's pending actions manager. | ||
* | ||
|
@@ -135,13 +149,22 @@ export default class Autosave extends Plugin { | |
this._pendingActions = editor.plugins.get( PendingActions ); | ||
|
||
this.listenTo( doc, 'change:data', () => { | ||
this._incrementCounter(); | ||
if ( !this._saveCallbacks.length ) { | ||
return; | ||
} | ||
|
||
if ( this.state == 'synchronized' ) { | ||
this._action = this._pendingActions.add( this.editor.t( 'Saving changes' ) ); | ||
this.state = 'waiting'; | ||
|
||
const willOriginalFunctionBeCalled = this._throttledSave(); | ||
this._debouncedSave(); | ||
} | ||
|
||
if ( !willOriginalFunctionBeCalled ) { | ||
this._decrementCounter(); | ||
else if ( this.state == 'waiting' ) { | ||
this._debouncedSave(); | ||
} | ||
|
||
// Do nothing if the plugin is in `external-saving` state. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A comment is needed that is will be handled based on the document version. |
||
} ); | ||
|
||
// Flush on the editor's destroy listener with the highest priority to ensure that | ||
|
@@ -175,7 +198,7 @@ export default class Autosave extends Plugin { | |
* @protected | ||
*/ | ||
_flush() { | ||
this._throttledSave.flush(); | ||
this._debouncedSave.flush(); | ||
} | ||
|
||
/** | ||
|
@@ -188,74 +211,62 @@ export default class Autosave extends Plugin { | |
_save() { | ||
const version = this.editor.model.document.version; | ||
|
||
const saveCallbacks = []; | ||
|
||
if ( this.adapter && this.adapter.save ) { | ||
saveCallbacks.push( this.adapter.save ); | ||
} | ||
|
||
if ( this._config.save ) { | ||
saveCallbacks.push( this._config.save ); | ||
} | ||
|
||
// Change may not produce an operation, so the document's version | ||
// can be the same after that change. | ||
if ( | ||
version < this._lastDocumentVersion || | ||
!saveCallbacks.length || | ||
this.editor.state === 'initializing' | ||
) { | ||
this._throttledSave.flush(); | ||
this._decrementCounter(); | ||
this._debouncedSave.cancel(); | ||
|
||
return; | ||
} | ||
|
||
this._lastDocumentVersion = version; | ||
|
||
// Wait one promise cycle to be sure that: | ||
// 1. The save method is always asynchronous. | ||
// 2. Save callbacks are not called inside conversions or while editor's state changes. | ||
this.state = 'saving'; | ||
|
||
// Wait one promise cycle to be sure that save callbacks are not called | ||
// inside a conversion or when the editor's state changes. | ||
Promise.resolve() | ||
.then( () => Promise.all( | ||
saveCallbacks.map( cb => cb( this.editor ) ) | ||
this._saveCallbacks.map( cb => cb( this.editor ) ) | ||
) ) | ||
.then( () => { | ||
this._decrementCounter(); | ||
if ( this.editor.model.document.version > this._lastDocumentVersion ) { | ||
this.state = 'waiting'; | ||
this._debouncedSave(); | ||
} else { | ||
this.state = 'synchronized'; | ||
this._pendingActions.remove( this._action ); | ||
this._action = null; | ||
} | ||
} ); | ||
} | ||
|
||
/** | ||
* Increments counter and adds pending action if it not exists. | ||
* Save callbacks. | ||
* | ||
* @private | ||
* @type {Array.<Function>} | ||
*/ | ||
_incrementCounter() { | ||
const t = this.editor.t; | ||
|
||
this._saveActionCounter++; | ||
get _saveCallbacks() { | ||
const saveCallbacks = []; | ||
|
||
if ( !this._action ) { | ||
this._action = this._pendingActions.add( t( 'Saving changes' ) ); | ||
if ( this.adapter && this.adapter.save ) { | ||
saveCallbacks.push( this.adapter.save ); | ||
} | ||
} | ||
|
||
/** | ||
* Decrements counter and removes pending action if counter is empty, | ||
* which means, that no new save action occurred. | ||
* | ||
* @private | ||
*/ | ||
_decrementCounter() { | ||
this._saveActionCounter--; | ||
|
||
if ( this._saveActionCounter === 0 ) { | ||
this._pendingActions.remove( this._action ); | ||
this._action = null; | ||
if ( this._config.save ) { | ||
saveCallbacks.push( this._config.save ); | ||
} | ||
|
||
return saveCallbacks; | ||
} | ||
} | ||
|
||
mix( Autosave, ObservableMixin ); | ||
|
||
/** | ||
* An interface that requires the `save()` method. | ||
* | ||
|
@@ -324,3 +335,22 @@ export default class Autosave extends Plugin { | |
* @param {module:core/editor/editor~Editor} editor The editor instance. | ||
* @returns {Promise.<*>} | ||
*/ | ||
|
||
/** | ||
* The minimum amount of time that need to pass after last action to call the provided callback. | ||
* | ||
* ClassicEditor | ||
* .create( editorElement, { | ||
* autosave: { | ||
* save( editor ) { | ||
* return saveData( editor.getData() ); | ||
* }, | ||
* waitingTime: 1000 | ||
* } | ||
* } ); | ||
* .then( ... ) | ||
* .catch( ... ); | ||
* | ||
* @property module:autosave/autosave~AutosaveConfig#waitingTime | ||
* @type {Number} | ||
*/ |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://github.com/ckeditor/ckeditor5-autosave/issues/10#issuecomment-408118358
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, I am not sure if 2000 is not too much. Maybe 1000?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, I overlooked it.
The last call will be on the
Editor#destroy
orWindow#beforeunload
, so IMO we can use a higher value here.