Skip to content

Commit 74b5829

Browse files
committed
Move getUniqueListKey method into ve.dm.InternalListNodeGroup
I find this method especially interesting because it is the only place where two (!) of the properties in the InternalListNodeGroup class are ever used. This is clearly private stuff that should be hidden to not confuse users of this data structure. In this patch I'm intentionally moving the code without making any other change. Bug: T397395 Change-Id: Ifd0a2d38b22cc232bb8d7cdc8454593093083e16
1 parent aceca6b commit 74b5829

File tree

2 files changed

+32
-28
lines changed

2 files changed

+32
-28
lines changed

src/dm/ve.dm.InternalList.js

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -148,32 +148,10 @@ ve.dm.InternalList.prototype.getNodeGroup = function ( groupName ) {
148148
};
149149

150150
/**
151-
* Get a unique list key for a given group.
152-
*
153-
* The returned list key is added to the list of unique list keys used in this group so that it
154-
* won't be allocated again. It will also be associated to oldListKey so that if the same oldListKey
155-
* is passed in again later, the previously allocated name will be returned.
156-
*
157-
* @param {string} groupName Name of the group
158-
* @param {string} oldListKey Current list key to associate the generated list key with
159-
* @param {string} prefix Prefix to distinguish generated keys from non-generated ones
160-
* @return {string} Generated unique list key, or existing unique key associated with oldListKey
151+
* @deprecated please use `.getNodeGroup( … ).getUniqueListKey( … )` instead
161152
*/
162153
ve.dm.InternalList.prototype.getUniqueListKey = function ( groupName, oldListKey, prefix ) {
163-
const group = this.getNodeGroup( groupName );
164-
165-
if ( group.uniqueListKeys[ oldListKey ] !== undefined ) {
166-
return group.uniqueListKeys[ oldListKey ];
167-
}
168-
169-
let num = 0;
170-
while ( group.keyedNodes[ prefix + num ] || group.uniqueListKeysInUse[ prefix + num ] ) {
171-
num++;
172-
}
173-
174-
group.uniqueListKeys[ oldListKey ] = prefix + num;
175-
group.uniqueListKeysInUse[ prefix + num ] = true;
176-
return prefix + num;
154+
return this.getNodeGroup( groupName ).getUniqueListKey( oldListKey, prefix );
177155
};
178156

179157
/**

src/dm/ve.dm.InternalListNodeGroup.js

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,43 @@ ve.dm.InternalListNodeGroup = function VeDmInternalListNodeGroup() {
4040
this.indexOrder = [];
4141

4242
/**
43-
* @internal private to {@link ve.dm.InternalList.getUniqueListKey}, do not use!
43+
* @private
4444
* @property {Object.<string,string>} uniqueListKeys Internal cache for previously generated
45-
* listKeys to make sure the same {@link ve.dm.InternalList.getUniqueListKey} call always
46-
* returns the same value
45+
* listKeys to make sure the same {@link getUniqueListKey} call always returns the same value
4746
*/
4847
this.uniqueListKeys = {};
4948

5049
/**
51-
* @internal private to {@link ve.dm.InternalList.getUniqueListKey}, do not use!
50+
* @private
5251
* @property {Object.<string,boolean>} uniqueListKeysInUse Internal cache to mark listKeys as
5352
* used. The values are meaningless.
5453
*/
5554
this.uniqueListKeysInUse = {};
5655
};
56+
57+
/**
58+
* Get a unique list key for this group.
59+
*
60+
* The returned list key is added to the list of unique list keys used in this group so that it
61+
* won't be allocated again. It will also be associated to oldListKey so that if the same oldListKey
62+
* is passed in again later, the previously allocated name will be returned.
63+
*
64+
* @param {string} oldListKey Current list key to associate the generated list key with
65+
* @param {string} prefix Prefix to distinguish generated keys from non-generated ones
66+
* @return {string} Generated unique list key, or existing unique key associated with oldListKey
67+
*/
68+
ve.dm.InternalListNodeGroup.prototype.getUniqueListKey = function ( oldListKey, prefix ) {
69+
if ( oldListKey in this.uniqueListKeys ) {
70+
return this.uniqueListKeys[ oldListKey ];
71+
}
72+
73+
let num = 0;
74+
while ( this.keyedNodes[ prefix + num ] || this.uniqueListKeysInUse[ prefix + num ] ) {
75+
num++;
76+
}
77+
78+
this.uniqueListKeys[ oldListKey ] = prefix + num;
79+
// FIXME: We can as well store the last number instead of this list, reducing the footprint
80+
this.uniqueListKeysInUse[ prefix + num ] = true;
81+
return prefix + num;
82+
};

0 commit comments

Comments
 (0)