Skip to content

Commit

Permalink
[BUGFIX lts] Ensures that tracked properties initialize property
Browse files Browse the repository at this point in the history
Fixes tracked property initialization for classic classes.
  • Loading branch information
Chris Garrett committed Sep 30, 2020
1 parent 15217a3 commit 5d5f966
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 21 deletions.
Expand Up @@ -14,6 +14,44 @@ import { Component } from '../../utils/helpers';
moduleFor(
'Component Tracked Properties',
class extends RenderingTestCase {
'@test simple test using classic component'() {
let personId = 0;
class Person {
@tracked first;
@tracked last;

constructor(first, last) {
this.id = personId++;
this.first = first;
this.last = last;
}
}

class PersonComponent extends Component {
@tracked first;
@tracked last;

get person() {
return new Person(this.first, this.last);
}
}

this.registerComponent('person-wrapper', {
ComponentClass: PersonComponent,
template: '{{@first}} {{@last}} | {{this.person.first}} {{this.person.last}}',
});

this.render('<PersonWrapper @first={{first}} @last={{last}} />', {
first: 'robert',
last: 'jackson',
});

this.assertText('robert jackson | robert jackson');

runTask(() => this.context.set('first', 'max'));
this.assertText('max jackson | max jackson');
}

'@test simple test using glimmerish component'() {
let personId = 0;
class Person {
Expand Down
9 changes: 1 addition & 8 deletions packages/@ember/-internals/metal/lib/decorator.ts
@@ -1,6 +1,5 @@
import { Meta, meta as metaFor, peekMeta } from '@ember/-internals/meta';
import { assert } from '@ember/debug';
import { _WeakSet as WeakSet } from '@glimmer/util';

export type DecoratorPropertyDescriptor = (PropertyDescriptor & { initializer?: any }) | undefined;

Expand Down Expand Up @@ -80,17 +79,11 @@ function DESCRIPTOR_SETTER_FUNCTION(
name: string,
descriptor: ComputedDescriptor
): (value: any) => void {
let func = function CPSETTER_FUNCTION(this: object, value: any): void {
return function CPSETTER_FUNCTION(this: object, value: any): void {
return descriptor.set(this, name, value);
};

CP_SETTER_FUNCS.add(func);

return func;
}

export const CP_SETTER_FUNCS = new WeakSet();

export function makeComputedDecorator(
desc: ComputedDescriptor,
DecoratorClass: { prototype: object }
Expand Down
14 changes: 4 additions & 10 deletions packages/@ember/-internals/metal/lib/property_set.ts
@@ -1,13 +1,8 @@
import {
HAS_NATIVE_PROXY,
lookupDescriptor,
setWithMandatorySetter,
toString,
} from '@ember/-internals/utils';
import { HAS_NATIVE_PROXY, setWithMandatorySetter, toString } from '@ember/-internals/utils';
import { assert } from '@ember/debug';
import EmberError from '@ember/error';
import { DEBUG } from '@glimmer/env';
import { CP_SETTER_FUNCS } from './decorator';
import { descriptorForProperty } from './decorator';
import { isPath } from './path_cache';
import { notifyPropertyChange } from './property_events';
import { _getPath as getPath, getPossibleMandatoryProxyValue } from './property_get';
Expand Down Expand Up @@ -72,10 +67,9 @@ export function set(obj: object, keyName: string, value: any, tolerant?: boolean
return setPath(obj, keyName, value, tolerant);
}

let descriptor = lookupDescriptor(obj, keyName);
let setter = descriptor === null ? undefined : descriptor.set;
let descriptor = descriptorForProperty(obj, keyName);

if (setter !== undefined && CP_SETTER_FUNCS.has(setter)) {
if (descriptor !== undefined) {
obj[keyName] = value;
return value;
}
Expand Down
2 changes: 0 additions & 2 deletions packages/@ember/-internals/metal/lib/tracked.ts
Expand Up @@ -5,7 +5,6 @@ import { DEBUG } from '@glimmer/env';
import { consumeTag, dirtyTagFor, tagFor, trackedData } from '@glimmer/validator';
import { CHAIN_PASS_THROUGH } from './chain-tags';
import {
CP_SETTER_FUNCS,
Decorator,
DecoratorPropertyDescriptor,
isElementDescriptor,
Expand Down Expand Up @@ -185,7 +184,6 @@ function descriptorForField([target, key, desc]: [

metaFor(target).writeDescriptors(key, newDesc);

CP_SETTER_FUNCS.add(set);
CHAIN_PASS_THROUGH.add(newDesc);

return newDesc;
Expand Down
Expand Up @@ -111,7 +111,7 @@ function initialize(obj, properties) {
}

if (isDescriptor) {
possibleDesc.set(obj, keyName, value);
obj[keyName] = value;
} else if (typeof obj.setUnknownProperty === 'function' && !(keyName in obj)) {
obj.setUnknownProperty(keyName, value);
} else {
Expand Down

0 comments on commit 5d5f966

Please sign in to comment.