Skip to content

Commit

Permalink
minor refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
tlylt committed Aug 21, 2023
1 parent d0ff17d commit e7d5a9e
Showing 1 changed file with 27 additions and 21 deletions.
48 changes: 27 additions & 21 deletions packages/core/src/plugins/default/markbind-plugin-plantuml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,43 +25,49 @@ const LockManager = require('../../utils/LockManager');

const JAR_PATH = path.resolve(__dirname, 'plantuml.jar');

// On first generation, all the diagrams will be added to the map, and whenever a diagram is generated,
// it will be marked as fresh.
// Upon editing a PUML file or a non-PUML file that triggers a hot reload on the site,
// the stale diagram will be removed from the map, and the map will be reset to all stale.
// If the diagram is inside the map, the generateDiagram function will not regenerate the diagram.
// This is to prevent generating the same diagram twice.
/**
* On first generation, all the diagrams will be added to the map.
* Whenever a diagram is generated, it will be marked as fresh.
* Upon editing a PUML file or a non-PUML file that triggers a hot reload on the site,
* the stale diagram will (later) be removed from the map, and the map will be reset to all stale.
* If the diagram is inside the map, the generateDiagram function will not regenerate the diagram.
* This is to prevent generating the same diagram twice.
*/
const processedDiagrams = new Map<string, DiagramStatus>();

// Remove diagrams that are no longer used in the tracking hash map
function clearDeadDiagrams(diagrams: Map<string, DiagramStatus>) {
/**
* Removes all stale diagrams from the `processedDiagrams` map and deletes the files
*/
function clearStaleDiagrams(diagrams: Map<string, DiagramStatus>) {
Array.from(diagrams.entries())
.filter(([, value]) => !value.isFresh)
.forEach(([key, value]) => {

Check warning on line 44 in packages/core/src/plugins/default/markbind-plugin-plantuml.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/plugins/default/markbind-plugin-plantuml.ts#L41-L44

Added lines #L41 - L44 were not covered by tests
if (fs.existsSync(value.filePath)) {
fs.unlink(value.filePath, (err) => {
if (err) logger.error(`Error generating ${value.filePath}`);
});
try {
fs.unlinkSync(value.filePath);

Check warning on line 47 in packages/core/src/plugins/default/markbind-plugin-plantuml.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/plugins/default/markbind-plugin-plantuml.ts#L46-L47

Added lines #L46 - L47 were not covered by tests
} catch (error) {
logger.error(`Error deleting ${value.filePath}: ${error}`);

Check warning on line 49 in packages/core/src/plugins/default/markbind-plugin-plantuml.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/plugins/default/markbind-plugin-plantuml.ts#L49

Added line #L49 was not covered by tests
}
}
diagrams.delete(key);

Check warning on line 52 in packages/core/src/plugins/default/markbind-plugin-plantuml.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/plugins/default/markbind-plugin-plantuml.ts#L52

Added line #L52 was not covered by tests
});
}

// Set all diagrams to be stale (isFresh = false)
function initDiagrams(diagrams: Map<string, DiagramStatus>) {
/**
* Performs cleanup before site generation.
* Deletes all known stale diagrams (isFresh = false)
* and prepares the map for the next site generation/hot reload.
*/
function cleanDiagrams(diagrams: Map<string, DiagramStatus>) {
clearStaleDiagrams(diagrams);

Check warning on line 62 in packages/core/src/plugins/default/markbind-plugin-plantuml.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/plugins/default/markbind-plugin-plantuml.ts#L61-L62

Added lines #L61 - L62 were not covered by tests

// Set all existing diagrams to be stale.
// If they are not edited or deleted, they will be marked as fresh in `generateDiagram`.
Array.from(diagrams.values()).forEach((value) => {
value.isFresh = false;

Check warning on line 67 in packages/core/src/plugins/default/markbind-plugin-plantuml.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/plugins/default/markbind-plugin-plantuml.ts#L66-L67

Added lines #L66 - L67 were not covered by tests
});
}

// Clear all stale diagrams (isFresh = false) and set all diagrams to be stale
// This is called before site generation
// The fresh diagrams will be marked as isFresh during site generation
function cleanDiagrams(diagrams: Map<string, DiagramStatus>) {
clearDeadDiagrams(diagrams);
initDiagrams(diagrams);
}

let graphvizCheckCompleted = false;

/**
Expand Down

0 comments on commit e7d5a9e

Please sign in to comment.