How to use @nteract/messaging - 10 common examples

To help you get started, we’ve selected a few @nteract/messaging examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github nteract / nteract / packages / editor / __tests__ / editor.spec.tsx View on Github external
it("handles tooltip", done => {
    const sent = new Subject();
    const received = new Subject();
    const mockSocket = Subject.create(sent, received);
    const channels = mockSocket;

    const cm = {
      getCursor: () => ({ line: 0 }),
      getValue: () => "map",
      indexFromPos: () => 3,
      posFromIndex: x => ({ ch: x, line: 0 })
    };

    const message = createMessage("inspect_request");
    const observable = tooltip.tooltipObservable(channels, cm, message);

    // Craft the response to their message
    const response = createMessage("inspect_reply");
    response.content = {
      data: [
        "Init signature: map(self, /, *args, **kwargs)↵Docstring:     ↵map(func, *iterables) --> map object↵↵Make an iterator that computes the function using arguments from↵each of the iterables.  Stops when the shortest iterable is exhausted.↵Type:           type↵"
      ],
      cursor_pos: 3,
      detail_level: 0
    }; // Likely hokey values
    response.parent_header = Object.assign({}, message.header);

    // Listen on the Observable
    observable.subscribe(
      msg => {
github nteract / nteract / packages / editor / __tests__ / complete.spec.ts View on Github external
const received = new Subject();
    const mockSocket = Subject.create(sent, received);
    const channels: Channels = mockSocket;

    const cm: Doc = ({
      getCursor: () => ({ line: 2 }),
      getValue: () => "\n\nimport thi",
      indexFromPos: () => 12,
      posFromIndex: x => ({ ch: x, line: 3 })
    } as unknown) as Doc;

    const message = createMessage("complete_request");
    const observable = complete.codeCompleteObservable(channels, cm, message);

    // Craft the response to their message
    const response = createMessage("complete_reply");
    response.content = {
      matches: ["import this"],
      cursor_start: 9,
      cursor_end: 10
    }; // Likely hokey values
    response.parent_header = Object.assign({}, message.header);

    // Listen on the Observable
    observable.subscribe(
      msg => {
        expect(msg.from).toEqual({ line: 3, ch: 9 });
        expect(msg.list[0].text).toEqual("import this");
        expect(msg.to).toEqual({ ch: 10, line: 3 });
      },
      err => {
        throw err;
github nteract / nteract / applications / desktop / src / notebook / kernel / shutdown.js View on Github external
// Validate the input, do nothing if invalid kernel info is provided.
  if (!(kernel && (kernel.channels || kernel.spawn))) {
    return Promise.resolve(true);
  }

  // Fallback to forcefully shutting the kernel down.
  function handleShutdownFailure(err) {
    console.error(
      `Could not gracefully shutdown the kernel because of the
      following error.  nteract will now forcefully shutdown the kernel.`,
      err
    );
    forceShutdownKernel(kernel);
  }

  const request = shutdownRequest({ restart: false });

  const shutDownReply = kernel.channels
    .pipe(
      childOf(request),
      ofMessageType("shutdown_reply"),
      map(msg => msg.content)
    )
    .toPromise();

  kernel.channels.next(request);
  // Attempt to gracefully terminate the kernel.

  return shutDownReply
    .then(k => {
      // At this point, the kernel has cleaned up its resources.  Now we can
      // terminate the process and cleanup handles by calling forceShutdownKernel
github nteract / nteract / applications / desktop / src / notebook / epics / zeromq-kernels.ts View on Github external
}

      const kernel = selectors.kernel(state$.value, { kernelRef });

      if (!kernel) {
        // tslint:disable-next-line:no-console
        console.warn("tried to kill a kernel that doesn't exist");
        return empty();
      }

      // Ignore the action if the specified kernel is not ZMQ.
      if (kernel.type !== "zeromq") {
        return empty();
      }

      const request = shutdownRequest({ restart: false });

      // Try to make a shutdown request
      // If we don't get a response within X time, force a shutdown
      // Either way do the same cleanup
      const shutDownHandling = kernel.channels.pipe(
        childOf(request),
        ofMessageType("shutdown_reply"),
        first(),
        // If we got a reply, great! :)
        map((msg: { content: { restart: boolean } }) =>
          actions.shutdownReplySucceeded({ content: msg.content, kernelRef })
        ),
        // If we don't get a response within 2s, assume failure :(
        timeout(1000 * 2),
        catchError(err =>
          of(actions.shutdownReplyTimedOut({ error: err, kernelRef }))
github nteract / nteract / applications / desktop / src / notebook / epics / zeromq-kernels.ts View on Github external
}

      const kernel = selectors.kernel(state$.value, { kernelRef });

      if (!kernel) {
        // tslint:disable-next-line:no-console
        console.warn("tried to kill a kernel that doesn't exist");
        return empty();
      }

      // Ignore the action if the specified kernel is not ZMQ.
      if (kernel.type !== "zeromq") {
        return empty();
      }

      const request = shutdownRequest({ restart: false });

      // Try to make a shutdown request
      // If we don't get a response within X time, force a shutdown
      // Either way do the same cleanup
      const shutDownHandling = kernel.channels.pipe(
        childOf(request),
        ofMessageType("shutdown_reply"),
        first(),
        // If we got a reply, great! :)
        map((msg: { content: { restart: boolean } }) =>
          actions.shutdownReplySucceeded({ content: msg.content, kernelRef })
        ),
        // If we don't get a response within 2s, assume failure :(
        timeout(1000 * 2),
        catchError(err =>
          of(actions.shutdownReplyTimedOut({ error: err, kernelRef }))
github nteract / create-nteract-app / src / template / components / code-state.js View on Github external
getKernelInfo() {
    // Set up a receiver for kernel info
    let kernelInfo = null;
    if (!this.props.kernel) {
      return;
    }

    this.props.kernel.channels
      .pipe(
        messaging.ofMessageType("kernel_info_reply"),
        first(),
        map(msg => msg.content)
      )
      .subscribe(content => {
        kernelInfo = content;
      });

    var intervalId;
    intervalId = setInterval(() => {
      if (kernelInfo) {
        clearInterval(intervalId);
        return;
      }
      this.props.kernel.channels.next(messaging.kernelInfoRequest());
    }, 300);
  }
github nteract / nteract / packages / fs-kernels / src / kernel.ts View on Github external
shutdownEpic(timeoutMs: number = 2000) {
    const request: JupyterMessage<"shutdown_request", any> = shutdownRequest({
      restart: false
    });

    // Try to make a shutdown request
    // If we don't get a response within X time, force a shutdown
    // Either way do the same cleanup
    const shutDownHandling = this.channels.pipe(
      /* Get the first response to our message request. */
      childOf(request),
      ofMessageType("shutdown_reply"),
      first(),
      // If we got a reply, great! :)
      map((msg: { content: { restart: boolean } }) => {
        return {
          status: "shutting down",
          content: msg.content
        };
      }),
      /**
       * If we don't get a response within timeoutMs, then throw an error.
       */
      timeout(timeoutMs),
      catchError(err => of({ error: err, status: "error" })),
      /**
       * Even if we don't receive a shutdown_reply from the kernel to our
       * shutdown_request, we will go forward with cleaning up the RxJS
github nteract / nteract / packages / editor / src / jupyter / tooltip.ts View on Github external
export function tooltipObservable(
  channels: Channels,
  editor: CMI,
  message: JupyterMessage
) {
  const tip$ = channels.pipe(
    childOf(message),
    ofMessageType("inspect_reply"),
    map((entry: JupyterMessage) => entry.content),
    first(),
    map(results => ({
      dict: results.data
    }))
  );
  // On subscription, send the message
  return Observable.create((observer: Observer) => {
    const subscription = tip$.subscribe(observer);
    channels.next(message);
    return subscription;
  });
}
github nteract / nteract / packages / editor / src / jupyter / complete.tsx View on Github external
export function codeCompleteObservable(
  channels: Channels,
  editor: Doc,
  message: JupyterMessage
) {
  const completion$ = channels.pipe(
    childOf(message),
    ofMessageType("complete_reply"),
    map(entry => entry.content),
    first(),
    map(expand_completions(editor)),
    timeout(15000) // Large timeout for slower languages; this is just here to make sure we eventually clean up resources
  );

  // On subscription, send the message
  return Observable.create((observer: Observer) => {
    const subscription = completion$.subscribe(observer);
    channels.next(message);
    return subscription;
  });
}
github nteract / create-nteract-app / src / template / components / code-area.js View on Github external
getKernelInfo() {
    // Set up a receiver for kernel info
    let kernelInfo = null;
    this.props.kernel.channels
      .pipe(
        messaging.ofMessageType("kernel_info_reply"),
        first(),
        map(msg => msg.content)
      )
      .subscribe(content => {
        kernelInfo = content;
      });

    var intervalId;
    intervalId = setInterval(() => {
      if (kernelInfo) {
        clearInterval(intervalId);
        return;
      }
      this.props.kernel.channels.next(messaging.kernelInfoRequest());
    }, 300);
  }