-
Notifications
You must be signed in to change notification settings - Fork 488
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
10fd6a3
commit 67561a8
Showing
12 changed files
with
288 additions
and
16 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
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,198 @@ | ||
/** | ||
* Copyright Camunda Services GmbH and/or licensed to Camunda Services GmbH | ||
* under one or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information regarding copyright | ||
* ownership. | ||
* | ||
* Camunda licenses this file to you under the MIT; you may not use this file | ||
* except in compliance with the MIT License. | ||
*/ | ||
|
||
/** | ||
* @typedef { { | ||
* uri: string, | ||
* ids: string[], | ||
* linkedIds: string[], | ||
* type: string | ||
* } } File | ||
* | ||
* @typedef { { | ||
* uri: string, | ||
* id: string, | ||
* type: string; | ||
* } } Link | ||
*/ | ||
|
||
const path = require('path'); | ||
|
||
const { toFilePath } = require('./util'); | ||
|
||
/** | ||
* Keeps track of references between files. | ||
*/ | ||
module.exports = class References { | ||
|
||
/** | ||
* @type { Map<string, File> } | ||
*/ | ||
filesByUri = new Map(); | ||
|
||
/** | ||
* @param { import('./logger').default } logger | ||
* @param { import('node:events').EventEmitter } eventBus | ||
*/ | ||
constructor(logger, eventBus) { | ||
this._logger = logger; | ||
this._eventBus = eventBus; | ||
|
||
eventBus.on('indexer:updated', (item) => { | ||
try { | ||
const { | ||
uri, | ||
metadata: { | ||
ids = [], | ||
linkedIds = [], | ||
type | ||
} | ||
} = item; | ||
|
||
this.addFile({ | ||
uri, | ||
ids, | ||
linkedIds, | ||
type | ||
}); | ||
} catch (err) { | ||
this._logger.error('references:failed to process ' + item.uri, err); | ||
} | ||
}); | ||
|
||
eventBus.on('indexer:removed', (item) => { | ||
this.removeFile(item.uri); | ||
}); | ||
|
||
eventBus.on('references:changed', () => { | ||
this._logger.info('references:changed'); | ||
}); | ||
} | ||
|
||
/** | ||
* @internal | ||
*/ | ||
_changed() { | ||
clearTimeout(this._changedTimer); | ||
|
||
this._changedTimer = setTimeout(() => { | ||
this._eventBus.emit('references:changed', Array.from(this.filesByUri.values()).reduce((references, file) => { | ||
return { | ||
...references, | ||
[ file.uri ]: { | ||
linkedFiles: this.findLinkedFiles(file), | ||
linkingFiles: this.findLinkingFiles(file) | ||
} | ||
}; | ||
}, {})); | ||
}, 300); | ||
} | ||
|
||
addFile(file) { | ||
this.filesByUri.set(file.uri, file); | ||
|
||
this._changed(); | ||
} | ||
|
||
removeFile(uri) { | ||
this.filesByUri.delete(uri); | ||
|
||
this._changed(); | ||
} | ||
|
||
/** | ||
* Find linked files. | ||
*/ | ||
findLinkedFiles({ linkedIds, type, uri }) { | ||
if (type === 'processApplication') { | ||
return Array.from(this.filesByUri.values()).filter(file => { | ||
return hasProcessApplication(uri, file.uri); | ||
}); | ||
} | ||
|
||
return linkedIds | ||
.reduce((linkedFiles, { linkedId, type }) => { | ||
const linkedFile = this.findLinkedFile(linkedId, type); | ||
|
||
if (linkedFile) { | ||
return [ | ||
...linkedFiles, | ||
linkedFile | ||
]; | ||
} | ||
|
||
return linkedFiles; | ||
}, []); | ||
} | ||
|
||
/** | ||
* Find linked file. | ||
*/ | ||
findLinkedFile(id, type) { | ||
const linkedFile = Array.from(this.filesByUri.values()) | ||
.filter(file => file.type === type) | ||
.find(file => file.ids.includes(id)); | ||
|
||
if (linkedFile) { | ||
return { | ||
uri: linkedFile.uri, | ||
id, | ||
type | ||
}; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
/** | ||
* Find files linking to file. | ||
*/ | ||
findLinkingFiles({ ids, type, uri }) { | ||
const files = Array.from(this.filesByUri.values()); | ||
|
||
return files | ||
.reduce((acc, file) => { | ||
const linkedId = file.linkedIds.find(linkedId => linkedId.type === type && ids.includes(linkedId.linkedId)); | ||
|
||
if (linkedId) { | ||
return [ | ||
...acc, | ||
{ | ||
elementId: linkedId.elementId, | ||
linkedId: linkedId.linkedId, | ||
type: file.type, | ||
uri: file.uri | ||
} | ||
]; | ||
} | ||
|
||
if (file.type === 'processApplication' && hasProcessApplication(file.uri, uri)) { | ||
return [ | ||
...acc, | ||
{ | ||
type: file.type, | ||
uri: file.uri | ||
} | ||
]; | ||
} | ||
|
||
return acc; | ||
}, []); | ||
} | ||
}; | ||
|
||
function hasProcessApplication(processApplicationUri, fileUri) { | ||
const processApplicationPath = toFilePath(processApplicationUri), | ||
filePath = toFilePath(fileUri); | ||
|
||
const processApplicationDirectory = path.dirname(processApplicationPath); | ||
|
||
return filePath.startsWith(processApplicationDirectory); | ||
} |
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
Oops, something went wrong.