-
Notifications
You must be signed in to change notification settings - Fork 140
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16951 from Nexus-Mods/collections-improvements
Collections improvements
- Loading branch information
Showing
9 changed files
with
232 additions
and
137 deletions.
There are no files selected for viewing
237 changes: 132 additions & 105 deletions
237
src/extensions/download_management/DownloadManager.ts
Large diffs are not rendered by default.
Oops, something went wrong.
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
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 |
---|---|---|
@@ -1,23 +1,36 @@ | ||
import { AttributeExtractor } from '../../../types/IExtensionContext'; | ||
|
||
import Promise from 'bluebird'; | ||
import * as _ from 'lodash'; | ||
import { log } from '../../../util/log'; | ||
|
||
const attributeExtractors: Array<{ priority: number, extractor: AttributeExtractor}> = []; | ||
|
||
export function registerAttributeExtractor(priority: number, extractor: AttributeExtractor) { | ||
attributeExtractors.push({ priority, extractor }); | ||
attributeExtractors.sort((lhs, rhs) => rhs.priority - lhs.priority); | ||
} | ||
|
||
function filterUndefined(input: { [key: string]: any }) { | ||
return _.omitBy(input, val => val === undefined); | ||
return Object.fromEntries(Object.entries(input).filter(([_, val]) => val !== undefined)); | ||
} | ||
|
||
// Every mod installation is run through the attributeExtractors in order of priority. | ||
// Imagine the simplest use case where installing a collection with 1000 mods - and one extractor takes over 1.5 seconds to run, | ||
// that's at a minimum 25 minutes of waiting for the user. Keep in mind that incorrect usage of the attributeExtractors in community | ||
// extensions will raise this time even further. This is why we have a timeout of 1 second for each extractor. All core extractors | ||
// should never take more than a few milliseconds to run. | ||
function extractorOrSkip(extractor: AttributeExtractor, input: any, modPath: string): Promise<any> { | ||
return Promise.race([ | ||
extractor(input, modPath), | ||
new Promise((_, reject) => setTimeout(() => reject(new Error('Extractor timed out')), 1000)) | ||
]).catch(err => { | ||
log('error', `Extractor skipped: "${extractor.name ?? extractor.toString()}" - ${err.message}`); | ||
return {}; | ||
}); | ||
} | ||
|
||
function filterModInfo(input: any, modPath: string): Promise<any> { | ||
return Promise.map( | ||
attributeExtractors.sort((lhs, rhs) => rhs.priority - lhs.priority), | ||
extractor => extractor.extractor(input, modPath), | ||
).then(infoBlobs => Object.assign({}, ...infoBlobs.map(filterUndefined))); | ||
return Promise.all(attributeExtractors.map(extractor => extractorOrSkip(extractor.extractor, input, modPath))) | ||
.then(infoBlobs => Object.assign({}, ...infoBlobs.map(filterUndefined))); | ||
} | ||
|
||
export default filterModInfo; |
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