Skip to content

Commit 417addc

Browse files
committed
reduce duplicate writes from editor
1 parent 02ea6f1 commit 417addc

1 file changed

Lines changed: 16 additions & 4 deletions

File tree

src/CodeEditor/rx/editor.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,26 +32,38 @@ export const editorRx = Rx.family((workspace: Workspace) => {
3232
{ immediate: true }
3333
)
3434

35+
const prevContent = new Map<string, string>()
36+
const write = (path: string, content: string) =>
37+
Effect.gen(function* (_) {
38+
const prev = prevContent.get(path)
39+
if (prev === content) return
40+
prevContent.set(path, content)
41+
yield* handle.write(path, content)
42+
})
43+
3544
const save = Effect.suspend(() => {
3645
const file = get.once(selectedFile)
3746
const path = workspace.pathTo(file)
3847
if (path._tag === "None") return Effect.void
39-
return handle.write(path.value, editor.editor.getValue())
48+
return write(path.value, editor.editor.getValue())
4049
})
4150

4251
const sync = (fullPath: FullPath, path: string, file: File) =>
4352
content(path, file).pipe(
44-
Stream.tap((content) => editor.load(fullPath, file, content)),
53+
Stream.tap((content) => {
54+
prevContent.set(path, content)
55+
return editor.load(fullPath, file, content)
56+
}),
4557
Stream.flatMap((_) => editor.content.pipe(Stream.drop(1)), {
4658
switch: true
4759
}),
4860
Stream.debounce("2 seconds"),
49-
Stream.tap((content) => handle.write(path, content)),
61+
Stream.tap((content) => write(path, content)),
5062
Stream.ensuring(
5163
Effect.suspend(() => {
5264
const content = editor.editor.getValue()
5365
if (!content.trim()) return Effect.void
54-
return handle.write(path, content)
66+
return write(path, content)
5567
})
5668
)
5769
)

0 commit comments

Comments
 (0)