CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
sagemathinc

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/sync-fs/lib/patch.ts
Views: 687
1
import type { FilesystemState, FilesystemStatePatch } from "./types";
2
3
export function makePatch(
4
s0: FilesystemState,
5
s1: FilesystemState,
6
): FilesystemStatePatch {
7
const patch: FilesystemStatePatch = {};
8
for (const path in s1) {
9
const n = s1[path];
10
if (s0[path] !== n) {
11
patch[path] = n;
12
}
13
}
14
for (const path in s0) {
15
if (s1[path] === undefined) {
16
patch[path] = null;
17
}
18
}
19
return patch;
20
}
21
22
export function applyPatch(s: FilesystemState, patch: FilesystemStatePatch) {
23
const t = { ...s };
24
for (const path in patch) {
25
const v = patch[path];
26
if (v == null) {
27
delete t[path];
28
} else {
29
t[path] = v;
30
}
31
}
32
return t;
33
}
34
35