mirror of
https://github.com/outline/outline.git
synced 2026-11-26 07:33:19 +03:00
* fix: Reduce peak memory when duplicating large document trees, stabilize useActionContext identity Two regressions found via automated review of PRs merged in the last 24 hours: - documentDuplicator (#13270) now assigns ids across a whole document tree before writing any duplicates, but the planning pass eagerly loaded every descendant's full content, holding the entire tree's content in memory at once for large trees. Content is now fetched lazily, immediately before each document is written, bounding peak memory to roughly one document at a time. Fixes #13296. - useActionContext returned a freshly spread object on every render when overrides were passed, which DropdownMenu's new isEmpty check (#13292) depends on — this broke memoization and caused every action's visible() callback to be re-resolved on every render of every DropdownMenu on the page, even while closed. The hook now returns a referentially stable object as long as its shallow contents haven't changed. Fixes #13297. * fix: Correct shallowEqual false positive for differing key sets shallowEqual only iterated a's keys and never checked they existed on b, so objects with entirely different keys (e.g. {a: undefined} vs {b: undefined}) could compare equal whenever the mismatched key happened to read back the same value on both sides. Also adds the suggested a === b fast path. Addresses Copilot review comments on #13298. * refactor: Simplify content-loaded check, use es-toolkit's isEqualWith for shallowEqual - Drop the hasContent field/plumbing through DuplicateItem and buildItem; check dataValues.content directly instead (direct property access throws under sequelize-strict-attributes when the column was excluded from the query, so the check has to read dataValues, which bypasses the guard). - Replace the hand-rolled shallowEqual with a one-level-deep comparison built on es-toolkit/compat's isEqualWith, already used elsewhere in the app, instead of maintaining bespoke key-set comparison logic. - Trim comments per review feedback. Addresses review comments from tommoor on #13298. * refactor: Inline isEqualWith directly in useStableValue Drop the standalone shallowEqual wrapper and its dedicated test file; useStableValue now calls es-toolkit/compat's isEqualWith directly. Addresses review feedback from tommoor on #13298. --------- Co-authored-by: Claude <noreply@anthropic.com>
266 lines
7.7 KiB
TypeScript
266 lines
7.7 KiB
TypeScript
import { randomUUID } from "node:crypto";
|
||
import { Op } from "sequelize";
|
||
import { Collection, Document } from "@server/models";
|
||
import { DocumentHelper } from "@server/models/helpers/DocumentHelper";
|
||
import type { DocumentReference } from "@server/models/helpers/ProsemirrorHelper";
|
||
import { ProsemirrorHelper } from "@server/models/helpers/ProsemirrorHelper";
|
||
import type { APIContext } from "@server/types";
|
||
import { generateUrlId } from "@server/utils/url";
|
||
import documentCreator from "./documentCreator";
|
||
|
||
type Props = {
|
||
/** The document to duplicate */
|
||
document: Document;
|
||
/** The collection to add the duplicated document to */
|
||
collection?: Collection | null;
|
||
/** Override of the parent document to add the duplicate to */
|
||
parentDocumentId?: string;
|
||
/** Override of the duplicated document title */
|
||
title?: string;
|
||
/** Override of the duplicated document publish state */
|
||
publish?: boolean;
|
||
/** Whether to duplicate child documents */
|
||
recursive?: boolean;
|
||
};
|
||
|
||
type ManyProps = {
|
||
/** The documents to duplicate, in the order they should be created */
|
||
documents: Document[];
|
||
/** The collection to add the duplicated documents to */
|
||
collection?: Collection | null;
|
||
/** Override of the duplicated documents publish state */
|
||
publish?: boolean;
|
||
/** Whether to duplicate child documents */
|
||
recursive?: boolean;
|
||
};
|
||
|
||
/** A document to duplicate, and where its duplicate should be placed. */
|
||
type Root = {
|
||
/** The document to duplicate */
|
||
document: Document;
|
||
/** Override of the duplicated document title */
|
||
title?: string;
|
||
/** Override of the parent document to add the duplicate to */
|
||
parentDocumentId?: string;
|
||
};
|
||
|
||
/** A document to duplicate, with the identifiers assigned to its duplicate. */
|
||
type DuplicateItem = {
|
||
/** The document being duplicated */
|
||
original: Document;
|
||
/** The id assigned to the duplicate */
|
||
id: string;
|
||
/** The url identifier assigned to the duplicate */
|
||
urlId: string;
|
||
/** The title of the duplicate */
|
||
title: string;
|
||
/** The children to duplicate under the duplicate */
|
||
children: DuplicateItem[];
|
||
};
|
||
|
||
/**
|
||
* Duplicates a document, optionally including its child documents. Links
|
||
* between the duplicated documents are remapped to point at the copies.
|
||
*
|
||
* @param ctx the API context containing the acting user and transaction.
|
||
* @param props the document to duplicate and optional overrides.
|
||
* @returns the duplicated documents.
|
||
*/
|
||
export default async function documentDuplicator(
|
||
ctx: APIContext,
|
||
{ document, collection, parentDocumentId, title, publish, recursive }: Props
|
||
): Promise<Document[]> {
|
||
return duplicateRoots(ctx, {
|
||
roots: [{ document, title, parentDocumentId }],
|
||
collection,
|
||
publish,
|
||
recursive,
|
||
});
|
||
}
|
||
|
||
/**
|
||
* Duplicates several documents as one unit, so that links between them are
|
||
* remapped to the copies no matter which of the documents they cross between.
|
||
*
|
||
* @param ctx the API context containing the acting user and transaction.
|
||
* @param props the documents to duplicate and optional overrides.
|
||
* @returns the duplicated documents.
|
||
*/
|
||
export async function documentsDuplicator(
|
||
ctx: APIContext,
|
||
{ documents, collection, publish, recursive }: ManyProps
|
||
): Promise<Document[]> {
|
||
return duplicateRoots(ctx, {
|
||
roots: documents.map((document) => ({ document })),
|
||
collection,
|
||
publish,
|
||
recursive,
|
||
});
|
||
}
|
||
|
||
async function duplicateRoots(
|
||
ctx: APIContext,
|
||
{
|
||
roots,
|
||
collection,
|
||
publish,
|
||
recursive,
|
||
}: {
|
||
roots: Root[];
|
||
collection?: Collection | null;
|
||
publish?: boolean;
|
||
recursive?: boolean;
|
||
}
|
||
): Promise<Document[]> {
|
||
const newDocuments: Document[] = [];
|
||
const references = new Map<string, DocumentReference>();
|
||
const originalCollections = new Map<string, Collection | null>();
|
||
|
||
async function originalCollectionFor(original: Document) {
|
||
const collectionId = original.collectionId;
|
||
if (!collectionId) {
|
||
return null;
|
||
}
|
||
if (!originalCollections.has(collectionId)) {
|
||
originalCollections.set(
|
||
collectionId,
|
||
await Collection.findByPk(collectionId, {
|
||
attributes: {
|
||
include: ["documentStructure"],
|
||
},
|
||
})
|
||
);
|
||
}
|
||
return originalCollections.get(collectionId) ?? null;
|
||
}
|
||
|
||
async function buildItem(
|
||
original: Document,
|
||
originalCollection: Collection | null,
|
||
titleOverride?: string
|
||
): Promise<DuplicateItem> {
|
||
const id = randomUUID();
|
||
const urlId = generateUrlId();
|
||
const itemTitle = titleOverride ?? original.title;
|
||
|
||
const reference = {
|
||
id,
|
||
path: Document.getPath({ title: itemTitle, urlId }),
|
||
};
|
||
references.set(original.id, reference);
|
||
references.set(original.urlId, reference);
|
||
|
||
return {
|
||
original,
|
||
id,
|
||
urlId,
|
||
title: itemTitle,
|
||
children: recursive
|
||
? await buildChildItems(original, originalCollection)
|
||
: [],
|
||
};
|
||
}
|
||
|
||
async function buildChildItems(
|
||
original: Document,
|
||
originalCollection: Collection | null
|
||
) {
|
||
// Structural fields only – content is re-fetched per document in
|
||
// `duplicateItem`, so a large tree isn't held in memory at once.
|
||
const childDocuments = await original.findChildDocuments(
|
||
{
|
||
archivedAt: original.archivedAt
|
||
? {
|
||
[Op.ne]: null,
|
||
}
|
||
: {
|
||
[Op.eq]: null,
|
||
},
|
||
},
|
||
{
|
||
...ctx,
|
||
attributes: { exclude: ["state", "content", "text"] },
|
||
}
|
||
);
|
||
|
||
const sorted = DocumentHelper.sortDocumentsByStructure(
|
||
childDocuments,
|
||
originalCollection?.getDocumentTree(original.id)?.children ?? []
|
||
).reverse(); // we have to reverse since the child documents will be added in reverse order
|
||
|
||
const items: DuplicateItem[] = [];
|
||
for (const childDocument of sorted) {
|
||
items.push(await buildItem(childDocument, originalCollection));
|
||
}
|
||
return items;
|
||
}
|
||
|
||
async function duplicateItem(
|
||
item: DuplicateItem,
|
||
options: { parentDocumentId?: string; publish: boolean }
|
||
) {
|
||
const original =
|
||
item.original.dataValues.content !== undefined
|
||
? item.original
|
||
: await Document.findByPk(item.original.id, {
|
||
transaction: ctx.state.transaction,
|
||
rejectOnEmpty: true,
|
||
});
|
||
|
||
const duplicated = await documentCreator(ctx, {
|
||
id: item.id,
|
||
urlId: item.urlId,
|
||
parentDocumentId: options.parentDocumentId,
|
||
publish: options.publish,
|
||
collectionId: collection?.id,
|
||
icon: original.icon,
|
||
color: original.color,
|
||
fullWidth: original.fullWidth,
|
||
title: item.title,
|
||
content: ProsemirrorHelper.replaceDocumentReferences(
|
||
ProsemirrorHelper.removeMarks(DocumentHelper.toProsemirror(original), [
|
||
"comment",
|
||
]),
|
||
references
|
||
),
|
||
sourceMetadata: {
|
||
...original.sourceMetadata,
|
||
originalDocumentId: original.id,
|
||
},
|
||
});
|
||
|
||
duplicated.collection = collection ?? null;
|
||
newDocuments.push(duplicated);
|
||
|
||
for (const child of item.children) {
|
||
await duplicateItem(child, {
|
||
...options,
|
||
parentDocumentId: duplicated.id,
|
||
});
|
||
}
|
||
}
|
||
|
||
// The identifiers of every duplicate are assigned before any content is
|
||
// written so that links between the documents being duplicated can be
|
||
// remapped to the copies, leaving the duplicate self-contained.
|
||
const items: DuplicateItem[] = [];
|
||
for (const root of roots) {
|
||
items.push(
|
||
await buildItem(
|
||
root.document,
|
||
await originalCollectionFor(root.document),
|
||
root.title
|
||
)
|
||
);
|
||
}
|
||
|
||
for (const [index, item] of items.entries()) {
|
||
await duplicateItem(item, {
|
||
parentDocumentId: roots[index].parentDocumentId,
|
||
publish: publish ?? !!roots[index].document.publishedAt,
|
||
});
|
||
}
|
||
|
||
return newDocuments;
|
||
}
|