Skip to content

Commit 5949a1c

Browse files
authored
fix: don't throw when manager.get() doesn't return a charm (#509)
1 parent fa78a99 commit 5949a1c

File tree

1 file changed

+40
-8
lines changed
  • typescript/packages/common-charm/src

1 file changed

+40
-8
lines changed

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

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1-
import { JSONSchema, Module, NAME, Recipe, TYPE, UI } from "@commontools/builder";
1+
import {
2+
JSONSchema,
3+
Module,
4+
NAME,
5+
Recipe,
6+
TYPE,
7+
UI,
8+
} from "@commontools/builder";
29
import {
310
type Cell,
411
createRef,
@@ -97,7 +104,11 @@ export class CharmManager {
97104
async pin(charm: Cell<Charm>) {
98105
await storage.syncCell(this.pinned);
99106
// Check if already pinned
100-
if (!filterOutEntity(this.pinnedCharms, charm).some((c) => isSameEntity(c, charm))) {
107+
if (
108+
!filterOutEntity(this.pinnedCharms, charm).some((c) =>
109+
isSameEntity(c, charm)
110+
)
111+
) {
101112
this.pinnedCharms.push(charm);
102113
await idle();
103114
}
@@ -159,11 +170,16 @@ export class CharmManager {
159170
const recipeId = await this.syncRecipe(charm);
160171
const recipe = recipeId ? getRecipe(recipeId) : undefined;
161172

173+
if (!recipe || charm.get() === undefined) {
174+
console.warn(`Not a charm: ${JSON.stringify(getEntityId(charm))}`);
175+
}
176+
162177
let resultSchema: JSONSchema | undefined = recipe?.resultSchema;
163178

164179
// Unless there is a non-object schema, add UI and NAME properties if present
165180
if (!resultSchema || resultSchema.type === "object") {
166-
const { [UI]: hasUI, [NAME]: hasName } = charm.getAsDocLink().cell!.get();
181+
const { [UI]: hasUI, [NAME]: hasName } =
182+
charm.getAsDocLink().cell.get() ?? {};
167183
if (hasUI || hasName) {
168184
// Copy the original schema, so we can modify properties without
169185
// affecting other uses of the same spell.
@@ -200,7 +216,11 @@ export class CharmManager {
200216
path: string[] = [],
201217
schema?: JSONSchema,
202218
): Promise<Cell<T>> {
203-
return (await storage.syncCellById(this.space, id)).asCell(path, undefined, schema);
219+
return (await storage.syncCellById(this.space, id)).asCell(
220+
path,
221+
undefined,
222+
schema,
223+
);
204224
}
205225

206226
// Return Cell with argument content according to the schema of the charm.
@@ -209,7 +229,9 @@ export class CharmManager {
209229
const recipeId = source?.get()?.[TYPE];
210230
const recipe = getRecipe(recipeId);
211231
const argumentSchema = recipe?.argumentSchema;
212-
return source?.key("argument").asSchema(argumentSchema!) as Cell<T> | undefined;
232+
return source?.key("argument").asSchema(argumentSchema!) as
233+
| Cell<T>
234+
| undefined;
213235
}
214236

215237
// note: removing a charm doesn't clean up the charm's cells
@@ -230,7 +252,11 @@ export class CharmManager {
230252
return false;
231253
}
232254

233-
async runPersistent(recipe: Recipe | Module, inputs?: any, cause?: any): Promise<Cell<Charm>> {
255+
async runPersistent(
256+
recipe: Recipe | Module,
257+
inputs?: any,
258+
cause?: any,
259+
): Promise<Cell<Charm>> {
234260
await idle();
235261

236262
// Fill in missing parameters from other charms. It's a simple match on
@@ -286,7 +312,10 @@ export class CharmManager {
286312

287313
await syncAllMentionedCells(inputs);
288314

289-
const doc = await storage.syncCellById(this.space, createRef({ recipe, inputs }, cause));
315+
const doc = await storage.syncCellById(
316+
this.space,
317+
createRef({ recipe, inputs }, cause),
318+
);
290319
const resultDoc = run(recipe, inputs, doc);
291320

292321
// FIXME(ja): should we add / sync explicitly here?
@@ -300,7 +329,10 @@ export class CharmManager {
300329
const recipeId = charm.getSourceCell()?.get()?.[TYPE];
301330
if (!recipeId) return Promise.resolve(undefined);
302331

303-
return Promise.all([this.syncRecipeCells(recipeId), this.syncRecipeBlobby(recipeId)]).then(
332+
return Promise.all([
333+
this.syncRecipeCells(recipeId),
334+
this.syncRecipeBlobby(recipeId),
335+
]).then(
304336
() => recipeId,
305337
);
306338
}

0 commit comments

Comments
 (0)