Skip to content
This repository was archived by the owner on Nov 6, 2018. It is now read-only.

Commit 2e70835

Browse files
committed
refactor: move Windows#active to CXP#activeWindow for convenience
BREAKING CHANGE: Use `CXP#activeWindow` instead of `Windows#active` to get the currently active window.
1 parent 9fcfff3 commit 2e70835

File tree

4 files changed

+34
-33
lines changed

4 files changed

+34
-33
lines changed

src/extension/api.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ export interface SourcegraphExtensionAPI<C = Settings> {
3434
*/
3535
windows: Windows
3636

37+
/**
38+
* The active window, or `null` if there is no active window. The active window is the window that was
39+
* focused most recently.
40+
*/
41+
activeWindow: Window | null
42+
3743
/**
3844
* Command registration and execution.
3945
*/
@@ -147,12 +153,6 @@ export interface Windows extends Observable<Window[]> {
147153
*/
148154
all: Window[]
149155

150-
/**
151-
* The active window, or `null` if there is no active window. The active window is the window that was
152-
* focused most recently.
153-
*/
154-
active: Window | null
155-
156156
/**
157157
* Display a prompt and request text input from the user.
158158
*

src/extension/extension.ts

+10-3
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,17 @@ import { Commands, Configuration, ExtensionContext, Observable, SourcegraphExten
1212
import { createExtCommands } from './features/commands'
1313
import { createExtConfiguration } from './features/configuration'
1414
import { createExtContext } from './features/context'
15-
import { createExtWindows } from './features/windows'
15+
import { ExtWindows } from './features/windows'
1616

1717
class ExtensionHandle<C> implements SourcegraphExtensionAPI<C> {
1818
public readonly configuration: Configuration<C> & Observable<C>
19-
public readonly windows: Windows & Observable<Window[]>
19+
public get windows(): Windows & Observable<Window[]> {
20+
return this._windows
21+
}
2022
public readonly commands: Commands
2123
public readonly context: ExtensionContext
2224

25+
private _windows: ExtWindows
2326
private subscription = new Subscription()
2427

2528
constructor(public readonly rawConnection: MessageConnection, public readonly initializeParams: InitializeParams) {
@@ -29,7 +32,7 @@ class ExtensionHandle<C> implements SourcegraphExtensionAPI<C> {
2932
this,
3033
initializeParams.configurationCascade as ConfigurationCascade<C>
3134
)
32-
this.windows = createExtWindows(this)
35+
this._windows = new ExtWindows(this)
3336
this.commands = createExtCommands(this)
3437
this.context = createExtContext(this)
3538
}
@@ -38,6 +41,10 @@ class ExtensionHandle<C> implements SourcegraphExtensionAPI<C> {
3841
return this.initializeParams.root
3942
}
4043

44+
public get activeWindow(): Window | null {
45+
return this._windows.activeWindow
46+
}
47+
4148
public close(): void {
4249
this.subscription.unsubscribe()
4350
}

src/extension/features/windows.test.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,22 @@ import {
88
ShowInputParams,
99
ShowInputRequest,
1010
} from '../../protocol'
11-
import { Observable, Window, Windows } from '../api'
11+
import { Window } from '../api'
1212
import { observableValue } from '../util'
13-
import { createExtWindows } from './windows'
13+
import { ExtWindows } from './windows'
1414

1515
describe('ExtWindows', () => {
16-
function create(): { extWindows: Windows & Observable<Window[]>; mockConnection: MockMessageConnection } {
16+
function create(): { extWindows: ExtWindows; mockConnection: MockMessageConnection } {
1717
const mockConnection = new MockMessageConnection()
18-
const extWindows = createExtWindows({ rawConnection: mockConnection })
18+
const extWindows = new ExtWindows({ rawConnection: mockConnection })
1919
return { extWindows, mockConnection }
2020
}
2121

2222
it('starts empty', () => {
2323
const { extWindows } = create()
2424
assert.deepStrictEqual(observableValue(extWindows), [{ isActive: true, activeComponent: null }] as Window[])
2525
assert.deepStrictEqual(extWindows.all, [{ isActive: true, activeComponent: null }] as Window[])
26-
assert.deepStrictEqual(extWindows.active, { isActive: true, activeComponent: null } as Window)
26+
assert.deepStrictEqual(extWindows.activeWindow, { isActive: true, activeComponent: null } as Window)
2727
})
2828

2929
describe('component', () => {
@@ -37,7 +37,7 @@ describe('ExtWindows', () => {
3737
]
3838
assert.deepStrictEqual(observableValue(extWindows), expectedWindows)
3939
assert.deepStrictEqual(extWindows.all, expectedWindows)
40-
assert.deepStrictEqual(extWindows.active, expectedWindows[0])
40+
assert.deepStrictEqual(extWindows.activeWindow, expectedWindows[0])
4141
})
4242

4343
it('handles when the open resource is closed', () => {

src/extension/features/windows.ts

+12-18
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,13 @@ import {
1010
} from '../../protocol'
1111
import { Observable, SourcegraphExtensionAPI, Window, Windows } from '../api'
1212

13-
class ExtWindows extends BehaviorSubject<Window[]> implements Windows, Observable<Window[]> {
13+
/**
14+
* Implements the Sourcegraph extension API's {@link SourcegraphExtensionAPI#windows} value.
15+
*
16+
* @param ext The Sourcegraph extension API handle.
17+
* @return The {@link SourcegraphExtensionAPI#windows} value.
18+
*/
19+
export class ExtWindows extends BehaviorSubject<Window[]> implements Windows, Observable<Window[]> {
1420
constructor(private ext: Pick<SourcegraphExtensionAPI<any>, 'rawConnection'>) {
1521
super([
1622
{
@@ -25,10 +31,10 @@ class ExtWindows extends BehaviorSubject<Window[]> implements Windows, Observabl
2531
})
2632
ext.rawConnection.onNotification(DidCloseTextDocumentNotification.type, params => {
2733
if (
28-
this.active &&
29-
this.active.activeComponent &&
30-
this.active.activeComponent.resource &&
31-
this.active.activeComponent.resource === params.textDocument.uri
34+
this.activeWindow &&
35+
this.activeWindow.activeComponent &&
36+
this.activeWindow.activeComponent.resource &&
37+
this.activeWindow.activeComponent.resource === params.textDocument.uri
3238
) {
3339
this.next([{ ...this.value[0], activeComponent: null }])
3440
}
@@ -39,7 +45,7 @@ class ExtWindows extends BehaviorSubject<Window[]> implements Windows, Observabl
3945
return this.value
4046
}
4147

42-
public get active(): Window | null {
48+
public get activeWindow(): Window | null {
4349
return this.value.find(({ isActive }) => isActive) || null
4450
}
4551

@@ -56,15 +62,3 @@ class ExtWindows extends BehaviorSubject<Window[]> implements Windows, Observabl
5662

5763
public readonly [Symbol.observable] = () => this
5864
}
59-
60-
/**
61-
* Creates the Sourcegraph extension API's {@link SourcegraphExtensionAPI#windows} value.
62-
*
63-
* @param ext The Sourcegraph extension API handle.
64-
* @return The {@link SourcegraphExtensionAPI#windows} value.
65-
*/
66-
export function createExtWindows<C>(
67-
ext: Pick<SourcegraphExtensionAPI<C>, 'rawConnection'>
68-
): Windows & Observable<Window[]> {
69-
return new ExtWindows(ext)
70-
}

0 commit comments

Comments
 (0)