Compare commits
3 Commits
feat/e2b
...
fix-editor
Author | SHA1 | Date | |
---|---|---|---|
7dd67f72d8 | |||
5bf264b807 | |||
6f6926a621 |
@ -29,9 +29,7 @@ npm run dev
|
|||||||
|
|
||||||
### Backend
|
### Backend
|
||||||
|
|
||||||
The backend consists of a primary Express and Socket.io server, and 3 Cloudflare Workers microservices for the D1 database, R2 storage, and Workers AI. The D1 database also contains a [service binding](https://developers.cloudflare.com/workers/runtime-apis/bindings/service-bindings/) to the R2 storage worker. Each open sandbox instantiates a secure Linux sandboxes on E2B, which is used for the terminal and live preview.
|
The backend consists of a primary Express and Socket.io server, and 3 Cloudflare Workers microservices for the D1 database, R2 storage, and Workers AI. The D1 database also contains a [service binding](https://developers.cloudflare.com/workers/runtime-apis/bindings/service-bindings/) to the R2 storage worker.
|
||||||
|
|
||||||
You will need to make an account on [E2B](https://e2b.dev/) to get an API key.
|
|
||||||
|
|
||||||
#### Socket.io server
|
#### Socket.io server
|
||||||
|
|
||||||
@ -183,4 +181,3 @@ It should be in the form `category(scope or module): message` in your commit mes
|
|||||||
- [Express](https://expressjs.com/)
|
- [Express](https://expressjs.com/)
|
||||||
- [Socket.io](https://socket.io/)
|
- [Socket.io](https://socket.io/)
|
||||||
- [Drizzle ORM](https://orm.drizzle.team/)
|
- [Drizzle ORM](https://orm.drizzle.team/)
|
||||||
- [E2B](https://e2b.dev/)
|
|
||||||
|
@ -5,4 +5,3 @@ PORT=4000
|
|||||||
WORKERS_KEY=
|
WORKERS_KEY=
|
||||||
DATABASE_WORKER_URL=
|
DATABASE_WORKER_URL=
|
||||||
STORAGE_WORKER_URL=
|
STORAGE_WORKER_URL=
|
||||||
E2B_API_KEY=
|
|
@ -104,6 +104,16 @@ export default function CodeEditor({
|
|||||||
const room = useRoom()
|
const room = useRoom()
|
||||||
const [provider, setProvider] = useState<TypedLiveblocksProvider>()
|
const [provider, setProvider] = useState<TypedLiveblocksProvider>()
|
||||||
const userInfo = useSelf((me) => me.info)
|
const userInfo = useSelf((me) => me.info)
|
||||||
|
|
||||||
|
// Liveblocks providers map to prevent reinitializing providers
|
||||||
|
type ProviderData = {
|
||||||
|
provider: LiveblocksProvider<never, never, never, never>;
|
||||||
|
yDoc: Y.Doc;
|
||||||
|
yText: Y.Text;
|
||||||
|
binding?: MonacoBinding;
|
||||||
|
onSync: (isSynced: boolean) => void;
|
||||||
|
};
|
||||||
|
const providersMap = useRef(new Map<string, ProviderData>());
|
||||||
|
|
||||||
// Refs for libraries / features
|
// Refs for libraries / features
|
||||||
const editorContainerRef = useRef<HTMLDivElement>(null)
|
const editorContainerRef = useRef<HTMLDivElement>(null)
|
||||||
@ -332,43 +342,77 @@ export default function CodeEditor({
|
|||||||
|
|
||||||
if (!editorRef || !tab || !model) return
|
if (!editorRef || !tab || !model) return
|
||||||
|
|
||||||
const yDoc = new Y.Doc()
|
let providerData: ProviderData;
|
||||||
const yText = yDoc.getText(tab.id)
|
|
||||||
const yProvider: any = new LiveblocksProvider(room, yDoc)
|
// When a file is opened for the first time, create a new provider and store in providersMap.
|
||||||
|
if (!providersMap.current.has(tab.id)) {
|
||||||
|
const yDoc = new Y.Doc();
|
||||||
|
const yText = yDoc.getText(tab.id);
|
||||||
|
const yProvider = new LiveblocksProvider(room, yDoc);
|
||||||
|
|
||||||
const onSync = (isSynced: boolean) => {
|
// Inserts the file content into the editor once when the tab is changed.
|
||||||
if (isSynced) {
|
const onSync = (isSynced: boolean) => {
|
||||||
const text = yText.toString()
|
if (isSynced) {
|
||||||
if (text === "") {
|
const text = yText.toString()
|
||||||
if (activeFileContent) {
|
if (text === "") {
|
||||||
yText.insert(0, activeFileContent)
|
if (activeFileContent) {
|
||||||
} else {
|
yText.insert(0, activeFileContent)
|
||||||
setTimeout(() => {
|
} else {
|
||||||
yText.insert(0, editorRef.getValue())
|
setTimeout(() => {
|
||||||
}, 0)
|
yText.insert(0, editorRef.getValue())
|
||||||
|
}, 0)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
yProvider.on("sync", onSync)
|
||||||
|
|
||||||
|
// Save the provider to the map.
|
||||||
|
providerData = { provider: yProvider, yDoc, yText, onSync };
|
||||||
|
providersMap.current.set(tab.id, providerData);
|
||||||
|
|
||||||
|
} else {
|
||||||
|
// When a tab is opened that has been open before, reuse the existing provider.
|
||||||
|
providerData = providersMap.current.get(tab.id)!;
|
||||||
}
|
}
|
||||||
|
|
||||||
yProvider.on("sync", onSync)
|
|
||||||
|
|
||||||
setProvider(yProvider)
|
|
||||||
|
|
||||||
const binding = new MonacoBinding(
|
const binding = new MonacoBinding(
|
||||||
yText,
|
providerData.yText,
|
||||||
model,
|
model,
|
||||||
new Set([editorRef]),
|
new Set([editorRef]),
|
||||||
yProvider.awareness as Awareness
|
providerData.provider.awareness as unknown as Awareness
|
||||||
)
|
);
|
||||||
|
|
||||||
|
providerData.binding = binding;
|
||||||
|
|
||||||
|
setProvider(providerData.provider);
|
||||||
|
|
||||||
return () => {
|
return () => {
|
||||||
yDoc.destroy()
|
// Cleanup logic
|
||||||
yProvider.destroy()
|
if (binding) {
|
||||||
binding.destroy()
|
binding.destroy();
|
||||||
yProvider.off("sync", onSync)
|
}
|
||||||
}
|
if (providerData.binding) {
|
||||||
}, [editorRef, room, activeFileContent])
|
providerData.binding = undefined;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}, [room, activeFileContent]);
|
||||||
|
|
||||||
|
// Added this effect to clean up when the component unmounts
|
||||||
|
useEffect(() => {
|
||||||
|
return () => {
|
||||||
|
// Clean up all providers when the component unmounts
|
||||||
|
providersMap.current.forEach((data) => {
|
||||||
|
if (data.binding) {
|
||||||
|
data.binding.destroy();
|
||||||
|
}
|
||||||
|
data.provider.disconnect();
|
||||||
|
data.yDoc.destroy();
|
||||||
|
});
|
||||||
|
providersMap.current.clear();
|
||||||
|
};
|
||||||
|
}, []);
|
||||||
|
|
||||||
// Connection/disconnection effect
|
// Connection/disconnection effect
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
Reference in New Issue
Block a user