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

resourceBlueprint #635

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions ember-resources/src/core/blueprint.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { assert } from '@ember/debug';

import { resourceFactory } from './function-based';

import type { Resource } from './class-based';
import type { resource } from './function-based';

function isFunctionResource(x: unknown): x is ReturnType<typeof resource> {
return Boolean(x);
}

function isClassResource(x: unknown): x is typeof Resource {
return Boolean(x);
}

/**
* Helper for automatically supporting the various instances
* of resource invocation.
* Both with and without this /
* with and without `@use`
*
* Unfortunately, using this means that you may not provide alternate JSDoc Documentation per override.
*/
export function resourceBlueprint<Args extends unknown[] = unknown[], ResourceType = unknown>(
resourceDefinition: ResourceType
) {
if (isClassResource(resourceDefinition)) {
return (...args: Args) => {
if (args.length === 2) {
let [destroyable, options] = args;

/**
* If any "root level" config changes, we need to throw-away everything.
* otherwise individual-property reactivity can be managed on a per-property
* "thunk"-basis
*/
return resourceDefinition.from(destroyable, () => options);
}

let [options] = args;

return resourceDefinition.from(() => options);
};
}

if (isFunctionResource(resourceDefinition)) {
// TODO
//
// I think `resource` may need to return secret data on it to make this work.
if (false /* how to check arity? */) {
// Allows invocation with Args.
resourceFactory(resourceDefinition);
// TODO
}
}

assert(`Passed resourceDefinition is not a recognized resource type`);
}