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

curry method for JS use #1409

Open
wagenet opened this issue Mar 13, 2023 · 0 comments
Open

curry method for JS use #1409

wagenet opened this issue Mar 13, 2023 · 0 comments

Comments

@wagenet
Copy link
Contributor

wagenet commented Mar 13, 2023

import { type ComponentLike } from '@glint/template';
import {
	type ComponentSignatureArgs,
	type ComponentSignatureBlocks,
	type ComponentSignatureElement,
} from '@glint/template/-private/signature';

import type Owner from '@ember/owner';
import { createPrimitiveRef } from '@glimmer/reference';
import { curry } from '@glimmer/runtime';

// From @glimmer/interfaces
export const enum CurriedType {
	Component = 0,
	Helper = 1,
	Modifier = 2,
}

export class CurriedValue<_T extends CurriedType = CurriedType> {}

/**
 * Attaches arguments to a component.
 *
 * This function behaves like the `{{component}}` helper but without the string lookup
 * behavior.
 */
export function curryComponent<S, ArgKeys extends keyof ComponentSignatureArgs<S>['Named']>(
	component: ComponentLike<S>,
	owner: Owner,
	args: { [K in ArgKeys]: ComponentSignatureArgs<S>['Named'][K] },
): ComponentLike<{
	Element: ComponentSignatureElement<S>;
	Args: {
		Named: Omit<ComponentSignatureArgs<S>['Named'], ArgKeys> &
			Partial<Pick<ComponentSignatureArgs<S>['Named'], ArgKeys>>;
		Positional: ComponentSignatureArgs<S>['Positional'];
	};
	Blocks: ComponentSignatureBlocks<S>;
}> {
	const mappedArgs = Object.fromEntries(Object.entries(args).map(([key, value]) => [key, createPrimitiveRef(value)]));
	// SAFETY: Not actually safe, but it should be correct in practice
	return curry(0, component, owner, { named: mappedArgs, positional: [] }) as any;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant