Skip to content

Commit

Permalink
move preload to Editor service
Browse files Browse the repository at this point in the history
  • Loading branch information
tim-smart committed May 14, 2024
1 parent 17dfdef commit 773d572
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 14 deletions.
15 changes: 3 additions & 12 deletions src/CodeEditor/rx/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,10 @@ export const editorRx = Rx.family((workspace: Workspace) => {
workspaceHandleRx(workspace)
)
const el = yield* get.some(element)
const { monaco } = yield* Monaco
const { makeEditorWithATA } = yield* MonacoATA
const editor = yield* makeEditorWithATA(el)
const monaco = yield* MonacoATA
const editor = yield* monaco.makeEditorWithATA(el)

yield* Effect.forEach(workspace.filePaths, ([file, path]) => {
if (file.language === "typescript") {
const uri = monaco.Uri.parse(path)
if (monaco.editor.getModel(uri) === null) {
monaco.editor.createModel(file.initialContent, file.language, uri)
}
}
return Effect.void
})
yield* editor.preload(workspace)

get.subscribe(
editorThemeRx,
Expand Down
22 changes: 20 additions & 2 deletions src/CodeEditor/services/Monaco.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { File } from "@/domain/Workspace"
import { File, Workspace } from "@/domain/Workspace"
import { Data, Effect, GlobalValue, Layer, Stream } from "effect"
import * as monaco from "monaco-editor/esm/vs/editor/editor.api"

Expand Down Expand Up @@ -97,14 +97,32 @@ const make = Effect.gen(function* () {
return model
})

const preload = (workspace: Workspace) =>
Effect.forEach(
workspace.filePaths,
([file, path]) =>
Effect.sync(() => {
const uri = monaco.Uri.parse(path)
if (monaco.editor.getModel(uri)) {
return
}
monaco.editor.createModel(
file.initialContent,
file.language,
uri
)
}),
{ discard: true }
)

const content = Stream.async<string>((emit) => {
const cancel = editor.onDidChangeModelContent(() => {
emit.single(editor.getValue())
})
return Effect.sync(() => cancel.dispose())
})

return { editor, load, content, theme } as const
return { editor, load, preload, content, theme } as const
})

function listen<A>(event: monaco.IEvent<A>) {
Expand Down

0 comments on commit 773d572

Please sign in to comment.