Skip to content

Commit ce775b3

Browse files
committed
fix: add summary patching
1 parent 4cd038f commit ce775b3

File tree

4 files changed

+26
-6
lines changed

4 files changed

+26
-6
lines changed

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
"udt-collaboration-server": "./bin/cli.js"
99
},
1010
"scripts": {
11-
"start": "micro-dev ./app.js",
12-
"start:prod": "micro-dev ./app.js",
11+
"start": "micro-dev --watch src ./app.js",
12+
"start:prod": "micro ./app.js",
1313
"test": " npm run test:ava && npm run lint && npm run test:prettier",
1414
"prettier": "prettier --write \"src/**/*.js\"",
1515
"test:prettier": "prettier --check \"src/**/*.js\"",
@@ -26,7 +26,8 @@
2626
"micro-dev": "^3.0.0",
2727
"micro-query": "^0.3.0",
2828
"microrouter": "^3.1.3",
29-
"randomstring": "^1.1.5"
29+
"randomstring": "^1.1.5",
30+
"rfc6902": "^4.0.1"
3031
},
3132
"devDependencies": {
3233
"@semantic-release/git": "^9.0.0",

src/utils/apply-patch.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const applySamplePatch = require("./apply-sample-patch")
22
const applyAddSamplePatch = require("./apply-add-sample-patch")
33
const applySummaryPatch = require("./apply-summary-patch")
4+
const rfc6902 = require("rfc6902")
45

56
module.exports = async ({ db, patch: patches, sessionId, userName = null }) => {
67
const session = db
@@ -9,10 +10,11 @@ module.exports = async ({ db, patch: patches, sessionId, userName = null }) => {
910

1011
if (!session) throw new Error("Session Not Found")
1112

13+
const originalSummaryObject = JSON.parse(session.summary_object)
1214
const workingSummaryObject = JSON.parse(session.summary_object)
1315

1416
for (const patch of patches) {
15-
if (patch.path === "/samples/-" && patch.op === "add") {
17+
if (patch.path.startsWith("/samples/") && patch.op === "add") {
1618
await applyAddSamplePatch({ db, patch, sessionId, workingSummaryObject })
1719
} else if (patch.path.startsWith("/samples/")) {
1820
await applySamplePatch({ db, patch, sessionId, workingSummaryObject })
@@ -21,6 +23,13 @@ module.exports = async ({ db, patch: patches, sessionId, userName = null }) => {
2123
}
2224
}
2325

26+
// Create a summary patch
27+
const summaryPatch = rfc6902.createPatch(
28+
originalSummaryObject,
29+
workingSummaryObject
30+
)
31+
patches.push(...summaryPatch)
32+
2433
// Commit new summary / session_state
2534
db.prepare(
2635
`

src/utils/apply-sample-patch.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,18 @@ module.exports = async ({ db, patch, sessionId, workingSummaryObject }) => {
99
if (!m[1]) throw new Error(`Invalid sample patch to path "${patch.path}"`)
1010

1111
let { sampleIndex, sampleId } = getSampleIndexAndId(sessionId, m[1])
12-
const sampleChangePath = m[2]
12+
let sampleChangePath = m[2]
13+
14+
if (patch.op === "replace" && !sampleChangePath) {
15+
sampleChangePath = ""
16+
}
1317

1418
if (!sampleChangePath && patch.op === "remove") {
1519
applyRemoveSample({ sessionId, db, sampleId, workingSummaryObject })
1620
return
1721
}
18-
if (!sampleChangePath) {
22+
23+
if (!sampleChangePath && typeof sampleChangePath !== "string") {
1924
throw new Error(`Unusual PATCH: ${JSON.stringify(patch)}`)
2025
}
2126

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3264,6 +3264,11 @@ reusify@^1.0.4:
32643264
resolved "https://registry.yarnpkg.com/reusify/-/reusify-1.0.4.tgz#90da382b1e126efc02146e90845a88db12925d76"
32653265
integrity sha512-U9nH88a3fc/ekCF1l0/UP1IosiuIjyTh7hBvXVMHYgVcfGvt897Xguj2UOLDeI5BG2m7/uwyaLVT6fbtCwTyzw==
32663266

3267+
rfc6902@^4.0.1:
3268+
version "4.0.1"
3269+
resolved "https://registry.yarnpkg.com/rfc6902/-/rfc6902-4.0.1.tgz#1a6474e1e9c17a3f812fabc68cb3df9d7e46cc34"
3270+
integrity sha512-RZOewbzE7AGAAPxxoTg137y4rCzA9w+5F9O+j+JdwnXH9uNNIpDPf8eH4sIViqgjfvOak+nlBRiy17TPDn+PLA==
3271+
32673272
32683273
version "2.6.3"
32693274
resolved "https://registry.yarnpkg.com/rimraf/-/rimraf-2.6.3.tgz#b2d104fe0d8fb27cf9e0a1cda8262dd3833c6cab"

0 commit comments

Comments
 (0)