Skip to content
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

Revert "Add onError option to the mountable package" #635

Merged
merged 6 commits into from
Oct 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 0 additions & 11 deletions packages/mountable/__mocks__/@stlite/common-react.ts

This file was deleted.

1 change: 0 additions & 1 deletion packages/mountable/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@
},
"jest": {
"roots": [
"<rootDir>",
"<rootDir>/src"
],
"collectCoverageFrom": [
Expand Down
7 changes: 6 additions & 1 deletion packages/mountable/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ import StreamlitApp from "./StreamlitApp";
import { StliteKernel } from "@stlite/kernel";
import { getParentUrl } from "./url";
import { canonicalizeMountOptions, MountOptions } from "./options";
import { ToastContainer, StliteKernelWithToast } from "@stlite/common-react";
import {
ToastContainer,
makeToastKernelCallbacks,
StliteKernelWithToast,
} from "@stlite/common-react";
import "react-toastify/dist/ReactToastify.css";
import "@stlite/common-react/src/toastify-components/toastify.css";

Expand Down Expand Up @@ -34,6 +38,7 @@ export function mount(
const kernel = new StliteKernel({
...canonicalizeMountOptions(options),
wheelBaseUrl,
...makeToastKernelCallbacks(),
});

ReactDOM.render(
Expand Down
30 changes: 0 additions & 30 deletions packages/mountable/src/options.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { makeToastKernelCallbacks } from "@stlite/common-react";
import { canonicalizeMountOptions, resolveUrl } from "./options";

describe("resolveUrl()", () => {
Expand Down Expand Up @@ -64,7 +63,6 @@ describe("canonicalizeMountOptions()", () => {
},
requirements: [],
archives: [],
...makeToastKernelCallbacks(),
});
});

Expand Down Expand Up @@ -96,7 +94,6 @@ describe("canonicalizeMountOptions()", () => {
},
},
archives: [],
...makeToastKernelCallbacks(),
});
});

Expand Down Expand Up @@ -142,7 +139,6 @@ describe("canonicalizeMountOptions()", () => {
options: {},
},
],
...makeToastKernelCallbacks(),
});
});

Expand All @@ -163,7 +159,6 @@ describe("canonicalizeMountOptions()", () => {
},
},
archives: [],
...makeToastKernelCallbacks(),
});
});

Expand All @@ -177,31 +172,6 @@ describe("canonicalizeMountOptions()", () => {
entrypoint: "streamlit_app.py",
files: {},
archives: [],
...makeToastKernelCallbacks(),
});
});

it("wraps the `onError` option and also delegates to the original toast callback if `true` returned", () => {
const toastCallbacks = makeToastKernelCallbacks();

const onError = jest.fn();
const options = canonicalizeMountOptions({ onError });

const error = new Error("foo");
options.onError!(error);
expect(onError).toHaveBeenCalledWith(error);
expect(toastCallbacks.onError).toHaveBeenCalledWith(error);
});

it("wraps the `onError` option but prevents the toast if `false` returned", () => {
const toastCallbacks = makeToastKernelCallbacks();

const onError = jest.fn(() => false);
const options = canonicalizeMountOptions({ onError });

const error = new Error("foo");
options.onError!(error);
expect(onError).toHaveBeenCalledWith(error);
expect(toastCallbacks.onError).not.toHaveBeenCalled();
});
});
20 changes: 0 additions & 20 deletions packages/mountable/src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import type {
EmscriptenFile,
EmscriptenFileUrl,
} from "@stlite/kernel";
import { makeToastKernelCallbacks } from "@stlite/common-react";

export interface SimplifiedStliteKernelOptions {
entrypoint?: string;
Expand All @@ -16,10 +15,6 @@ export interface SimplifiedStliteKernelOptions {
allowedOriginsResp?: StliteKernelOptions["allowedOriginsResp"];
pyodideUrl?: StliteKernelOptions["pyodideUrl"];
streamlitConfig?: StliteKernelOptions["streamlitConfig"];
// We won't add onProgress and onLoad callbacks until they are required by some users to keep the API simple.
onError?: (
...args: Parameters<NonNullable<StliteKernelOptions["onError"]>>
) => boolean | void;
}

function canonicalizeFiles(
Expand Down Expand Up @@ -86,8 +81,6 @@ export type MountOptions = string | SimplifiedStliteKernelOptions;
export function canonicalizeMountOptions(
options: string | SimplifiedStliteKernelOptions
): StliteKernelOptions {
const toastCallbacks = makeToastKernelCallbacks();

if (typeof options === "string") {
const mainScript = options;
return {
Expand All @@ -99,23 +92,12 @@ export function canonicalizeMountOptions(
},
archives: [],
requirements: [],
...toastCallbacks,
};
}

const files = canonicalizeFiles(options.files);
const archives = canonicalizeArchives(options.archives);

const onErrorOption = options.onError;
const onError: StliteKernelOptions["onError"] = onErrorOption
? (...args) => {
const shouldShowToast = onErrorOption(...args);
if (shouldShowToast !== false) {
toastCallbacks.onError(...args);
}
}
: toastCallbacks.onError;

return {
entrypoint: options.entrypoint || DEFAULT_ENTRYPOINT,
files,
Expand All @@ -124,7 +106,5 @@ export function canonicalizeMountOptions(
allowedOriginsResp: options.allowedOriginsResp,
pyodideUrl: options.pyodideUrl,
streamlitConfig: options.streamlitConfig,
...toastCallbacks,
onError,
};
}