Skip to content

Commit

Permalink
add tag info for ember-inspector
Browse files Browse the repository at this point in the history
  • Loading branch information
patricklx committed Feb 12, 2024
1 parent 1cb493b commit 3318240
Show file tree
Hide file tree
Showing 7 changed files with 216 additions and 4 deletions.
13 changes: 13 additions & 0 deletions packages/@glimmer-workspace/integration-tests/lib/setup-harness.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,19 @@ export async function setupQunit() {
qunit.moduleDone(pause);
}

// @ts-expect-error missing in types, does exist: https://api.qunitjs.com/callbacks/QUnit.on/#the-testend-event
QUnit.on('testEnd', (testEnd) => {
if (testEnd.status === 'failed') {
testEnd.errors.forEach((assertion: any) => {
console.error(assertion.stack);
// message: speedometer
// actual: 75
// expected: 88
// stack: at dmc.test.js:12
});
}
});

qunit.done(({ failed }) => {
if (failed > 0) {
console.log('[HARNESS] fail');
Expand Down
3 changes: 2 additions & 1 deletion packages/@glimmer/validator/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ 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 * from './lib/tracked-utils';
export {
beginTrackFrame,
beginUntrackFrame,
Expand Down
12 changes: 11 additions & 1 deletion packages/@glimmer/validator/lib/meta.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { ConstantTag, UpdatableTag } from '@glimmer/interfaces';
import type { ConstantTag, Tag, UpdatableTag } from '@glimmer/interfaces';

import type { Indexable } from './utils';

Expand All @@ -15,6 +15,7 @@ function isObjectLike<T>(u: T): u is Indexable & T {
export type TagMeta = Map<PropertyKey, UpdatableTag>;

const TRACKED_TAGS = new WeakMap<object, TagMeta>();
const TAG_META = Symbol('TAG_META');

export function dirtyTagFor<T extends object>(
obj: T,
Expand Down Expand Up @@ -65,7 +66,16 @@ export function tagFor<T extends object>(
if (tag === undefined) {
tag = createUpdatableTag();
tags.set(key, tag);

(tag as any)[TAG_META] = {
propertyKey: key,
object: obj,
};
}

return tag;
}

export function infoForTag(tag: Tag) {
return (tag as any)[TAG_META];
}
80 changes: 80 additions & 0 deletions packages/@glimmer/validator/lib/tracked-utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import type { Tag } from '@glimmer/interfaces';

import type { MonomorphicTagImpl } from './validators';

import { infoForTag, tagFor } from './meta';
import { track } from './tracking';
import { validateTag, valueForTag } from './validators';

type Info = {
tag: MonomorphicTagImpl;
prevValue: number;
dependencies: {
object: object;
propertyKey: string;
changed: boolean;
}[];
};

export function getTrackedDependencies(obj: Record<string, any>, property: string, info?: Info) {
info = info || ({} as Info);
const tag = info?.tag || track(() => obj[property]);
const dependencies = [];
// do not include tracked properties from dependencies

const subtags = (Array.isArray(tag.subtag) ? [tag, ...tag.subtag] : [tag, tag.subtag]).filter(
(t) => !!t
) as Tag[];
for (const subtag of subtags) {
if (subtag === tag) continue;
dependencies.push({ ...infoForTag(subtag), tag: subtag });
if (subtag.subtag && !Array.isArray(subtag.subtag)) {
dependencies.push({ ...infoForTag(subtag.subtag) });
}
}

let maxRevision = valueForTag(tag);

const hasChange = (info.prevValue && maxRevision !== info.prevValue) || false;
let latestValue = info.prevValue || 0;

info.dependencies = dependencies.map((t) => {
if (t.tag.lastValue > latestValue) {
latestValue = t.tag.lastValue;
}
const changed = hasChange && t.tag.lastValue > info!.prevValue;
return { object: t.object, propertyKey: t.propertyKey, changed };
});

info.prevValue = maxRevision;

return info;
}

type TrackedInfo = {
changed: string[];
propertyInfo: Record<string, any>;
};

export function getChangedProperties(obj: object, trackedInfo?: TrackedInfo) {
trackedInfo = trackedInfo || ({} as TrackedInfo);
trackedInfo['changed'] = [];
trackedInfo.propertyInfo = trackedInfo.propertyInfo || {};
for (const name in obj) {
const tag = tagFor(obj, name);
const revision = valueForTag(tag);
let tagInfo = trackedInfo.propertyInfo?.[name] || {
tag: tag,
revision,
};
if (!tagInfo.tag) return;
trackedInfo.propertyInfo[name] = tagInfo;

const changed = !validateTag(tagInfo.tag, tagInfo.revision);
tagInfo.revision = revision;
if (changed) {
trackedInfo['changed'].push(name);
}
}
return trackedInfo;
}
2 changes: 1 addition & 1 deletion packages/@glimmer/validator/lib/validators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ function allowsCycles(tag: Tag): boolean {
}
}

class MonomorphicTagImpl<T extends MonomorphicTagId = MonomorphicTagId> {
export class MonomorphicTagImpl<T extends MonomorphicTagId = MonomorphicTagId> {
static combine(this: void, tags: Tag[]): Tag {
switch (tags.length) {
case 0:
Expand Down
11 changes: 10 additions & 1 deletion packages/@glimmer/validator/test/meta-test.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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');
});
});
99 changes: 99 additions & 0 deletions packages/@glimmer/validator/test/tracked-utils-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import { getChangedProperties, getTrackedDependencies, trackedData } from '@glimmer/validator';

import { module, test } from './-utils';

module('@glimmer/validator: tracked-utils', () => {
class TestObject {
declare item1: string;
declare item2: string;
item3 = '';
constructor() {}

get getterWithTracked() {
return this.item1 + ' world' + this.item2;
}
}

{
const { getter, setter } = trackedData<TestObject, 'item1'>('item1', () => '');
Object.defineProperty(TestObject.prototype, 'item1', {
enumerable: true,
get(this) {
return getter(this);
},
set(this, v) {
return setter(this, v);
},
});
}
{
const { getter, setter } = trackedData<TestObject, 'item2'>('item2', () => '');
Object.defineProperty(TestObject.prototype, 'item2', {
enumerable: true,
get(this) {
return getter(this);
},
set(this, v) {
return setter(this, v);
},
});
}

test('it can detect changed properties', (assert) => {
const obj = new TestObject();
let trackedInfo = getChangedProperties(obj);
assert.deepEqual(trackedInfo?.changed, []);

obj.item1 = 'hello';

assert.deepEqual(getChangedProperties(obj, trackedInfo)?.changed, ['item1']);
assert.deepEqual(getChangedProperties(obj, trackedInfo)?.changed, []);

obj.item1 = 'hi';
obj.item2 = 'hi';
assert.deepEqual(getChangedProperties(obj, trackedInfo)?.changed, ['item1', 'item2']);
});

test('it can detect tracked dependencies', (assert) => {
const obj = new TestObject();
let info = getTrackedDependencies(obj, 'getterWithTracked');
assert.deepEqual(info.dependencies, [
{
changed: false,
object: obj,
propertyKey: 'item1',
},
{
changed: false,
object: obj,
propertyKey: 'item2',
},
]);

obj.item1 = 'hi';
assert.deepEqual(getTrackedDependencies(obj, 'getterWithTracked', info).dependencies, [
{
changed: true,
object: obj,
propertyKey: 'item1',
},
{
changed: false,
object: obj,
propertyKey: 'item2',
},
]);
assert.deepEqual(getTrackedDependencies(obj, 'getterWithTracked', info).dependencies, [
{
changed: false,
object: obj,
propertyKey: 'item1',
},
{
changed: false,
object: obj,
propertyKey: 'item2',
},
]);
});
});

0 comments on commit 3318240

Please sign in to comment.