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

Full pair syntax support #5

Merged
merged 13 commits into from
Oct 14, 2024
Merged

Full pair syntax support #5

merged 13 commits into from
Oct 14, 2024

Conversation

hautajoki
Copy link
Collaborator

@hautajoki hautajoki commented Oct 14, 2024

This pr adds compile-time pair support in add, remove, set, has and get functions. Previously these functions could only be used when both the predicate and the object of the relationship were known. Now we can define the object as a runtime entity id.

interface Fruit {}
interface Likes {}
interface Eats {
	count: number;
}

const alice = spawn();
const bob = spawn();
const charlie = spawn();

const banana = spawn();
add<Fruit>(banana);

add<Pair<Likes>>(alice, bob);
add<Pair<Likes>>(alice, charlie);

add<Pair<Likes, Fruit>>(bob);

set<Pair<Eats>>(bob, banana, { count: 5 });
set<Pair<Eats, Fruit>>(alice, { count: 12 });

for (const [entity] of query().pair<Likes>(alice)) {
	const likedEntity = target<Likes>(entity);
	print(`Entity ${entity} likes ${likedEntity}`);
}

for (const [entity, eatsData] of query<[Pair<Eats, Fruit>]>()) {
	const eatsTarget = target<Eats>(entity);
	print(`Entity ${entity} eats ${eatsData.count} fruit (${eatsTarget})`);
}

// Using Pair<P> to match any target (wildcard), equivalent to Pair<Likes, Wildcard>
for (const [entity] of query<[Pair<Likes>]>()) {
	const likedTarget = target<Likes>(entity);
	print(`Entity ${entity} likes ${likedTarget}`);
}

Copy link
Collaborator

@Ukendio Ukendio left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm!

@hautajoki hautajoki merged commit a7337f9 into master Oct 14, 2024
1 check passed
@hautajoki hautajoki deleted the full-pair-syntax-support branch October 14, 2024 21:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants