Skip to content

A platform for community discussion. Free, open, simple.

DEV: replace static tracked property registry with dynamic introspection (#37908)

Sérgio Saquetimc88a9c285a40
diff --git a/frontend/discourse/app/lib/tracked-tools.js b/frontend/discourse/app/lib/tracked-tools.js
index cb5191b6c85..fc5285ba4b5 100644
--- a/frontend/discourse/app/lib/tracked-tools.js
+++ b/frontend/discourse/app/lib/tracked-tools.js
@@ -1,5 +1,5 @@
import { tracked } from "@glimmer/tracking";
-import { meta as metaFor } from "@ember/-internals/meta";
+import { meta as metaFor, peekMeta } from "@ember/-internals/meta";
import { TrackedDescriptor } from "@ember/-internals/metal";
import { next } from "@ember/runloop";
import { TrackedArray, TrackedSet } from "@ember-compat/tracked-built-ins";
@@ -187,7 +187,7 @@ export class DeferredTrackedSet {
* or null/undefined if those values were passed
* @throws {Error} If value is not an array, TrackedArray, null or undefined
*/
-function ensureTrackedArray(value) {
+function ensureTrackedArray(value, propertyName) {
if (typeof value === "undefined" || value === null) {
return value;
}
@@ -201,7 +201,7 @@ function ensureTrackedArray(value) {
}
throw new Error(
- `Expected an array, TrackedArray, null, or undefined, got ${typeof value}. Value received: ${value}`
+ `[${propertyName}]: Expected an array, TrackedArray, null, or undefined, got ${typeof value}. Value received: ${value}`
);
}
@@ -224,7 +224,7 @@ export function trackedArray(target, key, desc) {
const originalInitializer = desc.initializer;
desc.initializer = function () {
const initialValue = originalInitializer.apply(this);
- return ensureTrackedArray(initialValue);
+ return ensureTrackedArray(initialValue, key);
};
}
@@ -236,7 +236,7 @@ export function trackedArray(target, key, desc) {
});
function trackedArraySetter(value) {
- set.call(this, ensureTrackedArray(value));
+ set.call(this, ensureTrackedArray(value, key));
}
// When using EmberObject.create(...), Ember accesses the tracked properties directly
@@ -258,3 +258,77 @@ export function trackedArray(target, key, desc) {
isTracked: true,
};
}
+
+/**
+ * Enumerates all tracked property keys from an object instance.
+ *
+ * **Warning:** This function uses Ember internal APIs (`peekMeta`, `TrackedDescriptor`)
+ * which are not part of the public API and may change without notice in future Ember versions.
+ *
+ * @param {Object} obj - The object instance to enumerate tracked keys from
+ * @returns {Array<string>} An array of tracked property keys.
+ * Returns an empty array if the object has no prototype or no tracked properties.
+ *
+ * @example
+ * class MyClass {
+ * @tracked name = "Alice";
+ * @tracked age = 30;
+ * regularProp = "not tracked";
+ * }
+ *
+ * const instance = new MyClass();
+ * const trackedKeys = enumerateTrackedKeys(instance);
+ * // Returns: ["name", "age"]
+ */
+export function enumerateTrackedKeys(obj) {
+ const prototype = obj?.constructor?.prototype;
+ if (!prototype) {
+ return [];
+ }
+
+ const result = new Set();
+
+ // Walk the prototype chain to collect tracked keys from parent classes too
+ let meta = peekMeta(prototype);
+ while (meta) {
+ const descriptors = meta._descriptors;
+
+ if (descriptors) {
+ for (const [key, desc] of descriptors) {
+ if (desc instanceof TrackedDescriptor) {
+ result.add(key);
+ }
+ }
+ }
+
+ meta = meta.parent;
+ }
+
+ return [...result];
+}
+
+/**
+ * Enumerates all tracked property entries from an object instance.
+ *
+ * **Warning:** This function uses Ember internal APIs (`peekMeta`, `TrackedDescriptor`)
+ * which are not part of the public API and may change without notice in future Ember versions.
+ *
+ * @param {Object} obj - The object instance to enumerate tracked entries from
+ * @returns {Array<[string, any]>} An array of [key, value] pairs for all tracked properties.
+ * Returns an empty array if the object has no prototype or no tracked properties.
+ *
+ * @example
+ * class MyClass {
+ * @tracked name = "Alice";
+ * @tracked age = 30;
+ * regularProp = "not tracked";
+ * }
+ *
+ * const instance = new MyClass();
+ * const trackedEntries = enumerateTrackedEntries(instance);
+ * // Returns: [["name", "Alice"], ["age", 30]]
+ */
+export function enumerateTrackedEntries(obj) {
+ const keys = enumerateTrackedKeys(obj);
+ return keys.map((key) => [key, obj[key]]);
+}
diff --git a/frontend/discourse/app/models/post.js b/frontend/discourse/app/models/post.js
index c53542ee127..ae3863b3af4 100644
--- a/frontend/discourse/app/models/post.js
+++ b/frontend/discourse/app/models/post.js
@@ -11,7 +11,10 @@ import { propertyEqual } from "discourse/lib/computed";
import discourseComputed from "discourse/lib/decorators";
import { cook } from "discourse/lib/text";
import { fancyTitle } from "discourse/lib/topic-fancy-title";
-import { defineTrackedProperty } from "discourse/lib/tracked-tools";
+import {
+ defineTrackedProperty,
+ enumerateTrackedKeys,
+} from "discourse/lib/tracked-tools";
import { userPath } from "discourse/lib/url";
import { postUrl } from "discourse/lib/utilities";
import ActionSummary from "discourse/models/action-summary";
@@ -23,7 +26,6 @@ import User from "discourse/models/user";
import { i18n } from "discourse-i18n";
const pluginTrackedProperties = new Set();
-const trackedPropertiesForPostUpdate = new Set();
/**
* @internal
@@ -46,22 +48,6 @@ export function clearAddedTrackedPostProperties() {
pluginTrackedProperties.clear();
}
-/**
- * Decorator to mark a property as post property as tracked.
- *
- * It extends the standard Ember @tracked behavior to also keep track of the fields
- * that need to be copied when using `post.updateFromPost`.
- *
- * @param {Object} target - The target object.
- * @param {string} propertyKey - The key of the property to track.
- * @param {PropertyDescriptor} descriptor - The property descriptor.
- * @returns {PropertyDescriptor} The updated property descriptor.
- */
-function trackedPostProperty(target, propertyKey, descriptor) {
- trackedPropertiesForPostUpdate.add(propertyKey);
- return tracked(target, propertyKey, descriptor);
-}
-
export default class Post extends RestModel {
static munge(json) {
json.likeAction ??= null;
@@ -150,75 +136,69 @@ export default class Post extends RestModel {
@service currentUser;
@service site;
+ @tracked action_code;
+ @tracked action_code_path;
+ @tracked action_code_who;
+ @tracked actions_summary;
+ @tracked admin;
+ @tracked badges_granted;
+ @tracked bookmarked;
+ @tracked can_delete;
+ @tracked can_edit;
+ @tracked can_permanently_delete;
+ @tracked can_recover;
+ @tracked can_see_hidden_post;
+ @tracked can_view_edit_history;
+ @tracked cooked;
+ @tracked cooked_hidden;
+ @tracked created_at;
@tracked customShare = null;
-
- // Use @trackedPostProperty here instead of Glimmer's @tracked because we need to know which properties are tracked
- // in order to correctly update the post in the updateFromPost method. Currently this is not possible using only
- // the standard tracked method because these properties are added to the class prototype and are not enumarated by
- // object.keys().
- // See https://github.com/emberjs/ember.js/issues/18220
- @trackedPostProperty action_code;
- @trackedPostProperty action_code_path;
- @trackedPostProperty action_code_who;
- @trackedPostProperty actions_summary;
- @trackedPostProperty admin;
- @trackedPostProperty badges_granted;
- @trackedPostProperty bookmarked;
- @trackedPostProperty can_delete;
- @trackedPostProperty can_edit;
- @trackedPostProperty can_permanently_delete;
- @trackedPostProperty can_recover;
- @trackedPostProperty can_see_hidden_post;
- @trackedPostProperty can_view_edit_history;
- @trackedPostProperty cooked;
- @trackedPostProperty cooked_hidden;
- @trackedPostProperty created_at;
- @trackedPostProperty deleted_at;
- @trackedPostProperty deleted_by;
- @trackedPostProperty excerpt;
- @trackedPostProperty expandedExcerpt;
- @trackedPostProperty group_moderator;
- @trackedPostProperty hasGap;
- @trackedPostProperty hidden;
- @trackedPostProperty id;
- @trackedPostProperty is_auto_generated;
- @trackedPostProperty last_wiki_edit;
- @trackedPostProperty likeAction;
- @trackedPostProperty link_counts;
- @trackedPostProperty locked;
- @trackedPostProperty moderator;
- @trackedPostProperty name;
- @trackedPostProperty notice;
- @trackedPostProperty notice_created_by_user;
- @trackedPostProperty post_number;
- @trackedPostProperty post_type;
- @trackedPostProperty primary_group_name;
- @trackedPostProperty quoted;
- @trackedPostProperty read;
- @trackedPostProperty readers_count;
- @trackedPostProperty reply_count;
- @trackedPostProperty reply_to_user;
- @trackedPostProperty staff;
- @trackedPostProperty staged;
- @trackedPostProperty title_is_group;
- @trackedPostProperty topic;
- @trackedPostProperty topic_id;
- @trackedPostProperty trust_level;
- @trackedPostProperty updated_at;
- @trackedPostProperty user_deleted;
- @trackedPostProperty user_id;
- @trackedPostProperty user_suspended;
- @trackedPostProperty user_title;
- @trackedPostProperty username;
- @trackedPostProperty version;
- @trackedPostProperty via_email;
- @trackedPostProperty wiki;
- @trackedPostProperty yours;
- @trackedPostProperty user_custom_fields;
- @trackedPostProperty post_localizations;
- @trackedPostProperty is_localized;
- @trackedPostProperty language;
- @trackedPostProperty localization_outdated;
+ @tracked deleted_at;
+ @tracked deleted_by;
+ @tracked excerpt;
+ @tracked expandedExcerpt;
+ @tracked group_moderator;
+ @tracked hasGap;
+ @tracked hidden;
+ @tracked id;
+ @tracked is_auto_generated;
+ @tracked is_localized;
+ @tracked language;
+ @tracked last_wiki_edit;
+ @tracked likeAction;
+ @tracked link_counts;
+ @tracked localization_outdated;
+ @tracked locked;
+ @tracked moderator;
+ @tracked name;
+ @tracked notice;
+ @tracked notice_created_by_user;
+ @tracked post_localizations;
+ @tracked post_number;
+ @tracked post_type;
+ @tracked primary_group_name;
+ @tracked quoted;
+ @tracked read;
+ @tracked readers_count;
+ @tracked reply_count;
+ @tracked reply_to_user;
+ @tracked staff;
+ @tracked staged;
+ @tracked title_is_group;
+ @tracked topic;
+ @tracked topic_id;
+ @tracked trust_level;
+ @tracked updated_at;
+ @tracked user_custom_fields;
+ @tracked user_deleted;
+ @tracked user_id;
+ @tracked user_suspended;
+ @tracked user_title;
+ @tracked username;
+ @tracked version;
+ @tracked via_email;
+ @tracked wiki;
+ @tracked yours;
@alias("can_edit") canEdit; // for compatibility with existing code
@equal("trust_level", 0) new_user;
@@ -631,7 +611,7 @@ export default class Post extends RestModel {
updateFromPost(otherPost) {
[
...Object.keys(otherPost),
- ...trackedPropertiesForPostUpdate,
+ ...enumerateTrackedKeys(otherPost),
...pluginTrackedProperties,
].forEach((key) => {
let value = otherPost[key],
diff --git a/frontend/discourse/app/models/rest.js b/frontend/discourse/app/models/rest.js
index d3e99ccbdcc..035e75f4fef 100644
--- a/frontend/discourse/app/models/rest.js
+++ b/frontend/discourse/app/models/rest.js
@@ -5,6 +5,7 @@ import { equal } from "@ember/object/computed";
import { getOwner, setOwner } from "@ember/owner";
import { Promise } from "rsvp";
import { getOwnerWithFallback } from "discourse/lib/get-owner";
+import { enumerateTrackedEntries } from "discourse/lib/tracked-tools";
export default class RestModel extends EmberObject {
// Overwrite and JSON will be passed through here before `create` and `update`
@@ -16,7 +17,10 @@ export default class RestModel extends EmberObject {
args = args || {};
args.__munge = this.munge;
- const createArgs = this.munge(args, args.store);
+ const createArgs = this.munge(
+ { ...args, ...Object.fromEntries(enumerateTrackedEntries(args)) },
+ args.store
+ );
// Some Discourse code calls `model.create()` directly without going through the
// store. In that case the owner is not set, and injections will fail. This workaround ensures
diff --git a/frontend/discourse/tests/unit/lib/tracked-tools-test.gjs b/frontend/discourse/tests/unit/lib/tracked-tools-test.gjs
index 3db4a9881c0..9ffacf7492a 100644
--- a/frontend/discourse/tests/unit/lib/tracked-tools-test.gjs
+++ b/frontend/discourse/tests/unit/lib/tracked-tools-test.gjs
@@ -1,4 +1,4 @@
-import { cached } from "@glimmer/tracking";
+import { cached, tracked } from "@glimmer/tracking";
import { run } from "@ember/runloop";
import { settled } from "@ember/test-helpers";
import { TrackedArray } from "@ember-compat/tracked-built-ins";
@@ -6,6 +6,8 @@ import { module, test } from "qunit";
import {
dedupeTracked,
DeferredTrackedSet,
+ enumerateTrackedEntries,
+ enumerateTrackedKeys,
trackedArray,
} from "discourse/lib/tracked-tools";
@@ -314,4 +316,451 @@ module("Unit | tracked-tools", function () {
);
});
});
+
+ module("enumerateTrackedKeys", function () {
+ test("returns tracked property keys from an object", function (assert) {
+ class Person {
+ @tracked name = "Alice";
+ @tracked age = 30;
+ regularProp = "not tracked";
+ }
+
+ const instance = new Person();
+ const result = enumerateTrackedKeys(instance);
+
+ assert.deepEqual(
+ result,
+ ["name", "age"],
+ "returns only tracked property keys"
+ );
+ assert.false(
+ result.includes("regularProp"),
+ "non-tracked property is not included"
+ );
+ });
+
+ test("returns empty array for null or undefined", function (assert) {
+ const resultNull = enumerateTrackedKeys(null);
+ const resultUndefined = enumerateTrackedKeys(undefined);
+
+ assert.deepEqual(resultNull, [], "returns empty array for null");
+ assert.deepEqual(
+ resultUndefined,
+ [],
+ "returns empty array for undefined"
+ );
+ });
+
+ test("returns empty array for objects without tracked properties", function (assert) {
+ class SimpleClass {
+ prop1 = "value1";
+ prop2 = "value2";
+ }
+
+ const instance = new SimpleClass();
+ const result = enumerateTrackedKeys(instance);
+
+ assert.deepEqual(
+ result,
+ [],
+ "returns empty array for non-tracked properties"
+ );
+ });
+
+ test("handles objects with only tracked properties", function (assert) {
+ class AllTracked {
+ @tracked first = "one";
+ @tracked second = "two";
+ @tracked third = "three";
+ }
+
+ const instance = new AllTracked();
+ const result = enumerateTrackedKeys(instance);
+
+ assert.deepEqual(
+ result.sort(),
+ ["first", "second", "third"].sort(),
+ "returns all tracked property keys"
+ );
+ assert.strictEqual(result.length, 3, "returns correct number of keys");
+ });
+
+ test("includes @dedupeTracked properties", function (assert) {
+ class TestClass {
+ @tracked normalTracked = "normal";
+ @dedupeTracked dedupedTracked = "deduped";
+ regularProp = "regular";
+ }
+
+ const instance = new TestClass();
+ const result = enumerateTrackedKeys(instance);
+
+ assert.true(
+ result.includes("normalTracked"),
+ "includes @tracked property"
+ );
+ assert.true(
+ result.includes("dedupedTracked"),
+ "includes @dedupeTracked property"
+ );
+ assert.false(result.includes("regularProp"), "excludes regular property");
+ });
+
+ test("includes @trackedArray properties", function (assert) {
+ class TestClass {
+ @tracked name = "test";
+ @trackedArray items = ["a", "b", "c"];
+ regularProp = "regular";
+ }
+
+ const instance = new TestClass();
+ const result = enumerateTrackedKeys(instance);
+
+ assert.true(result.includes("name"), "includes @tracked property");
+ assert.true(result.includes("items"), "includes @trackedArray property");
+ assert.false(result.includes("regularProp"), "excludes regular property");
+ });
+
+ test("handles inherited tracked properties", function (assert) {
+ class Parent {
+ @tracked parentProp = "parent";
+ }
+
+ class Child extends Parent {
+ @tracked childProp = "child";
+ }
+
+ const instance = new Child();
+ const result = enumerateTrackedKeys(instance);
+
+ assert.true(
+ result.includes("parentProp"),
+ "includes parent tracked property"
+ );
+ assert.true(
+ result.includes("childProp"),
+ "includes child tracked property"
+ );
+ assert.strictEqual(
+ result.length,
+ 2,
+ "includes both parent and child properties"
+ );
+ });
+
+ test("handles multi-level inheritance", function (assert) {
+ class GrandParent {
+ @tracked grandProp = "grand";
+ }
+
+ class Parent extends GrandParent {
+ @tracked parentProp = "parent";
+ }
+
+ class Child extends Parent {
+ @tracked childProp = "child";
+ }
+
+ const instance = new Child();
+ const result = enumerateTrackedKeys(instance);
+
+ assert.true(
+ result.includes("grandProp"),
+ "includes grandparent property"
+ );
+ assert.true(result.includes("parentProp"), "includes parent property");
+ assert.true(result.includes("childProp"), "includes child property");
+ assert.strictEqual(result.length, 3, "includes all inherited properties");
+ });
+
+ test("returns keys regardless of current property values", function (assert) {
+ class TestClass {
+ @tracked nullProp = null;
+ @tracked undefinedProp = undefined;
+ @tracked stringProp = "value";
+ }
+
+ const instance = new TestClass();
+ const result = enumerateTrackedKeys(instance);
+
+ assert.true(
+ result.includes("nullProp"),
+ "includes property with null value"
+ );
+ assert.true(
+ result.includes("undefinedProp"),
+ "includes property with undefined value"
+ );
+ assert.true(
+ result.includes("stringProp"),
+ "includes property with string value"
+ );
+ assert.strictEqual(
+ result.length,
+ 3,
+ "returns all keys regardless of values"
+ );
+ });
+
+ test("handles objects with various tracked value types", function (assert) {
+ class MixedTypes {
+ @tracked string = "text";
+ @tracked number = 42;
+ @tracked boolean = true;
+ @tracked object = { nested: "value" };
+ @tracked array = [1, 2, 3];
+ }
+
+ const instance = new MixedTypes();
+ const result = enumerateTrackedKeys(instance);
+
+ assert.deepEqual(
+ result.sort(),
+ ["array", "boolean", "number", "object", "string"].sort(),
+ "returns keys for all value types"
+ );
+ });
+ });
+
+ module("enumerateTrackedEntries", function () {
+ test("returns tracked property entries from an object", function (assert) {
+ class Person {
+ @tracked name = "Alice";
+ @tracked age = 30;
+ regularProp = "not tracked";
+ }
+
+ const instance = new Person();
+ const result = enumerateTrackedEntries(instance);
+
+ assert.deepEqual(
+ result,
+ [
+ ["name", "Alice"],
+ ["age", 30],
+ ],
+ "returns entries for tracked properties"
+ );
+ });
+
+ test("returns empty array for null or undefined", function (assert) {
+ const resultNull = enumerateTrackedEntries(null);
+ const resultUndefined = enumerateTrackedEntries(undefined);
+
+ assert.deepEqual(resultNull, [], "returns empty array for null");
+ assert.deepEqual(
+ resultUndefined,
+ [],
+ "returns empty array for undefined"
+ );
+ });
+
+ test("returns empty array for objects without tracked properties", function (assert) {
+ class SimpleClass {
+ prop1 = "value1";
+ prop2 = "value2";
+ }
+
+ const instance = new SimpleClass();
+ const result = enumerateTrackedEntries(instance);
+
+ assert.deepEqual(
+ result,
+ [],
+ "returns empty array for non-tracked properties"
+ );
+ });
+
+ test("handles objects with only tracked properties", function (assert) {
+ class AllTracked {
+ @tracked first = "one";
+ @tracked second = "two";
+ @tracked third = "three";
+ }
+
+ const instance = new AllTracked();
+ const result = enumerateTrackedEntries(instance);
+
+ assert.strictEqual(result.length, 3, "returns three entries");
+ const obj = Object.fromEntries(result);
+ assert.strictEqual(obj.first, "one");
+ assert.strictEqual(obj.second, "two");
+ assert.strictEqual(obj.third, "three");
+ });
+
+ test("includes @dedupeTracked properties", function (assert) {
+ class TestClass {
+ @tracked normalTracked = "normal";
+ @dedupeTracked dedupedTracked = "deduped";
+ regularProp = "regular";
+ }
+
+ const instance = new TestClass();
+ const result = enumerateTrackedEntries(instance);
+
+ const obj = Object.fromEntries(result);
+ assert.strictEqual(obj.normalTracked, "normal");
+ assert.strictEqual(obj.dedupedTracked, "deduped");
+ assert.strictEqual(obj.regularProp, undefined);
+ });
+
+ test("includes @trackedArray properties", function (assert) {
+ class TestClass {
+ @tracked name = "test";
+ @trackedArray items = ["a", "b", "c"];
+ regularProp = "regular";
+ }
+
+ const instance = new TestClass();
+ const result = enumerateTrackedEntries(instance);
+
+ const obj = Object.fromEntries(result);
+ assert.strictEqual(obj.name, "test");
+ assert.true(obj.items instanceof TrackedArray);
+ assert.deepEqual(Array.from(obj.items), ["a", "b", "c"]);
+ assert.strictEqual(obj.regularProp, undefined);
+ });
+
+ test("handles inherited tracked properties", function (assert) {
+ class Parent {
+ @tracked parentProp = "parent";
+ }
+
+ class Child extends Parent {
+ @tracked childProp = "child";
+ }
+
+ const instance = new Child();
+ const result = enumerateTrackedEntries(instance);
+
+ assert.strictEqual(
+ result.length,
+ 2,
+ "includes both parent and child properties"
+ );
+ const obj = Object.fromEntries(result);
+ assert.strictEqual(
+ obj.parentProp,
+ "parent",
+ "includes parent tracked property"
+ );
+ assert.strictEqual(
+ obj.childProp,
+ "child",
+ "includes child tracked property"
+ );
+ });
+
+ test("handles multi-level inheritance", function (assert) {
+ class GrandParent {
+ @tracked grandProp = "grand";
+ }
+
+ class Parent extends GrandParent {
+ @tracked parentProp = "parent";
+ }
+
+ class Child extends Parent {
+ @tracked childProp = "child";
+ }
+
+ const instance = new Child();
+ const result = enumerateTrackedEntries(instance);
+
+ assert.strictEqual(result.length, 3);
+ const obj = Object.fromEntries(result);
+ assert.strictEqual(obj.grandProp, "grand");
+ assert.strictEqual(obj.parentProp, "parent");
+ assert.strictEqual(obj.childProp, "child");
+ });
+
+ test("reflects current property values", function (assert) {
+ class Counter {
+ @tracked count = 0;
+ }
+
+ const instance = new Counter();
+ assert.strictEqual(
+ Object.fromEntries(enumerateTrackedEntries(instance)).count,
+ 0,
+ "initial value is 0"
+ );
+
+ instance.count = 5;
+ assert.strictEqual(
+ Object.fromEntries(enumerateTrackedEntries(instance)).count,
+ 5,
+ "updated value is reflected"
+ );
+
+ instance.count = 100;
+ assert.strictEqual(
+ Object.fromEntries(enumerateTrackedEntries(instance)).count,
+ 100,
+ "further updates are reflected"
+ );
+ });
+
+ test("handles tracked properties with null/undefined values", function (assert) {
+ class TestClass {
+ @tracked nullProp = null;
+ @tracked undefinedProp = undefined;
+ @tracked stringProp = "value";
+ }
+
+ const instance = new TestClass();
+ const result = enumerateTrackedEntries(instance);
+
+ const obj = Object.fromEntries(result);
+ assert.strictEqual(obj.nullProp, null, "null value is preserved");
+ assert.strictEqual(
+ obj.undefinedProp,
+ undefined,
+ "undefined value is preserved"
+ );
+ assert.strictEqual(obj.stringProp, "value", "string value is correct");
+ assert.true("nullProp" in obj, "null property key exists in result");
+ assert.true(
+ "undefinedProp" in obj,
+ "undefined property key exists in result"
+ );
+ });
+
+ test("handles objects with various tracked value types", function (assert) {
+ class MixedTypes {
+ @tracked string = "text";
+ @tracked number = 42;
+ @tracked boolean = true;
+ @tracked object = { nested: "value" };
+ @tracked array = [1, 2, 3];
+ }
+
+ const instance = new MixedTypes();
+ const result = enumerateTrackedEntries(instance);
+
+ const obj = Object.fromEntries(result);
+ assert.strictEqual(obj.string, "text");
+ assert.strictEqual(obj.number, 42);
+ assert.true(obj.boolean);
+ assert.deepEqual(obj.object, { nested: "value" });
+ assert.deepEqual(obj.array, [1, 2, 3]);
+ });
+
+ test("can be converted to object with Object.fromEntries", function (assert) {
+ class TestClass {
+ @tracked firstName = "Alice";
+ @tracked lastName = "Smith";
+ }
+
+ const instance = new TestClass();
+ const entries = enumerateTrackedEntries(instance);
+ const obj = Object.fromEntries(entries);
+
+ assert.deepEqual(
+ obj,
+ { firstName: "Alice", lastName: "Smith" },
+ "entries can be converted back to object"
+ );
+ });
+ });
});