Skip to content

Commit

Permalink
Add pending widget methods, reduce breakage
Browse files Browse the repository at this point in the history
Signed-off-by: Colin Grant <colin.grant@ericsson.com>
  • Loading branch information
Colin Grant committed Jun 7, 2021
1 parent 25d0708 commit bf7e2c0
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 11 deletions.
18 changes: 16 additions & 2 deletions packages/core/src/browser/widget-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,16 +165,30 @@ export class WidgetManager {
return undefined;
}

/**
* Try to get the existing widget for the given description.
* @param factoryId The widget factory id.
* @param options The widget factory specific information.
*
* @returns A promise that resolves to the widget, if any exists. The promise may be pending, so be cautious when assuming that it will not reject.
*/
tryGetPendingWidget<T extends Widget>(factoryId: string, options?: any): MaybePromise<T> | undefined {
const key = this.toKey({ factoryId, options });
return this.doGetWidget(key);
}

/**
* Get the widget for the given description.
* @param factoryId The widget factory id.
* @param options The widget factory specific information.
*
* @returns a promise resolving to the widget if available, else `undefined`.
*/
getWidget<T extends Widget>(factoryId: string, options?: any): MaybePromise<T> | undefined {
async getWidget<T extends Widget>(factoryId: string, options?: any): Promise<T | undefined> {
const key = this.toKey({ factoryId, options });
return this.doGetWidget<T>(key);
const pendingWidget = this.doGetWidget<T>(key);
const widget = pendingWidget && await pendingWidget;
return widget;
}

protected doGetWidget<T extends Widget>(key: string): MaybePromise<T> | undefined {
Expand Down
9 changes: 7 additions & 2 deletions packages/core/src/browser/widget-open-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ export abstract class WidgetOpenHandler<W extends BaseWidget> implements OpenHan
*
* @returns a promise that resolves to the existing widget or `undefined` if no widget for the given uri exists.
*/
getByUri(uri: URI): MaybePromise<W> | undefined {
getByUri(uri: URI): Promise<W | undefined> {
return this.getWidget(uri);
}

Expand All @@ -136,7 +136,12 @@ export abstract class WidgetOpenHandler<W extends BaseWidget> implements OpenHan
return this.widgetManager.getWidgets(this.id) as W[];
}

protected getWidget(uri: URI, options?: WidgetOpenerOptions): MaybePromise<W> | undefined {
protected tryGetPendingWidget(uri: URI, options?: WidgetOpenerOptions): MaybePromise<W> | undefined {
const factoryOptions = this.createWidgetOptions(uri, options);
return this.widgetManager.tryGetPendingWidget(this.id, factoryOptions);
}

protected getWidget(uri: URI, options?: WidgetOpenerOptions): Promise<W | undefined> {
const widgetOptions = this.createWidgetOptions(uri, options);
return this.widgetManager.getWidget<W>(this.id, widgetOptions);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/********************************************************************************
* Copyright (C) 2018 Google and others.
* Copyright (C) 2018-2021 Google and others.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0 which is available at
Expand Down
10 changes: 7 additions & 3 deletions packages/editor-preview/src/browser/editor-preview-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,16 @@ export class EditorPreviewManager extends EditorManager {
this.toDisposeOnPreviewChange.push(widget.onDidDispose(() => this.toDisposeOnPreviewChange.dispose()));
}

protected getWidget(uri: URI, options?: EditorOpenerOptions): MaybePromise<EditorWidget> | undefined {
return super.getWidget(uri, { ...options, preview: true }) ?? super.getWidget(uri, { ...options, preview: false });
protected tryGetPendingWidget(uri: URI, options?: EditorOpenerOptions): MaybePromise<EditorWidget> | undefined {
return super.tryGetPendingWidget(uri, { ...options, preview: true }) ?? super.tryGetPendingWidget(uri, { ...options, preview: false });
}

protected async getWidget(uri: URI, options?: EditorOpenerOptions): Promise<EditorWidget | undefined> {
return (await super.getWidget(uri, { ...options, preview: true })) ?? super.getWidget(uri, { ...options, preview: false });
}

protected async getOrCreateWidget(uri: URI, options?: EditorOpenerOptions): Promise<EditorWidget> {
return this.getWidget(uri, options) ?? super.getOrCreateWidget(uri, options);
return this.tryGetPendingWidget(uri, options) ?? super.getOrCreateWidget(uri, options);
}

protected createWidgetOptions(uri: URI, options?: EditorOpenerOptions): EditorPreviewOptions {
Expand Down
15 changes: 12 additions & 3 deletions packages/editor/src/browser/editor-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,16 +82,16 @@ export class EditorManager extends NavigatableWidgetOpenHandler<EditorWidget> {
this.updateCurrentEditor();
}

getByUri(uri: URI, options?: EditorOpenerOptions): MaybePromise<EditorWidget> | undefined {
getByUri(uri: URI, options?: EditorOpenerOptions): Promise<EditorWidget | undefined> {
return this.getWidget(uri, options);
}

getOrCreateByUri(uri: URI, options?: EditorOpenerOptions): Promise<EditorWidget> {
return this.getOrCreateWidget(uri, options);
}

protected getWidget(uri: URI, options?: EditorOpenerOptions): MaybePromise<EditorWidget> | undefined {
const editorPromise = super.getWidget(uri, options);
protected tryGetPendingWidget(uri: URI, options?: EditorOpenerOptions): MaybePromise<EditorWidget> | undefined {
const editorPromise = super.tryGetPendingWidget(uri, options);
if (editorPromise) {
// Reveal selection before attachment to manage nav stack. (https://github.com/eclipse-theia/theia/issues/8955)
if (!(editorPromise instanceof Widget)) {
Expand All @@ -103,6 +103,15 @@ export class EditorManager extends NavigatableWidgetOpenHandler<EditorWidget> {
return editorPromise;
}

protected async getWidget(uri: URI, options?: EditorOpenerOptions): Promise<EditorWidget | undefined> {
const editor = await super.getWidget(uri, options);
if (editor) {
// Reveal selection before attachment to manage nav stack. (https://github.com/eclipse-theia/theia/issues/8955)
this.revealSelection(editor, options, uri);
}
return editor;
}

protected async getOrCreateWidget(uri: URI, options?: EditorOpenerOptions): Promise<EditorWidget> {
const editor = await super.getOrCreateWidget(uri, options);
// Reveal selection before attachment to manage nav stack. (https://github.com/eclipse-theia/theia/issues/8955)
Expand Down

0 comments on commit bf7e2c0

Please sign in to comment.