Skip to content

Commit e4242d3

Browse files
committed
progress
1 parent 577dfc2 commit e4242d3

File tree

4 files changed

+62
-31
lines changed

4 files changed

+62
-31
lines changed

packages/@ember/-internals/metal/lib/decorator.ts

Lines changed: 40 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
identifyModernDecoratorArgs,
88
isModernDecoratorArgs,
99
} from './decorator-util';
10+
import { findDescriptor } from '@ember/-internals/utils/lib/lookup-descriptor';
1011

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

@@ -167,45 +168,60 @@ function makeDescriptor(
167168
return computedDesc;
168169
}
169170

171+
function once() {
172+
let needsToRun = true;
173+
return function (fn: () => void): void {
174+
if (needsToRun) {
175+
fn();
176+
needsToRun = false;
177+
}
178+
};
179+
}
180+
170181
function computedDecorator2023(args: Parameters<Decorator>, desc: ComputedDescriptor) {
171182
const dec = identifyModernDecoratorArgs(args);
172-
173-
let setup: undefined | ((prototype: any, propDesc: any) => void) = function (
174-
prototype,
175-
propDesc
176-
) {
177-
desc.setup(prototype, dec.context.name as string, propDesc, metaFor(prototype));
178-
setup = undefined;
179-
};
183+
let setup = once();
180184

181185
switch (dec.kind) {
182186
case 'field':
183187
dec.context.addInitializer(function (this: any) {
184-
setup?.(this.constructor.prototype, undefined);
188+
setup(() => {
189+
desc.setup(
190+
this.constructor.prototype,
191+
dec.context.name as string,
192+
undefined,
193+
metaFor(this.constructor.prototype)
194+
);
195+
});
185196
Object.defineProperty(
186197
this,
187198
dec.context.name,
188199
makeDescriptor(desc, dec.context.name as string)
189200
);
190201
});
191-
break;
202+
return undefined;
203+
case 'setter':
192204
case 'getter': {
193-
let propDesc = {
194-
get: dec.value as any,
195-
};
196-
dec.context.addInitializer(function (this: any) {
197-
setup?.(this.constructor.prototype, propDesc);
198-
});
199-
return makeDescriptor(desc, dec.context.name as string, propDesc).get;
200-
}
201-
case 'setter': {
202-
let propDesc = {
203-
set: dec.value as any,
204-
};
205205
dec.context.addInitializer(function (this: any) {
206-
setup?.(this.constructor.prototype, propDesc);
206+
setup(() => {
207+
let found = findDescriptor(this, dec.context.name);
208+
if (!found) {
209+
return;
210+
}
211+
desc.setup(
212+
found.object,
213+
dec.context.name as string,
214+
found.descriptor,
215+
metaFor(found.object)
216+
);
217+
Object.defineProperty(
218+
found.object,
219+
dec.context.name,
220+
makeDescriptor(desc, dec.context.name as string, found.descriptor)
221+
);
222+
});
207223
});
208-
return makeDescriptor(desc, dec.context.name as string, propDesc).set;
224+
return undefined;
209225
}
210226
case 'method':
211227
assert(

packages/@ember/-internals/metal/tests/computed_decorator_test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ moduleFor(
7171
['@test computed property can be defined and accessed on a class constructor'](assert) {
7272
let count = 0;
7373

74+
debugger;
7475
class Obj {
7576
static bar = 123;
7677

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
1-
export default function lookupDescriptor(obj: object, keyName: string | symbol) {
1+
export function findDescriptor(
2+
obj: object,
3+
keyName: string | symbol
4+
): { object: object; descriptor: PropertyDescriptor } | null {
25
let current: object | null = obj;
36
do {
47
let descriptor = Object.getOwnPropertyDescriptor(current, keyName);
58
if (descriptor !== undefined) {
6-
return descriptor;
9+
return { descriptor, object: current };
710
}
811
current = Object.getPrototypeOf(current);
912
} while (current !== null);
1013
return null;
1114
}
15+
16+
export default function lookupDescriptor(
17+
obj: object,
18+
keyName: string | symbol
19+
): PropertyDescriptor | null {
20+
return findDescriptor(obj, keyName)?.descriptor ?? null;
21+
}

packages/@ember/object/index.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
identifyModernDecoratorArgs,
1717
isModernDecoratorArgs,
1818
} from '@ember/-internals/metal/lib/decorator-util';
19+
import { findDescriptor } from '@ember/-internals/utils/lib/lookup-descriptor';
1920

2021
export {
2122
notifyPropertyChange,
@@ -328,11 +329,14 @@ function action2023(args: Parameters<Decorator>) {
328329
let needsSetup = true;
329330
dec.context.addInitializer(function (this: any) {
330331
if (needsSetup) {
331-
Object.defineProperty(
332-
this.constructor.prototype,
333-
dec.context.name,
334-
setupAction(this.constructor.prototype, dec.context.name, dec.value)
335-
);
332+
let found = findDescriptor(this, dec.context.name);
333+
if (found?.object) {
334+
Object.defineProperty(
335+
found.object,
336+
dec.context.name,
337+
setupAction(found.object, dec.context.name, dec.value)
338+
);
339+
}
336340
needsSetup = false;
337341
}
338342
});

0 commit comments

Comments
 (0)