Skip to content

Commit

Permalink
Update to LockManager
Browse files Browse the repository at this point in the history
  • Loading branch information
SPWwj committed Jul 31, 2023
1 parent 16f3edd commit 6e7cddb
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 62 deletions.
12 changes: 9 additions & 3 deletions packages/core/src/plugins/default/markbind-plugin-plantuml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import * as urlUtil from '../../utils/urlUtil';
import { PluginContext } from '../Plugin';
import { NodeProcessorConfig } from '../../html/NodeProcessor';
import { MbNode } from '../../utils/node';
import { createLockFile, deleteLockFile } from '../../utils/lockFileManager';
const LockManager = require('../../utils/LockManager');

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

Expand All @@ -29,8 +29,13 @@ let graphvizCheckCompleted = false;
* @param content puml dsl used to generate the puml diagram
*/
function generateDiagram(imageOutputPath: string, content: string) {
const lockId = LockManager.createLock();

// Avoid generating twice
if (processedDiagrams.has(imageOutputPath)) { return; }
if (processedDiagrams.has(imageOutputPath)) {
LockManager.deleteLock(lockId);
return;
}
processedDiagrams.add(imageOutputPath);

// Creates output dir if it doesn't exist
Expand All @@ -49,6 +54,7 @@ function generateDiagram(imageOutputPath: string, content: string) {
logger.error(`Error generating ${imageOutputPath}`);
}
deleteLockFile(lockFileName);
LockManager.deleteLock(lockId);
});

childProcess.stderr?.on('data', (errorMsg) => {
Expand All @@ -59,7 +65,7 @@ function generateDiagram(imageOutputPath: string, content: string) {
// This goes to the log file, but not shown on the console
logger.debug(errorLog);
// Delete the lock file after generating the diagram
deleteLockFile(lockFileName);
LockManager.deleteLock(lockId);
});
}

Expand Down
48 changes: 48 additions & 0 deletions packages/core/src/utils/LockManager.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { v4 as uuidv4 } from 'uuid';

class LockManager {
private static _instance: LockManager;
private locks: Map<string, boolean>;

private constructor() {
this.locks = new Map();
}

public static get instance() {
if (!LockManager._instance) {
LockManager._instance = new LockManager();
}

return LockManager._instance;
}

createLock(): string {
const lockId = uuidv4();
this.locks.set(lockId, true);
return lockId;
}

deleteLock(lockId: string): void {
this.locks.delete(lockId);
}

deleteAllLocks(): void {
this.locks.clear();
}

waitForLockRelease(): Promise<void> {
return new Promise((resolve) => {
const checkLocks = () => {
if (this.locks.size === 0) {
this.deleteAllLocks();
resolve();
} else {
setTimeout(checkLocks, 100);
}
};
checkLocks();
});
}
}

export = LockManager.instance;
59 changes: 0 additions & 59 deletions packages/core/src/utils/lockFileManager.ts

This file was deleted.

0 comments on commit 6e7cddb

Please sign in to comment.