Remove separate private deferDoc property in ve.dm.MWReferenceModel

This is a direct follow-up for what I started in I3c9b9bb. Again all
this does is moving existing code around. The separate this.deferDoc
property is never used anywhere:
https://codesearch.wmcloud.org/search/?q=deferDoc&files=%5C.js%24
Instead I store the deferred function directly in the target property
this.doc and resolve it when needed.

Note that the concept of a parentDoc still exists. I tried to make
this as obvious as possible via comments and by arranging the code
accordingly:
1. A lot of code calls setDocument which directly sets this.doc. The
   parentDoc is never used in this case.
2. The parentDoc is only needed to auto-generate the (empty) document
   for the reference when a new reference is created.

That's also why the existing code always calls the constructor with
the parentDoc, even if this ends being unused in many cases: It's a
simple fallback.

Bug: T363096
Change-Id: I7874f187b2b397ed511b695ca9ff95838fcff302
This commit is contained in:
thiemowmde 2024-05-13 17:52:25 +02:00
parent 81f774c789
commit 6c5c2dc6cb

View file

@ -12,7 +12,10 @@
* *
* @constructor * @constructor
* @mixes OO.EventEmitter * @mixes OO.EventEmitter
* @param {ve.dm.Document} [parentDoc] Document that contains or will contain the reference * @param {ve.dm.Document} [parentDoc] The parent Document we can use to auto-generate a blank
* Document for the reference in case {@see setDocument} was never called
* @property {ve.dm.Document|Function|undefined} doc Might be deferred via a function, to be
* lazy-evaluated when {@see getDocument} is called
*/ */
ve.dm.MWReferenceModel = function VeDmMWReferenceModel( parentDoc ) { ve.dm.MWReferenceModel = function VeDmMWReferenceModel( parentDoc ) {
// Mixin constructors // Mixin constructors
@ -24,13 +27,14 @@ ve.dm.MWReferenceModel = function VeDmMWReferenceModel( parentDoc ) {
this.listGroup = ''; this.listGroup = '';
this.listIndex = null; this.listIndex = null;
this.group = ''; this.group = '';
this.doc = null; if ( parentDoc ) {
this.deferDoc = () => parentDoc.cloneWithData( [ this.doc = () => parentDoc.cloneWithData( [
{ type: 'paragraph', internal: { generated: 'wrapper' } }, { type: 'paragraph', internal: { generated: 'wrapper' } },
{ type: '/paragraph' }, { type: '/paragraph' },
{ type: 'internalList' }, { type: 'internalList' },
{ type: '/internalList' } { type: '/internalList' }
] ); ] );
}
}; };
/* Inheritance */ /* Inheritance */
@ -56,7 +60,7 @@ ve.dm.MWReferenceModel.static.newFromReferenceNode = function ( node ) {
ref.listGroup = attributes.listGroup; ref.listGroup = attributes.listGroup;
ref.listIndex = attributes.listIndex; ref.listIndex = attributes.listIndex;
ref.group = attributes.refGroup; ref.group = attributes.refGroup;
ref.deferDoc = function () { ref.doc = function () {
// cloneFromRange is very expensive, so lazy evaluate it // cloneFromRange is very expensive, so lazy evaluate it
return doc.cloneFromRange( internalList.getItemNode( attributes.listIndex ).getRange() ); return doc.cloneFromRange( internalList.getItemNode( attributes.listIndex ).getRange() );
}; };
@ -228,12 +232,11 @@ ve.dm.MWReferenceModel.prototype.getGroup = function () {
* *
* Auto-generates a blank document if no document exists. * Auto-generates a blank document if no document exists.
* *
* @return {ve.dm.Document} Reference document * @return {ve.dm.Document} The (small) document with the content of the reference
*/ */
ve.dm.MWReferenceModel.prototype.getDocument = function () { ve.dm.MWReferenceModel.prototype.getDocument = function () {
if ( !this.doc ) { if ( typeof this.doc === 'function' ) {
this.doc = this.deferDoc(); this.doc = this.doc();
delete this.deferDoc;
} }
return this.doc; return this.doc;
}; };
@ -250,7 +253,7 @@ ve.dm.MWReferenceModel.prototype.setGroup = function ( group ) {
/** /**
* Set the reference document. * Set the reference document.
* *
* @param {ve.dm.Document} doc Reference document * @param {ve.dm.Document} doc The (small) document with the content of the reference
*/ */
ve.dm.MWReferenceModel.prototype.setDocument = function ( doc ) { ve.dm.MWReferenceModel.prototype.setDocument = function ( doc ) {
this.doc = doc; this.doc = doc;