Skip to content

Commit

Permalink
Merge pull request #20388 from emberjs/fix-zebra-mixins
Browse files Browse the repository at this point in the history
[BUGFIX LTS] Don't run getters while applying mixins
  • Loading branch information
chriskrycho committed Mar 8, 2023
2 parents c682675 + a037ae8 commit 03bc5f9
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 13 deletions.
56 changes: 56 additions & 0 deletions packages/@ember/-internals/runtime/tests/mixins/accessor_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import EmberObject from '@ember/object';
import { moduleFor, RenderingTestCase } from 'internal-test-helpers';

moduleFor(
'runtime: Mixin Accessors',
class extends RenderingTestCase {
['@test works with getters'](assert) {
let value = 'building';

let Base = EmberObject.extend({
get foo() {
if (value === 'building') {
throw Error('base should not be called yet');
}

return "base's foo";
},
});

// force Base to be finalized so its properties will contain `foo`
Base.proto();

class Child extends Base {
get foo() {
if (value === 'building') {
throw Error('child should not be called yet');
}

return "child's foo";
}
}

Child.proto();

let Grandchild = Child.extend({
get foo() {
if (value === 'building') {
throw Error('grandchild should not be called yet');
}

return value;
},
});

let instance = Grandchild.create();

value = 'done building';

assert.equal(instance.foo, 'done building', 'getter defined correctly');

value = 'changed value';

assert.equal(instance.foo, 'changed value', 'the value is a real getter, not a snapshot');
}
}
);
32 changes: 19 additions & 13 deletions packages/@ember/object/mixin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ import { guidFor, observerListenerMetaFor, ROOT, wrap } from '@ember/-internals/
import { assert } from '@ember/debug';
import { DEBUG } from '@glimmer/env';
import { _WeakSet } from '@glimmer/util';
import type {
ComputedDecorator,
ComputedPropertyGetter,
ComputedPropertyObj,
ComputedPropertySetter,
ComputedDescriptor,
import {
type ComputedDecorator,
type ComputedPropertyGetter,
type ComputedPropertyObj,
type ComputedPropertySetter,
type ComputedDescriptor,
isClassicDecorator,
} from '@ember/-internals/metal';
import {
ComputedProperty,
Expand Down Expand Up @@ -235,7 +236,7 @@ function mergeMixins(
keys: string[],
keysWithSuper: string[]
): void {
let currentMixin;
let currentMixin: MixinLike | undefined;

for (let i = 0; i < mixins.length; i++) {
currentMixin = mixins[i];
Expand Down Expand Up @@ -309,12 +310,17 @@ function mergeProps(
let desc = meta.peekDescriptors(key);

if (desc === undefined) {
// The superclass did not have a CP, which means it may have
// observers or listeners on that property.
let prev = (values[key] = base[key]);

if (typeof prev === 'function') {
updateObserversAndListeners(base, key, prev, false);
// If the value is a classic decorator, we don't want to actually
// access it, because that will execute the decorator while we're
// building the class.
if (!isClassicDecorator(value)) {
// The superclass did not have a CP, which means it may have
// observers or listeners on that property.
let prev = (values[key] = base[key]);

if (typeof prev === 'function') {
updateObserversAndListeners(base, key, prev, false);
}
}
} else {
descs[key] = desc;
Expand Down

0 comments on commit 03bc5f9

Please sign in to comment.