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

first inline SubApp PoC #1472

Merged
merged 2 commits into from
Dec 19, 2019
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
27 changes: 19 additions & 8 deletions packages/subapp-web/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,24 +105,35 @@ export function loadSubApp(info, renderStart) {
return info;
}

export function dynamicLoadSubApp(options) {
export function dynamicLoadSubApp({ name, id, timeout = 15000, onLoad, onError }) {
// TODO: timeout and callback
const wsa = window.webSubApps;
const lname = options.name.toLowerCase();
const lname = name.toLowerCase();

if (wsa._bundles[lname] === undefined) {
window.loadSubAppBundles(lname);
}
const startTime = Date.now();

const load = delay => {
setTimeout(() => {
const subApp = wsa[options.name];
const element = document.getElementById(options.id);
if (element && subApp && subApp.start) {
subApp.start({ id: options.id });
} else {
load(50);
const subApp = wsa[name];
if (subApp) {
if (!id) {
return onLoad();
} else {
const element = document.getElementById(id);
if (element && subApp.start) {
return subApp.start({ id });
}
}
}

if (timeout > 50 && Date.now() - startTime > timeout) {
return onError(new Error("dynamicLoadSubApp Timeout"));
}

return load(50);
}, delay);
};

Expand Down
43 changes: 43 additions & 0 deletions samples/poc-subapp/src/components/deals.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,46 @@ import { dynamicLoadSubApp } from "subapp-web";
import PropTypes from "prop-types";
import { connect } from "react-redux";

class SubApp extends React.Component {
constructor() {
super();
this.state = { ready: false };
}

render() {
// is subapp loaded?
// TODO: handle SSR
if (typeof window === "undefined") {
return "";
}
const { name } = this.props;
const wsa = window.webSubApps;
const lname = name.toLowerCase();

if (wsa._bundles[lname] && wsa[name]) {
const subapp = window.webSubApps[name];
return (
<div className="col-sm-4">
<div className="panel panel-primary">
<div className="panel-body">
<subapp.info.Component {...this.props} />
</div>
</div>
</div>
);
} else {
const onLoad = () => this.setState({ ready: true });
dynamicLoadSubApp({
name: "Deal",
onLoad
});

// if not, return loadingComponent
return "";
}
}
}

const DealSubApp = props => {
const { id } = props;

Expand Down Expand Up @@ -47,6 +87,9 @@ const Deals = props => {
<DealSubApp {...props} id="deal_2" />
<DealSubApp {...props} id="deal_3" />
</div>
<div>
<SubApp name="Deal" deal="hello" />
</div>
</div>
);
};
Expand Down
4 changes: 2 additions & 2 deletions samples/poc-subapp/src/deal/subapp-deal.jsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import React from "react";
import { loadSubApp } from "subapp-web";

const Deal = () => {
return <div>SPECIAL DEAL - SPECIAL DEAL</div>;
const Deal = props => {
return <div>SPECIAL DEAL - SPECIAL DEAL - {props.deal}</div>;
};

export default loadSubApp({ name: "Deal", Component: Deal });