Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Website: Implements source=latest-dev and source=latest #4139

Merged
merged 1 commit into from
Aug 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions website/src/website/components/Loader.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import * as React from "react";
export class Loader<T> extends React.Component<
{ children: (value: T) => React.ReactChild; loader: () => Promise<T> },
{ value: T | undefined; hasValue: boolean }
> {
constructor(props: any) {
super(props);
this.state = { value: undefined, hasValue: false };
if (!this.state.value) {
this.props.loader().then((value) => {
this.setState({
hasValue: true,
value,
});
});
}
}

render() {
if (!this.state.hasValue) {
return null;
}
return this.props.children(this.state.value!);
}
}

/**
* Decorates a component so that it only gets mounted when monaco is loaded.
*/
export function withLoader(
loader: () => Promise<void>
): <TProps>(
Component: React.FunctionComponent<TProps> | React.ComponentClass<TProps>
) => any {
return (Component) => {
return (props: any) => (
<Loader loader={loader}>{() => <Component {...props} />}</Loader>
);
};
}
1 change: 1 addition & 0 deletions website/src/website/components/monaco/MonacoLoader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export class MonacoLoader extends React.Component<
return this.props.children(this.state.monaco);
}
}

/**
* Decorates a component so that it only gets mounted when monaco is loaded.
*/
Expand Down
14 changes: 14 additions & 0 deletions website/src/website/pages/playground/PlaygroundModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -666,6 +666,20 @@ class Source {
sourceStr: string | undefined,
sourceLanguagesStr: string | undefined
): Source {
if (sourceStr === "latest-dev") {
// The versions are already loaded
const versions = getNpmVersionsSync(undefined);
const version = versions.find((v) => v.indexOf("-dev-") !== -1);
return new Source(version, undefined, sourceLanguagesStr);
}
if (sourceStr === "latest") {
return new Source(
monacoEditorVersion,
undefined,
sourceLanguagesStr
);
}

if (sourceStr && sourceStr.startsWith("v")) {
return new Source(
sourceStr.substring(1),
Expand Down
11 changes: 11 additions & 0 deletions website/src/website/pages/playground/PlaygroundPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,18 @@ import * as React from "react";
import { hotComponent } from "../../utils/hotComponent";
import { PlaygroundModel } from "./PlaygroundModel";
import { PlaygroundPageContent } from "./PlaygroundPageContent";
import { withLoader } from "../../components/Loader";
import { getNpmVersions } from "./getNpmVersionsSync";

@withLoader(async () => {
if (
new URLSearchParams(window.location.search).get("source") ===
"latest-dev"
) {
// So that the source class can resolve that value
await getNpmVersions();
}
})
@hotComponent(module)
@observer
export class PlaygroundPage extends React.Component {
Expand Down
7 changes: 6 additions & 1 deletion website/src/website/pages/playground/getNpmVersionsSync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export function getNpmVersionsSync(
currentVersion: string | undefined
): string[] {
if (!npmVersionsObservable) {
npmVersionsObservable = new ObservablePromise(getNpmVersions());
npmVersionsObservable = new ObservablePromise(loadNpmVersions());
}
return (
npmVersionsObservable.value || (currentVersion ? [currentVersion] : [])
Expand All @@ -16,6 +16,11 @@ export function getNpmVersionsSync(
let npmVersionsPromise: Promise<string[]> | undefined;

export async function getNpmVersions(): Promise<string[]> {
getNpmVersionsSync(undefined);
return npmVersionsPromise!;
}

async function loadNpmVersions(): Promise<string[]> {
if (npmVersionsPromise === undefined) {
npmVersionsPromise = _getNpmVersions();
}
Expand Down