Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add tracked & cache descriptor detection for ember-inspector #410

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/@glimmer/tracking/src/cached.ts
@@ -1,5 +1,6 @@
import { DEBUG } from '@glimmer/env';
import { createCache, getValue } from '@glimmer/validator';
import { CACHED } from './debug';

/**
* @decorator
Expand Down Expand Up @@ -87,6 +88,7 @@ export const cached: PropertyDecorator = (...args: any[]) => {

return getValue(caches.get(this));
};
(descriptor.get as any)[CACHED] = true;
};

function throwCachedExtraneousParens(): never {
Expand Down
15 changes: 15 additions & 0 deletions packages/@glimmer/tracking/src/debug.ts
@@ -0,0 +1,15 @@
export const TRACKED = Symbol('TRACKED')
export const CACHED = Symbol('CACHED')

export function isTracked(obj: Object, key: string) {
const proto = Object.getPrototypeOf(obj);
const desc = Object.getOwnPropertyDescriptor(obj, key) || proto && Object.getOwnPropertyDescriptor(proto, key)
return desc.get && (desc.get as any)[TRACKED] || false;
}


export function isCached(obj: Object, key: string) {
const proto = Object.getPrototypeOf(obj);
const desc = Object.getOwnPropertyDescriptor(obj, key) || proto && Object.getOwnPropertyDescriptor(proto, key)
return desc.get && (desc.get as any)[CACHED] || false;
}
24 changes: 14 additions & 10 deletions packages/@glimmer/tracking/src/tracked.ts
@@ -1,5 +1,6 @@
import { DEBUG } from '@glimmer/env';
import { trackedData } from '@glimmer/validator';
import { TRACKED } from './debug';

/**
* @decorator
Expand Down Expand Up @@ -129,18 +130,21 @@ function descriptorForField<T extends object, K extends keyof T>(

const { getter, setter } = trackedData<T, K>(key, desc && desc.initializer);

function get(this: T): any {
return getter(this)
}

function set(this: T, newValue: any): void {
setter(this, newValue);
}

(get as any)[TRACKED] = true;

return {
enumerable: true,
configurable: true,

// eslint-disable-next-line @typescript-eslint/no-explicit-any
get(this: T): any {
return getter(this);
},

// eslint-disable-next-line @typescript-eslint/no-explicit-any
set(this: T, newValue: any): void {
setter(this, newValue);
},
get,
set,
};
}

9 changes: 9 additions & 0 deletions packages/@glimmer/tracking/test/cached-decorator-test.ts
@@ -1,4 +1,6 @@
/* eslint-disable @typescript-eslint/explicit-function-return-type */
import { isCached, isTracked } from '../src/debug';

const { test } = QUnit;

import { DEBUG } from '@glimmer/env';
Expand All @@ -25,6 +27,13 @@ test('it works', function (assert) {
}

const person = new Person();


test('cached properties can be detected for inspector', (assert) => {
assert.strictEqual(isCached(person, 'fullName'), true);
assert.strictEqual(isCached(person, 'lastName'), false);
});

assert.verifySteps([], 'getter is not called after class initialization');

assert.strictEqual(person.fullName, 'Jen Weber');
Expand Down
8 changes: 8 additions & 0 deletions packages/@glimmer/tracking/test/tracked-decorator-test.ts
@@ -1,4 +1,6 @@
/* tslint:disable:no-unused-expression */
import { isTracked } from '../src/debug';

const { test } = QUnit;

import { DEBUG } from '@glimmer/env';
Expand All @@ -23,6 +25,12 @@ import { assertValidAfterUnrelatedBump } from './helpers/tags';
assert.strictEqual(obj.firstName, 'Edsger');
});

test('tracked properties can be detected for inspector', (assert) => {
const obj = new F.Toran();
assert.strictEqual(isTracked(obj, 'firstName'), true);
assert.strictEqual(isTracked(obj, 'lastName'), false);
});

test('can request a tag for a property', (assert) => {
const obj = new F.Tom();
assert.strictEqual(obj.firstName, 'Tom');
Expand Down