Skip to content

Commit 2dae7bf

Browse files
authored
PATCH /text Runtime Improvements (#429)
* Runtime improvement on the /text amd /bounds routes * remove debug logging * whitespace * whitespace * one user fetch * not a diff * Change from standup, catch errors from the page and project update * Change from standup, catch errors from the page and project update * fix for copilot comment
1 parent 62fd563 commit 2dae7bf

2 files changed

Lines changed: 51 additions & 11 deletions

File tree

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,4 +99,3 @@ For production deployments:
9999
4. Never commit production secrets to the repository
100100

101101
See [CONFIG.md](./CONFIG.md) for detailed deployment instructions.
102-

utilities/shared.js

Lines changed: 51 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -119,21 +119,62 @@ export const updatePageAndProject = async (page, project, userId) => {
119119
if (!project) throw new Error(`Must know project to update Page`)
120120
if (!page) throw new Error(`A Page must be provided to update`)
121121
if (!userId) throw new Error(`Must know user id to update layer`)
122-
page.creator ??= await fetchUserAgent(userId)
123-
// .update() returns a Page prepped for saving to Project
124-
const updatedPage = await page.update()
122+
const agent = await fetchUserAgent(userId)
123+
page.creator ??= agent
124+
let error_out
125125
const layerIndex = project.data.layers.findIndex(l => l.pages.some(p => p.id.split('/').pop() === page.id.split('/').pop()))
126-
if (layerIndex < 0 || layerIndex === undefined || layerIndex === null) throw new Error("Cannot update Page. Its Layer was not found.")
126+
if (layerIndex < 0 || layerIndex === undefined || layerIndex === null) {
127+
error_out = new Error("Cannot update Page. Its Layer was not found.")
128+
error_out.status = 500
129+
throw error_out
130+
}
127131
const layer = project.data.layers[layerIndex]
128132
const pageIndex = layer.pages.findIndex(p => p.id.split('/').pop() === page.id.split('/').pop())
129-
layer.pages[pageIndex] = updatedPage
130-
if (updatedPage.id.startsWith(process.env.RERUMIDPREFIX)) {
131-
// If Page id has changed, we need to update the Layer (and the Project)
133+
134+
// Determine if page will actually be saved to RERUM (same logic as Page.update())
135+
const isAlreadyInRerum = page.id.startsWith(process.env.RERUMIDPREFIX)
136+
const hasContent = page.items?.length > 0
137+
const willBeSavedToRerum = isAlreadyInRerum || hasContent
138+
139+
if (willBeSavedToRerum) {
140+
// Predict the RERUM page ID (same logic as Page.#setRerumId)
141+
const rerumPageId = isAlreadyInRerum
142+
? page.id
143+
: `${process.env.RERUMIDPREFIX}${page.id.split("/").pop()}`
144+
145+
// Create formatted page with predicted ID for layer reference
146+
const formattedPage = {
147+
id: rerumPageId,
148+
label: page.label,
149+
target: page.target,
150+
items: page.items ?? []
151+
}
152+
153+
// Update layer's pages array BEFORE creating Layer
154+
layer.pages[pageIndex] = formattedPage
132155
const updatedLayer = new Layer(project._id, layer)
133-
updatedLayer.creator ??= await fetchUserAgent(userId)
134-
project.data.layers[layerIndex] = await updatedLayer.update()
135-
await recordModification(project, page.id, userId)
156+
updatedLayer.creator ??= agent
157+
158+
try {
159+
const [, finalLayer] = await Promise.all([
160+
page.update(),
161+
updatedLayer.update()
162+
])
163+
project.data.layers[layerIndex] = finalLayer
164+
await recordModification(project, rerumPageId, userId)
165+
} catch (err) {
166+
error_out = new Error(`There was an error updating Page and Project data`)
167+
error_out.status = 500
168+
console.error(`There was an error updating Page and Project data`, err)
169+
throw error_out
170+
}
171+
} else {
172+
// Page won't be saved to RERUM (no content, not already in RERUM)
173+
// Just update local page reference in layer without touching RERUM
174+
const updatedPage = await page.update()
175+
layer.pages[pageIndex] = updatedPage
136176
}
177+
137178
await project.update()
138179
}
139180

0 commit comments

Comments
 (0)