Skip to content

Commit

Permalink
fix: pass props to subapps through lazyloaded subapp (#1949)
Browse files Browse the repository at this point in the history
* [minor] pass props in lazyloadsubapp

* [minor] lazyloadsubapp accept props

---------

Co-authored-by: Shubham sharma <[email protected]>
  • Loading branch information
shubham2811 and Shubham sharma authored Mar 2, 2023
1 parent 36d4359 commit 43da556
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 21 deletions.
36 changes: 23 additions & 13 deletions packages/subapp-server/test/spec/fastify-plugin.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

const { fastifyPlugin } = require("../../lib/fastify-plugin");
const Path = require("path");
const { runFinally, asyncVerify, runTimeout } = require("run-verify");
const { runFinally, runVerify, runTimeout } = require("run-verify");
const http = require("http");

describe("fastify-plugin", function () {
it("loads server from file system", async () => {
const server = await require("@xarc/fastify-server")({
deferStart: true,
connection: { port: 0, host: "localhost" }
connection: { port: 3011, host: "localhost" }
});

const srcDir = Path.join(__dirname, "../data/fastify-plugin-test");
Expand All @@ -28,7 +28,7 @@ describe("fastify-plugin", function () {
stats: Path.join(__dirname, "../data/fastify-plugin-test/stats.json")
};

return asyncVerify(
return runVerify(
runTimeout(4500),
() => fastifyPlugin(server, opt),
() => server.start(),
Expand Down Expand Up @@ -71,7 +71,7 @@ describe("fastify-plugin", function () {
stats: Path.join(__dirname, "../data/fastify-plugin-test/stats.json")
};

return asyncVerify(
return runVerify(
runTimeout(4500),
() => fastifyPlugin(server, opt),
() => server.start(),
Expand Down Expand Up @@ -108,20 +108,30 @@ describe("fastify-plugin", function () {
}
});

asyncVerify(
return runVerify(
() => fastifyPlugin(server, {}),
() => server.start(),
() => {
http.get("http://localhost:3002/", res => {
expect(res.statusCode).to.equal(200);
let data = "";
res.on("data", chunk => (data += chunk));
res.on("done", () => {
expect(data.to.contain("Hello World"));
async () => {
const res = await server.inject({
method: "GET",
url: `http://localhost:3002/`
});
expect(res.statusCode).to.equal(200);
let data = "";
res.on("data", chunk => (data += chunk));
res.on("done", () => {
expect(data.to.contain("Hello World"));
});
http.get("http://localhost:3002/", response => {
expect(response.statusCode).to.equal(200);
let d = "";
response.on("data", chunk => (d += chunk));
response.on("done", () => {
expect(d.to.contain("Hello World"));
});
});
},
runFinally(() => server.close())
);
});
}).timeout(5000);
});
7 changes: 5 additions & 2 deletions packages/subapp-web/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ export function loadSubApp(info, renderStart, options) {
subApp.start = (instance, options, info) => {
instance = instance || subApp.preStart(instance, options, info);
info = info || subApp.info;
if (!instance.props) {
instance.props = options.props;
}
// if user provided a start function, then user is expected to
// have reference to info
const callStart = () => {
Expand Down Expand Up @@ -184,7 +187,7 @@ export function isLoaded(name) {
return Boolean(xarc.getSubApp(name));
}

export function lazyLoadSubApp({ name, id, timeout = 15000, onLoad, onError, fallback, ns }) {
export function lazyLoadSubApp({ name, id, timeout = 15000, onLoad, onError, fallback, ns, props }) {
// TODO: timeout and callback
const lname = name.toLowerCase();

Expand All @@ -194,7 +197,7 @@ export function lazyLoadSubApp({ name, id, timeout = 15000, onLoad, onError, fal
} else {
const element = document.getElementById(id);
if (element && subApp.start) {
return subApp.start(instance, { id });
return subApp.start(instance, { id, props });
}
}
};
Expand Down
13 changes: 7 additions & 6 deletions samples/poc-subapp-redux/src/components/deals.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,23 @@ class SubApp extends React.Component {
if (typeof window === "undefined") {
return "";
}
const { name } = this.props;
const { name, ...rest } = this.props;

const subapp = xarc.getSubApp(name);
if (xarc.getBundle(name) && subapp) {
return (
<div className="col-sm-4">
<div className="panel panel-primary">
<div className="panel-body">{subapp.start(null, { props: this.props })}</div>
<div className="panel-body">{subapp.start(null, { props: rest })}</div>
</div>
</div>
);
} else {
const onLoad = () => this.setState({ ready: true });
dynamicLoadSubApp({
name: "Deal",
onLoad
onLoad,
props: rest
});

// if not, return loadingComponent
Expand All @@ -39,9 +40,9 @@ class SubApp extends React.Component {
}

const DealSubApp = props => {
const { id } = props;
const { id, ...rest } = props;

dynamicLoadSubApp({ name: "Deal", id });
dynamicLoadSubApp({ name: "Deal", id, props: rest });

return (
<div className="col-sm-4">
Expand Down Expand Up @@ -94,7 +95,7 @@ Deals.propTypes = {
};

const mapStateToProps = state => {
return { value: state.number.value };
return { value: state.number };
};

const ReduxDeals = connect(mapStateToProps, dispatch => ({ dispatch }))(Deals);
Expand Down

0 comments on commit 43da556

Please sign in to comment.