Skip to content

Expose createProxy #43

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 24, 2025
Merged

Expose createProxy #43

merged 4 commits into from
Apr 24, 2025

Conversation

jtpio
Copy link
Collaborator

@jtpio jtpio commented Apr 24, 2025

Alternative to #42.

Provide a createProxy from the host package to more conveniently register callbacks that can be invoked from an extension in the IFrame. Users of the host package could technically create the proxy with comlink directly, but since comlink is currently abstracted away it makes sense to keep that way and just expose what is necessary.

Based on Comlink.proxy used for callbacks: https://github.com/GoogleChromeLabs/comlink?tab=readme-ov-file#callbacks

Fixes #9

Changes

  • Expose createProxy from jupyter-iframe-commands-host
  • Document usage

Example

Host

import { createBridge, createProxy } from 'jupyter-iframe-commands-host';

const commandBridge = createBridge({ iframeId: 'jupyterlab' });

const kernelStatus = async ({ displayName, isBusy }) => {
  console.log('Received kernel status update from the iframe');
  console.log('Display Name:', displayName);
  console.log('Is Busy:', isBusy);
};

await commandBridge.execute('kernel-status', createProxy({ kernelStatus }));

Custom extension

const demoPlugin: JupyterFrontEndPlugin<void> = {
  id: 'jupyter-iframe-commands:demo-callback',
  autoStart: true,
  description: 'A demo plugin to show how to use the jupyter-iframe-commands.',
  requires: [ILabStatus, INotebookTracker],
  activate: async (
    app: JupyterFrontEnd,
    labStatus: ILabStatus,
    tracker: INotebookTracker
  ) => {
    const { commands } = app;

    commands.addCommand('kernel-status', {
      label: 'Kernel Status',
      execute: args => {
        console.log('Kernel status command executed with args:', args);

        const kernelStatus = args['kernelStatus'] as unknown as (
          args?: any
        ) => void;

        labStatus.busySignal.connect(async () => {
          const kernelSpec =
            await tracker.currentWidget?.sessionContext?.session?.kernel?.spec;
          const displayName = kernelSpec
            ? kernelSpec.display_name
            : 'Loading...';
          kernelStatus({ displayName, isBusy: labStatus.isBusy });
        });
      }
    });
  }
};

When testing the above snippets with the demo:

jupyter-iframe-commands.mp4

@jtpio jtpio added the enhancement New feature or request label Apr 24, 2025
@jtpio jtpio marked this pull request as draft April 24, 2025 08:27
@jtpio jtpio marked this pull request as ready for review April 24, 2025 09:16
@jtpio jtpio requested a review from gjmooney April 24, 2025 09:24
Copy link
Collaborator

@gjmooney gjmooney left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@gjmooney gjmooney merged commit da6352e into TileDB-Inc:main Apr 24, 2025
8 checks passed
@jtpio jtpio deleted the iframe-host-messaging branch April 24, 2025 09:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Enable IFrame -> host messaging
2 participants