From e403516e8f9a711b83af9a692936a38001915da8 Mon Sep 17 00:00:00 2001 From: patrick Date: Fri, 3 Nov 2023 11:30:54 +0100 Subject: [PATCH] add tag info for ember-inspector --- packages/@glimmer/validator/index.ts | 2 +- packages/@glimmer/validator/lib/meta.ts | 16 ++++++++++++++++ packages/@glimmer/validator/test/meta-test.ts | 11 ++++++++++- 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/packages/@glimmer/validator/index.ts b/packages/@glimmer/validator/index.ts index 40c764d9cc..e647efe314 100644 --- a/packages/@glimmer/validator/index.ts +++ b/packages/@glimmer/validator/index.ts @@ -13,7 +13,7 @@ if (globalObj[GLIMMER_VALIDATOR_REGISTRATION] === true) { globalObj[GLIMMER_VALIDATOR_REGISTRATION] = true; export { debug } from './lib/debug'; -export { dirtyTagFor, tagFor, type TagMeta, tagMetaFor } from './lib/meta'; +export { dirtyTagFor, infoForTag, tagFor, type TagMeta, tagMetaFor } from './lib/meta'; export { trackedData } from './lib/tracked-data'; export { beginTrackFrame, diff --git a/packages/@glimmer/validator/lib/meta.ts b/packages/@glimmer/validator/lib/meta.ts index 68ff0d94e8..c96a8e3f63 100644 --- a/packages/@glimmer/validator/lib/meta.ts +++ b/packages/@glimmer/validator/lib/meta.ts @@ -15,6 +15,13 @@ function isObjectLike(u: T): u is Indexable & T { export type TagMeta = Map; const TRACKED_TAGS = new WeakMap(); +const TAG_INFO = new WeakMap< + UpdatableTag|ConstantTag, + { + object: Object; + propertyKey: string | number | symbol; + } +>(); export function dirtyTagFor( obj: T, @@ -67,5 +74,14 @@ export function tagFor( tags.set(key, tag); } + TAG_INFO.set(tag, { + propertyKey: key, + object: obj, + }); + return tag; } + +export function infoForTag(tag: UpdatableTag|ConstantTag) { + return TAG_INFO.get(tag); +} diff --git a/packages/@glimmer/validator/test/meta-test.ts b/packages/@glimmer/validator/test/meta-test.ts index cf6a739c74..f826fd60ec 100644 --- a/packages/@glimmer/validator/test/meta-test.ts +++ b/packages/@glimmer/validator/test/meta-test.ts @@ -1,4 +1,4 @@ -import { dirtyTagFor, tagFor, validateTag, valueForTag } from '@glimmer/validator'; +import { dirtyTagFor, infoForTag, tagFor, validateTag, valueForTag } from '@glimmer/validator'; import { module, test } from './-utils'; @@ -18,4 +18,13 @@ module('@glimmer/validator: meta', () => { assert.notOk(validateTag(tag, snapshot)); }); + + test('it can provide the object and property for the tag given object', (assert) => { + let obj = {}; + let tag = tagFor(obj, 'foo'); + + let info = infoForTag(tag)!; + assert.strictEqual(info.object, obj); + assert.strictEqual(info.propertyKey, 'foo'); + }); });