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

Commit 61858e0

Browse files
committed
feat(environment): support extended Extension types for addl props
1 parent 5bf63b7 commit 61858e0

File tree

2 files changed

+16
-14
lines changed

2 files changed

+16
-14
lines changed

src/environment/controller.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ type ConfigurationUpdate = ConfigurationUpdateParams & MessageSource & PromiseCa
5555
/**
5656
* The controller for the environment.
5757
*/
58-
export class Controller implements Unsubscribable {
59-
private _environment = new BehaviorSubject<Environment>(EMPTY_ENVIRONMENT)
58+
export class Controller<X extends Extension = Extension> implements Unsubscribable {
59+
private _environment = new BehaviorSubject<Environment<X>>(EMPTY_ENVIRONMENT)
6060

6161
private clients: ClientEntry[] = []
6262

@@ -95,7 +95,7 @@ export class Controller implements Unsubscribable {
9595
})
9696
}
9797

98-
public setEnvironment(nextEnvironment: Environment): void {
98+
public setEnvironment(nextEnvironment: Environment<X>): void {
9999
if (isEqual(this._environment.value, nextEnvironment)) {
100100
return // no change
101101
}
@@ -137,7 +137,7 @@ export class Controller implements Unsubscribable {
137137
})
138138
const settings = this._environment.pipe(
139139
map(({ extensions }) => (extensions ? extensions.find(x => x.id === key.id) : null)),
140-
filter((x): x is Extension => !!x),
140+
filter((x): x is X => !!x),
141141
map(x => x.settings),
142142
distinctUntilChanged((a, b) => isEqual(a, b))
143143
)
@@ -189,7 +189,7 @@ export class Controller implements Unsubscribable {
189189
)
190190
}
191191

192-
public readonly environment: ObservableEnvironment = createObservableEnvironment(this._environment)
192+
public readonly environment: ObservableEnvironment<X> = createObservableEnvironment<X>(this._environment)
193193

194194
public set trace(value: Trace) {
195195
for (const client of this.clients) {

src/environment/environment.ts

+11-9
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@ import { Extension } from './extension'
1010
*
1111
* This models the state of editor-like tools that display documents, allow selections and scrolling in documents,
1212
* and support extension configuration.
13+
*
14+
* @template X extension type, to support storing additional properties on extensions
1315
*/
14-
export interface Environment {
16+
export interface Environment<X extends Extension = Extension> {
1517
/**
1618
* The root URI of the environment, or null if there is none (which means the extension is unable to access any
1719
* documents in the environment).
@@ -26,11 +28,11 @@ export interface Environment {
2628
readonly component: Component | null
2729

2830
/** The active extensions, or null if there are none. */
29-
readonly extensions: Extension[] | null
31+
readonly extensions: X[] | null
3032
}
3133

3234
/** An empty CXP environment. */
33-
export const EMPTY_ENVIRONMENT: Environment = { root: null, component: null, extensions: null }
35+
export const EMPTY_ENVIRONMENT: Environment<any> = { root: null, component: null, extensions: null }
3436

3537
/** An application component that displays a [TextDocument](#TextDocument). */
3638
export interface Component {
@@ -52,9 +54,9 @@ export interface Component {
5254
*
5355
* Includes derived observables for convenience.
5456
*/
55-
export interface ObservableEnvironment {
57+
export interface ObservableEnvironment<X extends Extension = Extension> {
5658
/** The environment (and changes to it). */
57-
readonly environment: Observable<Environment> & { readonly value: Environment }
59+
readonly environment: Observable<Environment<X>> & { readonly value: Environment<X> }
5860

5961
/** The environment's root URI (and changes to it). */
6062
readonly root: Observable<URI | null>
@@ -67,16 +69,16 @@ export interface ObservableEnvironment {
6769
}
6870

6971
/** An ObservableEnvironment that always represents the empty environment and never emits changes. */
70-
export const EMPTY_OBSERVABLE_ENVIRONMENT: ObservableEnvironment = {
72+
export const EMPTY_OBSERVABLE_ENVIRONMENT: ObservableEnvironment<any> = {
7173
environment: { ...of(EMPTY_ENVIRONMENT), value: EMPTY_ENVIRONMENT } as ObservableEnvironment['environment'],
7274
root: of(null),
7375
component: of(null),
7476
textDocument: of(null),
7577
}
7678

77-
export function createObservableEnvironment(
78-
environment: Observable<Environment> & { readonly value: Environment }
79-
): ObservableEnvironment {
79+
export function createObservableEnvironment<X extends Extension>(
80+
environment: Observable<Environment<X>> & { readonly value: Environment<X> }
81+
): ObservableEnvironment<X> {
8082
const component = environment.pipe(
8183
map(({ component }) => component),
8284
distinctUntilChanged((a, b) => isEqual(a, b))

0 commit comments

Comments
 (0)