Skip to content

Commit

Permalink
Replace custom promiseWithTimeout() with existing functions
Browse files Browse the repository at this point in the history
  • Loading branch information
nstrayer committed Apr 1, 2024
1 parent b829e7f commit 1182b4f
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ import { URI } from 'vs/base/common/uri';
import { Schemas } from 'vs/base/common/network';
import { dirname } from 'vs/base/common/resources';
import { localize } from 'vs/nls';
import { promiseWithTimeout } from '../../common/utils/promiseWithTimeout';
import { CancellationTokenSource } from 'vs/base/common/cancellation';
import { createCancelablePromise, raceTimeout } from 'vs/base/common/async';

/**
* This should match the error message defined in the command definition
Expand Down Expand Up @@ -65,15 +64,15 @@ export function DeferredImage({ src = 'no-source', ...props }: React.ComponentPr

const conversionTimeoutMs = 3000;
const errorTimeoutMs = 1000;
const tokenSource = new CancellationTokenSource();

let delayedErrorMsg: NodeJS.Timeout;

promiseWithTimeout(
const conversionCancellablePromise = createCancelablePromise(() => raceTimeout(
services.commandService.executeCommand('positronNotebookHelpers.convertImageToBase64', src, baseLocation),
conversionTimeoutMs,
tokenSource.token
).then((payload) => {
conversionTimeoutMs
));

conversionCancellablePromise.then((payload) => {
if (typeof payload === 'string') {
setResults({ status: 'success', data: payload });
} else if (isConversionErrorMsg(payload)) {
Expand All @@ -96,7 +95,7 @@ export function DeferredImage({ src = 'no-source', ...props }: React.ComponentPr

return () => {
clearTimeout(delayedErrorMsg);
tokenSource.cancel();
conversionCancellablePromise.cancel();
};
}, [src, baseLocation, services]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ import { DeferredImage } from './DeferredImage';
import { useServices } from 'vs/workbench/contrib/positronNotebook/browser/ServicesProvider';
import { ExternalLink } from 'vs/base/browser/ui/ExternalLink/ExternalLink';
import { localize } from 'vs/nls';
import { promiseWithTimeout } from 'vs/workbench/contrib/positronNotebook/common/utils/promiseWithTimeout';
import { CancellationTokenSource } from 'vs/base/common/cancellation';
import { createCancelablePromise, raceTimeout } from 'vs/base/common/async';

/**
* Component that render markdown content from a string.
Expand Down Expand Up @@ -50,13 +49,12 @@ function useMarkdown(content: string): MarkdownRenderResults {

React.useEffect(() => {

const tokenSource = new CancellationTokenSource();

promiseWithTimeout(
const conversionCancellablePromise = createCancelablePromise(() => raceTimeout(
services.commandService.executeCommand('markdown.api.render', content),
5000,
tokenSource.token
).then((html) => {
));

conversionCancellablePromise.then((html) => {
if (typeof html !== 'string') {
setRenderedHtml({
status: 'error',
Expand All @@ -80,7 +78,7 @@ function useMarkdown(content: string): MarkdownRenderResults {
});
});

return () => tokenSource.cancel();
return () => conversionCancellablePromise.cancel();
}, [content, services]);

return renderedHtml;
Expand Down

This file was deleted.

0 comments on commit 1182b4f

Please sign in to comment.