Skip to content

Commit f6c255f

Browse files
jkomorosclaude
andcommitted
docs: Modernize lift() pattern examples per Berni's feedback
Simplify pattern instance storage examples: - Remove toSchema() wrapper - use simpler lift<Type, Return> syntax - Remove [ID] field with Math.random() (anti-pattern for causal IDs) - Remove unnecessary type casting (modern types handle it) - Add note about proper ID generation in production Addresses Berni's review comments on PR #1998: - Line 505: toSchema not needed with current framework - Line 515: [ID] + Math.random() is anti-pattern - Line 536: Type casting unnecessary Examples now show modern, simplified approach while maintaining the core lesson (use lift() when storing pattern instances). 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
1 parent a298f73 commit f6c255f

File tree

2 files changed

+18
-17
lines changed

2 files changed

+18
-17
lines changed

docs/common/HANDLERS.md

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -501,19 +501,17 @@ type Entry = {
501501
};
502502

503503
// ✅ CORRECT: Wrap the push operation in a lift function
504-
const storePattern = lift(
505-
toSchema<{
506-
charm: any;
507-
patternsList: Cell<Entry[]>;
508-
isInitialized: Cell<boolean>;
509-
}>(),
510-
undefined,
504+
const storePattern = lift<{
505+
charm: any;
506+
patternsList: Cell<Entry[]>;
507+
isInitialized: Cell<boolean>;
508+
}, undefined>(
511509
({ charm, patternsList, isInitialized }) => {
512510
if (!isInitialized.get()) {
513511
// ✅ CORRECT: .push() happens inside lift, not handler
514512
patternsList.push({
515-
[ID]: Math.random().toString(36).substring(2, 10),
516-
local_id: Math.random().toString(36).substring(2, 10),
513+
// Note: Using a stable ID here - in real code use proper ID generation
514+
local_id: `pattern-${Date.now()}`,
517515
charm
518516
});
519517
isInitialized.set(true);
@@ -530,11 +528,11 @@ const createPattern = handler<
530528
const isInitialized = cell(false);
531529
const pattern = MyPattern({ title: "New" });
532530

533-
// ✅ CORRECT: Return the lift function call
531+
// ✅ CORRECT: Return the lift function call (no casting needed with modern types)
534532
return storePattern({
535533
charm: pattern,
536-
patternsList: patternsList as unknown as OpaqueRef<Entry[]>,
537-
isInitialized: isInitialized as unknown as Cell<boolean>
534+
patternsList,
535+
isInitialized
538536
});
539537
}
540538
);

docs/common/PATTERNS.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -810,12 +810,15 @@ const createChat = handler<unknown, { chatsList: Cell<Entry[]> }>(
810810
);
811811

812812
// ✅ CORRECT - Use lift() to wrap the push operation
813-
const storeChat = lift(
814-
toSchema<{ charm: any; chatsList: Cell<Entry[]>; isInitialized: Cell<boolean> }>(),
815-
undefined,
813+
const storeChat = lift<{
814+
charm: any;
815+
chatsList: Cell<Entry[]>;
816+
isInitialized: Cell<boolean>;
817+
}, undefined>(
816818
({ charm, chatsList, isInitialized }) => {
817819
if (!isInitialized.get()) {
818-
chatsList.push({ [ID]: randomId, local_id: randomId, charm });
820+
// Use stable ID - in production use proper ID generation
821+
chatsList.push({ local_id: `chat-${Date.now()}`, charm });
819822
isInitialized.set(true);
820823
}
821824
}
@@ -826,7 +829,7 @@ const createChat = handler<unknown, { chatsList: Cell<Entry[]> }>(
826829
const isInitialized = cell(false);
827830
const chat = Chat({ title: "New Chat", messages: [] });
828831

829-
// ✅ Handler returns the lift call instead of pushing directly
832+
// ✅ Handler returns the lift call (no casting needed with modern types)
830833
return storeChat({ charm: chat, chatsList, isInitialized });
831834
}
832835
);

0 commit comments

Comments
 (0)