Skip to content

Commit

Permalink
first inline SubApp PoC
Browse files Browse the repository at this point in the history
  • Loading branch information
jchip committed Dec 19, 2019
1 parent 20b59a4 commit abd1b18
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 10 deletions.
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, 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 });

0 comments on commit abd1b18

Please sign in to comment.