Skip to content

Commit 833d56c

Browse files
committed
add array of nested objects with guaranteed order
1 parent 4436cc7 commit 833d56c

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

index.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -732,6 +732,7 @@ export abstract class NamespaceBase extends ReflectionObject {
732732

733733
/** Nested objects of this namespace as an array for iteration. */
734734
public readonly nestedArray: ReflectionObject[];
735+
public readonly orderedNestedMessages: ReflectionObject[];
735736

736737
/**
737738
* Converts this namespace to a namespace descriptor.

src/namespace.js

+7
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,12 @@ function Namespace(name, options) {
108108
* @private
109109
*/
110110
this._nestedArray = null;
111+
112+
/**
113+
* Array of nested messages in the order they were parsed from the schema.
114+
* @type {ReflectionObject[]}
115+
*/
116+
this.orderedNestedMessages = null;
111117
}
112118

113119
function clearCache(namespace) {
@@ -239,6 +245,7 @@ Namespace.prototype.add = function add(object) {
239245
throw Error("duplicate name '" + object.name + "' in " + this);
240246
}
241247
}
248+
242249
this.nested[object.name] = object;
243250
object.onAdd(this);
244251
return clearCache(this);

src/root.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -306,12 +306,18 @@ Root.prototype._handleAdd = function _handleAdd(object) {
306306

307307
} else if (!(object instanceof OneOf)) /* everything else is a namespace */ {
308308

309-
if (object instanceof Type) // Try to handle any deferred extensions
309+
if (object instanceof Type) { // Try to handle any deferred extensions
310+
if (!object.parent.orderedNestedMessages) {
311+
object.parent.orderedNestedMessages = [];
312+
}
313+
object.parent.orderedNestedMessages.push(object);
314+
310315
for (var i = 0; i < this.deferred.length;)
311316
if (tryHandleExtension(this, this.deferred[i]))
312317
this.deferred.splice(i, 1);
313318
else
314319
++i;
320+
}
315321
for (var j = 0; j < /* initializes */ object.nestedArray.length; ++j) // recurse into the namespace
316322
this._handleAdd(object._nestedArray[j]);
317323
if (exposeRe.test(object.name))

0 commit comments

Comments
 (0)