Skip to content

Commit 54c2413

Browse files
committed
Maybe progress
1 parent 2aa226d commit 54c2413

File tree

3 files changed

+38
-38
lines changed

3 files changed

+38
-38
lines changed

ember-resources/src/intermediate-representation.ts

+5-17
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { ReadonlyCell } from './cell.ts';
1010
import { ResourceManagerFactory } from './resource-manager.ts';
1111
import { CURRENT, INTERNAL } from './types.ts';
1212
import { registerUsable, TYPE_KEY } from './use.ts';
13+
import { shallowFlat } from './utils.ts';
1314

1415
import type { Destructor, Reactive, ResourceFunction } from './types.ts';
1516
import type Owner from '@ember/owner';
@@ -79,8 +80,9 @@ const TYPE = 'function-based';
7980

8081
registerUsable(TYPE, (context: object, config: Builder<unknown>) => {
8182
let owner = getOwner(context) || context;
83+
let instance = config.create(owner as Owner);
8284

83-
return config.create(owner as Owner)[RESOURCE_CACHE];
85+
return instance[RESOURCE_CACHE];
8486
});
8587

8688
/**
@@ -131,7 +133,7 @@ export class Resource<Value> {
131133
);
132134
assert(
133135
`Expected the resource's \`use(...)\` utility to have been passed another resource, but something else was passed.`,
134-
INTERNAL in usable,
136+
INTERNAL in usable || usable instanceof Builder,
135137
);
136138

137139
let previousCache = this.#usableCache.get(usable);
@@ -175,17 +177,7 @@ export class Resource<Value> {
175177
}
176178

177179
get current() {
178-
let maybeValue = getValue(this.#cache);
179-
180-
if (typeof maybeValue === 'function') {
181-
return maybeValue();
182-
}
183-
184-
if (isReactive(maybeValue)) {
185-
return maybeValue[CURRENT];
186-
}
187-
188-
return maybeValue;
180+
return shallowFlat(this.#cache);
189181
}
190182

191183
[DEBUG_NAME]() {
@@ -194,7 +186,3 @@ export class Resource<Value> {
194186
}
195187

196188
setHelperManager(ResourceManagerFactory, Builder.prototype);
197-
198-
function isReactive<Value>(maybe: unknown): maybe is Reactive<Value> {
199-
return typeof maybe === 'object' && maybe !== null && CURRENT in maybe;
200-
}

ember-resources/src/use.ts

+2-16
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { associateDestroyableChild } from '@ember/destroyable';
99
import { invokeHelper } from '@ember/helper';
1010

1111
import { ReadonlyCell } from './cell.ts';
12+
import { getCurrentValue, shallowFlat } from './utils.ts';
1213

1314
import type {
1415
INTERNAL,
@@ -107,17 +108,6 @@ export function use(
107108
assert(`Unknown arity for \`use\`. Received ${args.length} arguments`, false);
108109
}
109110

110-
function getCurrentValue<Value>(value: Value | Reactive<Value>): Value {
111-
/**
112-
* If we are working with a cell, forward the '.current' call to it.
113-
*/
114-
if (typeof value === 'object' && value !== null && 'current' in value) {
115-
return value.current;
116-
}
117-
118-
return value;
119-
}
120-
121111
function classContextLink<Value>(
122112
context: object,
123113
definition: Value | (() => Value),
@@ -214,8 +204,6 @@ function descriptorGetter(initializer: unknown | (() => unknown)) {
214204

215205
let usable = USABLES.get(config.type) || USABLES.get(config[TYPE_KEY]);
216206

217-
console.log({ usable });
218-
219207
assert(
220208
`Expected the initialized value with @use to have been a registerd "usable". Available usables are: ${[
221209
...USABLES.keys(),
@@ -231,9 +219,7 @@ function descriptorGetter(initializer: unknown | (() => unknown)) {
231219
associateDestroyableChild(this, cache);
232220
}
233221

234-
let value = getValue(cache);
235-
236-
return getCurrentValue(value);
222+
return shallowFlat(cache);
237223
},
238224
};
239225
}

ember-resources/src/utils.ts

+31-5
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,41 @@
11
// @ts-ignore
2-
import { getValue } from '@glimmer/tracking/primitives/cache';
2+
import { type createCache, getValue } from '@glimmer/tracking/primitives/cache';
33
import { assert } from '@ember/debug';
4-
// @ts-ignore
5-
import { invokeHelper } from '@ember/helper';
64

7-
import { INTERMEDIATE_VALUE } from './types.ts';
5+
import { CURRENT, INTERMEDIATE_VALUE, type Reactive } from './types.ts';
86

97
import type { Builder, Resource } from './intermediate-representation.ts';
10-
import type { InternalFunctionResourceConfig } from './types.ts';
118
import type Owner from '@ember/owner';
129

10+
export function shallowFlat<Value>(cache: ReturnType<typeof createCache>): Value {
11+
let maybeValue = getValue(cache);
12+
13+
if (typeof maybeValue === 'function') {
14+
return maybeValue();
15+
}
16+
17+
if (isReactive(maybeValue)) {
18+
return maybeValue[CURRENT] as Value;
19+
}
20+
21+
return maybeValue as Value;
22+
}
23+
24+
export function isReactive<Value>(maybe: unknown): maybe is Reactive<Value> {
25+
return typeof maybe === 'object' && maybe !== null && CURRENT in maybe;
26+
}
27+
28+
export function getCurrentValue<Value>(value: Value | Reactive<Value>): Value {
29+
/**
30+
* If we are working with a cell, forward the '.current' call to it.
31+
*/
32+
if (typeof value === 'object' && value !== null && 'current' in value) {
33+
return value.current;
34+
}
35+
36+
return value;
37+
}
38+
1339
/**
1440
* This is what allows resource to be used without @use.
1541
* The caveat though is that a property must be accessed

0 commit comments

Comments
 (0)