Skip to content

Commit 9fae3cc

Browse files
authored
remove direct use of sourceCell from jumble (#497)
1 parent ec8aede commit 9fae3cc

File tree

2 files changed

+25
-14
lines changed

2 files changed

+25
-14
lines changed

typescript/packages/common-charm/src/charm.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,12 +158,14 @@ export class CharmManager {
158158
}
159159

160160
// Return Cell with argument content according to the schema of the charm.
161-
getArgument<T = any>(charm: Cell<Charm | T>): T {
161+
getArgument<T = any>(charm: Cell<Charm | T>): Cell<T> | undefined {
162162
const source = charm.getSourceCell(processSchema);
163163
const recipeId = source?.get()?.[TYPE];
164164
const recipe = getRecipe(recipeId);
165165
const argumentSchema = recipe?.argumentSchema;
166-
return source?.key("argument").asSchema(argumentSchema!) as T;
166+
return source?.key("argument").asSchema(argumentSchema!) as
167+
| Cell<T>
168+
| undefined;
167169
}
168170

169171
// note: removing a charm doesn't clean up the charm's cells

typescript/packages/jumble/src/components/commands.ts

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -112,20 +112,21 @@ export interface SelectOption {
112112
export const castSpellAsCharm = async (
113113
charmManager: CharmManager,
114114
recipeKey: string,
115-
blobId: string,
115+
argument: Cell<any>,
116116
) => {
117-
if (recipeKey && blobId) {
117+
if (recipeKey && argument) {
118118
console.log("Syncing...");
119119
const recipeId = recipeKey.replace("spell-", "");
120120
await charmManager.syncRecipeBlobby(recipeId);
121121

122122
const recipe = getRecipe(recipeId);
123123
if (!recipe) return;
124124

125-
console.log("Syncing blob...");
126-
const cell = await charmManager.getCellById({ "/": blobId }, ["argument"]);
127125
console.log("Casting...");
128-
const charm: Cell<Charm> = await charmManager.runPersistent(recipe, cell);
126+
const charm: Cell<Charm> = await charmManager.runPersistent(
127+
recipe,
128+
argument,
129+
);
129130
charmManager.add([charm]);
130131
return charm.entityId;
131132
}
@@ -495,17 +496,17 @@ async function handleUseDataInSpell(deps: CommandContext) {
495496
}
496497

497498
const charm = await deps.charmManager.get(deps.focusedCharmId);
498-
const sourceCell = charm?.getSourceCell();
499-
500-
const sourceId = getEntityId(sourceCell)?.["/"];
501-
if (!sourceId) {
502-
throw new Error("No source ID found");
499+
if (!charm) throw new Error("No current charm found");
500+
const argument = deps.charmManager.getArgument(charm);
501+
if (!argument) {
502+
throw new Error("No sourceCell/argument found for current charm");
503503
}
504+
504505
deps.setLoading(true);
505506
const newCharm = await castSpellAsCharm(
506507
deps.charmManager,
507508
selectedSpell.id,
508-
sourceId,
509+
argument,
509510
);
510511

511512
if (!newCharm) {
@@ -578,10 +579,18 @@ async function handleUseSpellOnOtherData(deps: CommandContext) {
578579
}
579580

580581
deps.setLoading(true);
582+
583+
console.log("Syncing blob...");
584+
// TODO(ben,seefeld): We might want spellcaster to return docId/path
585+
// pairs and use those directly instead of hardcoding `argument` here.
586+
const argument = await deps.charmManager.getCellById({
587+
"/": selectedCell.id,
588+
}, ["argument"]);
589+
581590
const newCharm = await castSpellAsCharm(
582591
deps.charmManager,
583592
spellId,
584-
selectedCell.id,
593+
argument,
585594
);
586595
if (!newCharm) throw new Error("Failed to cast spell");
587596

0 commit comments

Comments
 (0)