Skip to content

Commit

Permalink
fix ancestor logic
Browse files Browse the repository at this point in the history
  • Loading branch information
tintinthong committed Feb 5, 2025
1 parent ce4ed57 commit 9f0fe1c
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 19 deletions.
14 changes: 13 additions & 1 deletion packages/base/links-to-editor.gts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
RealmURLContextName,
type ResolvedCodeRef,
getNarrowestType,
Loader,
} from '@cardstack/runtime-common';
import { AddButton, IconButton } from '@cardstack/boxel-ui/components';
import { IconMinusCircle } from '@cardstack/boxel-ui/icons';
Expand Down Expand Up @@ -146,7 +147,7 @@ export class LinksToEditor extends GlimmerComponent<Signature> {

private chooseCard = restartableTask(async () => {
let type = identifyCard(this.args.field.card) ?? baseCardRef;
type = await getNarrowestType(this.args.subclassType, type);
type = await getNarrowestType(this.args.subclassType, type, myLoader());
let chosenCard: CardDef | undefined = await chooseCard(
{ filter: { type } },
{
Expand All @@ -164,3 +165,14 @@ export class LinksToEditor extends GlimmerComponent<Signature> {
}
});
}

function myLoader(): Loader {
// we know this code is always loaded by an instance of our Loader, which sets
// import.meta.loader.

// When type-checking realm-server, tsc sees this file and thinks
// it will be transpiled to CommonJS and so it complains about this line. But
// this file is always loaded through our loader and always has access to import.meta.
// @ts-ignore
return (import.meta as any).loader;
}
14 changes: 13 additions & 1 deletion packages/base/links-to-many-component.gts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {
RealmURLContextName,
type ResolvedCodeRef,
getNarrowestType,
Loader,
} from '@cardstack/runtime-common';
import { IconMinusCircle, IconX, FourLines } from '@cardstack/boxel-ui/icons';
import { eq } from '@cardstack/boxel-ui/helpers';
Expand Down Expand Up @@ -98,7 +99,7 @@ class LinksToManyEditor extends GlimmerComponent<Signature> {
selectedCards?.map((card: any) => ({ not: { eq: { id: card.id } } })) ??
[];
let type = identifyCard(this.args.field.card) ?? baseCardRef;
type = await getNarrowestType(this.args.subclassType, type);
type = await getNarrowestType(this.args.subclassType, type, myLoader());
let filter = {
every: [{ type }, ...selectedCardsQuery],
};
Expand Down Expand Up @@ -488,3 +489,14 @@ export function getLinksToManyComponent({
},
});
}

function myLoader(): Loader {
// we know this code is always loaded by an instance of our Loader, which sets
// import.meta.loader.

// When type-checking realm-server, tsc sees this file and thinks
// it will be transpiled to CommonJS and so it complains about this line. But
// this file is always loaded through our loader and always has access to import.meta.
// @ts-ignore
return (import.meta as any).loader;
}
25 changes: 8 additions & 17 deletions packages/runtime-common/code-ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,22 +233,11 @@ function refEquals(ref1: CodeRef, ref2: CodeRef): boolean {
if (!isResolvedCodeRef(ref1) || !isResolvedCodeRef(ref2)) {
return false;
}
return ref1.name === ref1.module && ref1.module === ref2.module;
return ref1.name === ref2.name && ref1.module === ref2.module;
}

function myLoader(): Loader {
// we know this code is always loaded by an instance of our Loader, which sets
// import.meta.loader.

// When type-checking realm-server, tsc sees this file and thinks
// it will be transpiled to CommonJS and so it complains about this line. But
// this file is always loaded through our loader and always has access to import.meta.
// @ts-ignore
return (import.meta as any).loader;
}

async function getAncestorRef(codeRef: CodeRef) {
let card = await loadCard(codeRef, { loader: myLoader() });
async function getAncestorRef(codeRef: CodeRef, loader: Loader) {
let card = await loadCard(codeRef, { loader: loader });
let ancestor = getAncestor(card);
return identifyCard(ancestor);
}
Expand All @@ -258,13 +247,14 @@ async function getAncestorRef(codeRef: CodeRef) {
async function isInsideAncestorChain(
codeRef: CodeRef,
codeRefAncestor: CodeRef,
loader: Loader,
): Promise<boolean | undefined> {
if (refEquals(codeRef, codeRefAncestor)) {
return true;
} else {
let newAncestorRef = await getAncestorRef(codeRef);
let newAncestorRef = await getAncestorRef(codeRef, loader);
if (newAncestorRef) {
return isInsideAncestorChain(newAncestorRef, codeRefAncestor);
return isInsideAncestorChain(newAncestorRef, codeRefAncestor, loader);
} else {
return undefined;
}
Expand All @@ -275,11 +265,12 @@ async function isInsideAncestorChain(
export async function getNarrowestType(
subclassType: CodeRef | undefined,
type: CodeRef,
loader: Loader,
) {
let narrowTypeExists: boolean = false;
if (subclassType) {
narrowTypeExists =
(await isInsideAncestorChain(subclassType, type)) ?? false;
(await isInsideAncestorChain(subclassType, type, loader)) ?? false;
}
let narrowestType = narrowTypeExists && subclassType ? subclassType : type;
return narrowestType;
Expand Down

0 comments on commit 9f0fe1c

Please sign in to comment.