Skip to content

Commit 6c198d5

Browse files
authored
fix(built-in): always return wished cell, even if value isn't defined yet (commontoolsinc#2073)
fix(built-in): always return wished cell, even if value isn't defined yet
1 parent fcef381 commit 6c198d5

File tree

6 files changed

+13
-55
lines changed

6 files changed

+13
-55
lines changed

packages/api/index.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1228,13 +1228,9 @@ export type CompileAndRunFunction = <T = any, S = any>(
12281228
) => OpaqueRef<BuiltInCompileAndRunState<S>>;
12291229

12301230
export type NavigateToFunction = (cell: OpaqueRef<any>) => OpaqueRef<string>;
1231-
export type WishFunction = {
1232-
<T = unknown>(target: Opaque<string>): OpaqueRef<T | undefined>;
1233-
<T = unknown>(
1234-
target: Opaque<string>,
1235-
defaultValue: Opaque<T>,
1236-
): OpaqueRef<T>;
1237-
};
1231+
export type WishFunction = <T = unknown>(
1232+
target: Opaque<string>,
1233+
) => OpaqueRef<T>;
12381234

12391235
export type CreateNodeFactoryFunction = <T = any, R = any>(
12401236
moduleSpec: Module,

packages/patterns/chatbot-list-view.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import {
33
Cell,
44
Default,
5+
derive,
56
handler,
67
ID,
78
ifElse,
@@ -236,7 +237,10 @@ const extractLocalMentionable = lift<
236237
export default recipe<Input, Output>(
237238
"Launcher",
238239
({ selectedCharm, charmsList, theme }) => {
239-
const allCharms = wish<MentionableCharm[]>("#allCharms", []);
240+
const allCharms = derive<MentionableCharm[], MentionableCharm[]>(
241+
wish<MentionableCharm[]>("#allCharms"),
242+
(c) => c ?? [],
243+
);
240244
logCharmsList({ charmsList: charmsList as unknown as Cell<CharmEntry[]> });
241245

242246
populateChatList({

packages/patterns/chatbot-note-composed.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import {
88
handler,
99
NAME,
1010
navigateTo,
11-
Opaque,
1211
OpaqueRef,
1312
recipe,
1413
wish,
@@ -27,8 +26,8 @@ import {
2726

2827
import { type MentionableCharm } from "./backlinks-index.tsx";
2928

30-
function schemaifyWish<T>(path: string, def: Opaque<T>) {
31-
return derive<T, T>(wish<T>(path, def), (i) => i);
29+
function schemaifyWish<T>(path: string, def: T) {
30+
return derive(wish<T>(path) as T, (i) => i ?? def);
3231
}
3332

3433
type ChatbotNoteInput = {

packages/patterns/note.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import {
88
handler,
99
NAME,
1010
navigateTo,
11-
type Opaque,
1211
type OpaqueRef,
1312
patternTool,
1413
recipe,
@@ -100,8 +99,8 @@ const handleCharmLinkClicked = handler<void, { charm: Cell<MentionableCharm> }>(
10099
},
101100
);
102101

103-
function schemaifyWish<T>(path: string, def: T | Opaque<T>) {
104-
return derive<T, T>(wish<T>(path, def as Opaque<T>), (i) => i);
102+
function schemaifyWish<T>(path: string, def: T) {
103+
return derive(wish<T>(path) as T, (i) => i ?? def);
105104
}
106105

107106
const Note = recipe<Input, Output>(

packages/runner/src/builtins/wish.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -201,10 +201,6 @@ export function wish(
201201
: parsed.path;
202202
const resolvedCell = resolvePath(baseResolution.cell, combinedPath);
203203

204-
if (resolvedCell.get() !== undefined) {
205-
sendResult(tx, resolvedCell);
206-
} else {
207-
sendResult(tx, hasDefault ? defaultCell : undefined);
208-
}
204+
sendResult(tx, resolvedCell);
209205
};
210206
}

packages/runner/test/wish.test.ts

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -402,40 +402,4 @@ describe("wish built-in", () => {
402402
console.error = originalError;
403403
}
404404
});
405-
406-
it("uses provided default when target is missing", async () => {
407-
const fallback = [{ name: "Fallback" }];
408-
409-
// Set up space cell with an empty default pattern
410-
const spaceCell = runtime.getCell(space, space).withTx(tx);
411-
const defaultPatternCell = runtime.getCell(space, "default-pattern").withTx(
412-
tx,
413-
);
414-
defaultPatternCell.set({}); // Empty default pattern
415-
(spaceCell as any).key("defaultPattern").set(defaultPatternCell);
416-
417-
await tx.commit();
418-
await runtime.idle();
419-
tx = runtime.edit();
420-
421-
const wishRecipe = recipe("wish default", () => {
422-
const missing = wish("/missing", fallback);
423-
return { missing };
424-
});
425-
426-
const resultCell = runtime.getCell<{ missing?: unknown }>(
427-
space,
428-
"wish built-in default",
429-
undefined,
430-
tx,
431-
);
432-
const result = runtime.run(tx, wishRecipe, {}, resultCell);
433-
await tx.commit();
434-
await runtime.idle();
435-
tx = runtime.edit();
436-
437-
await runtime.idle();
438-
439-
expect(result.key("missing").get()).toEqual(fallback);
440-
});
441405
});

0 commit comments

Comments
 (0)