-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix loading performance for pages with many links caused by excessive…
… re-computation (#900) Resolves: rdar://136247329
- Loading branch information
Showing
10 changed files
with
167 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/** | ||
* This source file is part of the Swift.org open source project | ||
* | ||
* Copyright (c) 2024 Apple Inc. and the Swift project authors | ||
* Licensed under Apache License v2.0 with Runtime Library Exception | ||
* | ||
* See https://swift.org/LICENSE.txt for license information | ||
* See https://swift.org/CONTRIBUTORS.txt for Swift project authors | ||
*/ | ||
|
||
import AppStore from 'docc-render/stores/AppStore'; | ||
|
||
const TopicReferenceTypes = new Set([ | ||
'section', | ||
'topic', | ||
]); | ||
|
||
function isFromIncludedArchive(id) { | ||
const { includedArchiveIdentifiers } = AppStore.state; | ||
|
||
// for backwards compatibility purposes, treat all references as being | ||
// from included archives if there is no data for it | ||
if (!includedArchiveIdentifiers.length) { | ||
return true; | ||
} | ||
|
||
return includedArchiveIdentifiers.some(archiveId => ( | ||
id?.startsWith(`doc://${archiveId}/`) | ||
)); | ||
} | ||
|
||
function filterInactiveReferences(references = {}) { | ||
return Object.keys(references).reduce((newRefs, id) => { | ||
const originalRef = references[id]; | ||
const { url, ...refWithoutUrl } = originalRef; | ||
return TopicReferenceTypes.has(originalRef.type) ? ({ | ||
...newRefs, | ||
[id]: isFromIncludedArchive(id) ? originalRef : refWithoutUrl, | ||
}) : ({ | ||
...newRefs, | ||
[id]: originalRef, | ||
}); | ||
}, {}); | ||
} | ||
|
||
// eslint-disable-next-line import/prefer-default-export | ||
export { filterInactiveReferences }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/** | ||
* This source file is part of the Swift.org open source project | ||
* | ||
* Copyright (c) 2024 Apple Inc. and the Swift project authors | ||
* Licensed under Apache License v2.0 with Runtime Library Exception | ||
* | ||
* See https://swift.org/LICENSE.txt for license information | ||
* See https://swift.org/CONTRIBUTORS.txt for Swift project authors | ||
*/ | ||
|
||
import AppStore from 'docc-render/stores/AppStore'; | ||
import { filterInactiveReferences } from 'docc-render/utils/references'; | ||
|
||
const aa = { | ||
identifier: 'doc://A/documentation/A/a', | ||
url: '/documentation/A/a', | ||
title: 'A.A', | ||
type: 'topic', | ||
}; | ||
const ab = { | ||
identifier: 'doc://A/documentation/A/b', | ||
url: '/documentation/A/b', | ||
title: 'A.B', | ||
type: 'topic', | ||
}; | ||
const bb = { | ||
identifier: 'doc://B/documentation/B/b', | ||
url: '/documentation/B/b', | ||
title: 'B.B', | ||
type: 'topic', | ||
}; | ||
const bbb = { | ||
identifier: 'doc://BB/documentation/BB/b', | ||
url: '/documentation/BB/b#b', | ||
title: 'BB.B', | ||
type: 'section', | ||
}; | ||
const c = { | ||
identifier: 'https://abc.dev', | ||
url: 'https://abc.dev', | ||
title: 'C', | ||
type: 'link', | ||
}; | ||
|
||
const references = { | ||
[aa.identifier]: aa, | ||
[ab.identifier]: ab, | ||
[bb.identifier]: bb, | ||
[bbb.identifier]: bbb, | ||
[c.identifier]: c, | ||
}; | ||
|
||
describe('filterInactiveReferences', () => { | ||
it('does not filter any refs when `includedArchiveIdentifiers` is empty', () => { | ||
AppStore.setIncludedArchiveIdentifiers([]); | ||
expect(filterInactiveReferences(references)).toEqual(references); | ||
}); | ||
|
||
it('does not filter any refs when `includedArchiveIdentifiers` includes all ref archives', () => { | ||
AppStore.setIncludedArchiveIdentifiers(['A', 'B', 'BB']); | ||
expect(filterInactiveReferences(references)).toEqual(references); | ||
}); | ||
|
||
it('removes `url` from non-external refs that aren\'t part of included archive', () => { | ||
AppStore.setIncludedArchiveIdentifiers(['B']); | ||
const filteredRefs = filterInactiveReferences(references); | ||
|
||
expect(Object.keys(filteredRefs)).toEqual(Object.keys(references)); | ||
|
||
expect(filteredRefs[aa.identifier].url).toBeFalsy(); | ||
expect(filteredRefs[ab.identifier].url).toBeFalsy(); | ||
expect(filteredRefs[bb.identifier].url).toBe(bb.url); | ||
expect(filteredRefs[bbb.identifier].url).toBeFalsy(); | ||
expect(filteredRefs[c.identifier].url).toBe(c.url); | ||
}); | ||
}); |