Skip to content

Commit 0560a0c

Browse files
committed
progress
1 parent c8e4481 commit 0560a0c

File tree

3 files changed

+45
-5
lines changed

3 files changed

+45
-5
lines changed

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

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
// of @cached, so any changes made to one should also be made to the other
44
import { DEBUG } from '@glimmer/env';
55
import { createCache, getValue } from '@glimmer/validator';
6+
import {
7+
type Decorator,
8+
identifyModernDecoratorArgs,
9+
isModernDecoratorArgs,
10+
} from './decorator-util';
611

712
/**
813
* @decorator
@@ -84,7 +89,7 @@ import { createCache, getValue } from '@glimmer/validator';
8489
the subsequent cache invalidations of the `@cached` properties who were
8590
using this `trackedProp`.
8691
87-
Remember that setting tracked data should only be done during initialization,
92+
Remember that setting tracked data should only be done during initialization,
8893
or as the result of a user action. Setting tracked data during render
8994
(such as in a getter), is not supported.
9095
@@ -94,6 +99,10 @@ import { createCache, getValue } from '@glimmer/validator';
9499
@public
95100
*/
96101
export const cached: MethodDecorator = (...args: any[]) => {
102+
if (isModernDecoratorArgs(args)) {
103+
return cached2023(args) as any;
104+
}
105+
97106
const [target, key, descriptor] = args;
98107

99108
// Error on `@cached()`, `@cached(...args)`, and `@cached propName = value;`
@@ -123,6 +132,23 @@ export const cached: MethodDecorator = (...args: any[]) => {
123132
};
124133
};
125134

135+
function cached2023(args: Parameters<Decorator>) {
136+
const dec = identifyModernDecoratorArgs(args);
137+
switch (dec.kind) {
138+
case 'getter': {
139+
const caches = new WeakMap();
140+
return function (this: any) {
141+
if (!caches.has(this)) {
142+
caches.set(this, createCache(dec.value.bind(this)));
143+
}
144+
return getValue(caches.get(this));
145+
};
146+
}
147+
default:
148+
throw new Error(`unsupported use of @cached on ${dec.kind} ${dec.context.name?.toString()}`);
149+
}
150+
}
151+
126152
function throwCachedExtraneousParens(): never {
127153
throw new Error(
128154
'You attempted to use @cached(), which is not necessary nor supported. Remove the parentheses and you will be good to go!'

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -886,8 +886,11 @@ export function computed(
886886
...args: ElementDescriptor | string[] | ComputedDecoratorKeysAndConfig
887887
): ComputedDecorator | DecoratorPropertyDescriptor | void {
888888
if (isModernDecoratorArgs(args)) {
889-
console.log(`ignoring computed on ${args[1].kind} ${args[1].name?.toString()}`);
890-
return;
889+
let decorator = makeComputedDecorator(
890+
new ComputedProperty([]),
891+
ComputedDecoratorImpl
892+
) as ComputedDecorator;
893+
return decorator(...(args as [any, any]));
891894
}
892895

893896
assert(

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ export function makeComputedDecorator(
128128
string,
129129
DecoratorPropertyDescriptor | undefined,
130130
Meta | undefined,
131-
boolean | undefined
131+
boolean | undefined,
132132
];
133133

134134
return makeDescriptor(
@@ -166,7 +166,7 @@ function makeDescriptor(
166166
!COMPUTED_GETTERS.has(propertyDesc.get)
167167
);
168168

169-
let meta = argsLength === 3 ? metaFor(target) : maybeMeta;
169+
let meta = argsLength < 4 ? metaFor(target) : maybeMeta;
170170
desc.setup(target, key, propertyDesc, meta!);
171171

172172
let computedDesc: PropertyDescriptor = {
@@ -190,6 +190,17 @@ function computedDecorator2023(args: Parameters<Decorator>, desc: ComputedDescri
190190
);
191191
});
192192
break;
193+
case 'getter':
194+
dec.context.addInitializer(function (this: any) {
195+
Object.defineProperty(
196+
this,
197+
dec.context.name,
198+
makeDescriptor(desc, 2, this, dec.context.name as string, {
199+
get: dec.value as any,
200+
})
201+
);
202+
});
203+
break;
193204
default:
194205
console.log(
195206
`unimplemented: computedDecorator on ${dec.kind} ${dec.context.name?.toString()}`

0 commit comments

Comments
 (0)