-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.tsx
More file actions
178 lines (156 loc) · 4.8 KB
/
main.tsx
File metadata and controls
178 lines (156 loc) · 4.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/**
* Application Entry Point - Outliving React
*
* This example demonstrates that the system outlives React.
* We can unmount and remount React, and the system continues running.
*/
import { StrictMode } from "react";
import { createRoot, type Root } from "react-dom/client";
import { startSystem, haltSystem } from "braided";
import { systemConfig } from "./system";
import { SystemProvider } from "./system";
import { App } from "./App";
import type { StartedSystem } from "braided";
import React from "react";
let reactRoot: Root | null = null;
let system: StartedSystem<typeof systemConfig> | null = null;
/**
* Mount React with the system
*/
function mountReact() {
if (reactRoot || !system) return;
console.log("⚛️ Mounting React...");
const rootElement = document.getElementById("root")!;
reactRoot = createRoot(rootElement);
// Note that in this scenario, we are using the SystemProvider to provide the system to the app.
// This is not mandatory, in general, resources can be accessed directly when using a singleton manager.
// In here, we are using it to demonstrate the pattern.
reactRoot.render(
<StrictMode>
<SystemProvider system={system}>
<App />
</SystemProvider>
</StrictMode>
);
console.log("✅ React mounted");
updateControls();
}
/**
* Unmount React (system continues running!)
*/
function unmountReact() {
if (!reactRoot) return;
console.log("🔴 Unmounting React...");
reactRoot.unmount();
reactRoot = null;
// Clear the root element
const rootElement = document.getElementById("root")!;
rootElement.innerHTML = "";
console.log("✅ React unmounted (system still running!)");
updateControls();
}
/**
* Update control panel
*/
function updateControls() {
const mountBtn = document.getElementById("mount-btn") as HTMLButtonElement;
const unmountBtn = document.getElementById(
"unmount-btn"
) as HTMLButtonElement;
const statusText = document.getElementById("status-text")!;
if (reactRoot) {
mountBtn.disabled = true;
unmountBtn.disabled = false;
statusText.textContent = "🟢 React is mounted";
statusText.style.color = "#10b981";
} else {
mountBtn.disabled = false;
unmountBtn.disabled = true;
statusText.textContent = "🔴 React is unmounted (system still running!)";
statusText.style.color = "#ef4444";
}
}
/**
* Start the system before React
*/
async function main() {
console.log("🚀 Starting system...");
const result = await startSystem(systemConfig);
system = result.system;
if (result.errors.size > 0) {
console.error("❌ System startup errors:", result.errors);
} else {
console.log("✅ System started successfully");
}
// Create control panel
const controlPanel = document.createElement("div");
controlPanel.innerHTML = `
<div style="
position: fixed;
top: 20px;
right: 20px;
padding: 20px;
background: white;
border: 2px solid #3b82f6;
border-radius: 8px;
box-shadow: 0 4px 6px rgba(0,0,0,0.1);
z-index: 1000;
min-width: 250px;
">
<h3 style="margin: 0 0 10px 0; color: #3b82f6;">Control Panel</h3>
<p id="status-text" style="margin: 0 0 15px 0; font-weight: bold;"></p>
<div style="display: flex; flex-direction: column; gap: 10px;">
<button id="mount-btn" style="
padding: 10px;
font-size: 14px;
cursor: pointer;
background: #10b981;
color: white;
border: none;
border-radius: 4px;
">Mount React</button>
<button id="unmount-btn" style="
padding: 10px;
font-size: 14px;
cursor: pointer;
background: #ef4444;
color: white;
border: none;
border-radius: 4px;
">Unmount React</button>
</div>
<p style="
margin: 15px 0 0 0;
font-size: 12px;
color: #6b7280;
">
Try unmounting and remounting React. The system (session, data, background task) continues running!
</p>
</div>
`;
document.body.appendChild(controlPanel);
// Wire up buttons
document.getElementById("mount-btn")!.addEventListener("click", mountReact);
document
.getElementById("unmount-btn")!
.addEventListener("click", unmountReact);
// Mount React initially
mountReact();
// Cleanup on page unload
window.addEventListener("beforeunload", async () => {
console.log("🛑 Page unloading, halting system...");
if (system) {
await haltSystem(systemConfig, system);
}
console.log("✅ System halted");
});
}
main().catch((error) => {
console.error("💥 Fatal error:", error);
document.getElementById("root")!.innerHTML = `
<div style="padding: 40px; color: red;">
<h1>Failed to start application</h1>
<pre>${error.message}</pre>
</div>
`;
});