Skip to content

Commit

Permalink
use own types
Browse files Browse the repository at this point in the history
  • Loading branch information
mscolnick committed Jul 29, 2024
1 parent 2cadbc2 commit 2459e41
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 4 deletions.
15 changes: 11 additions & 4 deletions frontend/src/plugins/impl/anywidget/AnyWidgetPlugin.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/* Copyright 2024 Marimo. All rights reserved. */
/* eslint-disable @typescript-eslint/no-explicit-any */
import { z } from "zod";
import type { AnyWidget, AnyModel, Initialize, Render } from "@anywidget/types";

import type { IPluginProps } from "@/plugins/types";
import { useEffect, useRef } from "react";
Expand All @@ -12,7 +11,7 @@ import { useDeepCompareMemoize } from "@/hooks/useDeepCompareMemoize";
import { ErrorBanner } from "../common/error-banner";
import { createPlugin } from "@/plugins/core/builder";
import { rpc } from "@/plugins/core/rpc";
import type { EventHandler } from "./types";
import type { AnyModel, AnyWidget, EventHandler, Experimental } from "./types";
import { Logger } from "@/utils/Logger";
import { useEventListener } from "@/hooks/useEventListener";
import { MarimoIncomingMessageEvent } from "@/core/dom/events";
Expand Down Expand Up @@ -104,10 +103,18 @@ async function runAnyWidgetModule(
model: Model<T>,
el: HTMLElement,
) {
const experimental: Experimental = {
invoke: async (name, msg, options) => {
const message =
"anywidget.invoke not supported in marimo. Please file an issue at https://github.com/marimo-team/marimo/issues";
Logger.warn(message);
throw new Error(message);
},
};
const widget =
typeof widgetDef === "function" ? await widgetDef() : widgetDef;
await widget.initialize?.({ model });
await widget.render?.({ model, el });
await widget.initialize?.({ model, experimental });
await widget.render?.({ model, el, experimental });
}

function isAnyWidgetModule(mod: any): mod is { default: AnyWidget } {
Expand Down
37 changes: 37 additions & 0 deletions frontend/src/plugins/impl/anywidget/types.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
/* Copyright 2024 Marimo. All rights reserved. */
/* eslint-disable @typescript-eslint/no-invalid-void-type */
/* eslint-disable @typescript-eslint/no-explicit-any */

// Copied from https://github.com/manzt/anywidget/blob/main/packages/types/index.ts
// with slight modifications
// - removed widget_manager
// We copy this for now since `@anywidget/types` pulls in jquery and backbone, which we don't need

type Awaitable<T> = T | Promise<T>;
export type EventHandler = (...args: any[]) => void;
type ObjectHash = Record<string, any>;
export type ChangeEventHandler<Payload> = (_: unknown, value: Payload) => void;
Expand Down Expand Up @@ -36,3 +39,37 @@ export interface AnyModel<T extends ObjectHash = ObjectHash> {
): void;
widget_manager: {};
}

export interface Experimental {
invoke: <T>(
name: string,
msg?: any,
options?: {
buffers?: DataView[];
signal?: AbortSignal;
},
) => Promise<[T, DataView[]]>;
}
export interface RenderProps<T extends ObjectHash = ObjectHash> {
model: AnyModel<T>;
el: HTMLElement;
experimental: Experimental;
}
export type Render<T extends ObjectHash = ObjectHash> = (
props: RenderProps<T>,
) => Awaitable<void | (() => Awaitable<void>)>;
export interface InitializeProps<T extends ObjectHash = ObjectHash> {
model: AnyModel<T>;
experimental: Experimental;
}

export type Initialize<T extends ObjectHash = ObjectHash> = (
props: InitializeProps<T>,
) => Awaitable<void | (() => Awaitable<void>)>;
interface WidgetDef<T extends ObjectHash = ObjectHash> {
initialize?: Initialize<T>;
render?: Render<T>;
}
export type AnyWidget<T extends ObjectHash = ObjectHash> =
| WidgetDef<T>
| (() => Awaitable<WidgetDef<T>>);

0 comments on commit 2459e41

Please sign in to comment.