Skip to content

Commit

Permalink
fix race condition and cleanup code
Browse files Browse the repository at this point in the history
  • Loading branch information
CurryYangxx committed May 13, 2024
1 parent 226d1fb commit b16c98a
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 10 deletions.
1 change: 1 addition & 0 deletions packages/insomnia/src/ui/routes/actions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -737,6 +737,7 @@ export const generateCollectionFromApiSpecAction: ActionFunction = async ({
const spectralRunner = new SpectralRunner();

const results = (await spectralRunner.runDiagnostics({ contents: apiSpec.contents, rulesetPath })).filter(isLintError);
spectralRunner.terminate();
if (apiSpec.contents && results && results.length) {
throw new Error('Error Generating Configuration');
}
Expand Down
9 changes: 4 additions & 5 deletions packages/insomnia/src/ui/routes/design.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -234,20 +234,19 @@ const Design: FC = () => {
message => message.type === 'warning'
);

const runnerRef = useRef<SpectralRunner>();
const spectralRunnerRef = useRef<SpectralRunner>();

const registerCodeMirrorLint = (rulesetPath: string) => {
CodeMirror.registerHelper('lint', 'openapi', async (contents: string) => {
let runner = runnerRef.current;
let runner = spectralRunnerRef.current;

if (!runner) {
runner = new SpectralRunner();
runnerRef.current = runner;
spectralRunnerRef.current = runner;
}

try {
const diagnostics = await runner.runDiagnostics({ contents, rulesetPath });

const lintResult = diagnostics.map(({ severity, code, message, range }) => {
return {
from: CodeMirror.Pos(
Expand Down Expand Up @@ -280,7 +279,7 @@ const Design: FC = () => {
useUnmount(() => {
// delete the helper to avoid it run multiple times when user enter the page next time
CodeMirror.registerHelper('lint', 'openapi', undefined);
runnerRef.current?.terminate();
spectralRunnerRef.current?.terminate();
});

const onCodeEditorChange = useMemo(() => {
Expand Down
2 changes: 1 addition & 1 deletion packages/insomnia/src/ui/worker/spectral.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Spectral } from '@stoplight/spectral-core';
// @ts-expect-error - tsconfig needs to be updated to separate main/renderer code
import { bundleAndLoadRuleset } from '@stoplight/spectral-ruleset-bundler/with-loader';
import { oas } from '@stoplight/spectral-rulesets';
import fs from 'fs/promises';
import fs from 'fs';

interface SpectralRunParams {
contents: string;
Expand Down
7 changes: 3 additions & 4 deletions packages/insomnia/src/ui/worker/spetral-run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,14 @@ export class SpectralRunner {
}

public async runDiagnostics({ contents, rulesetPath }: { contents: string; rulesetPath: string }) {
this.taskId = this.taskId++;
return new Promise<ISpectralDiagnostic[]>((resolve, reject) => {
this.taskId = ++this.taskId;
return new Promise<ISpectralDiagnostic[]>(resolve => {
this.worker.onmessage = e => {
const { id, diagnostics } = e.data;

// onmessage callback will be called several times in one promise, and promise can be resolved or rejected only once, so we cant reject it here
if (id === this.taskId && diagnostics) {
resolve(diagnostics);
} else {
reject(e.data);
}
};

Expand Down

0 comments on commit b16c98a

Please sign in to comment.