Skip to content

Instantly share code, notes, and snippets.

@sccolbert
Forked from blink1073/kernelfuture.ts
Last active September 15, 2015 19:55
Show Gist options
  • Save sccolbert/e499eaf2f3d519c761a0 to your computer and use it in GitHub Desktop.
Save sccolbert/e499eaf2f3d519c761a0 to your computer and use it in GitHub Desktop.
KernelFuture
function createFuture(kernel: IKernel, msgId: string, shellPromise: Promise<IKernelMessage>): IKernelFuture {
var future = new KernelFutureHandler(msgId);
kernel.iopubReceived.connect(future.handleIOPub, future);
kernel.stdinReceived.connect(future.handlerStdin, future);
shellPromise.then(msg => future.handleReply(kernel, msg));
return future;
}
/**
* Implementation of a kernel future.
*/
class KernelFutureHandler implements IKernelFuture {
constructor(msgId: string) {
this._msgId = msgId;
}
/**
* Get the current autoDispose status of the future.
*/
get autoDispose(): boolean {
return this._testFlag(KernelFutureFlag.AutoDispose);
}
/**
* Set the current autoDispose behavior of the future.
*
* If True, it will self-dispose() after onDone() is called.
*/
set autoDispose(value: boolean) {
if (value) {
this._setFlag(KernelFutureFlag.AutoDispose);
} else {
this._clearFlag(KernelFutureFlag.AutoDispose);
}
}
/**
* Check for message done state.
*/
get isDone(): boolean {
return this._testFlag(KernelFutureFlag.IsDone);
}
/**
* Get the reply handler.
*/
get onReply(): (msg: IKernelMessage) => void {
return this._reply;
}
/**
* Set the reply handler.
*/
set onReply(cb: (msg: IKernelMessage) => void) {
this._reply = cb;
}
/**
* Get the iopub handler.
*/
get onIOPub(): (msg: IKernelMessage) => void {
return this._iopub;
}
/**
* Set the iopub handler.
*/
set onIOPub(cb: (msg: IKernelMessage) => void) {
this._iopub = cb;
}
/**
* Get the done handler.
*/
get onDone(): (msg: IKernelMessage) => void {
return this._done;
}
/**
* Set the done handler.
*/
set onDone(cb: (msg: IKernelMessage) => void) {
this._done = cb;
}
/**
* Get the stdin handler.
*/
get onStdin(): (msg: IKernelMessage) => void {
return this._stdin;
}
/**
* Set the stdin handler.
*/
set onStdin(cb: (msg: IKernelMessage) => void) {
this._stdin = cb;
}
/**
* Dispose and unregister the future.
*/
dispose(): void {
disconnectReceiver(this);
this._stdin = null;
this._iopub = null;
this._reply = null;
this._done = null;
}
handleReply(sender: IKernel, msg: IKernelMessage): void {
var reply = this._reply;
if (reply) reply(msg);
this._setFlag(KernelFutureFlag.GotReply);
if (this._testFlag(KernelFutureFlag.GotIdle)) {
this._handleDone(msg);
}
}
handleStdin(sender: IKernel, msg: IKernelMessage): void {
if (!msg.parent_header) {
return;
}
if (msg.parent_header.msg_id !== this._msgId) {
return;
}
var stdin = this._stdin;
if (stdin) stdin(msg);
}
handleIOPub(sender: IKernel, msg: IKernelMessage): void {
if (!msg.parent_header) {
return;
}
if (msg.parent_header.msg_id !== this._msgId) {
return;
}
var iopub = this._iopub;
if (iopub) iopub(msg);
if (msg.header.msg_type === 'status' &&
msg.content.execution_state === 'idle') {
this._setFlag(KernelFutureFlag.GotIdle);
if (this._testFlag(KernelFutureFlag.GotReply)) {
this._handleDone(msg);
}
}
}
/**
* Handle a message done status.
*/
private _handleDone(msg: IKernelMessage): void {
if (this.isDone) {
return;
}
this._setFlag(KernelFutureFlag.IsDone);
var done = this._done;
if (done) done(msg);
this._done = null;
if (this._testFlag(KernelFutureFlag.AutoDispose)) {
this.dispose();
}
}
/**
* Test whether the given future flag is set.
*/
private _testFlag(flag: KernelFutureFlag): boolean {
return (this._status & flag) !== 0;
}
/**
* Set the given future flag.
*/
private _setFlag(flag: KernelFutureFlag): void {
this._status |= flag;
}
/**
* Clear the given future flag.
*/
private _clearFlag(flag: KernelFutureFlag): void {
this._status &= ~flag;
}
private _status = 0;
private _msgId = '';
private _stdin: (msg: IKernelMessage) => void = null;
private _iopub: (msg: IKernelMessage) => void = null;
private _reply: (msg: IKernelMessage) => void = null;
private _done: (msg: IKernelMessage) => void = null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment