|
| 1 | +/** |
| 2 | + * Manual Lifecycle Demo Component |
| 3 | + * |
| 4 | + * This demonstrates the complexity required when using interpretManual(). |
| 5 | + * Compare this to the simplicity of using interpret() with atoms! |
| 6 | + */ |
| 7 | + |
| 8 | +import { Button } from "./ui/button"; |
| 9 | +import { useManualActor, useLifecycleLogs, clearLogs, type LifecycleLog } from "../data-access/manual-actor"; |
| 10 | +import { cn } from "../lib/utils"; |
| 11 | + |
| 12 | +const LogPanel = ({ logs }: { logs: LifecycleLog[] }) => ( |
| 13 | + <div className="bg-gray-950 rounded-lg p-4 font-mono text-xs"> |
| 14 | + <div className="flex items-center justify-between mb-3"> |
| 15 | + <span className="text-gray-400 font-bold">Lifecycle Log</span> |
| 16 | + <button onClick={clearLogs} className="text-gray-500 hover:text-gray-300 text-xs"> |
| 17 | + Clear |
| 18 | + </button> |
| 19 | + </div> |
| 20 | + <div className="space-y-1 h-64 overflow-y-auto"> |
| 21 | + {logs.length === 0 ? ( |
| 22 | + <div className="text-gray-600">Logs will appear here...</div> |
| 23 | + ) : ( |
| 24 | + logs.map((log, i) => ( |
| 25 | + <div |
| 26 | + key={i} |
| 27 | + className={cn( |
| 28 | + "py-0.5", |
| 29 | + log.type === "info" && "text-blue-400", |
| 30 | + log.type === "warning" && "text-yellow-400", |
| 31 | + log.type === "error" && "text-red-400", |
| 32 | + log.type === "success" && "text-green-400", |
| 33 | + log.message.startsWith(" →") && "pl-4 text-gray-400" |
| 34 | + )} |
| 35 | + > |
| 36 | + <span className="text-gray-600 mr-2"> |
| 37 | + {log.timestamp.toLocaleTimeString()} |
| 38 | + </span> |
| 39 | + {log.message} |
| 40 | + </div> |
| 41 | + )) |
| 42 | + )} |
| 43 | + </div> |
| 44 | + </div> |
| 45 | +); |
| 46 | + |
| 47 | +export const ManualLifecycleDemo = () => { |
| 48 | + const { count, tickCount, isStopped, increment, decrement, stop, restart } = useManualActor(); |
| 49 | + const logs = useLifecycleLogs(); |
| 50 | + |
| 51 | + return ( |
| 52 | + <div className="p-8 max-w-4xl mx-auto"> |
| 53 | + <h1 className="text-2xl font-bold text-white mb-2"> |
| 54 | + interpretManual() Demo |
| 55 | + </h1> |
| 56 | + <p className="text-gray-400 mb-6"> |
| 57 | + This counter has an activity that ticks every 100ms. Watch what happens when you stop/restart. |
| 58 | + </p> |
| 59 | + |
| 60 | + {/* Counter Display */} |
| 61 | + <div className={cn( |
| 62 | + "rounded-xl p-8 mb-6 text-center transition-all", |
| 63 | + isStopped ? "bg-red-950 border-2 border-red-800" : "bg-slate-800" |
| 64 | + )}> |
| 65 | + {isStopped && ( |
| 66 | + <div className="text-red-400 text-sm mb-4 font-bold"> |
| 67 | + ACTOR STOPPED - Activity interrupted! |
| 68 | + </div> |
| 69 | + )} |
| 70 | + |
| 71 | + <div className="text-6xl font-bold text-white mb-2">{count}</div> |
| 72 | + <div className="text-gray-400 text-sm mb-6"> |
| 73 | + Ticks: {tickCount} {!isStopped && <span className="text-green-400">(counting...)</span>} |
| 74 | + </div> |
| 75 | + |
| 76 | + <div className="flex gap-3 justify-center mb-6"> |
| 77 | + <Button onClick={decrement} disabled={isStopped} variant="outline" size="lg"> |
| 78 | + -1 |
| 79 | + </Button> |
| 80 | + <Button onClick={increment} disabled={isStopped} variant="outline" size="lg"> |
| 81 | + +1 |
| 82 | + </Button> |
| 83 | + </div> |
| 84 | + |
| 85 | + <div className="flex gap-3 justify-center"> |
| 86 | + <Button |
| 87 | + onClick={stop} |
| 88 | + disabled={isStopped} |
| 89 | + variant="destructive" |
| 90 | + > |
| 91 | + Stop Actor (actor.stop()) |
| 92 | + </Button> |
| 93 | + <Button |
| 94 | + onClick={restart} |
| 95 | + variant="default" |
| 96 | + className="bg-green-700 hover:bg-green-600" |
| 97 | + > |
| 98 | + {isStopped ? "Start Actor" : "Restart Actor"} |
| 99 | + </Button> |
| 100 | + </div> |
| 101 | + </div> |
| 102 | + |
| 103 | + {/* Comparison Box */} |
| 104 | + <div className="grid grid-cols-2 gap-4 mb-6"> |
| 105 | + <div className="bg-green-950 border border-green-800 rounded-lg p-4"> |
| 106 | + <div className="text-green-400 font-bold mb-2">✅ interpret() (Recommended)</div> |
| 107 | + <ul className="text-green-300 text-sm space-y-1"> |
| 108 | + <li>• Automatic cleanup via Scope</li> |
| 109 | + <li>• No risk of memory leaks</li> |
| 110 | + <li>• Works great with atoms</li> |
| 111 | + <li>• Simpler code</li> |
| 112 | + </ul> |
| 113 | + </div> |
| 114 | + <div className="bg-red-950 border border-red-800 rounded-lg p-4"> |
| 115 | + <div className="text-red-400 font-bold mb-2">⚠️ interpretManual() (This demo)</div> |
| 116 | + <ul className="text-red-300 text-sm space-y-1"> |
| 117 | + <li>• ~1.6x faster actor creation</li> |
| 118 | + <li>• YOU must call actor.stop()</li> |
| 119 | + <li>• Risk of memory leaks</li> |
| 120 | + <li>• Complex lifecycle code</li> |
| 121 | + </ul> |
| 122 | + </div> |
| 123 | + </div> |
| 124 | + |
| 125 | + {/* Lifecycle Log */} |
| 126 | + <LogPanel logs={logs} /> |
| 127 | + |
| 128 | + {/* Code Example */} |
| 129 | + <div className="mt-6 bg-gray-950 rounded-lg p-4"> |
| 130 | + <div className="text-gray-400 font-bold mb-2 text-sm">Required Cleanup Pattern:</div> |
| 131 | + <pre className="text-xs text-gray-300 overflow-x-auto"> |
| 132 | +{`// In your React component: |
| 133 | +useEffect(() => { |
| 134 | + initializeActor(); // Create with interpretManual() |
| 135 | + return () => { |
| 136 | + cleanupActor(); // MUST call actor.stop() here! |
| 137 | + }; |
| 138 | +}, []); |
| 139 | +
|
| 140 | +// If you forget cleanupActor(), the actor LEAKS: |
| 141 | +// - Activities keep running |
| 142 | +// - Timers keep firing |
| 143 | +// - Memory never freed`} |
| 144 | + </pre> |
| 145 | + </div> |
| 146 | + </div> |
| 147 | + ); |
| 148 | +}; |
0 commit comments