Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

de-mystify resource internals #1166

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Prev Previous commit
Next Next commit
Oh, can I actually delete these hacks?
  • Loading branch information
NullVoxPopuli committed Jan 25, 2025
commit be0597dee0c5c273557c565bb2fc042fc3f9bae2
2 changes: 0 additions & 2 deletions ember-resources/src/resource.ts
Original file line number Diff line number Diff line change
@@ -202,6 +202,4 @@ export function resource<Value>(
let configured = new Builder(setup, CREATE_KEY);

return configured;

return wrapForPlainUsage(context, configured);
}
63 changes: 0 additions & 63 deletions ember-resources/src/utils.ts
Original file line number Diff line number Diff line change
@@ -35,66 +35,3 @@ export function getCurrentValue<Value>(value: Value | Reactive<Value>): Value {

return value;
}

/**
* This is what allows resource to be used without @use.
* The caveat though is that a property must be accessed
* on the return object.
*
* A resource not using use *must* be an object.
*/
export function wrapForPlainUsage<Value>(context: object, builder: Builder<Value>) {
let cache: Resource<Value>;

/*
* Having an object that we use invokeHelper + getValue on
* is how we convert the "function" in to a reactive utility
* (along with the following proxy for accessing anything on this 'value')
*
*/
const target = {
get [INTERMEDIATE_VALUE]() {
if (!cache) {
cache = builder.create();
cache.link(context);
}

// SAFETY: the types for the helper manager APIs aren't fully defined to infer
// nor allow passing the value.
return cache.current;
},
};

/**
* This proxy takes everything called on or accessed on "target"
* and forwards it along to target[INTERMEDIATE_VALUE] (where the actual resource instance is)
*
* It's important to only access .[INTERMEDIATE_VALUE] within these proxy-handler methods so that
* consumers "reactively entangle with" the Resource.
*/
return new Proxy(target, {
get(target, key): unknown {
const state = target[INTERMEDIATE_VALUE];

assert('[BUG]: it should not have been possible for this to be undefined', state);

return Reflect.get(state, key, state);
},

ownKeys(target): (string | symbol)[] {
const value = target[INTERMEDIATE_VALUE];

assert('[BUG]: it should not have been possible for this to be undefined', value);

return Reflect.ownKeys(value);
},

getOwnPropertyDescriptor(target, key): PropertyDescriptor | undefined {
const value = target[INTERMEDIATE_VALUE];

assert('[BUG]: it should not have been possible for this to be undefined', value);

return Reflect.getOwnPropertyDescriptor(value, key);
},
}) as never as Value;
}
2 changes: 1 addition & 1 deletion test-app/tests/core/raw-api-test.gts
Original file line number Diff line number Diff line change
@@ -5,7 +5,7 @@ import { click, render } from '@ember/test-helpers';
import { module, test } from 'qunit';
import { setupRenderingTest, setupTest } from 'ember-qunit';

import { cell, resource, resourceFactory } from 'ember-resources';
import { resource } from 'ember-resources';

module('RAW', function (hooks) {
setupTest(hooks);
Loading
Oops, something went wrong.