From 833d56c34ce01c0f3d7cf671fc936fd921c26540 Mon Sep 17 00:00:00 2001 From: David Grisham Date: Tue, 26 Dec 2023 09:21:36 -0700 Subject: [PATCH] add array of nested objects with guaranteed order --- index.d.ts | 1 + src/namespace.js | 7 +++++++ src/root.js | 8 +++++++- 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/index.d.ts b/index.d.ts index 750ad2f73..a1b932e5f 100644 --- a/index.d.ts +++ b/index.d.ts @@ -732,6 +732,7 @@ export abstract class NamespaceBase extends ReflectionObject { /** Nested objects of this namespace as an array for iteration. */ public readonly nestedArray: ReflectionObject[]; + public readonly orderedNestedMessages: ReflectionObject[]; /** * Converts this namespace to a namespace descriptor. diff --git a/src/namespace.js b/src/namespace.js index 731afc75f..5032afc8f 100644 --- a/src/namespace.js +++ b/src/namespace.js @@ -108,6 +108,12 @@ function Namespace(name, options) { * @private */ this._nestedArray = null; + + /** + * Array of nested messages in the order they were parsed from the schema. + * @type {ReflectionObject[]} + */ + this.orderedNestedMessages = null; } function clearCache(namespace) { @@ -239,6 +245,7 @@ Namespace.prototype.add = function add(object) { throw Error("duplicate name '" + object.name + "' in " + this); } } + this.nested[object.name] = object; object.onAdd(this); return clearCache(this); diff --git a/src/root.js b/src/root.js index 9441a7fc3..678b01510 100644 --- a/src/root.js +++ b/src/root.js @@ -306,12 +306,18 @@ Root.prototype._handleAdd = function _handleAdd(object) { } else if (!(object instanceof OneOf)) /* everything else is a namespace */ { - if (object instanceof Type) // Try to handle any deferred extensions + if (object instanceof Type) { // Try to handle any deferred extensions + if (!object.parent.orderedNestedMessages) { + object.parent.orderedNestedMessages = []; + } + object.parent.orderedNestedMessages.push(object); + for (var i = 0; i < this.deferred.length;) if (tryHandleExtension(this, this.deferred[i])) this.deferred.splice(i, 1); else ++i; + } for (var j = 0; j < /* initializes */ object.nestedArray.length; ++j) // recurse into the namespace this._handleAdd(object._nestedArray[j]); if (exposeRe.test(object.name))