-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
/
kernelStatus.tsx
231 lines (204 loc) · 5.62 KB
/
kernelStatus.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
// Copyright (c) Jupyter Development Team.
// Distributed under the terms of the Modified BSD License.
import React from 'react';
import { IClientSession, VDomRenderer, VDomModel } from '@jupyterlab/apputils';
import { Text } from '@jupyterlab/coreutils';
import { Kernel, Session } from '@jupyterlab/services';
import { interactiveItem, TextItem } from '..';
import { JSONExt } from '@phosphor/coreutils';
/**
* A pure functional component for rendering kernel status.
*/
function KernelStatusComponent(
props: KernelStatusComponent.IProps
): React.ReactElement<KernelStatusComponent.IProps> {
return (
<TextItem
onClick={props.handleClick}
source={`${props.kernelName} | ${Text.titleCase(props.status)}`}
title={`Change kernel for ${props.activityName}`}
/>
);
}
/**
* A namespace for KernelStatusComponent statics.
*/
namespace KernelStatusComponent {
/**
* Props for the kernel status component.
*/
export interface IProps {
/**
* A click handler for the kernel status component. By default
* we have it bring up the kernel change dialog.
*/
handleClick: () => void;
/**
* The name the kernel.
*/
kernelName: string;
/**
* The name of the activity using the kernel.
*/
activityName: string;
/**
* The status of the kernel.
*/
status: Kernel.Status;
}
}
/**
* A VDomRenderer widget for displaying the status of a kernel.
*/
export class KernelStatus extends VDomRenderer<KernelStatus.Model> {
/**
* Construct the kernel status widget.
*/
constructor(opts: KernelStatus.IOptions) {
super();
this._handleClick = opts.onClick;
this.model = new KernelStatus.Model();
this.addClass(interactiveItem);
}
/**
* Render the kernel status item.
*/
render() {
if (this.model === null) {
return null;
} else {
return (
<KernelStatusComponent
status={this.model.status}
kernelName={this.model.kernelName}
activityName={this.model.activityName}
handleClick={this._handleClick}
/>
);
}
}
private _handleClick: () => void;
}
/**
* A namespace for KernelStatus statics.
*/
export namespace KernelStatus {
/**
* A VDomModel for the kernel status indicator.
*/
export class Model extends VDomModel {
/**
* The name of the kernel.
*/
get kernelName() {
return this._kernelName;
}
/**
* The current status of the kernel.
*/
get status() {
return this._kernelStatus;
}
/**
* A display name for the activity.
*/
get activityName(): string {
return this._activityName;
}
set activityName(val: string) {
const oldVal = this._activityName;
if (oldVal === val) {
return;
}
this._activityName = val;
this.stateChanged.emit(void 0);
}
/**
* The current client session associated with the kernel status indicator.
*/
get session(): IClientSession | null {
return this._session;
}
set session(session: IClientSession | null) {
const oldSession = this._session;
if (oldSession !== null) {
oldSession.statusChanged.disconnect(this._onKernelStatusChanged);
oldSession.kernelChanged.disconnect(this._onKernelChanged);
}
const oldState = this._getAllState();
this._session = session;
if (this._session === null) {
this._kernelStatus = 'unknown';
this._kernelName = 'unknown';
} else {
this._kernelStatus = this._session.status;
this._kernelName = this._session.kernelDisplayName;
this._session.statusChanged.connect(this._onKernelStatusChanged);
this._session.kernelChanged.connect(this._onKernelChanged);
}
this._triggerChange(oldState, this._getAllState());
}
/**
* React to changes to the kernel status.
*/
private _onKernelStatusChanged = (
_session: IClientSession,
status: Kernel.Status
) => {
this._kernelStatus = status;
this.stateChanged.emit(void 0);
};
/**
* React to changes in the kernel.
*/
private _onKernelChanged = (
_session: IClientSession,
change: Session.IKernelChangedArgs
) => {
const oldState = this._getAllState();
const { newValue } = change;
if (newValue !== null) {
newValue
.getSpec()
.then(spec => {
// sync setting of status and display name
this._kernelStatus = newValue.status;
this._kernelName = spec.display_name;
this._triggerChange(oldState, this._getAllState());
})
.catch(err => {
throw err;
});
} else {
this._kernelStatus = 'unknown';
this._kernelName = 'unknown';
this._triggerChange(oldState, this._getAllState());
}
};
private _getAllState(): [string, string, string] {
return [this._kernelName, this._kernelStatus, this._activityName];
}
private _triggerChange(
oldState: [string, string, string],
newState: [string, string, string]
) {
if (JSONExt.deepEqual(oldState, newState)) {
this.stateChanged.emit(void 0);
}
}
private _activityName: string = 'activity';
private _kernelName: string = 'unknown';
private _kernelStatus: Kernel.Status = 'unknown';
private _session: IClientSession | null = null;
}
/**
* Options for creating a KernelStatus object.
*/
export interface IOptions {
/**
* A click handler for the item. By default
* we launch a kernel selection dialog.
*/
onClick: () => void;
}
}